Class: TestML::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/testml/compiler.rb

Direct Known Subclasses

Lite, Pegex

Defined Under Namespace

Classes: Lite, Pegex

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codeObject

Returns the value of attribute code.



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

def code
  @code
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#directivesObject

Returns the value of attribute directives.



8
9
10
# File 'lib/testml/compiler.rb', line 8

def directives
  @directives
end

#functionObject

Returns the value of attribute function.



9
10
11
# File 'lib/testml/compiler.rb', line 9

def function
  @function
end

#textObject

Returns the value of attribute text.



7
8
9
# File 'lib/testml/compiler.rb', line 7

def text
  @text
end

Instance Method Details

#compile(input) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/testml/compiler.rb', line 11

def compile(input)
  preprocess(input, 'top')
  compile_code
  compile_data

  if @directives['DumpAST']
    XXX @function
  end

  @function.namespace ||= {}
  @function.namespace['TestML'] = @directives['TestML']

  @function.outer = TestML::Function.new
  return @function
end

#preprocess(input, top = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/testml/compiler.rb', line 27

def preprocess(input, top=nil)
  parts = input.split /^((?:\%\w+.*|\#.*|\ *)\n)/
  input = ''

  @directives = {
    'TestML' => nil,
    'DataMarker' => nil,
    'BlockMarker' => '===',
    'PointMarker' => '---',
  }

  order_error = false
  parts.each do |part|
    next if part.empty?
    if part =~ /^(\#.*|\ *)\n/
      input << "\n"
      next
    end
    if part =~ /^%(\w+)\s*(.*?)\s*\n/
      directive, value = $1, $2
      input << "\n"
      if directive == 'TestML'
        fail "Invalid TestML directive" \
          unless value =~ /^\d+\.\d+\.\d+$/
        fail "More than one TestML directive found" \
          if @directives['TestML']
        @directives['TestML'] = TestML::Str.new(value)
        next
      end
      order_error = true unless @directives['TestML']
      if directive == 'Include'
        runtime = $TestMLRuntimeSingleton \
          or fail "Can't process Include. No runtime available"
        include_ = self.class.new
        include_.preprocess(runtime.read_testml_file(value))
        input << include_.text
        @directives['DataMarker'] =
          include_.directives['DataMarker']
        @directives['BlockMarker'] =
          include_.directives['BlockMarker']
        @directives['PointMarker'] =
          include_.directives['PointMarker']
        fail "Can't define %TestML in an Included file" \
          if include_.directives['TestML']
      elsif directive =~ /^(DataMarker|BlockMarker|PointMarker)$/
        @directives[directive] = value
      elsif directive =~ /^(DebugPegex|DumpAST)$/
        value = true if value.empty?
        @directives[directive] = value
      else
        fail "Unknown TestML directive '$#{directive}'"
      end
    else
      order_error = true if !input.empty? and !@directives['TestML']
      input << part
    end
  end

  if top
    fail "No TestML directive found" \
      unless @directives['TestML']
    fail "%TestML directive must be the first (non-comment) statement" \
      if order_error

    @directives['DataMarker'] ||= @directives['BlockMarker']
    if split = input.index("\n#{@directives['DataMarker']}")
      @code = input[0..(split)]
      @data = input[(split + 1)..-1]
    else
      @code = input
      @data = ''
    end

    @code.gsub! /^\\(\\*[\%\#])/, '\1'
    @data.gsub! /^\\(\\*[\%\#])/, '\1'
  else
    @text = input
  end
end