Class: Syme::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/syme/bootstrap/parser.rb

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



3
4
5
# File 'lib/syme/bootstrap/parser.rb', line 3

def initialize
  @error_position = 0
end

Instance Method Details

#parse(string) ⇒ Object

The #parse_string method is defined in the parser C extension.



9
10
11
12
13
14
# File 'lib/syme/bootstrap/parser.rb', line 9

def parse(string)
  @string = string
  ast = parse_string string
  show_syntax_error unless ast
  ast
end

#parse_file(name) ⇒ Object



16
17
18
19
# File 'lib/syme/bootstrap/parser.rb', line 16

def parse_file(name)
  string = IO.read name
  parse string
end

#show_syntax_errorObject

Parsing callbacks



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/syme/bootstrap/parser.rb', line 23

def show_syntax_error
  error_line = nil
  count = 0

  @string.each_line do |line|
    count += line.size
    if count > @error_position
      error_line = line
      break
    end
  end

  message = <<-EOM

#{error_line.chomp}
#{" " *(error_line.size - (count - @error_position))}^
EOM
  raise Syntax::SyntaxError, message
end

#syntax_error(pos) ⇒ Object



43
44
45
# File 'lib/syme/bootstrap/parser.rb', line 43

def syntax_error(pos)
  @error_position = pos
end