2014-07-14 03:52:18 -05:00
|
|
|
lexer grammar RustLexer;
|
|
|
|
|
2015-04-21 05:02:12 -05:00
|
|
|
@lexer::members {
|
|
|
|
public boolean is_at(int pos) {
|
|
|
|
return _input.index() == pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-14 16:13:38 -05:00
|
|
|
tokens {
|
2015-05-05 18:26:16 -05:00
|
|
|
EQ, LT, LE, EQEQ, NE, GE, GT, ANDAND, OROR, NOT, TILDE, PLUS,
|
2014-07-14 16:13:38 -05:00
|
|
|
MINUS, STAR, SLASH, PERCENT, CARET, AND, OR, SHL, SHR, BINOP,
|
2015-10-27 15:05:23 -05:00
|
|
|
BINOPEQ, LARROW, AT, DOT, DOTDOT, DOTDOTDOT, COMMA, SEMI, COLON,
|
2014-07-14 16:13:38 -05:00
|
|
|
MOD_SEP, RARROW, FAT_ARROW, LPAREN, RPAREN, LBRACKET, RBRACKET,
|
2015-05-05 18:29:58 -05:00
|
|
|
LBRACE, RBRACE, POUND, DOLLAR, UNDERSCORE, LIT_CHAR, LIT_BYTE,
|
2015-09-03 02:54:53 -05:00
|
|
|
LIT_INTEGER, LIT_FLOAT, LIT_STR, LIT_STR_RAW, LIT_BYTE_STR,
|
|
|
|
LIT_BYTE_STR_RAW, QUESTION, IDENT, LIFETIME, WHITESPACE, DOC_COMMENT,
|
2015-05-05 18:29:58 -05:00
|
|
|
COMMENT, SHEBANG, UTF8_BOM
|
2014-07-14 16:13:38 -05:00
|
|
|
}
|
|
|
|
|
2015-01-14 16:51:51 -06:00
|
|
|
import xidstart , xidcontinue;
|
2014-07-14 03:52:18 -05:00
|
|
|
|
2014-07-14 16:13:38 -05:00
|
|
|
|
2014-07-14 03:52:18 -05:00
|
|
|
/* Expression-operator symbols */
|
|
|
|
|
|
|
|
EQ : '=' ;
|
|
|
|
LT : '<' ;
|
|
|
|
LE : '<=' ;
|
|
|
|
EQEQ : '==' ;
|
|
|
|
NE : '!=' ;
|
|
|
|
GE : '>=' ;
|
|
|
|
GT : '>' ;
|
|
|
|
ANDAND : '&&' ;
|
|
|
|
OROR : '||' ;
|
|
|
|
NOT : '!' ;
|
|
|
|
TILDE : '~' ;
|
|
|
|
PLUS : '+' ;
|
|
|
|
MINUS : '-' ;
|
|
|
|
STAR : '*' ;
|
|
|
|
SLASH : '/' ;
|
|
|
|
PERCENT : '%' ;
|
|
|
|
CARET : '^' ;
|
|
|
|
AND : '&' ;
|
|
|
|
OR : '|' ;
|
|
|
|
SHL : '<<' ;
|
|
|
|
SHR : '>>' ;
|
2015-10-27 15:05:23 -05:00
|
|
|
LARROW : '<-' ;
|
2014-07-14 03:52:18 -05:00
|
|
|
|
|
|
|
BINOP
|
|
|
|
: PLUS
|
2014-07-14 22:45:39 -05:00
|
|
|
| SLASH
|
2014-07-14 03:52:18 -05:00
|
|
|
| MINUS
|
|
|
|
| STAR
|
|
|
|
| PERCENT
|
|
|
|
| CARET
|
|
|
|
| AND
|
|
|
|
| OR
|
|
|
|
| SHL
|
|
|
|
| SHR
|
2015-10-27 15:05:23 -05:00
|
|
|
| LARROW
|
2014-07-14 03:52:18 -05:00
|
|
|
;
|
|
|
|
|
|
|
|
BINOPEQ : BINOP EQ ;
|
|
|
|
|
|
|
|
/* "Structural symbols" */
|
|
|
|
|
|
|
|
AT : '@' ;
|
|
|
|
DOT : '.' ;
|
|
|
|
DOTDOT : '..' ;
|
|
|
|
DOTDOTDOT : '...' ;
|
|
|
|
COMMA : ',' ;
|
|
|
|
SEMI : ';' ;
|
|
|
|
COLON : ':' ;
|
|
|
|
MOD_SEP : '::' ;
|
|
|
|
RARROW : '->' ;
|
|
|
|
FAT_ARROW : '=>' ;
|
|
|
|
LPAREN : '(' ;
|
|
|
|
RPAREN : ')' ;
|
|
|
|
LBRACKET : '[' ;
|
|
|
|
RBRACKET : ']' ;
|
|
|
|
LBRACE : '{' ;
|
|
|
|
RBRACE : '}' ;
|
|
|
|
POUND : '#';
|
|
|
|
DOLLAR : '$' ;
|
|
|
|
UNDERSCORE : '_' ;
|
|
|
|
|
|
|
|
// Literals
|
|
|
|
|
|
|
|
fragment HEXIT
|
|
|
|
: [0-9a-fA-F]
|
|
|
|
;
|
|
|
|
|
|
|
|
fragment CHAR_ESCAPE
|
|
|
|
: [nrt\\'"0]
|
|
|
|
| [xX] HEXIT HEXIT
|
|
|
|
| 'u' HEXIT HEXIT HEXIT HEXIT
|
|
|
|
| 'U' HEXIT HEXIT HEXIT HEXIT HEXIT HEXIT HEXIT HEXIT
|
2015-04-21 05:02:12 -05:00
|
|
|
| 'u{' HEXIT '}'
|
|
|
|
| 'u{' HEXIT HEXIT '}'
|
|
|
|
| 'u{' HEXIT HEXIT HEXIT '}'
|
|
|
|
| 'u{' HEXIT HEXIT HEXIT HEXIT '}'
|
|
|
|
| 'u{' HEXIT HEXIT HEXIT HEXIT HEXIT '}'
|
|
|
|
| 'u{' HEXIT HEXIT HEXIT HEXIT HEXIT HEXIT '}'
|
2014-07-14 03:52:18 -05:00
|
|
|
;
|
|
|
|
|
2014-11-19 03:25:48 -06:00
|
|
|
fragment SUFFIX
|
|
|
|
: IDENT
|
2014-07-14 03:52:18 -05:00
|
|
|
;
|
|
|
|
|
2015-04-21 05:02:12 -05:00
|
|
|
fragment INTEGER_SUFFIX
|
|
|
|
: { _input.LA(1) != 'e' && _input.LA(1) != 'E' }? SUFFIX
|
|
|
|
;
|
|
|
|
|
2014-11-19 03:25:48 -06:00
|
|
|
LIT_CHAR
|
2015-04-21 05:02:12 -05:00
|
|
|
: '\'' ( '\\' CHAR_ESCAPE
|
|
|
|
| ~[\\'\n\t\r]
|
|
|
|
| '\ud800' .. '\udbff' '\udc00' .. '\udfff'
|
|
|
|
)
|
|
|
|
'\'' SUFFIX?
|
2014-07-14 22:45:39 -05:00
|
|
|
;
|
|
|
|
|
2014-11-19 03:25:48 -06:00
|
|
|
LIT_BYTE
|
2015-04-21 05:02:12 -05:00
|
|
|
: 'b\'' ( '\\' ( [xX] HEXIT HEXIT
|
|
|
|
| [nrt\\'"0] )
|
|
|
|
| ~[\\'\n\t\r] '\udc00'..'\udfff'?
|
|
|
|
)
|
|
|
|
'\'' SUFFIX?
|
2014-07-14 03:52:18 -05:00
|
|
|
;
|
|
|
|
|
|
|
|
LIT_INTEGER
|
2015-04-21 05:02:12 -05:00
|
|
|
|
|
|
|
: [0-9][0-9_]* INTEGER_SUFFIX?
|
|
|
|
| '0b' [01_]+ INTEGER_SUFFIX?
|
|
|
|
| '0o' [0-7_]+ INTEGER_SUFFIX?
|
|
|
|
| '0x' [0-9a-fA-F_]+ INTEGER_SUFFIX?
|
2014-07-14 03:52:18 -05:00
|
|
|
;
|
|
|
|
|
|
|
|
LIT_FLOAT
|
2014-12-30 07:03:00 -06:00
|
|
|
: [0-9][0-9_]* ('.' {
|
2015-04-21 05:02:12 -05:00
|
|
|
/* dot followed by another dot is a range, not a float */
|
2014-12-30 07:03:00 -06:00
|
|
|
_input.LA(1) != '.' &&
|
2015-04-21 05:02:12 -05:00
|
|
|
/* dot followed by an identifier is an integer with a function call, not a float */
|
2014-12-30 07:03:00 -06:00
|
|
|
_input.LA(1) != '_' &&
|
2015-04-21 05:02:12 -05:00
|
|
|
!(_input.LA(1) >= 'a' && _input.LA(1) <= 'z') &&
|
|
|
|
!(_input.LA(1) >= 'A' && _input.LA(1) <= 'Z')
|
2014-12-30 07:03:00 -06:00
|
|
|
}? | ('.' [0-9][0-9_]*)? ([eE] [-+]? [0-9][0-9_]*)? SUFFIX?)
|
2014-07-14 03:52:18 -05:00
|
|
|
;
|
|
|
|
|
|
|
|
LIT_STR
|
2014-11-19 03:25:48 -06:00
|
|
|
: '"' ('\\\n' | '\\\r\n' | '\\' CHAR_ESCAPE | .)*? '"' SUFFIX?
|
2014-07-14 03:52:18 -05:00
|
|
|
;
|
|
|
|
|
2015-09-03 02:54:53 -05:00
|
|
|
LIT_BYTE_STR : 'b' LIT_STR ;
|
|
|
|
LIT_BYTE_STR_RAW : 'b' LIT_STR_RAW ;
|
2014-07-14 03:52:18 -05:00
|
|
|
|
|
|
|
/* this is a bit messy */
|
|
|
|
|
|
|
|
fragment LIT_STR_RAW_INNER
|
|
|
|
: '"' .*? '"'
|
|
|
|
| LIT_STR_RAW_INNER2
|
|
|
|
;
|
|
|
|
|
|
|
|
fragment LIT_STR_RAW_INNER2
|
|
|
|
: POUND LIT_STR_RAW_INNER POUND
|
|
|
|
;
|
|
|
|
|
|
|
|
LIT_STR_RAW
|
2014-11-19 03:25:48 -06:00
|
|
|
: 'r' LIT_STR_RAW_INNER SUFFIX?
|
2014-07-14 03:52:18 -05:00
|
|
|
;
|
|
|
|
|
2015-01-13 16:31:53 -06:00
|
|
|
|
|
|
|
QUESTION : '?';
|
|
|
|
|
2015-01-14 16:51:51 -06:00
|
|
|
IDENT : XID_Start XID_Continue* ;
|
2014-07-14 03:52:18 -05:00
|
|
|
|
2015-01-13 16:31:53 -06:00
|
|
|
fragment QUESTION_IDENTIFIER : QUESTION? IDENT;
|
|
|
|
|
2014-07-14 03:52:18 -05:00
|
|
|
LIFETIME : '\'' IDENT ;
|
|
|
|
|
|
|
|
WHITESPACE : [ \r\n\t]+ ;
|
|
|
|
|
2015-04-21 05:02:12 -05:00
|
|
|
UNDOC_COMMENT : '////' ~[\n]* -> type(COMMENT) ;
|
2014-07-14 19:27:28 -05:00
|
|
|
YESDOC_COMMENT : '///' ~[\r\n]* -> type(DOC_COMMENT) ;
|
|
|
|
OUTER_DOC_COMMENT : '//!' ~[\r\n]* -> type(DOC_COMMENT) ;
|
2015-04-21 05:02:12 -05:00
|
|
|
LINE_COMMENT : '//' ( ~[/\n] ~[\n]* )? -> type(COMMENT) ;
|
2014-07-14 03:52:18 -05:00
|
|
|
|
2014-07-14 16:13:38 -05:00
|
|
|
DOC_BLOCK_COMMENT
|
2014-07-14 22:45:39 -05:00
|
|
|
: ('/**' ~[*] | '/*!') (DOC_BLOCK_COMMENT | .)*? '*/' -> type(DOC_COMMENT)
|
2014-07-14 03:52:18 -05:00
|
|
|
;
|
|
|
|
|
2014-07-14 16:13:38 -05:00
|
|
|
BLOCK_COMMENT : '/*' (BLOCK_COMMENT | .)*? '*/' -> type(COMMENT) ;
|
2015-04-21 05:02:12 -05:00
|
|
|
|
|
|
|
/* these appear at the beginning of a file */
|
|
|
|
|
|
|
|
SHEBANG : '#!' { is_at(2) && _input.LA(1) != '[' }? ~[\r\n]* -> type(SHEBANG) ;
|
|
|
|
|
|
|
|
UTF8_BOM : '\ufeff' { is_at(1) }? -> skip ;
|