Class: TestML::Compiler::Pegex

Inherits:
TestML::Compiler show all
Defined in:
lib/testml/compiler/pegex.rb

Defined Under Namespace

Classes: AST, Grammar

Instance Attribute Summary collapse

Attributes inherited from TestML::Compiler

#code, #data, #directives, #function, #text

Instance Method Summary collapse

Methods inherited from TestML::Compiler

#compile, #preprocess

Instance Attribute Details

#parserObject

Returns the value of attribute parser.



5
6
7
# File 'lib/testml/compiler/pegex.rb', line 5

def parser
  @parser
end

Instance Method Details

#compile_codeObject



7
8
9
10
11
12
13
14
15
# File 'lib/testml/compiler/pegex.rb', line 7

def compile_code
  @parser = ::Pegex::Parser.new do |p|
    p.grammar = TestML::Compiler::Pegex::Grammar.new
    p.receiver = TestML::Compiler::Pegex::AST.new
  end
  fixup_grammar
  parser.parse(@code, 'code_section') \
    or fail "Parse TestML code section failed"
end

#compile_dataObject



17
18
19
20
21
22
23
# File 'lib/testml/compiler/pegex.rb', line 17

def compile_data
  if !@data.empty?
    parser.parse(@data, 'data_section') \
      or fail "Parse TestML data section failed"
  end
  @function = parser.receiver.function
end

#fixup_grammarObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/testml/compiler/pegex.rb', line 25

def fixup_grammar
  tree = @parser.grammar.tree
  point_lines = tree['point_lines']['.rgx']

  block_marker = @directives['BlockMarker']
  if block_marker
    block_marker.gsub! /([\$\%\^\*\+\?\|])/, '\\\\\1'
    tree['block_marker']['.rgx'] = %r!\A#{block_marker}!
    tree['point_lines']['.rgx'] = Regexp.new(
      point_lines.to_s.sub!(/===/, block_marker)
    )
  end

  point_marker = @directives['PointMarker']
  if point_marker
    point_marker.gsub! /([\$\%\^\*\+\?\|])/, '\\\\\1'
    tree['point_marker']['.rgx'] = %r!\A#{point_marker}!
    tree['point_lines']['.rgx'] = Regexp.new(
      point_lines.to_s.sub!(/\\-\\-\\-/, point_marker)
    )
  end
end