Class: Gherkin::Parser::Parser

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

Defined Under Namespace

Classes: Machine

Instance Method Summary collapse

Constructor Details

#initialize(formatter, raise_on_error = true, machine_name = 'root', force_ruby = false) ⇒ Parser

Initialize the parser. machine_name refers to a state machine table.



18
19
20
21
22
23
24
25
26
# File 'lib/gherkin/parser/parser.rb', line 18

def initialize(formatter, raise_on_error=true, machine_name='root', force_ruby=false)
  @formatter = formatter
  @listener = Listener::FormatterListener.new(@formatter)
  @raise_on_error = raise_on_error
  @machine_name = machine_name
  @machines = []
  push_machine(@machine_name)
  @lexer = I18nLexer.new(self, force_ruby)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Doesn’t yet fall back to super



44
45
46
47
48
49
50
51
52
# File 'lib/gherkin/parser/parser.rb', line 44

def method_missing(method, *args)
  # TODO: Catch exception and call super
  event(method.to_s, args[-1])
  @listener.__send__(method, *args)
  if method == :eof
    pop_machine
    push_machine(@machine_name)
  end
end

Instance Method Details

#errorsObject



39
40
41
# File 'lib/gherkin/parser/parser.rb', line 39

def errors
  @lexer.errors
end

#event(ev, line) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gherkin/parser/parser.rb', line 54

def event(ev, line)
  l = line ? @line_offset+line : nil
  machine.event(ev, l) do |state, legal_events|
    if @raise_on_error
      raise ParseError.new(state, ev, legal_events, @feature_uri, l)
    else
      # Only used for testing
      @listener.syntax_error(state, ev, legal_events, @feature_uri, l)
    end
  end
end

#expectedObject



78
79
80
# File 'lib/gherkin/parser/parser.rb', line 78

def expected
  machine.expected
end

#force_state(state) ⇒ Object



82
83
84
# File 'lib/gherkin/parser/parser.rb', line 82

def force_state(state)
  machine.instance_variable_set('@state', state)
end

#i18n_languageObject



35
36
37
# File 'lib/gherkin/parser/parser.rb', line 35

def i18n_language
  @lexer.i18n_language
end

#machineObject



74
75
76
# File 'lib/gherkin/parser/parser.rb', line 74

def machine
  @machines[-1]
end

#parse(gherkin, feature_uri, line_offset) ⇒ Object



28
29
30
31
32
33
# File 'lib/gherkin/parser/parser.rb', line 28

def parse(gherkin, feature_uri, line_offset)
  @feature_uri = feature_uri
  @line_offset = line_offset
  @listener.location(feature_uri)
  @lexer.scan(gherkin)
end

#pop_machineObject



70
71
72
# File 'lib/gherkin/parser/parser.rb', line 70

def pop_machine
  @machines.pop
end

#push_machine(name) ⇒ Object



66
67
68
# File 'lib/gherkin/parser/parser.rb', line 66

def push_machine(name)
  @machines.push(Machine.new(self, name))
end