Class: Rubex::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubex/lexer.rex.rb

Overview

– This file is automatically generated. Do not modify it. Generated by: oedipus_lex version 2.5.0. Source: lib/rubex/lexer.rex ++

Defined Under Namespace

Classes: LexerError, ScanError

Constant Summary collapse

SELF =
/self/
DEF =
/def/
CFUNC =
/cfunc/
RETURN =
/return/
/print/
IF =
/if/
ELSE =
/else/
ELSIF =
/elsif/
THEN =
/then/
STATIC_ARRAY =
/StaticArray/
FOR =
/for/
WHILE =
/while/
DO =
/do/
EACH =
/each/
TRUE =
/true/
FALSE =
/false/
NIL =
/nil/
STRUCT =
/struct/
UNION =
/union/
ALIAS =
/alias/
LIB =
/lib/
CLASS =
/class/
NULL =
/NULL/
ATTACH =
/attach/
BLOCK_GIVEN =
/block_given\?/
NO_GIL =
/no_gil/
REQUIRE_RUBEX =
/require_rubex/
IDENTIFIER =
/[a-zA-Z_][a-zA-Z_0-9]*/
COLON2 =
/::/
LPAREN =
/\(/
RPAREN =
/\)/
LSQUARE =
/\[/
RSQUARE =
/\]/
LBRACE =
/{/
RBRACE =
/}/
NL =
/\n/
COMMA =
/,/
SQUOTE =
/'/
DQUOTE =
/"/
SCOLON =
/;/
COLON =
/:/
INTEGER =
/-?\d+/
FLOAT =
/-?\d+\.\d+/
DOT =
/\./
QMARK =
/\?/
OCTOTHORP =
/#/
EXPO =
/\*\*/
EXPOASSIGN =
/\*\*=/
STAR =
/\*/
STARASSIGN =
/\*=/
DIVIDE =
/\//
DIVIDEASSIGN =
/\/=/
PLUS =
/\+/
PLUSASSIGN =
/\+=/
MINUS =
/\-/
MINUSASSIGN =
/\-=/
MODULUS =
/%/
MODULUSASSIGN =
/%=/
ASSIGN =
/=/
NEQ =
/!=/
EQ =
/==/
LT =
/</
LTEQ =
/<=/
GT =
/>/
GTEQ =
/>=/
ANDOP =
/&&/
OROP =
/\|\|/
BANG =
/!/
BIT_AND =
/&/
BIT_AND_ASSIGN =
/&=/
BIT_OR =
/\|/
BIT_OR_ASSIGN =
/\|=/
BIT_XOR =
/\^/
BIT_XOR_ASSIGN =
/\^=/
BIT_LSHIFT =
/<</
BIT_LSHIFT_ASSIGN =
/<<=/
BIT_RSHIFT =
/>>/
BIT_RSHIFT_ASSIGN =
/>>=/
BIT_NOT =
/~/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



95
96
97
# File 'lib/rubex/lexer.rex.rb', line 95

def filename
  @filename
end

#linenoObject

Returns the value of attribute lineno.



94
95
96
# File 'lib/rubex/lexer.rex.rb', line 94

def lineno
  @lineno
end

#ssObject Also known as: match

Returns the value of attribute ss.



96
97
98
# File 'lib/rubex/lexer.rex.rb', line 96

def ss
  @ss
end

#stateObject

Returns the value of attribute state.



97
98
99
# File 'lib/rubex/lexer.rex.rb', line 97

def state
  @state
end

Instance Method Details

#actionObject



107
108
109
# File 'lib/rubex/lexer.rex.rb', line 107

def action
  yield
end

#do_parseObject

def next_token



362
363
364
365
# File 'lib/rubex/lexer.rex.rb', line 362

def do_parse
  self.ss.string = self.ss.string.split("\n").map! { |e| e.rstrip }.join("\n")
  self.ss << "\n"
end

#locationObject



130
131
132
133
134
135
# File 'lib/rubex/lexer.rex.rb', line 130

def location
  [
    (filename || "<input>"),
    lineno,
  ].compact.join(":")
end

#matchesObject



101
102
103
104
105
# File 'lib/rubex/lexer.rex.rb', line 101

def matches
  m = (1..9).map { |i| ss[i] }
  m.pop until m[-1] or m.empty?
  m
end

#next_tokenObject



137
138
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/rubex/lexer.rex.rb', line 137

def next_token

  token = nil

  until ss.eos? or token do
    self.lineno += 1 if ss.peek(1) == "\n"
    token =
      case state
      when nil then
        case
        when text = ss.scan(/'.\'/) then
          action { [:tSINGLE_CHAR, text] }
        when text = ss.scan(/#{FLOAT}/) then
          action { [:tFLOAT, text] }
        when text = ss.scan(/#{INTEGER}/) then
          action { [:tINTEGER, text] }
        when ss.skip(/#{DQUOTE}/) then
          action { @state = :STRING_LITERAL; @string_text = ""; nil }
        when ss.skip(/#{OCTOTHORP}/) then
          action { @state = :COMMENT; nil }
        when text = ss.scan(/#{STATIC_ARRAY}/) then
          action { [:kSTATIC_ARRAY, text] }
        when text = ss.scan(/#{FOR}/) then
          action { [:kFOR, text]    }
        when text = ss.scan(/#{WHILE}/) then
          action { [:kWHILE, text]  }
        when text = ss.scan(/#{TRUE}/) then
          action { [:kTRUE, text]   }
        when text = ss.scan(/#{FALSE}/) then
          action { [:kFALSE, text]  }
        when text = ss.scan(/#{NIL}/) then
          action { [:kNIL, text]    }
        when text = ss.scan(/#{LIB}/) then
          action { [:kLIB, text]    }
        when text = ss.scan(/#{CLASS}/) then
          action { [:kCLASS, text]  }
        when text = ss.scan(/#{NULL}/) then
          action { [:kNULL, text] }
        when text = ss.scan(/fwd/) then
          action { [:kFWD, text] }
        when text = ss.scan(/#{ATTACH}/) then
          action { [:kATTACH, text] }
        when text = ss.scan(/#{BLOCK_GIVEN}/) then
          action { [:kBLOCK_GIVEN, text] }
        when text = ss.scan(/data\$/) then
          action { [:kDATA_VAR, text] }
        when text = ss.scan(/#{REQUIRE_RUBEX}/) then
          action { [:kREQUIRE_RUBEX, text] }
        when text = ss.scan(/#{DOT}#{EACH}/) then
          action { [:kDOT_EACH, text] }
        when text = ss.scan(/unsigned long long int/) then
          action { [:kDTYPE_ULLINT, text] }
        when text = ss.scan(/unsigned long int/) then
          action { [:kDTYPE_ULINT, text] }
        when text = ss.scan(/unsigned int/) then
          action { [:kDTYPE_UINT, text] }
        when text = ss.scan(/unsigned char/) then
          action { [:kDTYPE_UCHAR, text] }
        when text = ss.scan(/long int/) then
          action { [:kDTYPE_LINT, text] }
        when text = ss.scan(/long long int/) then
          action { [:kDTYPE_LLINT, text] }
        when text = ss.scan(/#{STRUCT}\ /) then
          action { [:kSTRUCT, text] }
        when text = ss.scan(/#{UNION}\ /) then
          action { [:kUNION, text]  }
        when text = ss.scan(/#{ALIAS}\ /) then
          action { [:kALIAS, text]  }
        when text = ss.scan(/#{NO_GIL}/) then
          action { [:kNO_GIL, text] }
        when text = ss.scan(/:#{IDENTIFIER}/) then
          action { [:tSYMBOL, text] }
        when text = ss.scan(/@#{IDENTIFIER}/) then
          action { [:tINSTANCE_VAR, text] }
        when text = ss.scan(/#{IDENTIFIER}/) then
          action { [:tIDENTIFIER, text] }
        when text = ss.scan(/#{LBRACE}/) then
          action { [:tLBRACE, text] }
        when text = ss.scan(/#{RBRACE}/) then
          action { [:tRBRACE, text] }
        when text = ss.scan(/#{LPAREN}/) then
          action { [:tLPAREN, text] }
        when text = ss.scan(/#{RPAREN}/) then
          action { [:tRPAREN, text] }
        when text = ss.scan(/#{LSQUARE}/) then
          action { [:tLSQUARE, text] }
        when text = ss.scan(/#{RSQUARE}/) then
          action { [:tRSQUARE, text] }
        when text = ss.scan(/#{COMMA}/) then
          action { [:tCOMMA, text] }
        when text = ss.scan(/#{SCOLON}/) then
          action { [:tSCOLON, text] }
        when text = ss.scan(/#{NL}/) then
          action { [:tNL, text] }
        when text = ss.scan(/#{QMARK}/) then
          action { [:tQMARK, text]}
        when text = ss.scan(/#{DOT}/) then
          action { [:tDOT, text]    }
        when text = ss.scan(/#{COLON2}/) then
          action { [:tCOLON2, text]  }
        when text = ss.scan(/#{COLON}/) then
          action { [:tCOLON, text]  }
        when text = ss.scan(/#{EXPOASSIGN}/) then
          action { [:tOP_ASSIGN, text]}
        when text = ss.scan(/#{PLUSASSIGN}/) then
          action { [:tOP_ASSIGN, text]}
        when text = ss.scan(/#{MINUSASSIGN}/) then
          action { [:tOP_ASSIGN, text]}
        when text = ss.scan(/#{STARASSIGN}/) then
          action { [:tOP_ASSIGN, text]}
        when text = ss.scan(/#{DIVIDEASSIGN}/) then
          action { [:tOP_ASSIGN, text]}
        when text = ss.scan(/#{EXPOASSIGN}/) then
          action { [:tOP_ASSIGN, text]}
        when text = ss.scan(/#{MODULUSASSIGN}/) then
          action { [:tOP_ASSIGN, text]}
        when text = ss.scan(/#{EXPO}/) then
          action { [:tEXPO, text]}
        when text = ss.scan(/#{PLUS}/) then
          action { [:tPLUS, text]}
        when text = ss.scan(/#{MINUS}/) then
          action { [:tMINUS, text]}
        when text = ss.scan(/#{STAR}/) then
          action { [:tSTAR, text]}
        when text = ss.scan(/#{DIVIDE}/) then
          action { [:tDIVIDE, text]}
        when text = ss.scan(/#{MODULUS}/) then
          action { [:tMODULUS, text]}
        when text = ss.scan(/#{EXPO}/) then
          action { [:tEXPO, text]}
        when text = ss.scan(/#{EQ}/) then
          action { [:tEQ, text]  }
        when text = ss.scan(/#{NEQ}/) then
          action { [:tNEQ, text]  }
        when text = ss.scan(/#{ASSIGN}/) then
          action { [:tASSIGN, text] }
        when text = ss.scan(/#{BANG}/) then
          action { [:tBANG, text] }
        when text = ss.scan(/#{ANDOP}/) then
          action { [:tANDOP, text] }
        when text = ss.scan(/#{OROP}/) then
          action { [:tOROP, text] }
        when text = ss.scan(/#{BIT_AND_ASSIGN}/) then
          action { [:tOP_ASSIGN, text] }  
        when text = ss.scan(/#{BIT_OR_ASSIGN}/) then
          action { [:tOP_ASSIGN  , text] }
        when text = ss.scan(/#{BIT_XOR_ASSIGN}/) then
          action { [:tOP_ASSIGN , text] }
        when text = ss.scan(/#{BIT_LSHIFT_ASSIGN}/) then
          action { [:tOP_ASSIGN, text] }
        when text = ss.scan(/#{BIT_RSHIFT_ASSIGN}/) then
          action { [:tOP_ASSIGN, text] }
        when text = ss.scan(/#{BIT_AND}/) then
          action { [:tBIT_AND      , text] } 
        when text = ss.scan(/#{BIT_OR}/) then
          action { [:tBIT_OR        , text] }  
        when text = ss.scan(/#{BIT_XOR}/) then
          action { [:tBIT_XOR       , text] }  
        when text = ss.scan(/#{BIT_LSHIFT}/) then
          action { [:tBIT_LSHIFT    , text] }  
        when text = ss.scan(/#{BIT_RSHIFT}/) then
          action { [:tBIT_RSHIFT      , text] }
        when text = ss.scan(/#{BIT_NOT}/) then
          action { [:tBIT_NOT, text] }
        when text = ss.scan(/#{LTEQ}/) then
          action { [:tLTEQ, text] }
        when text = ss.scan(/#{LT}/) then
          action { [:tLT, text] }
        when text = ss.scan(/#{GTEQ}/) then
          action { [:tGTEQ, text] }
        when text = ss.scan(/#{GT}/) then
          action { [:tGT, text] }
        when ss.skip(/^\n\s*$/) then
          # do nothing
        when ss.skip(/\s+/) then
          # do nothing
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'"
        end
      when :STRING_LITERAL then
        case
        when ss.skip(/#{DQUOTE}/) then
          action { @state = nil; return [:tSTRING, @string_text] }
        when text = ss.scan(/[^"\\]/) then
          action { @string_text << text; nil }
        when text = ss.scan(/\\/) then
          action { @state = :STRING_LITERAL_BSLASH; @string_text << text; nil }
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'"
        end
      when :STRING_LITERAL_BSLASH then
        case
        when text = ss.scan(/./) then
          action { @state = :STRING_LITERAL; @string_text << text; nil }
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'"
        end
      when :COMMENT then
        case
        when ss.skip(/./) then
          action { @state = :COMMENT; nil }
        when ss.skip(/\n/) then
          action { @state = nil; [:tNL, "\n"] }
        else
          text = ss.string[ss.pos .. -1]
          raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'"
        end
      else
        raise ScanError, "undefined state at #{location}: '#{state}'"
      end # token = case state

    next unless token # allow functions to trigger redo w/ nil
  end # while

  raise LexerError, "bad lexical result at #{location}: #{token.inspect}" unless
    token.nil? || (Array === token && token.size >= 2)

  # auto-switch state
  self.state = token.last if token && token.first == :state

  token
end

#parse(str) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/rubex/lexer.rex.rb', line 115

def parse str
  self.ss     = scanner_class.new str
  self.lineno = 1
  self.state  ||= nil

  do_parse
end

#parse_file(path) ⇒ Object



123
124
125
126
127
128
# File 'lib/rubex/lexer.rex.rb', line 123

def parse_file path
  self.filename = path
  open path do |f|
    parse f.read
  end
end

#scanner_classObject



111
112
113
# File 'lib/rubex/lexer.rex.rb', line 111

def scanner_class
  StringScanner
end