📄 java.g
字号:
// ( 1) ++ -- +(unary) -(unary) ~ (type)// [] () (method call) . (dot -- identifier qualification)// new () (explicit parenthesis)//// the last two are not usually on a precedence chart; I put them in// to point out that new has a higher precedence than '.', so you// can validy use// new Frame().show()//// Note that the above precedence levels map to the rules below...// Once you have a precedence chart, writing the appropriate rules as below// is usually very straightfoward// the mother of all expressionsexpression : assignmentExpression ;// This is a list of expressions.expressionList : expression (COMMA expression)* ;// assignment expression (level 13)assignmentExpression : conditionalExpression ( ( ASSIGN | PLUS_ASSIGN | MINUS_ASSIGN | STAR_ASSIGN | DIV_ASSIGN | MOD_ASSIGN | SR_ASSIGN | BSR_ASSIGN | SL_ASSIGN | BAND_ASSIGN | BXOR_ASSIGN | BOR_ASSIGN ) assignmentExpression )? ;// conditional test (level 12)conditionalExpression : logicalOrExpression ( QUESTION assignmentExpression COLON conditionalExpression )? ;// logical or (||) (level 11)logicalOrExpression : logicalAndExpression (LOR logicalAndExpression)* ;// logical and (&&) (level 10)logicalAndExpression : inclusiveOrExpression (LAND inclusiveOrExpression)* ;// bitwise or non-short-circuiting or (|) (level 9)inclusiveOrExpression : exclusiveOrExpression (BOR exclusiveOrExpression)* ;// exclusive or () (level 8)exclusiveOrExpression : andExpression (BXOR andExpression)* ;// bitwise or non-short-circuiting and (&) (level 7)andExpression : equalityExpression (BAND equalityExpression)* ;// equality/inequality (==/=) (level 6)equalityExpression : relationalExpression ((NOT_EQUAL | EQUAL) relationalExpression)* ;// boolean relational expressions (level 5)relationalExpression : shiftExpression ( ( ( LT | GT | LE | GE ) shiftExpression )* | 'instanceof' typeSpec ) ;// bit shift expressions (level 4)shiftExpression : additiveExpression ((SL | SR | BSR) additiveExpression)* ;// binary addition/subtraction (level 3)additiveExpression : multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)* ;// multiplication/division/modulo (level 2)multiplicativeExpression : unaryExpression ((STAR | DIV | MOD ) unaryExpression)* ;unaryExpression : INC unaryExpression | DEC unaryExpression | MINUS unaryExpression | PLUS unaryExpression | unaryExpressionNotPlusMinus ;unaryExpressionNotPlusMinus : BNOT unaryExpression | LNOT unaryExpression | LPAREN builtInTypeSpec RPAREN unaryExpression // Have to backtrack to see if operator follows. If no operator // follows, it's a typecast. No semantic checking needed to parse. // if it _looks_ like a cast, it _is_ a cast; else it's a '(expr)' | LPAREN classTypeSpec RPAREN unaryExpressionNotPlusMinus | postfixExpression ;// qualified names, array expressions, method invocation, post inc/decpostfixExpression : primaryExpression ( DOT IDENT ( LPAREN argList RPAREN )? | DOT 'this' | DOT 'super' ( // (new Outer()).super() (create enclosing instance) LPAREN argList RPAREN | DOT IDENT ( LPAREN argList RPAREN )? ) | DOT newExpression | LBRACK expression RBRACK )* ( // possibly add on a post-increment or post-decrement. // allows INC/DEC on too much, but semantics can check INC | DEC )? ;// the basic element of an expressionprimaryExpression : identPrimary ( options {greedy=true;}: DOT 'class' )? | constant | 'true' | 'false' | 'null' | newExpression | 'this' | 'super' | LPAREN assignmentExpression RPAREN // look for int.class and int[].class | builtInType ( LBRACK RBRACK )* DOT 'class' ;/** Match a, a.b.c refs, a.b.c(...) refs, a.b.c[], a.b.c[].class, * and a.b.c.class refs. Also this(...) and super(...). Match * this or super. */identPrimary : i=IDENT { identifiers.add( i.getText() ); } ( // .ident could match here or in postfixExpression. // We do want to match here. Turn off warning. options {greedy=true; k=2;} : DOT IDENT )* ( // ARRAY_DECLARATOR here conflicts with INDEX_OP in // postfixExpression on LBRACK RBRACK. // We want to match [] here, so greedy. This overcomes // limitation of linear approximate lookahead. options {greedy=true;} : ( LPAREN argList RPAREN ) | ( options {greedy=true;} : LBRACK RBRACK )+ )? ;/** object instantiation. * Trees are built as illustrated by the following input/tree pairs: * * new T() * * new * | * T -- ELIST * | * arg1 -- arg2 -- .. -- argn * * new int[] * * new * | * int -- ARRAY_DECLARATOR * * new int[] {1,2} * * new * | * int -- ARRAY_DECLARATOR -- ARRAY_INIT * | * EXPR -- EXPR * | | * 1 2 * * new int[3] * new * | * int -- ARRAY_DECLARATOR * | * EXPR * | * 3 * * new int[1][2] * * new * | * int -- ARRAY_DECLARATOR * | * ARRAY_DECLARATOR -- EXPR * | | * EXPR 1 * | * 2 * */newExpression : 'new' type ( LPAREN argList RPAREN (classBlock)? //java 1.1 // Note: This will allow bad constructs like // new int[4][][3] {exp,exp}. // There needs to be a semantic check here... // to make sure: // a) [ expr ] and [ ] are not mixed // b) [ expr ] and an init are not used together | newArrayDeclarator (arrayInitializer)? ) ;argList : ( expressionList | /*nothing*/ ) ;newArrayDeclarator : ( // CONFLICT: // newExpression is a primaryExpression which can be // followed by an array index reference. This is ok, // as the generated code will stay in this loop as // long as it sees an LBRACK (proper behavior) options {k=1;} //options {warnWhenFollowAmbig = false;} : LBRACK (expression)? RBRACK )+ ;constant : NUM_INT | CHAR_LITERAL | STRING_LITERAL | NUM_FLOAT ;//----------------------------------------------------------------------------// The Java scanner//----------------------------------------------------------------------------// OPERATORSQUESTION : '?' ;LPAREN : '(' ;RPAREN : ')' ;LBRACK : '[' ;RBRACK : ']' ;LCURLY : '{' ;RCURLY : '}' ;COLON : ':' ;COMMA : ',' ;DOT : '.' ;ASSIGN : '=' ;EQUAL : '==' ;LNOT : '!' ;BNOT : '~' ;NOT_EQUAL : '!=' ;DIV : '/' ;DIV_ASSIGN : '/=' ;PLUS : '+' ;PLUS_ASSIGN : '+=' ;INC : '++' ;MINUS : '-' ;MINUS_ASSIGN : '-=' ;DEC : '--' ;STAR : '*' ;STAR_ASSIGN : '*=' ;MOD : '%' ;MOD_ASSIGN : '%=' ;SR : '>>' ;SR_ASSIGN : '>>=' ;BSR : '>>>' ;BSR_ASSIGN : '>>>=' ;GE : '>=' ;GT : '>' ;SL : '<<' ;SL_ASSIGN : '<<=' ;LE : '<=' ;LT : '<' ;BXOR : '^' ;BXOR_ASSIGN : '^=' ;BOR : '|' ;BOR_ASSIGN : '|=' ;LOR : '||' ;BAND : '&' ;BAND_ASSIGN : '&=' ;LAND : '&&' ;SEMI : ';' ;// Whitespace -- ignoredWS : ( ' ' | '\t' | '\f' // handle newlines | ( '\r\n' // Evil DOS | '\r' // Macintosh | '\n' // Unix (the right way) ) )+ { channel=99; /*token = JavaParser.IGNORE_TOKEN;*/ } ;// Single-line commentsSL_COMMENT : '//' (options {greedy=false;} : .)* ('\r')? '\n' {channel=99; /*token = JavaParser.IGNORE_TOKEN;*/} ;// multiple-line commentsML_COMMENT : '/*' ( options {greedy=false;} : . )* '*/' {channel=99;/*token = JavaParser.IGNORE_TOKEN;*/} ;IDENT : ('a'..'z'|'A'..'Z'|'_'|'$') ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'$')* ;// From the java language specNUM_INT : DECIMAL_LITERAL | HEX_LITERAL | OCTAL_LITERAL ;fragmentDECIMAL_LITERAL: '1'..'9' ('0'..'9')* ('l'|'L')? ;fragmentHEX_LITERAL: '0' ('x'|'X') ('0'..'9'|'a'..'f'|'A'..'F')+ ('l'|'L')? ;fragmentOCTAL_LITERAL: '0' ('0'..'7')* ('l'|'L')? ;NUM_FLOAT : DIGITS '.' (DIGITS)? (EXPONENT_PART)? (FLOAT_TYPE_SUFFIX)? | '.' DIGITS (EXPONENT_PART)? (FLOAT_TYPE_SUFFIX)? | DIGITS EXPONENT_PART FLOAT_TYPE_SUFFIX | DIGITS EXPONENT_PART | DIGITS FLOAT_TYPE_SUFFIX ;fragmentDIGITS : ('0'..'9')+ ;/*fragmentEXPONENT_PART: ('e'|'E') ('+'|'-')? DIGITS ;*/fragmentEXPONENT_PART: ('e'|'E') ('+'|'-')? DIGITS ;fragmentFLOAT_TYPE_SUFFIX : ('f'|'F'|'d'|'D') ;CHAR_LITERAL : '\'' ( ~('\''|'\\') | ESCAPE_SEQUENCE ) '\'' ;STRING_LITERAL : '\"' ( ~('\"'|'\\') | ESCAPE_SEQUENCE )* '\"' ;fragmentESCAPE_SEQUENCE : '\\' 'b' | '\\' 't' | '\\' 'n' | '\\' 'f' | '\\' 'r' | '\\' '\"' | '\\' '\'' | '\\' '\\' | '\\' '0'..'3' OCTAL_DIGIT OCTAL_DIGIT | '\\' OCTAL_DIGIT OCTAL_DIGIT | '\\' OCTAL_DIGIT | UNICODE_CHAR ;fragmentUNICODE_CHAR : '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT ;fragmentHEX_DIGIT : '0'..'9'|'a'..'f'|'A'..'F' ;fragmentOCTAL_DIGIT : '0'..'7' ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -