Class: Predicator::Lexer
- Inherits:
-
Object
- Object
- Predicator::Lexer
- Defined in:
- lib/predicator/lexer.rex.rb
Overview
– This file is automatically generated. Do not modify it. Generated by: oedipus_lex version 2.5.0. Source: lib/predicator/lexer.rex ++
Defined Under Namespace
Classes: LexerError, ScanError
Constant Summary collapse
- SPACE =
/[ \t\r\n]/- LPAREN =
/\(/- RPAREN =
/\)/- LBRACKET =
/\[/- RBRACKET =
/\]/- TRUE =
/true\b/- FALSE =
/false\b/- BETWEEN =
/between\b/- IN =
/in\b/- BANG =
/!/- NOT =
/not\b/- DOT =
/\./- COMMA =
/,/- AND =
/and\b/- OR =
/or\b/- EQ =
/=/- GT =
/>/- LT =
/</- INTEGER =
/[+-]?\d(_?\d)*\b/- STRING =
/(["'])(?:\\?.)*?\1/- IDENTIFIER =
/[a-z][A-Za-z0-9_]*\b/
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#lineno ⇒ Object
Returns the value of attribute lineno.
-
#old_pos ⇒ Object
Returns the value of attribute old_pos.
-
#ss ⇒ Object
(also: #match)
Returns the value of attribute ss.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #action ⇒ Object
- #column ⇒ Object
-
#do_parse ⇒ Object
def next_token.
- #location ⇒ Object
- #matches ⇒ Object
- #next_token ⇒ Object
- #parse(str) ⇒ Object
- #parse_file(path) ⇒ Object
- #scanner_class ⇒ Object
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
37 38 39 |
# File 'lib/predicator/lexer.rex.rb', line 37 def filename @filename end |
#lineno ⇒ Object
Returns the value of attribute lineno.
36 37 38 |
# File 'lib/predicator/lexer.rex.rb', line 36 def lineno @lineno end |
#old_pos ⇒ Object
Returns the value of attribute old_pos.
53 54 55 |
# File 'lib/predicator/lexer.rex.rb', line 53 def old_pos @old_pos end |
#ss ⇒ Object Also known as: match
Returns the value of attribute ss.
38 39 40 |
# File 'lib/predicator/lexer.rex.rb', line 38 def ss @ss end |
#state ⇒ Object
Returns the value of attribute state.
39 40 41 |
# File 'lib/predicator/lexer.rex.rb', line 39 def state @state end |
Instance Method Details
#action ⇒ Object
49 50 51 |
# File 'lib/predicator/lexer.rex.rb', line 49 def action yield end |
#column ⇒ Object
55 56 57 58 |
# File 'lib/predicator/lexer.rex.rb', line 55 def column idx = ss.string.rindex("\n", old_pos) || -1 old_pos - idx - 1 end |
#do_parse ⇒ Object
def next_token
159 |
# File 'lib/predicator/lexer.rex.rb', line 159 def do_parse; end |
#location ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/predicator/lexer.rex.rb', line 79 def location [ (filename || "<input>"), lineno, column, ].compact.join(":") end |
#matches ⇒ Object
43 44 45 46 47 |
# File 'lib/predicator/lexer.rex.rb', line 43 def matches m = (1..9).map { |i| ss[i] } m.pop until m[-1] or m.empty? m end |
#next_token ⇒ Object
87 88 89 90 91 92 93 94 95 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/predicator/lexer.rex.rb', line 87 def next_token token = nil until ss.eos? or token do self.lineno += 1 if ss.peek(1) == "\n" self.old_pos = ss.pos token = case state when nil then case when ss.skip(/#{SPACE}/) then # do nothing when text = ss.scan(/#{LPAREN}/) then action { [:LPAREN, text] } when text = ss.scan(/#{RPAREN}/) then action { [:RPAREN, text] } when text = ss.scan(/#{LBRACKET}/) then action { [:LBRACKET, text] } when text = ss.scan(/#{RBRACKET}/) then action { [:RBRACKET, text] } when text = ss.scan(/#{TRUE}/) then action { [:TRUE, text] } when text = ss.scan(/#{FALSE}/) then action { [:FALSE, text] } when text = ss.scan(/#{BETWEEN}/) then action { [:BETWEEN, text] } when text = ss.scan(/#{IN}/) then action { [:IN, text] } when text = ss.scan(/#{BANG}/) then action { [:BANG, text] } when text = ss.scan(/#{NOT}/) then action { [:NOT, text] } when text = ss.scan(/#{DOT}/) then action { [:DOT, text] } when text = ss.scan(/#{COMMA}/) then action { [:COMMA, text] } when text = ss.scan(/#{AND}/) then action { [:AND, text] } when text = ss.scan(/#{OR}/) then action { [:OR, text] } when text = ss.scan(/#{EQ}/) then action { [:EQ, text] } when text = ss.scan(/#{GT}/) then action { [:GT, text] } when text = ss.scan(/#{LT}/) then action { [:LT, text] } when text = ss.scan(/#{INTEGER}/) then action { [:INTEGER, text] } when text = ss.scan(/#{STRING}/) then action { [:STRING, text[1...-1]] } when text = ss.scan(/#{IDENTIFIER}/) then action { [:IDENTIFIER, text] } 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
64 65 66 67 68 69 70 |
# File 'lib/predicator/lexer.rex.rb', line 64 def parse str self.ss = scanner_class.new str self.lineno = 1 self.state ||= nil do_parse end |
#parse_file(path) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/predicator/lexer.rex.rb', line 72 def parse_file path self.filename = path open path do |f| parse f.read end end |
#scanner_class ⇒ Object
60 61 62 |
# File 'lib/predicator/lexer.rex.rb', line 60 def scanner_class StringScanner end |