Class: Rattler::BackEnd::ParserGenerator::SequenceGenerator

Inherits:
ExprGenerator
  • Object
show all
Includes:
NestedSubGenerating, Parsers
Defined in:
lib/rattler/back_end/parser_generator/sequence_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parsers

define

Methods inherited from ExprGenerator

#gen_intermediate, #gen_intermediate_assert, #gen_intermediate_disallow, #gen_intermediate_skip

Constructor Details

#initialize(*args) ⇒ SequenceGenerator

Returns a new instance of SequenceGenerator.



11
12
13
14
# File 'lib/rattler/back_end/parser_generator/sequence_generator.rb', line 11

def initialize(*args)
  super
  @capture_names = []
end

Class Method Details

.nested(*args) ⇒ Object



177
178
179
# File 'lib/rattler/back_end/parser_generator/sequence_generator.rb', line 177

def SequenceGenerator.nested(*args)
  NestedSequenceGenerator.new(*args)
end

.top_level(*args) ⇒ Object



186
187
188
# File 'lib/rattler/back_end/parser_generator/sequence_generator.rb', line 186

def SequenceGenerator.top_level(*args)
  TopLevelSequenceGenerator.new(*args)
end

Instance Method Details

#gen_assert(sequence, scope = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rattler/back_end/parser_generator/sequence_generator.rb', line 31

def gen_assert(sequence, scope={})
  expr :block do
    lookahead do
      @g.block("#{result_name} = begin") do
        sequence.each do |_|
          @g.suffix(' &&') { scope = gen_matching _, scope }.newline
        end
        @g << "true"
      end
      @g.newline
    end
    @g << result_name
  end
end

#gen_basic(sequence, scope = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rattler/back_end/parser_generator/sequence_generator.rb', line 16

def gen_basic(sequence, scope={})
  with_backtracking do
    if sequence.capture_count == 1 and sequence.children.last.capturing?
      @g.intersperse_nl(sequence, ' &&') do |child|
        scope = gen_capturing(child, scope) { gen_nested child, :basic, scope }
      end
    else
      sequence.each do |child|
        @g.suffix(' &&') { scope = gen_capturing child, scope }.newline
      end
      @g << result_expr(sequence)
    end
  end
end

#gen_direct_action(sequence, code, scope = {}) ⇒ Object



66
67
68
69
70
# File 'lib/rattler/back_end/parser_generator/sequence_generator.rb', line 66

def gen_direct_action(sequence, code, scope={})
  gen_action_code(sequence, scope) do |new_scope|
    "(#{code.bind new_scope, @capture_names})"
  end
end

#gen_disallow(sequence, scope = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rattler/back_end/parser_generator/sequence_generator.rb', line 46

def gen_disallow(sequence, scope={})
  expr :block do
    lookahead do
      @g.block("#{result_name} = !begin") do
        @g.intersperse_nl(sequence, ' &&') do |_|
          scope = gen_matching _, scope
        end
      end
      @g.newline
    end
    @g << result_name
  end
end

#gen_dispatch_action(sequence, code, scope = {}) ⇒ Object



60
61
62
63
64
# File 'lib/rattler/back_end/parser_generator/sequence_generator.rb', line 60

def gen_dispatch_action(sequence, code, scope={})
  gen_action_code(sequence, scope) do |new_scope|
    code.bind new_scope, result_array_expr(sequence)
  end
end

#gen_skip(sequence, scope = {}) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/rattler/back_end/parser_generator/sequence_generator.rb', line 81

def gen_skip(sequence, scope={})
  with_backtracking do
    sequence.each do |_|
      @g.suffix(' &&') { scope = gen_matching _, scope }.newline
    end
    @g << "true"
  end
end

#gen_token(sequence, scope = {}) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/rattler/back_end/parser_generator/sequence_generator.rb', line 72

def gen_token(sequence, scope={})
  with_backtracking do
    sequence.each do |_|
      @g.suffix(' &&') { scope = gen_matching _, scope }.newline
    end
    @g << "@scanner.string[#{saved_pos_name}...(@scanner.pos)]"
  end
end