class Rsec::ParseContext

parse context inherits from StringScanner<br/> <br/> attributes:<br/> <pre>

[R]  string: string to parse
[RW] pos: current position
[R]  source: source file name
[R]  current_line_text: current line text
[R]  cache: for memoization

</pre>

Attributes

attr_names[RW]
cache[R]
last_fail_pos[R]
source[R]

Public Class Methods

new(str, source) click to toggle source
Calls superclass method
# File lib/rsec/utils.rb, line 33
def initialize str, source
  super(str)
  @source = source
  @cache = {}
  @last_fail_pos = 0
  @last_fail_mask = 0
end

Public Instance Methods

clear_cache() click to toggle source

clear packrat parser cache

# File lib/rsec/utils.rb, line 42
def clear_cache
  @cache.clear
end
col(pos) click to toggle source

get column number: position in line

# File lib/rsec/utils.rb, line 80
def col pos
  return 1 if pos == 0
  newline_pos = string.rindex "\n", pos - 1
  if newline_pos
    pos - newline_pos
  else
    pos + 1
  end
end
generate_error(source) click to toggle source

generate parse error

# File lib/rsec/utils.rb, line 57
def generate_error source
  if self.pos <= @last_fail_pos
    line = line @last_fail_pos
    col = col @last_fail_pos
    line_text = line_text @last_fail_pos
    expect_tokens = Fail.get_tokens @last_fail_mask
    expects = ", expect token [ #{expect_tokens.join ' | '} ]"
  else
    line = line pos
    col = col pos
    line_text = line_text pos
    expects = nil
  end
  msg = "\nin #{source}:#{line} at #{col}#{expects}"
  SyntaxError.new msg, line_text, line, col
end
line(pos) click to toggle source

get line number

# File lib/rsec/utils.rb, line 75
def line pos
  string[0...pos].count("\n") + 1
end
line_text(pos) click to toggle source

get line text containing pos the text is 80 at most

# File lib/rsec/utils.rb, line 92
def line_text pos
  from = string.rindex "\n", pos
  (from = string.rindex "\n", pos - 1) if from == pos
  from = from ? from + 1 : 0
  from = pos - 40 if (from < pos - 40)

  to = string.index("\n", pos)
  to = to ? to - 1 : string.size
  to = pos + 40 if (to > pos + 40)

  string[from..to]
end
on_fail(mask) click to toggle source

add fail message

# File lib/rsec/utils.rb, line 47
def on_fail mask
  if pos > @last_fail_pos
    @last_fail_pos = pos
    @last_fail_mask = mask
  elsif pos == @last_fail_pos
    @last_fail_mask |= mask
  end
end