1 # Lexer and its tokens.
2 # This file was generated by SableCC (http://www.sablecc.org/).
5 intrude import parser_nodes
9 var _text
: nullable String
14 if res
!= null then return res
25 fun parser_index
: Int is abstract
29 redef fun parser_index
: Int
34 init init_tk
(loc
: Location)
41 redef fun parser_index
: Int
46 init init_tk
(loc
: Location)
53 redef fun parser_index
: Int
58 init init_tk
(loc
: Location)
65 redef fun parser_index
: Int
70 init init_tk
(loc
: Location)
77 redef fun parser_index
: Int
82 init init_tk
(loc
: Location)
88 redef class TKwabstract
89 redef fun parser_index
: Int
94 init init_tk
(loc
: Location)
100 redef class TKwinterface
101 redef fun parser_index
: Int
106 init init_tk
(loc
: Location)
113 redef fun parser_index
: Int
118 init init_tk
(loc
: Location)
125 redef fun parser_index
: Int
130 init init_tk
(loc
: Location)
137 redef fun parser_index
: Int
142 init init_tk
(loc
: Location)
149 redef fun parser_index
: Int
154 init init_tk
(loc
: Location)
161 redef fun parser_index
: Int
166 init init_tk
(loc
: Location)
173 redef fun parser_index
: Int
178 init init_tk
(loc
: Location)
185 redef fun parser_index
: Int
190 init init_tk
(loc
: Location)
197 redef fun parser_index
: Int
202 init init_tk
(loc
: Location)
208 redef class TKwreadable
209 redef fun parser_index
: Int
214 init init_tk
(loc
: Location)
220 redef class TKwwritable
221 redef fun parser_index
: Int
226 init init_tk
(loc
: Location)
233 redef fun parser_index
: Int
238 init init_tk
(loc
: Location)
244 redef class TKwintern
245 redef fun parser_index
: Int
250 init init_tk
(loc
: Location)
256 redef class TKwextern
257 redef fun parser_index
: Int
262 init init_tk
(loc
: Location)
268 redef class TKwprotected
269 redef fun parser_index
: Int
274 init init_tk
(loc
: Location)
280 redef class TKwprivate
281 redef fun parser_index
: Int
286 init init_tk
(loc
: Location)
292 redef class TKwintrude
293 redef fun parser_index
: Int
298 init init_tk
(loc
: Location)
305 redef fun parser_index
: Int
310 init init_tk
(loc
: Location)
317 redef fun parser_index
: Int
322 init init_tk
(loc
: Location)
329 redef fun parser_index
: Int
334 init init_tk
(loc
: Location)
341 redef fun parser_index
: Int
346 init init_tk
(loc
: Location)
353 redef fun parser_index
: Int
358 init init_tk
(loc
: Location)
365 redef fun parser_index
: Int
370 init init_tk
(loc
: Location)
377 redef fun parser_index
: Int
382 init init_tk
(loc
: Location)
389 redef fun parser_index
: Int
394 init init_tk
(loc
: Location)
401 redef fun parser_index
: Int
406 init init_tk
(loc
: Location)
413 redef fun parser_index
: Int
418 init init_tk
(loc
: Location)
424 redef class TKwreturn
425 redef fun parser_index
: Int
430 init init_tk
(loc
: Location)
436 redef class TKwcontinue
437 redef fun parser_index
: Int
442 init init_tk
(loc
: Location)
449 redef fun parser_index
: Int
454 init init_tk
(loc
: Location)
461 redef fun parser_index
: Int
466 init init_tk
(loc
: Location)
472 redef class TKwassert
473 redef fun parser_index
: Int
478 init init_tk
(loc
: Location)
485 redef fun parser_index
: Int
490 init init_tk
(loc
: Location)
497 redef fun parser_index
: Int
502 init init_tk
(loc
: Location)
509 redef fun parser_index
: Int
514 init init_tk
(loc
: Location)
521 redef fun parser_index
: Int
526 init init_tk
(loc
: Location)
533 redef fun parser_index
: Int
538 init init_tk
(loc
: Location)
545 redef fun parser_index
: Int
550 init init_tk
(loc
: Location)
557 redef fun parser_index
: Int
562 init init_tk
(loc
: Location)
569 redef fun parser_index
: Int
574 init init_tk
(loc
: Location)
581 redef fun parser_index
: Int
586 init init_tk
(loc
: Location)
592 redef class TKwnullable
593 redef fun parser_index
: Int
598 init init_tk
(loc
: Location)
605 redef fun parser_index
: Int
610 init init_tk
(loc
: Location)
617 redef fun parser_index
: Int
622 init init_tk
(loc
: Location)
629 redef fun parser_index
: Int
634 init init_tk
(loc
: Location)
641 redef fun parser_index
: Int
646 init init_tk
(loc
: Location)
653 redef fun parser_index
: Int
658 init init_tk
(loc
: Location)
665 redef fun parser_index
: Int
670 init init_tk
(loc
: Location)
677 redef fun parser_index
: Int
682 init init_tk
(loc
: Location)
689 redef fun parser_index
: Int
694 init init_tk
(loc
: Location)
701 redef fun parser_index
: Int
706 init init_tk
(loc
: Location)
713 redef fun parser_index
: Int
718 init init_tk
(loc
: Location)
725 redef fun parser_index
: Int
730 init init_tk
(loc
: Location)
737 redef fun parser_index
: Int
742 init init_tk
(loc
: Location)
749 redef fun parser_index
: Int
754 init init_tk
(loc
: Location)
760 redef class TDotdotdot
761 redef fun parser_index
: Int
766 init init_tk
(loc
: Location)
773 redef fun parser_index
: Int
778 init init_tk
(loc
: Location)
785 redef fun parser_index
: Int
790 init init_tk
(loc
: Location)
797 redef fun parser_index
: Int
802 init init_tk
(loc
: Location)
809 redef fun parser_index
: Int
814 init init_tk
(loc
: Location)
821 redef fun parser_index
: Int
826 init init_tk
(loc
: Location)
833 redef fun parser_index
: Int
838 init init_tk
(loc
: Location)
845 redef fun parser_index
: Int
850 init init_tk
(loc
: Location)
857 redef fun parser_index
: Int
862 init init_tk
(loc
: Location)
869 redef fun parser_index
: Int
874 init init_tk
(loc
: Location)
881 redef fun parser_index
: Int
886 init init_tk
(loc
: Location)
893 redef fun parser_index
: Int
898 init init_tk
(loc
: Location)
905 redef fun parser_index
: Int
910 init init_tk
(loc
: Location)
917 redef fun parser_index
: Int
922 init init_tk
(loc
: Location)
929 redef fun parser_index
: Int
934 init init_tk
(loc
: Location)
941 redef fun parser_index
: Int
946 init init_tk
(loc
: Location)
952 redef class TStarship
953 redef fun parser_index
: Int
958 init init_tk
(loc
: Location)
965 redef fun parser_index
: Int
970 init init_tk
(loc
: Location)
977 redef fun parser_index
: Int
982 init init_tk
(loc
: Location)
989 redef fun parser_index
: Int
994 init init_tk
(loc
: Location)
1001 redef fun parser_index
: Int
1006 init init_tk
(loc
: Location)
1013 redef fun parser_index
: Int
1018 init init_tk
(loc
: Location)
1025 redef fun parser_index
: Int
1030 init init_tk
(loc
: Location)
1037 redef fun parser_index
: Int
1042 init init_tk
(loc
: Location)
1049 redef fun parser_index
: Int
1054 init init_tk
(loc
: Location)
1060 redef class TStartString
1061 redef fun parser_index
: Int
1066 init init_tk
(loc
: Location)
1072 redef class TMidString
1073 redef fun parser_index
: Int
1078 init init_tk
(loc
: Location)
1084 redef class TEndString
1085 redef fun parser_index
: Int
1090 init init_tk
(loc
: Location)
1097 redef fun parser_index
: Int
1102 init init_tk
(loc
: Location)
1108 redef class TBadString
1109 redef fun parser_index
: Int
1114 init init_tk
(loc
: Location)
1120 redef class TBadChar
1121 redef fun parser_index
: Int
1126 init init_tk
(loc
: Location)
1132 redef class TExternCodeSegment
1133 redef fun parser_index
: Int
1138 init init_tk
(loc
: Location)
1146 redef fun parser_index
: Int
1151 init init_tk
(loc
: Location)
1159 readable var _message
: String
1161 init init_error
(message
: String, loc
: Location)
1168 redef class ALexerError
1169 readable var _string
: String
1171 init init_lexer_error
(message
: String, loc
: Location, string
: String)
1173 init_error
(message
, loc
)
1178 redef class AParserError
1179 readable var _token
: Token
1181 init init_parser_error
(message
: String, loc
: Location, token
: Token)
1183 init_error
(message
, loc
)
1188 # The lexer extract NIT tokens from an input stream.
1189 # It is better user with the Parser
1193 var _token
: nullable Token
1195 # Lexer current state
1199 readable var _file
: SourceFile
1201 # Current character in the stream
1202 var _stream_pos
: Int = 0
1204 # Current line number in the input stream
1207 # Current column in the input stream
1210 # Was the last character a cariage-return?
1211 var _cr
: Bool = false
1213 # Constante state values
1214 private fun state_initial
: Int do return 0 end
1216 # Create a new lexer for a stream (and a name)
1217 init(file
: SourceFile)
1222 # Give the next token (but do not consume it)
1225 while _token
== null do
1228 return _token
.as(not null)
1231 # Give and consume the next token
1235 while result
== null do
1242 # Primitive method to return a token, or return null if it is discarded
1243 # Is used to implement `peek` and `next`
1244 protected fun get_token
: nullable Token
1248 var sp
= _stream_pos
1249 var start_stream_pos
= sp
1250 var start_pos
= _pos
1251 var start_line
= _line
1252 var string
= _file
.string
1253 var string_len
= string
.length
1255 var accept_state
= -1
1256 var accept_token
= -1
1257 var accept_length
= -1
1259 var accept_line
= -1
1262 if sp
>= string_len
then
1265 var c
= string
[sp
].ascii
1274 _file
.line_starts
[line
] = sp
1278 _file
.line_starts
[line
] = sp
1280 else if c
== 13 then
1284 _file
.line_starts
[line
] = sp
1291 var old_state
= dfa_state
1292 if dfa_state
< -1 then
1293 old_state
= -2 - dfa_state
1299 var high
= lexer_goto
(old_state
, 0) - 1
1302 while low
<= high
do
1303 var middle
= (low
+ high
) / 2
1304 var offset
= middle
* 3 + 1 # +1 because length is at 0
1306 if c
< lexer_goto
(old_state
, offset
) then
1308 else if c
> lexer_goto
(old_state
, offset
+1) then
1311 dfa_state
= lexer_goto
(old_state
, offset
+2)
1316 if dfa_state
> -2 then break
1324 if dfa_state
>= 0 then
1325 var tok
= lexer_accept
(dfa_state
)
1327 accept_state
= dfa_state
1329 accept_length
= sp
- start_stream_pos
1334 if accept_state
!= -1 then
1335 var location
= new Location(_file
, start_line
+ 1, accept_line
+ 1, start_pos
+ 1, accept_pos
)
1338 _stream_pos
= start_stream_pos
+ accept_length
1339 if accept_token
== 0 then
1342 if accept_token
== 1 then
1343 return new TEol.init_tk
(location
)
1345 if accept_token
== 2 then
1346 return new TComment.init_tk
(location
)
1348 if accept_token
== 3 then
1349 return new TKwmodule.init_tk
(location
)
1351 if accept_token
== 4 then
1352 return new TKwimport.init_tk
(location
)
1354 if accept_token
== 5 then
1355 return new TKwclass.init_tk
(location
)
1357 if accept_token
== 6 then
1358 return new TKwabstract.init_tk
(location
)
1360 if accept_token
== 7 then
1361 return new TKwinterface.init_tk
(location
)
1363 if accept_token
== 8 then
1364 return new TKwenum.init_tk
(location
)
1366 if accept_token
== 9 then
1367 return new TKwend.init_tk
(location
)
1369 if accept_token
== 10 then
1370 return new TKwmeth.init_tk
(location
)
1372 if accept_token
== 11 then
1373 return new TKwtype.init_tk
(location
)
1375 if accept_token
== 12 then
1376 return new TKwinit.init_tk
(location
)
1378 if accept_token
== 13 then
1379 return new TKwredef.init_tk
(location
)
1381 if accept_token
== 14 then
1382 return new TKwis.init_tk
(location
)
1384 if accept_token
== 15 then
1385 return new TKwdo.init_tk
(location
)
1387 if accept_token
== 16 then
1388 return new TKwreadable.init_tk
(location
)
1390 if accept_token
== 17 then
1391 return new TKwwritable.init_tk
(location
)
1393 if accept_token
== 18 then
1394 return new TKwvar.init_tk
(location
)
1396 if accept_token
== 19 then
1397 return new TKwintern.init_tk
(location
)
1399 if accept_token
== 20 then
1400 return new TKwextern.init_tk
(location
)
1402 if accept_token
== 21 then
1403 return new TKwprotected.init_tk
(location
)
1405 if accept_token
== 22 then
1406 return new TKwprivate.init_tk
(location
)
1408 if accept_token
== 23 then
1409 return new TKwintrude.init_tk
(location
)
1411 if accept_token
== 24 then
1412 return new TKwif.init_tk
(location
)
1414 if accept_token
== 25 then
1415 return new TKwthen.init_tk
(location
)
1417 if accept_token
== 26 then
1418 return new TKwelse.init_tk
(location
)
1420 if accept_token
== 27 then
1421 return new TKwwhile.init_tk
(location
)
1423 if accept_token
== 28 then
1424 return new TKwloop.init_tk
(location
)
1426 if accept_token
== 29 then
1427 return new TKwfor.init_tk
(location
)
1429 if accept_token
== 30 then
1430 return new TKwin.init_tk
(location
)
1432 if accept_token
== 31 then
1433 return new TKwand.init_tk
(location
)
1435 if accept_token
== 32 then
1436 return new TKwor.init_tk
(location
)
1438 if accept_token
== 33 then
1439 return new TKwnot.init_tk
(location
)
1441 if accept_token
== 34 then
1442 return new TKwreturn.init_tk
(location
)
1444 if accept_token
== 35 then
1445 return new TKwcontinue.init_tk
(location
)
1447 if accept_token
== 36 then
1448 return new TKwbreak.init_tk
(location
)
1450 if accept_token
== 37 then
1451 return new TKwabort.init_tk
(location
)
1453 if accept_token
== 38 then
1454 return new TKwassert.init_tk
(location
)
1456 if accept_token
== 39 then
1457 return new TKwnew.init_tk
(location
)
1459 if accept_token
== 40 then
1460 return new TKwisa.init_tk
(location
)
1462 if accept_token
== 41 then
1463 return new TKwonce.init_tk
(location
)
1465 if accept_token
== 42 then
1466 return new TKwsuper.init_tk
(location
)
1468 if accept_token
== 43 then
1469 return new TKwself.init_tk
(location
)
1471 if accept_token
== 44 then
1472 return new TKwtrue.init_tk
(location
)
1474 if accept_token
== 45 then
1475 return new TKwfalse.init_tk
(location
)
1477 if accept_token
== 46 then
1478 return new TKwnull.init_tk
(location
)
1480 if accept_token
== 47 then
1481 return new TKwas.init_tk
(location
)
1483 if accept_token
== 48 then
1484 return new TKwnullable.init_tk
(location
)
1486 if accept_token
== 49 then
1487 return new TKwisset.init_tk
(location
)
1489 if accept_token
== 50 then
1490 return new TKwlabel.init_tk
(location
)
1492 if accept_token
== 51 then
1493 return new TKwdebug.init_tk
(location
)
1495 if accept_token
== 52 then
1496 return new TOpar.init_tk
(location
)
1498 if accept_token
== 53 then
1499 return new TCpar.init_tk
(location
)
1501 if accept_token
== 54 then
1502 return new TObra.init_tk
(location
)
1504 if accept_token
== 55 then
1505 return new TCbra.init_tk
(location
)
1507 if accept_token
== 56 then
1508 return new TComma.init_tk
(location
)
1510 if accept_token
== 57 then
1511 return new TColumn.init_tk
(location
)
1513 if accept_token
== 58 then
1514 return new TQuad.init_tk
(location
)
1516 if accept_token
== 59 then
1517 return new TAssign.init_tk
(location
)
1519 if accept_token
== 60 then
1520 return new TPluseq.init_tk
(location
)
1522 if accept_token
== 61 then
1523 return new TMinuseq.init_tk
(location
)
1525 if accept_token
== 62 then
1526 return new TDotdotdot.init_tk
(location
)
1528 if accept_token
== 63 then
1529 return new TDotdot.init_tk
(location
)
1531 if accept_token
== 64 then
1532 return new TDot.init_tk
(location
)
1534 if accept_token
== 65 then
1535 return new TPlus.init_tk
(location
)
1537 if accept_token
== 66 then
1538 return new TMinus.init_tk
(location
)
1540 if accept_token
== 67 then
1541 return new TStar.init_tk
(location
)
1543 if accept_token
== 68 then
1544 return new TSlash.init_tk
(location
)
1546 if accept_token
== 69 then
1547 return new TPercent.init_tk
(location
)
1549 if accept_token
== 70 then
1550 return new TEq.init_tk
(location
)
1552 if accept_token
== 71 then
1553 return new TNe.init_tk
(location
)
1555 if accept_token
== 72 then
1556 return new TLt.init_tk
(location
)
1558 if accept_token
== 73 then
1559 return new TLe.init_tk
(location
)
1561 if accept_token
== 74 then
1562 return new TLl.init_tk
(location
)
1564 if accept_token
== 75 then
1565 return new TGt.init_tk
(location
)
1567 if accept_token
== 76 then
1568 return new TGe.init_tk
(location
)
1570 if accept_token
== 77 then
1571 return new TGg.init_tk
(location
)
1573 if accept_token
== 78 then
1574 return new TStarship.init_tk
(location
)
1576 if accept_token
== 79 then
1577 return new TBang.init_tk
(location
)
1579 if accept_token
== 80 then
1580 return new TAt.init_tk
(location
)
1582 if accept_token
== 81 then
1583 return new TClassid.init_tk
(location
)
1585 if accept_token
== 82 then
1586 return new TId.init_tk
(location
)
1588 if accept_token
== 83 then
1589 return new TAttrid.init_tk
(location
)
1591 if accept_token
== 84 then
1592 return new TNumber.init_tk
(location
)
1594 if accept_token
== 85 then
1595 return new TFloat.init_tk
(location
)
1597 if accept_token
== 86 then
1598 return new TString.init_tk
(location
)
1600 if accept_token
== 87 then
1601 return new TStartString.init_tk
(location
)
1603 if accept_token
== 88 then
1604 return new TMidString.init_tk
(location
)
1606 if accept_token
== 89 then
1607 return new TEndString.init_tk
(location
)
1609 if accept_token
== 90 then
1610 return new TChar.init_tk
(location
)
1612 if accept_token
== 91 then
1613 return new TBadString.init_tk
(location
)
1615 if accept_token
== 92 then
1616 return new TBadChar.init_tk
(location
)
1618 if accept_token
== 93 then
1619 return new TExternCodeSegment.init_tk
(location
)
1623 var location
= new Location(_file
, start_line
+ 1, start_line
+ 1, start_pos
+ 1, start_pos
+ 1)
1624 if sp
> start_stream_pos
then
1625 var text
= string
.substring
(start_stream_pos
, sp-start_stream_pos
)
1626 var token
= new ALexerError.init_lexer_error
("Syntax error: unknown token {text}.", location
, text
)
1629 var token
= new EOF.init_tk
(location
)