rust/src/grammar/RustLexer.g4

196 lines
4.0 KiB
Plaintext
Raw Normal View History

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 {
EQ, LT, LE, EQEQ, NE, GE, GT, ANDAND, OROR, NOT, TILDE, PLUT,
MINUS, STAR, SLASH, PERCENT, CARET, AND, OR, SHL, SHR, BINOP,
BINOPEQ, AT, DOT, DOTDOT, DOTDOTDOT, COMMA, SEMI, COLON,
MOD_SEP, RARROW, FAT_ARROW, LPAREN, RPAREN, LBRACKET, RBRACKET,
LBRACE, RBRACE, POUND, DOLLAR, UNDERSCORE, LIT_CHAR,
LIT_INTEGER, LIT_FLOAT, LIT_STR, LIT_STR_RAW, LIT_BINARY,
LIT_BINARY_RAW, IDENT, LIFETIME, WHITESPACE, DOC_COMMENT,
2015-04-21 05:02:12 -05:00
COMMENT, SHEBANG
2014-07-14 16:13:38 -05: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 : '>>' ;
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
;
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
;
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
;
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
;
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
: [0-9][0-9_]* ('.' {
2015-04-21 05:02:12 -05:00
/* dot followed by another dot is a range, not a float */
_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 */
_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')
}? | ('.' [0-9][0-9_]*)? ([eE] [-+]? [0-9][0-9_]*)? SUFFIX?)
2014-07-14 03:52:18 -05:00
;
LIT_STR
: '"' ('\\\n' | '\\\r\n' | '\\' CHAR_ESCAPE | .)*? '"' SUFFIX?
2014-07-14 03:52:18 -05:00
;
2015-04-21 05:02:12 -05:00
LIT_BINARY : 'b' LIT_STR ;
LIT_BINARY_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
: 'r' LIT_STR_RAW_INNER SUFFIX?
2014-07-14 03:52:18 -05:00
;
QUESTION : '?';
IDENT : XID_Start XID_Continue* ;
2014-07-14 03:52:18 -05: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 ;