Class: TestML::Compiler::Lite

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

Constant Summary collapse

WS =
%r!\s+!
ANY =
%r!.!
STAR =
%r!\*!
NUM =
%r!-?[0-9]+!
WORD =
%r!\w+!
HASH =
%r!#!
EQ =
%r!=!
TILDE =
%r!~!
LP =
%r!\(!
RP =
%r!\)!
DOT =
%r!\.!
COMMA =
%r!,!
SEMI =
%r!;!
SSTR =
%r!'(?:[^']*)'!
DSTR =
%r!"(?:[^"]*)"!
ENDING =
%r!(?:#{RP}|#{COMMA}|#{SEMI})!
POINT =
%r!#{STAR}#{WORD}!
QSTR =
%r!(?:#{SSTR}|#{DSTR})!
COMP =
%r!(?:#{EQ}#{EQ}|#{TILDE}#{TILDE})!
OPER =
%r!(?:#{COMP}|#{EQ})!
PUNCT =
%r!(?:#{LP}|#{RP}|#{DOT}|#{COMMA}|#{SEMI})!
TOKENS =
%r!(?:#{POINT}|#{NUM}|#{WORD}|#{QSTR}|#{PUNCT}|#{OPER})!

Instance Attribute Summary collapse

Attributes inherited from TestML::Compiler

#code, #data, #directives, #text

Instance Method Summary collapse

Methods inherited from TestML::Compiler

#compile, #preprocess

Instance Attribute Details

#functionObject

Returns the value of attribute function.



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

def function
  @function
end

#inputObject

Returns the value of attribute input.



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

def input
  @input
end

#pointsObject

Returns the value of attribute points.



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

def points
  @points
end

#tokensObject

Returns the value of attribute tokens.



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

def tokens
  @tokens
end

Instance Method Details

#compile_codeObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/testml/compiler/lite.rb', line 36

def compile_code
  @function = TestML::Function.new
  while not @code.empty? do
    @code.sub! /^(.*)(\r\n|\n|)/, ''
    @line = $1
    tokenize
    next if done
    parse_assignment ||
    parse_assertion ||
    fail_()
  end
end

#compile_dataObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/testml/compiler/lite.rb', line 139

def compile_data
  input = @data
  input.gsub! /^#.*\n/, "\n"
  input.gsub! /^\\/, ''
  blocks = input.split(/(^===.*?(?=^===|\z))/m).select{|el|!el.empty?}
  blocks.each{|block| block.sub! /\n+\z/, "\n"}

  data = []
  blocks.each do |string_block|
    block = TestML::Block.new
    string_block.gsub! /\A===\ +(.*?)\ *\n/, '' or
      fail "No block label! #{string_block}"
    block.label = $1
    while !string_block.empty? do
      next if string_block.sub! /\A\n+/, ''
      key, value = nil, nil
      if string_block.gsub!(/\A---\ +(\w+):\ +(.*)\n/, '') or
         string_block.gsub!(/\A---\ +(\w+)\n(.*?)(?=^---|\z)/m, '')
        key, value = $1, $2
      else
        fail "Failed to parse TestML string:\n#{string_block}"
      end
      block.points ||= {}
      block.points[key] = value

      if key =~ /^(ONLY|SKIP|LAST)$/
        block.points[key] = true
      end
    end
    data.push block
  end
  @function.data = data unless data.empty?
end

#doneObject



173
174
175
# File 'lib/testml/compiler/lite.rb', line 173

def done
  tokens.empty?
end

#fail_(message = nil) ⇒ Object



188
189
190
191
192
193
# File 'lib/testml/compiler/lite.rb', line 188

def fail_(message=nil)
  text = "Failed to compile TestML document.\n"
  text << "Reason: #{message}\n" if message
  text << "\nCode section of failure:\n#{@line}\n#{@code}\n"
  fail text
end

#parse_argsObject



128
129
130
131
132
133
134
135
136
137
# File 'lib/testml/compiler/lite.rb', line 128

def parse_args
  pop == '(' or fail
  args = []
  while peek != ')' do
    args.push parse_expression
    pop if peek == ','
  end
  pop
  return args
end

#parse_assertionObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/testml/compiler/lite.rb', line 72

def parse_assertion
  return unless @tokens.grep /^#{COMP}$/
  @points = []
  left = parse_expression
  token = pop
  op =
    token == '==' ? 'EQ' :
    token == '~~' ? 'HAS' :
    fail_
  right = parse_expression
  pop if !done and peek == ';'
  fail_ unless done

  @function.statements.push TestML::Statement.new(
    left,
    TestML::Assertion.new(
      op,
      right,
    ),
    points.empty? ? nil : points
  )
  return true
end

#parse_assignmentObject



62
63
64
65
66
67
68
69
70
# File 'lib/testml/compiler/lite.rb', line 62

def parse_assignment
  return unless peek(2) == '='
  var, op = pop(2)
  expr = parse_expression
  pop if !done and peek == ';'
  fail_() unless done
  @function.statements.push TestML::Assignment.new(var, expr)
  return true
end

#parse_expressionObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/testml/compiler/lite.rb', line 96

def parse_expression
  calls = []
  while !done and peek !~ /^(#{ENDING}|#{COMP})$/ do
    token = pop
    if token =~ /^#{NUM}$/
      calls.push TestML::Num.new(token.to_i)
    elsif token =~ /^#{QSTR}$/
      str = token[1..-2]
      calls.push TestML::Str.new(str)
    elsif token =~ /^#{WORD}$/
      call = TestML::Call.new(token)
      if !done and peek == '('
        call.args = parse_args
      end
      calls.push call
    elsif token =~ /^#{POINT}$/
      token =~ /(#{WORD})/ or fail
      points.push $1
      calls.push TestML::Point.new($1)
    else
      fail_("Unknown token '#{token}'")
    end
    if !done and peek == '.'
      pop
    end
  end

  return calls.size == 1 \
    ? calls[0]
    : TestML::Expression.new(calls)
end

#peek(index = 1) ⇒ Object



177
178
179
180
# File 'lib/testml/compiler/lite.rb', line 177

def peek(index=1)
  fail if index > @tokens.size
  @tokens[index - 1]
end

#pop(count = 1) ⇒ Object



182
183
184
185
186
# File 'lib/testml/compiler/lite.rb', line 182

def pop(count=1)
  fail if count > @tokens.size
  array = @tokens.slice! 0..(count-1)
  count > 1 ? array : array[0]
end

#tokenizeObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/testml/compiler/lite.rb', line 49

def tokenize
  @tokens = []
  while not @line.empty? do
    next if @line.sub!(/^#{WS}/, '')
    next if @line.sub!(/^#{HASH}#{ANY}*/, '')
    if @line.sub!(/^(#{TOKENS})/, '')
      @tokens.push $1
    else
      fail_("Failed to get token here: '#{@line}'")
    end
  end
end