beetl 2 文法初稿

原创
2013/10/21 12:38
阅读数 680

感谢antlr4,让事情变得容易

lexer grammar BeetlLexer;


If:'if';
For:'for';
Else:'else';
While:'while';
Switch:'switch';
Select:'select';
Return:'return';
Break:'break';
Continue:'continue';
Var:'var';    
Directive:'DIRECTIVE'|'directive';
Case:'case';
Default:'default';
Try:'try';
Catch:'catch';   

LEFT_BRACE:'{';
RIGHT_BRACE:'}';
LEFT_PAR:'(';
RIGHT_PAR:')';
LEFT_SQBR:'[';
RIGHT_SQBR:']';
INCREASE:'++';
DECREASE:'--';
VIRTUAL:'.~';
ADD:'+';
MIN:'-';
MUlTIP:'*';
DIV:'/';
MOD:'%';
EQUAL:'==';
NOT_EQUAL:'!=';
ASSIN:'=';
LARGE:'>';
LESS:'<';
NOT:'!';
AND:'&&';
OR:'||';
QUESTOIN:'?';
AT:'@';
NULL:'null';
TRUE:'true';
FALSE:'false';
END:';';
COMMA:',';
COLON:':';
PERIOD:'.';
FOR_IN:'in';
LEFT_TOKEN:'<<';
RIGHT_TOKEN:'>>';
LEFT_TEXT_TOKEN	:'<$';
     

// LEXER

HexLiteral : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix? ;

DecimalLiteral : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;

OctalLiteral : '0' ('0'..'7')+ IntegerTypeSuffix? ;

fragment
HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
IntegerTypeSuffix : ('l'|'L') ;

FloatingPointLiteral
    :   ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix?
    |   '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
    |   ('0'..'9')+ Exponent FloatTypeSuffix?
    |   ('0'..'9')+ FloatTypeSuffix
    |   ('0x' | '0X') (HexDigit )*
        ('.' (HexDigit)*)?
        ( 'p' | 'P' )
        ( '+' | '-' )?
        ( '0' .. '9' )+
        FloatTypeSuffix?
    ;

fragment
Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

fragment
FloatTypeSuffix : ('f'|'F'|'d'|'D') ;

CharacterLiteral
    :   '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
    ;

StringLiteral
    :  '"' ( EscapeSequence | ~('\\'|'"') )* '"'
    ;

fragment
EscapeSequence
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UnicodeEscape
    |   OctalEscape
    ;

fragment
OctalEscape
    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7')
    ;

fragment
UnicodeEscape
    :   '\\' 'u' HexDigit HexDigit HexDigit HexDigit
    ;


Identifier
    :   Letter (Letter|JavaIDDigit)*
    ;

/**I found this char range in JavaCC's grammar, but Letter and Digit overlap.
   Still works, but...
 */
fragment
Letter
    :  '$' |
       'A'..'Z' |
       '_' |
       'a'..'z' |
       'À'..'Ö' |
       'Ø'..'ö' |
       'ø'..'ÿ' |
       'Ā'..'῿' |
       '぀'..'㆏' |
       '㌀'..'㍿' |
       '㐀'..'㴭' |
       '一'..'鿿' |
       '豈'..'﫿'
    ;

fragment
JavaIDDigit
    :  '0'..'9' |
       '٠'..'٩' |
       '۰'..'۹' |
       '०'..'९' |
       '০'..'৯' |
       '੦'..'੯' |
       '૦'..'૯' |
       '୦'..'୯' |
       '௧'..'௯' |
       '౦'..'౯' |
       '೦'..'೯' |
       '൦'..'൯' |
       '๐'..'๙' |
       '໐'..'໙' |
       '၀'..'၉'
   ;
COMMENT_OPEN
    :   '/*'    ->mode(ISLAND),channel(HIDDEN) 
    ;
mode ISLAND;
PARAS:'@' Identifier ':' Identifier ('.' Identifier)*  ;
WS1  :   [ \r\t\n]+ -> channel(HIDDEN);
COMMENT_END: '*/'  -> mode(DEFAULT_MODE),channel(HIDDEN)
;

WS  :   [ \r\t\n]+ -> channel(HIDDEN)
    ;

LINE_COMMENT
    : '//' ~[\r\n]* '\r'? '\n' -> channel(HIDDEN)
    ;
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部