Class: TPPlus::Scanner
- Inherits:
-
Object
- Object
- TPPlus::Scanner
- Defined in:
- lib/tp_plus/scanner.rb
Instance Method Summary collapse
-
#initialize ⇒ Scanner
constructor
A new instance of Scanner.
- #isDigit?(ch) ⇒ Boolean
- #isLetter?(ch) ⇒ Boolean
- #next ⇒ Object
-
#next_token ⇒ Object
return token.
- #scan_setup(src) ⇒ Object
- #scanComment ⇒ Object
- #scanIdentifier ⇒ Object
- #scanLabel ⇒ Object
- #scanNumber ⇒ Object
- #scanReal ⇒ Object
- #scanString(type) ⇒ Object
- #skipWhitespace ⇒ Object
Constructor Details
#initialize ⇒ Scanner
Returns a new instance of Scanner.
3 4 |
# File 'lib/tp_plus/scanner.rb', line 3 def initialize end |
Instance Method Details
#isDigit?(ch) ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tp_plus/scanner.rb', line 30 def isDigit?(ch) return false if ch == -1 case ch when '0','1','2','3','4','5','6','7','8','9' return true else return false end end |
#isLetter?(ch) ⇒ Boolean
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/tp_plus/scanner.rb', line 41 def isLetter?(ch) return false if ch == -1 case ch when 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', '_' return true else return false end end |
#next ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/tp_plus/scanner.rb', line 16 def next if @rdOffset < @src.length @offset = @rdOffset @ch = @src[@rdOffset] if @ch == "\n" @lineno += 1 end @rdOffset += 1 else @offset = @src.length @ch = -1 end end |
#next_token ⇒ Object
return token
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 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 |
# File 'lib/tp_plus/scanner.rb', line 131 def next_token self.skipWhitespace tok = nil lit = "" ch = @ch if isLetter?(ch) lit = self.scanIdentifier if @ch == '[' tok = TPPlus::Token.lookup_data(lit) elsif lit == "DIV" tok = :DIV else # keywords are longer than 1 char, avoid lookup otherwise if lit.length > 1 if @prevDot case lit when "gp1","gp2","gp3","gp4","gp5" tok = :GROUP else tok = TPPlus::Token.lookup(lit) end else tok = TPPlus::Token.lookup(lit) end else tok = :WORD end end elsif isDigit?(ch) tok, lit = self.scanNumber else self.next # always make progress case ch when -1 return nil when '=' if @ch == '=' tok = :EEQUAL self.next else tok = :EQUAL end when ':' if @ch == "=" tok = :ASSIGN self.next else tok = :COLON end when "<" if @ch == "=" tok = :LTE self.next elsif @ch == ">" tok = :NOTEQUAL self.next else tok = :LT end when ">" if @ch == "=" tok = :GTE self.next else tok = :GT end when "+" tok = :PLUS when "-" tok = :MINUS when "*" tok = :STAR when "/" tok = :SLASH when "&" if @ch == "&" tok = :AND self.next elsif isLetter?(@ch) tok = :ADDRESS lit = self.scanIdentifier else tok = :ILLEGAL end when "|" if @ch == "|" tok = :OR self.next else tok = :ILLEGAL end when "%" tok = :MOD when ";" tok = :SEMICOLON when "." if self.isDigit?(@ch) tok, lit = self.scanReal else tok = :DOT end when "!" if @ch == "=" tok = :NOTEQUAL self.next else tok = :BANG end when "\"", "'" tok = :STRING lit = self.scanString(ch) when "#" tok = :COMMENT lit = self.scanComment when "@" tok = :LABEL lit = self.scanLabel when '(' tok = :LPAREN when ')' tok = :RPAREN when ',' tok = :COMMA when '[' tok = :LBRACK when ']' tok = :RBRACK when '{' tok = :LBRACE when '}' tok = :RBRACE when "\n" tok = :NEWLINE else tok = :ILLEGAL lit = ch end end if tok == :DOT @prevDot = true else @prevDot = false end return [tok, lit] end |
#scan_setup(src) ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/tp_plus/scanner.rb', line 6 def scan_setup(src) @src = src @lineno = 1 @ch = " " @offset = 0 @rdOffset = 0 @prevDot = false # for groups self.next end |
#scanComment ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/tp_plus/scanner.rb', line 100 def scanComment offs = @offset-1 # opening # already consumed while @ch != "\n" && @ch != -1 self.next end return @src[offs,(@offset-offs)] end |
#scanIdentifier ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/tp_plus/scanner.rb', line 60 def scanIdentifier offs = @offset while isLetter?(@ch) || isDigit?(@ch) self.next end # allow one ? or ! at end if @ch == '?' || @ch == '!' self.next end return @src[offs,(@offset-offs)] end |
#scanLabel ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/tp_plus/scanner.rb', line 121 def scanLabel offs = @offset while self.isLetter?(@ch) self.next end return @src[offs, (@offset-offs)] end |
#scanNumber ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/tp_plus/scanner.rb', line 83 def scanNumber offs = @offset while self.isDigit?(@ch) self.next end if @ch == '.' self.next while self.isDigit?(@ch) self.next end return [:REAL, @src[offs,(@offset-offs)].to_f] else return [:DIGIT, @src[offs,(@offset-offs)].to_i] end end |
#scanReal ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/tp_plus/scanner.rb', line 74 def scanReal offs = @offset-1 while self.isDigit?(@ch) self.next end return [:REAL, @src[offs,(@offset-offs)].to_f] end |
#scanString(type) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/tp_plus/scanner.rb', line 109 def scanString(type) offs = @offset while @ch != type && @ch != -1 self.next end # consume close self.next return @src[offs, (@offset-offs-1)] # -1 to remove trailing " or ' end |
#skipWhitespace ⇒ Object
54 55 56 57 58 |
# File 'lib/tp_plus/scanner.rb', line 54 def skipWhitespace while @ch == ' ' || @ch == "\t" || @ch == "\r" self.next end end |