e66dc877f8fe552c3fc61147d9b318efdc36ca6b
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
1210 # Name of the stream (as given to tokens)
1211 readable var _filename
: String
1213 # Input stream where character are read
1214 var _stream
: IStream
1216 # Pushback buffer to store unread character
1217 var _stream_buf
: Buffer
1219 # Number of character stored in the pushback buffer
1220 var _stream_pos
: Int
1222 # Current line number in the input stream
1225 # Current column in the input stream
1228 # Was the last character a cariage-return?
1229 var _cr
: Bool = false
1231 # If the end of stream?
1232 var _eof
: Bool = false
1234 # Current working text read from the input stream
1237 # Constante state values
1238 private fun state_initial
: Int do return 0 end
1240 # Create a new lexer for a stream (and a name)
1241 init(stream
: IStream, fname
: String)
1247 _stream_buf
= new Buffer
1250 # Give the next token (but do not consume it)
1253 while _token
== null do
1256 return _token
.as(not null)
1259 # Give and consume the next token
1263 while result
== null do
1270 # Get a token, or null if it is discarded
1271 private fun get_token
: nullable Token
1275 var start_pos
= _pos
1276 var start_line
= _line
1278 var accept_state
= -1
1279 var accept_token
= -1
1280 var accept_length
= -1
1282 var accept_line
= -1
1301 else if c
== 13 then
1313 var old_state
= dfa_state
1314 if dfa_state
< -1 then
1315 old_state
= -2 - dfa_state
1321 var high
= lexer_goto
(old_state
, 0) - 1
1324 while low
<= high
do
1325 var middle
= (low
+ high
) / 2
1326 var offset
= middle
* 3 + 1 # +1 because length is at 0
1328 if c
< lexer_goto
(old_state
, offset
) then
1330 else if c
> lexer_goto
(old_state
, offset
+1) then
1333 dfa_state
= lexer_goto
(old_state
, offset
+2)
1338 if dfa_state
> -2 then break
1348 if dfa_state
>= 0 then
1349 var tok
= lexer_accept
(dfa_state
)
1351 accept_state
= dfa_state
1353 accept_length
= text
.length
1358 if accept_state
!= -1 then
1359 var location
= new Location(_filename
, start_line
+ 1, accept_line
+ 1, start_pos
+ 1, accept_pos
)
1362 push_back
(accept_length
)
1363 if accept_token
== 0 then
1366 if accept_token
== 1 then
1367 var token_text
= text
.substring
(0, accept_length
)
1368 return new TEol.init_tk
(token_text
, location
)
1370 if accept_token
== 2 then
1371 var token_text
= text
.substring
(0, accept_length
)
1372 return new TComment.init_tk
(token_text
, location
)
1374 if accept_token
== 3 then
1375 var token_text
= text
.substring
(0, accept_length
)
1376 return new TKwmodule.init_tk
(token_text
, location
)
1378 if accept_token
== 4 then
1379 return new TKwimport.init_tk
(location
)
1381 if accept_token
== 5 then
1382 return new TKwclass.init_tk
(location
)
1384 if accept_token
== 6 then
1385 return new TKwabstract.init_tk
(location
)
1387 if accept_token
== 7 then
1388 return new TKwinterface.init_tk
(location
)
1390 if accept_token
== 8 then
1391 var token_text
= text
.substring
(0, accept_length
)
1392 return new TKwenum.init_tk
(token_text
, location
)
1394 if accept_token
== 9 then
1395 return new TKwspecial.init_tk
(location
)
1397 if accept_token
== 10 then
1398 return new TKwend.init_tk
(location
)
1400 if accept_token
== 11 then
1401 return new TKwmeth.init_tk
(location
)
1403 if accept_token
== 12 then
1404 return new TKwtype.init_tk
(location
)
1406 if accept_token
== 13 then
1407 return new TKwinit.init_tk
(location
)
1409 if accept_token
== 14 then
1410 return new TKwredef.init_tk
(location
)
1412 if accept_token
== 15 then
1413 return new TKwis.init_tk
(location
)
1415 if accept_token
== 16 then
1416 return new TKwdo.init_tk
(location
)
1418 if accept_token
== 17 then
1419 return new TKwreadable.init_tk
(location
)
1421 if accept_token
== 18 then
1422 return new TKwwritable.init_tk
(location
)
1424 if accept_token
== 19 then
1425 return new TKwvar.init_tk
(location
)
1427 if accept_token
== 20 then
1428 return new TKwintern.init_tk
(location
)
1430 if accept_token
== 21 then
1431 return new TKwextern.init_tk
(location
)
1433 if accept_token
== 22 then
1434 return new TKwprotected.init_tk
(location
)
1436 if accept_token
== 23 then
1437 return new TKwprivate.init_tk
(location
)
1439 if accept_token
== 24 then
1440 return new TKwintrude.init_tk
(location
)
1442 if accept_token
== 25 then
1443 return new TKwif.init_tk
(location
)
1445 if accept_token
== 26 then
1446 return new TKwthen.init_tk
(location
)
1448 if accept_token
== 27 then
1449 return new TKwelse.init_tk
(location
)
1451 if accept_token
== 28 then
1452 return new TKwwhile.init_tk
(location
)
1454 if accept_token
== 29 then
1455 return new TKwloop.init_tk
(location
)
1457 if accept_token
== 30 then
1458 return new TKwfor.init_tk
(location
)
1460 if accept_token
== 31 then
1461 return new TKwin.init_tk
(location
)
1463 if accept_token
== 32 then
1464 return new TKwand.init_tk
(location
)
1466 if accept_token
== 33 then
1467 return new TKwor.init_tk
(location
)
1469 if accept_token
== 34 then
1470 return new TKwnot.init_tk
(location
)
1472 if accept_token
== 35 then
1473 return new TKwreturn.init_tk
(location
)
1475 if accept_token
== 36 then
1476 return new TKwcontinue.init_tk
(location
)
1478 if accept_token
== 37 then
1479 return new TKwbreak.init_tk
(location
)
1481 if accept_token
== 38 then
1482 return new TKwabort.init_tk
(location
)
1484 if accept_token
== 39 then
1485 return new TKwassert.init_tk
(location
)
1487 if accept_token
== 40 then
1488 return new TKwnew.init_tk
(location
)
1490 if accept_token
== 41 then
1491 return new TKwisa.init_tk
(location
)
1493 if accept_token
== 42 then
1494 return new TKwonce.init_tk
(location
)
1496 if accept_token
== 43 then
1497 return new TKwsuper.init_tk
(location
)
1499 if accept_token
== 44 then
1500 return new TKwself.init_tk
(location
)
1502 if accept_token
== 45 then
1503 return new TKwtrue.init_tk
(location
)
1505 if accept_token
== 46 then
1506 return new TKwfalse.init_tk
(location
)
1508 if accept_token
== 47 then
1509 return new TKwnull.init_tk
(location
)
1511 if accept_token
== 48 then
1512 return new TKwas.init_tk
(location
)
1514 if accept_token
== 49 then
1515 return new TKwnullable.init_tk
(location
)
1517 if accept_token
== 50 then
1518 return new TKwisset.init_tk
(location
)
1520 if accept_token
== 51 then
1521 return new TKwlabel.init_tk
(location
)
1523 if accept_token
== 52 then
1524 return new TOpar.init_tk
(location
)
1526 if accept_token
== 53 then
1527 return new TCpar.init_tk
(location
)
1529 if accept_token
== 54 then
1530 return new TObra.init_tk
(location
)
1532 if accept_token
== 55 then
1533 return new TCbra.init_tk
(location
)
1535 if accept_token
== 56 then
1536 return new TComma.init_tk
(location
)
1538 if accept_token
== 57 then
1539 return new TColumn.init_tk
(location
)
1541 if accept_token
== 58 then
1542 return new TQuad.init_tk
(location
)
1544 if accept_token
== 59 then
1545 return new TAssign.init_tk
(location
)
1547 if accept_token
== 60 then
1548 return new TPluseq.init_tk
(location
)
1550 if accept_token
== 61 then
1551 return new TMinuseq.init_tk
(location
)
1553 if accept_token
== 62 then
1554 return new TDotdotdot.init_tk
(location
)
1556 if accept_token
== 63 then
1557 return new TDotdot.init_tk
(location
)
1559 if accept_token
== 64 then
1560 return new TDot.init_tk
(location
)
1562 if accept_token
== 65 then
1563 return new TPlus.init_tk
(location
)
1565 if accept_token
== 66 then
1566 return new TMinus.init_tk
(location
)
1568 if accept_token
== 67 then
1569 return new TStar.init_tk
(location
)
1571 if accept_token
== 68 then
1572 return new TSlash.init_tk
(location
)
1574 if accept_token
== 69 then
1575 return new TPercent.init_tk
(location
)
1577 if accept_token
== 70 then
1578 return new TEq.init_tk
(location
)
1580 if accept_token
== 71 then
1581 return new TNe.init_tk
(location
)
1583 if accept_token
== 72 then
1584 return new TLt.init_tk
(location
)
1586 if accept_token
== 73 then
1587 return new TLe.init_tk
(location
)
1589 if accept_token
== 74 then
1590 return new TLl.init_tk
(location
)
1592 if accept_token
== 75 then
1593 return new TGt.init_tk
(location
)
1595 if accept_token
== 76 then
1596 return new TGe.init_tk
(location
)
1598 if accept_token
== 77 then
1599 return new TGg.init_tk
(location
)
1601 if accept_token
== 78 then
1602 return new TStarship.init_tk
(location
)
1604 if accept_token
== 79 then
1605 return new TBang.init_tk
(location
)
1607 if accept_token
== 80 then
1608 var token_text
= text
.substring
(0, accept_length
)
1609 return new TClassid.init_tk
(token_text
, location
)
1611 if accept_token
== 81 then
1612 var token_text
= text
.substring
(0, accept_length
)
1613 return new TId.init_tk
(token_text
, location
)
1615 if accept_token
== 82 then
1616 var token_text
= text
.substring
(0, accept_length
)
1617 return new TAttrid.init_tk
(token_text
, location
)
1619 if accept_token
== 83 then
1620 var token_text
= text
.substring
(0, accept_length
)
1621 return new TNumber.init_tk
(token_text
, location
)
1623 if accept_token
== 84 then
1624 var token_text
= text
.substring
(0, accept_length
)
1625 return new TFloat.init_tk
(token_text
, location
)
1627 if accept_token
== 85 then
1628 var token_text
= text
.substring
(0, accept_length
)
1629 return new TChar.init_tk
(token_text
, location
)
1631 if accept_token
== 86 then
1632 var token_text
= text
.substring
(0, accept_length
)
1633 return new TString.init_tk
(token_text
, location
)
1635 if accept_token
== 87 then
1636 var token_text
= text
.substring
(0, accept_length
)
1637 return new TStartString.init_tk
(token_text
, location
)
1639 if accept_token
== 88 then
1640 var token_text
= text
.substring
(0, accept_length
)
1641 return new TMidString.init_tk
(token_text
, location
)
1643 if accept_token
== 89 then
1644 var token_text
= text
.substring
(0, accept_length
)
1645 return new TEndString.init_tk
(token_text
, location
)
1648 var location
= new Location(_filename
, start_line
+ 1, start_line
+ 1, start_pos
+ 1, start_pos
+ 1)
1649 if text
.length
> 0 then
1650 var token
= new AError.init_error
("Syntax error: unknown token {text}.", location
)
1653 var token
= new EOF(location
)
1661 # Read the next character.
1662 # The character is read from the stream of from the pushback buffer.
1663 private fun get_char
: Int
1671 var sp
= _stream_pos
1673 var res
= _stream_buf
[_stream_pos
]
1674 _stream_pos
= sp
- 1
1677 result
= _stream
.read_char
1680 if result
== -1 then
1687 # Unread some characters.
1688 # Unread characters are stored in the pushback buffer.
1689 private fun push_back
(accept_length
: Int)
1691 var length
= _text
.length
1693 while i
>= accept_length
do
1695 _stream_pos
= _stream_pos
+ 1
1696 _stream_buf
[_stream_pos
] = _text
[i
]