1 # Lexer and its tokens.
2 # This file was generated by SableCC (http://www.sablecc.org/).
5 intrude import parser_nodes
9 readable var _text
: String
11 fun parser_index
: Int is abstract
13 redef fun to_s
: String do
19 redef fun parser_index
: Int
24 init init_tk
(text
: String, loc
: Location)
32 redef fun parser_index
: Int
37 init init_tk
(text
: String, loc
: Location)
45 redef fun parser_index
: Int
50 init init_tk
(text
: String, loc
: Location)
58 redef fun parser_index
: Int
63 init init_tk
(loc
: Location)
71 redef fun parser_index
: Int
76 init init_tk
(loc
: Location)
83 redef class TKwabstract
84 redef fun parser_index
: Int
89 init init_tk
(loc
: Location)
91 _text
= once
"abstract"
96 redef class TKwinterface
97 redef fun parser_index
: Int
102 init init_tk
(loc
: Location)
104 _text
= once
"interface"
110 redef fun parser_index
: Int
115 init init_tk
(text
: String, loc
: Location)
122 redef class TKwspecial
123 redef fun parser_index
: Int
128 init init_tk
(loc
: Location)
130 _text
= once
"special"
136 redef fun parser_index
: Int
141 init init_tk
(loc
: Location)
149 redef fun parser_index
: Int
154 init init_tk
(loc
: Location)
162 redef fun parser_index
: Int
167 init init_tk
(loc
: Location)
175 redef fun parser_index
: Int
180 init init_tk
(loc
: Location)
188 redef fun parser_index
: Int
193 init init_tk
(loc
: Location)
201 redef fun parser_index
: Int
206 init init_tk
(loc
: Location)
214 redef fun parser_index
: Int
219 init init_tk
(loc
: Location)
226 redef class TKwreadable
227 redef fun parser_index
: Int
232 init init_tk
(loc
: Location)
234 _text
= once
"readable"
239 redef class TKwwritable
240 redef fun parser_index
: Int
245 init init_tk
(loc
: Location)
247 _text
= once
"writable"
253 redef fun parser_index
: Int
258 init init_tk
(loc
: Location)
265 redef class TKwintern
266 redef fun parser_index
: Int
271 init init_tk
(loc
: Location)
273 _text
= once
"intern"
278 redef class TKwextern
279 redef fun parser_index
: Int
284 init init_tk
(loc
: Location)
286 _text
= once
"extern"
291 redef class TKwprotected
292 redef fun parser_index
: Int
297 init init_tk
(loc
: Location)
299 _text
= once
"protected"
304 redef class TKwprivate
305 redef fun parser_index
: Int
310 init init_tk
(loc
: Location)
312 _text
= once
"private"
317 redef class TKwintrude
318 redef fun parser_index
: Int
323 init init_tk
(loc
: Location)
325 _text
= once
"intrude"
331 redef fun parser_index
: Int
336 init init_tk
(loc
: Location)
344 redef fun parser_index
: Int
349 init init_tk
(loc
: Location)
357 redef fun parser_index
: Int
362 init init_tk
(loc
: Location)
370 redef fun parser_index
: Int
375 init init_tk
(loc
: Location)
383 redef fun parser_index
: Int
388 init init_tk
(loc
: Location)
396 redef fun parser_index
: Int
401 init init_tk
(loc
: Location)
409 redef fun parser_index
: Int
414 init init_tk
(loc
: Location)
422 redef fun parser_index
: Int
427 init init_tk
(loc
: Location)
435 redef fun parser_index
: Int
440 init init_tk
(loc
: Location)
448 redef fun parser_index
: Int
453 init init_tk
(loc
: Location)
460 redef class TKwreturn
461 redef fun parser_index
: Int
466 init init_tk
(loc
: Location)
468 _text
= once
"return"
473 redef class TKwcontinue
474 redef fun parser_index
: Int
479 init init_tk
(loc
: Location)
481 _text
= once
"continue"
487 redef fun parser_index
: Int
492 init init_tk
(loc
: Location)
500 redef fun parser_index
: Int
505 init init_tk
(loc
: Location)
512 redef class TKwassert
513 redef fun parser_index
: Int
518 init init_tk
(loc
: Location)
520 _text
= once
"assert"
526 redef fun parser_index
: Int
531 init init_tk
(loc
: Location)
539 redef fun parser_index
: Int
544 init init_tk
(loc
: Location)
552 redef fun parser_index
: Int
557 init init_tk
(loc
: Location)
565 redef fun parser_index
: Int
570 init init_tk
(loc
: Location)
578 redef fun parser_index
: Int
583 init init_tk
(loc
: Location)
591 redef fun parser_index
: Int
596 init init_tk
(loc
: Location)
604 redef fun parser_index
: Int
609 init init_tk
(loc
: Location)
617 redef fun parser_index
: Int
622 init init_tk
(loc
: Location)
630 redef fun parser_index
: Int
635 init init_tk
(loc
: Location)
642 redef class TKwnullable
643 redef fun parser_index
: Int
648 init init_tk
(loc
: Location)
650 _text
= once
"nullable"
656 redef fun parser_index
: Int
661 init init_tk
(loc
: Location)
669 redef fun parser_index
: Int
674 init init_tk
(loc
: Location)
682 redef fun parser_index
: Int
687 init init_tk
(loc
: Location)
695 redef fun parser_index
: Int
700 init init_tk
(loc
: Location)
708 redef fun parser_index
: Int
713 init init_tk
(loc
: Location)
721 redef fun parser_index
: Int
726 init init_tk
(loc
: Location)
734 redef fun parser_index
: Int
739 init init_tk
(loc
: Location)
747 redef fun parser_index
: Int
752 init init_tk
(loc
: Location)
760 redef fun parser_index
: Int
765 init init_tk
(loc
: Location)
773 redef fun parser_index
: Int
778 init init_tk
(loc
: Location)
786 redef fun parser_index
: Int
791 init init_tk
(loc
: Location)
799 redef fun parser_index
: Int
804 init init_tk
(loc
: Location)
811 redef class TDotdotdot
812 redef fun parser_index
: Int
817 init init_tk
(loc
: Location)
825 redef fun parser_index
: Int
830 init init_tk
(loc
: Location)
838 redef fun parser_index
: Int
843 init init_tk
(loc
: Location)
851 redef fun parser_index
: Int
856 init init_tk
(loc
: Location)
864 redef fun parser_index
: Int
869 init init_tk
(loc
: Location)
877 redef fun parser_index
: Int
882 init init_tk
(loc
: Location)
890 redef fun parser_index
: Int
895 init init_tk
(loc
: Location)
903 redef fun parser_index
: Int
908 init init_tk
(loc
: Location)
916 redef fun parser_index
: Int
921 init init_tk
(loc
: Location)
929 redef fun parser_index
: Int
934 init init_tk
(loc
: Location)
942 redef fun parser_index
: Int
947 init init_tk
(loc
: Location)
955 redef fun parser_index
: Int
960 init init_tk
(loc
: Location)
968 redef fun parser_index
: Int
973 init init_tk
(loc
: Location)
981 redef fun parser_index
: Int
986 init init_tk
(loc
: Location)
994 redef fun parser_index
: Int
999 init init_tk
(loc
: Location)
1007 redef fun parser_index
: Int
1012 init init_tk
(loc
: Location)
1019 redef class TStarship
1020 redef fun parser_index
: Int
1025 init init_tk
(loc
: Location)
1033 redef fun parser_index
: Int
1038 init init_tk
(loc
: Location)
1045 redef class TClassid
1046 redef fun parser_index
: Int
1051 init init_tk
(text
: String, loc
: Location)
1059 redef fun parser_index
: Int
1064 init init_tk
(text
: String, loc
: Location)
1072 redef fun parser_index
: Int
1077 init init_tk
(text
: String, loc
: Location)
1085 redef fun parser_index
: Int
1090 init init_tk
(text
: String, loc
: Location)
1098 redef fun parser_index
: Int
1103 init init_tk
(text
: String, loc
: Location)
1111 redef fun parser_index
: Int
1116 init init_tk
(text
: String, loc
: Location)
1124 redef fun parser_index
: Int
1129 init init_tk
(text
: String, loc
: Location)
1136 redef class TStartString
1137 redef fun parser_index
: Int
1142 init init_tk
(text
: String, loc
: Location)
1149 redef class TMidString
1150 redef fun parser_index
: Int
1155 init init_tk
(text
: String, loc
: Location)
1162 redef class TEndString
1163 redef fun parser_index
: Int
1168 init init_tk
(text
: String, loc
: Location)
1177 redef fun parser_index
: Int
1190 readable var _message
: String
1192 init init_error
(message
: String, loc
: Location)
1200 # The lexer extract NIT tokens from an input stream.
1201 # It is better user with the Parser
1205 var _token
: nullable Token
1207 # Lexer current state
1211 readable var _file
: SourceFile
1213 # Current character in the stream
1214 var _stream_pos
: Int = 0
1216 # Current line number in the input stream
1219 # Current column in the input stream
1222 # Was the last character a cariage-return?
1223 var _cr
: Bool = false
1225 # Constante state values
1226 private fun state_initial
: Int do return 0 end
1228 # Create a new lexer for a stream (and a name)
1229 init(file
: SourceFile)
1234 # Give the next token (but do not consume it)
1237 while _token
== null do
1240 return _token
.as(not null)
1243 # Give and consume the next token
1247 while result
== null do
1254 # Get a token, or null if it is discarded
1255 private fun get_token
: nullable Token
1259 var sp
= _stream_pos
1260 var start_stream_pos
= sp
1261 var start_pos
= _pos
1262 var start_line
= _line
1263 var string
= _file
.string
1264 var string_len
= string
.length
1266 var accept_state
= -1
1267 var accept_token
= -1
1268 var accept_length
= -1
1270 var accept_line
= -1
1273 if sp
>= string_len
then
1276 var c
= string
[sp
].ascii
1285 _file
.line_starts
[line
] = sp
1289 _file
.line_starts
[line
] = sp
1291 else if c
== 13 then
1295 _file
.line_starts
[line
] = sp
1302 var old_state
= dfa_state
1303 if dfa_state
< -1 then
1304 old_state
= -2 - dfa_state
1310 var high
= lexer_goto
(old_state
, 0) - 1
1313 while low
<= high
do
1314 var middle
= (low
+ high
) / 2
1315 var offset
= middle
* 3 + 1 # +1 because length is at 0
1317 if c
< lexer_goto
(old_state
, offset
) then
1319 else if c
> lexer_goto
(old_state
, offset
+1) then
1322 dfa_state
= lexer_goto
(old_state
, offset
+2)
1327 if dfa_state
> -2 then break
1335 if dfa_state
>= 0 then
1336 var tok
= lexer_accept
(dfa_state
)
1338 accept_state
= dfa_state
1340 accept_length
= sp
- start_stream_pos
1345 if accept_state
!= -1 then
1346 var location
= new Location(_file
, start_line
+ 1, accept_line
+ 1, start_pos
+ 1, accept_pos
)
1349 _stream_pos
= start_stream_pos
+ accept_length
1350 if accept_token
== 0 then
1353 if accept_token
== 1 then
1354 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1355 return new TEol.init_tk
(token_text
, location
)
1357 if accept_token
== 2 then
1358 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1359 return new TComment.init_tk
(token_text
, location
)
1361 if accept_token
== 3 then
1362 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1363 return new TKwmodule.init_tk
(token_text
, location
)
1365 if accept_token
== 4 then
1366 return new TKwimport.init_tk
(location
)
1368 if accept_token
== 5 then
1369 return new TKwclass.init_tk
(location
)
1371 if accept_token
== 6 then
1372 return new TKwabstract.init_tk
(location
)
1374 if accept_token
== 7 then
1375 return new TKwinterface.init_tk
(location
)
1377 if accept_token
== 8 then
1378 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1379 return new TKwenum.init_tk
(token_text
, location
)
1381 if accept_token
== 9 then
1382 return new TKwspecial.init_tk
(location
)
1384 if accept_token
== 10 then
1385 return new TKwend.init_tk
(location
)
1387 if accept_token
== 11 then
1388 return new TKwmeth.init_tk
(location
)
1390 if accept_token
== 12 then
1391 return new TKwtype.init_tk
(location
)
1393 if accept_token
== 13 then
1394 return new TKwinit.init_tk
(location
)
1396 if accept_token
== 14 then
1397 return new TKwredef.init_tk
(location
)
1399 if accept_token
== 15 then
1400 return new TKwis.init_tk
(location
)
1402 if accept_token
== 16 then
1403 return new TKwdo.init_tk
(location
)
1405 if accept_token
== 17 then
1406 return new TKwreadable.init_tk
(location
)
1408 if accept_token
== 18 then
1409 return new TKwwritable.init_tk
(location
)
1411 if accept_token
== 19 then
1412 return new TKwvar.init_tk
(location
)
1414 if accept_token
== 20 then
1415 return new TKwintern.init_tk
(location
)
1417 if accept_token
== 21 then
1418 return new TKwextern.init_tk
(location
)
1420 if accept_token
== 22 then
1421 return new TKwprotected.init_tk
(location
)
1423 if accept_token
== 23 then
1424 return new TKwprivate.init_tk
(location
)
1426 if accept_token
== 24 then
1427 return new TKwintrude.init_tk
(location
)
1429 if accept_token
== 25 then
1430 return new TKwif.init_tk
(location
)
1432 if accept_token
== 26 then
1433 return new TKwthen.init_tk
(location
)
1435 if accept_token
== 27 then
1436 return new TKwelse.init_tk
(location
)
1438 if accept_token
== 28 then
1439 return new TKwwhile.init_tk
(location
)
1441 if accept_token
== 29 then
1442 return new TKwloop.init_tk
(location
)
1444 if accept_token
== 30 then
1445 return new TKwfor.init_tk
(location
)
1447 if accept_token
== 31 then
1448 return new TKwin.init_tk
(location
)
1450 if accept_token
== 32 then
1451 return new TKwand.init_tk
(location
)
1453 if accept_token
== 33 then
1454 return new TKwor.init_tk
(location
)
1456 if accept_token
== 34 then
1457 return new TKwnot.init_tk
(location
)
1459 if accept_token
== 35 then
1460 return new TKwreturn.init_tk
(location
)
1462 if accept_token
== 36 then
1463 return new TKwcontinue.init_tk
(location
)
1465 if accept_token
== 37 then
1466 return new TKwbreak.init_tk
(location
)
1468 if accept_token
== 38 then
1469 return new TKwabort.init_tk
(location
)
1471 if accept_token
== 39 then
1472 return new TKwassert.init_tk
(location
)
1474 if accept_token
== 40 then
1475 return new TKwnew.init_tk
(location
)
1477 if accept_token
== 41 then
1478 return new TKwisa.init_tk
(location
)
1480 if accept_token
== 42 then
1481 return new TKwonce.init_tk
(location
)
1483 if accept_token
== 43 then
1484 return new TKwsuper.init_tk
(location
)
1486 if accept_token
== 44 then
1487 return new TKwself.init_tk
(location
)
1489 if accept_token
== 45 then
1490 return new TKwtrue.init_tk
(location
)
1492 if accept_token
== 46 then
1493 return new TKwfalse.init_tk
(location
)
1495 if accept_token
== 47 then
1496 return new TKwnull.init_tk
(location
)
1498 if accept_token
== 48 then
1499 return new TKwas.init_tk
(location
)
1501 if accept_token
== 49 then
1502 return new TKwnullable.init_tk
(location
)
1504 if accept_token
== 50 then
1505 return new TKwisset.init_tk
(location
)
1507 if accept_token
== 51 then
1508 return new TKwlabel.init_tk
(location
)
1510 if accept_token
== 52 then
1511 return new TOpar.init_tk
(location
)
1513 if accept_token
== 53 then
1514 return new TCpar.init_tk
(location
)
1516 if accept_token
== 54 then
1517 return new TObra.init_tk
(location
)
1519 if accept_token
== 55 then
1520 return new TCbra.init_tk
(location
)
1522 if accept_token
== 56 then
1523 return new TComma.init_tk
(location
)
1525 if accept_token
== 57 then
1526 return new TColumn.init_tk
(location
)
1528 if accept_token
== 58 then
1529 return new TQuad.init_tk
(location
)
1531 if accept_token
== 59 then
1532 return new TAssign.init_tk
(location
)
1534 if accept_token
== 60 then
1535 return new TPluseq.init_tk
(location
)
1537 if accept_token
== 61 then
1538 return new TMinuseq.init_tk
(location
)
1540 if accept_token
== 62 then
1541 return new TDotdotdot.init_tk
(location
)
1543 if accept_token
== 63 then
1544 return new TDotdot.init_tk
(location
)
1546 if accept_token
== 64 then
1547 return new TDot.init_tk
(location
)
1549 if accept_token
== 65 then
1550 return new TPlus.init_tk
(location
)
1552 if accept_token
== 66 then
1553 return new TMinus.init_tk
(location
)
1555 if accept_token
== 67 then
1556 return new TStar.init_tk
(location
)
1558 if accept_token
== 68 then
1559 return new TSlash.init_tk
(location
)
1561 if accept_token
== 69 then
1562 return new TPercent.init_tk
(location
)
1564 if accept_token
== 70 then
1565 return new TEq.init_tk
(location
)
1567 if accept_token
== 71 then
1568 return new TNe.init_tk
(location
)
1570 if accept_token
== 72 then
1571 return new TLt.init_tk
(location
)
1573 if accept_token
== 73 then
1574 return new TLe.init_tk
(location
)
1576 if accept_token
== 74 then
1577 return new TLl.init_tk
(location
)
1579 if accept_token
== 75 then
1580 return new TGt.init_tk
(location
)
1582 if accept_token
== 76 then
1583 return new TGe.init_tk
(location
)
1585 if accept_token
== 77 then
1586 return new TGg.init_tk
(location
)
1588 if accept_token
== 78 then
1589 return new TStarship.init_tk
(location
)
1591 if accept_token
== 79 then
1592 return new TBang.init_tk
(location
)
1594 if accept_token
== 80 then
1595 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1596 return new TClassid.init_tk
(token_text
, location
)
1598 if accept_token
== 81 then
1599 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1600 return new TId.init_tk
(token_text
, location
)
1602 if accept_token
== 82 then
1603 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1604 return new TAttrid.init_tk
(token_text
, location
)
1606 if accept_token
== 83 then
1607 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1608 return new TNumber.init_tk
(token_text
, location
)
1610 if accept_token
== 84 then
1611 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1612 return new TFloat.init_tk
(token_text
, location
)
1614 if accept_token
== 85 then
1615 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1616 return new TChar.init_tk
(token_text
, location
)
1618 if accept_token
== 86 then
1619 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1620 return new TString.init_tk
(token_text
, location
)
1622 if accept_token
== 87 then
1623 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1624 return new TStartString.init_tk
(token_text
, location
)
1626 if accept_token
== 88 then
1627 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1628 return new TMidString.init_tk
(token_text
, location
)
1630 if accept_token
== 89 then
1631 var token_text
= string
.substring
(start_stream_pos
, accept_length
)
1632 return new TEndString.init_tk
(token_text
, location
)
1636 var location
= new Location(_file
, start_line
+ 1, start_line
+ 1, start_pos
+ 1, start_pos
+ 1)
1637 if sp
> start_stream_pos
then
1638 var text
= string
.substring
(start_stream_pos
, sp-start_stream_pos
)
1639 var token
= new AError.init_error
("Syntax error: unknown token {text}.", location
)
1642 var token
= new EOF(location
)