Class: Rsec::ParseContext

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/rsec/utils.rb

Overview

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>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, source) ⇒ ParseContext

Returns a new instance of ParseContext.



34
35
36
37
38
39
40
# File 'lib/rsec/utils.rb', line 34

def initialize str, source
  super(str)
  @source = source
  @cache = {}
  @last_fail_pos = 0
  @last_fail_mask = 0
end

Instance Attribute Details

#attr_namesObject

Returns the value of attribute attr_names.



32
33
34
# File 'lib/rsec/utils.rb', line 32

def attr_names
  @attr_names
end

#cacheObject (readonly)

Returns the value of attribute cache.



31
32
33
# File 'lib/rsec/utils.rb', line 31

def cache
  @cache
end

#last_fail_posObject (readonly)

Returns the value of attribute last_fail_pos.



31
32
33
# File 'lib/rsec/utils.rb', line 31

def last_fail_pos
  @last_fail_pos
end

#sourceObject (readonly)

Returns the value of attribute source.



31
32
33
# File 'lib/rsec/utils.rb', line 31

def source
  @source
end

Instance Method Details

#clear_cacheObject

clear packrat parser cache



43
44
45
# File 'lib/rsec/utils.rb', line 43

def clear_cache
  @cache.clear
end

#col(pos) ⇒ Object

get column number: position in line



81
82
83
84
85
86
87
88
89
# File 'lib/rsec/utils.rb', line 81

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) ⇒ Object

generate parse error



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rsec/utils.rb', line 58

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) ⇒ Object

get line number



76
77
78
# File 'lib/rsec/utils.rb', line 76

def line pos
  string[0...pos].count("\n") + 1
end

#line_text(pos) ⇒ Object

get line text containing pos the text is 80 at most



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rsec/utils.rb', line 93

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) ⇒ Object

add fail message



48
49
50
51
52
53
54
55
# File 'lib/rsec/utils.rb', line 48

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