⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java.g

📁 Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业务
💻 G
📖 第 1 页 / 共 4 页
字号:
						// this will also produce an ANTLR warning!				// Unfortunately a syntactic predicate can only select one of				// multiple alternatives on the same level, not break out of				// an enclosing loop, which is why this ugly hack (a fake				// empty alternative with always-false semantic predicate)				// is necessary.		)*		(			options {				// 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.				greedy=true;			}		:	(	lp:LPAREN^ {#lp.setType(METHOD_CALL);}				// if the input is valid, only the last IDENT may				// have preceding typeArguments... rather hacky, this is...				{if (#ta2 != null) astFactory.addASTChild(currentAST, #ta2);}				{if (#ta2 == null) astFactory.addASTChild(currentAST, #ta1);}				argList RPAREN!			)		|	( options {greedy=true;} :				lbc:LBRACK^ {#lbc.setType(ARRAY_DECLARATOR);} 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"^ (typeArguments)? 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 {Token first = LT(1);}	:	(	expressionList		|	/*nothing*/			{#argList = create(ELIST,"ELIST",first,LT(1));}		)	;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 {				warnWhenFollowAmbig = false;			}		:			lb:LBRACK^ {#lb.setType(ARRAY_DECLARATOR);}				(expression)?			RBRACK!		)+	;constant	:	NUM_INT	|	STRING_LITERAL	|	NUM_FLOAT	|	NUM_LONG	|	NUM_DOUBLE	;//----------------------------------------------------------------------------// The Java scanner//----------------------------------------------------------------------------class JavaLexer extends Lexer;options {	exportVocab=Java;		// call the vocabulary "Java"	testLiterals=false;		// don't automatically test for literals	k=4;					// four characters of lookahead	charVocabulary='\u0003'..'\uFFFF';	// without inlining some bitset tests, couldn't do unicode;	// I need to make ANTLR generate smaller bitsets; see	// bottom of JavaLexer.java	codeGenBitsetTestThreshold=20;}{    protected static final int SCS_TYPE = 3, SCS_VAL = 4, SCS_LIT = 8, SCS_LIMIT = 16;    protected static final int SCS_SQ_TYPE = 0, SCS_TQ_TYPE = 1, SCS_RE_TYPE = 2;    protected int stringCtorState = 0;  // hack string and regexp constructor boundaries    protected int lastSigTokenType = EOF;  // last returned non-whitespace token    /** flag for enabling the "assert" keyword */	private boolean assertEnabled = true;	/** flag for enabling the "enum" keyword */	private boolean enumEnabled = true;    /** flag for including whitespace tokens (for IDE preparsing) */    private boolean whitespaceIncluded = false;	/** Enable the "assert" keyword */	public void enableAssert(boolean shouldEnable) { assertEnabled = shouldEnable; }	/** Query the "assert" keyword state */	public boolean isAssertEnabled() { return assertEnabled; }	/** Enable the "enum" keyword */	public void enableEnum(boolean shouldEnable) { enumEnabled = shouldEnable; }	/** Query the "enum" keyword state */	public boolean isEnumEnabled() { return enumEnabled; }    /** This is a bit of plumbing which resumes collection of string constructor bodies,     *  after an embedded expression has been parsed.     *  Usage:  new JavaRecognizer(new JavaLexer(in).plumb()).     */    public TokenStream plumb() {        return new TokenStream() {            public Token nextToken() throws TokenStreamException {                if (stringCtorState >= SCS_LIT) {                    // This goo is modeled upon the ANTLR code for nextToken:                    int quoteType = (stringCtorState & SCS_TYPE);                    stringCtorState = 0;  // get out of this mode, now                    resetText();/*                    try {                        switch (quoteType) {                        case SCS_SQ_TYPE://todo: suitable replacement???     mSTRING_CTOR_END(true, false, false);                         	break;                        case SCS_TQ_TYPE://                            mSTRING_CTOR_END(true, false, true);                         	break;                        case SCS_RE_TYPE://                            mREGEXP_CTOR_END(true, false);                         	break;                        default:  throw new AssertionError(false);                        }                        lastSigTokenType = _returnToken.getType();                        return _returnToken;                    }*//* catch (RecognitionException e) {                        throw new TokenStreamRecognitionException(e);                    }*/ /*catch (CharStreamException cse) {                        if ( cse instanceof CharStreamIOException ) {                            throw new TokenStreamIOException(((CharStreamIOException)cse).io);                        }                        else {                            throw new TokenStreamException(cse.getMessage());                        }                    }*/                }                Token token = JavaLexer.this.nextToken();                int lasttype = token.getType();                if (whitespaceIncluded) {                    switch (lasttype) {  // filter out insignificant types                    case WS:                    case SL_COMMENT:                    case ML_COMMENT:                        lasttype = lastSigTokenType;  // back up!                    }                }                lastSigTokenType = lasttype;                return token;            }        };    }        protected JavaRecognizer parser;  // little-used link; TODO: get rid of}// 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		|	(	options {generateAmbigWarnings=false;}			:	"\r\n"	// Evil DOS			|	'\r'	// Macintosh			|	'\n'	// Unix (the right way)			)			{ newline(); }		)+		{ _ttype = Token.SKIP; }	;// Single-line commentsSL_COMMENT	:	"//"		(~('\n'|'\r'))* ('\n'|'\r'('\n')?)		{$setType(Token.SKIP); newline();}	;// multiple-line commentsML_COMMENT	:	"/*"		(	/*	'\r' '\n' can be matched in one alternative or by matching				'\r' in one iteration and '\n' in another. I am trying to				handle any flavor of newline that comes in, but the language				that allows both "\r\n" and "\r" and "\n" to all be valid				newline is ambiguous. Consequently, the resulting grammar				must be ambiguous. I'm shutting this warning off.			 */			options {				generateAmbigWarnings=false;			}		:			{ LA(2)!='/' }? '*'		|	'\r' '\n'		{newline();}		|	'\r'			{newline();}		|	'\n'			{newline();}		|	~('*'|'\n'|'\r')		)*		"*/"		{$setType(Token.SKIP);}	;// string literalsSTRING_LITERAL	:	'"' (ESC|~('"'|'\\'|'\n'|'\r'))* '"'	// no CHAR_LITERALs in groovy...	|	'\'' ( ESC | ~('\''|'\n'|'\r'|'\\') ) '\''	;// escape sequence -- note that this is protected; it can only be called// from another lexer rule -- it will not ever directly return a token to// the parser// There are various ambiguities hushed in this rule. The optional// '0'...'9' digit matches should be matched here rather than letting// them go back to STRING_LITERAL to be matched. ANTLR does the// right thing by matching immediately; hence, it's ok to shut off// the FOLLOW ambig warnings.protectedESC	:	'\\'		(	'n'		|	'r'		|	't'		|	'b'		|	'f'		|	'"'		|	'\''		|	'\\'		|	('u')+ HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT		|	'0'..'3'			(				options {					warnWhenFollowAmbig = false;				}			:	'0'..'7'				(					options {						warnWhenFollowAmbig = false;					}				:	'0'..'7'				)?			)?		|	'4'..'7'			(				options {					warnWhenFollowAmbig = false;				}			:	'0'..'7'			)?		)	;// hexadecimal digit (again, note it's protected!)protectedHEX_DIGIT	:	('0'..'9'|'A'..'F'|'a'..'f')	;// a dummy rule to force vocabulary to be all characters (except special// ones that ANTLR uses internally (0 to 2)protectedVOCAB	:	'\3'..'\377'	;// an identifier. Note that testLiterals is set to true! This means// that after we match the rule, we look in the literals table to see// if it's a literal or really an identiferIDENT	options {testLiterals=true;}	:	('a'..'z'|'A'..'Z'|'_'|'$') ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'$')*		{			// check if "assert" keyword is enabled			if (assertEnabled && "assert".equals($getText)) {				$setType(LITERAL_assert); // set token type for the rule in the parser			}			// check if "enum" keyword is enabled			if (enumEnabled && "enum".equals($getText)) {				$setType(LITERAL_enum); // set token type for the rule in the parser			}		}	;// a numeric literalNUM_INT	{boolean isDecimal=false; Token t=null;}	:	'.' {_ttype = DOT;}			(				(('0'..'9')+ (EXPONENT)? (f1:FLOAT_SUFFIX {t=f1;})?				{				if (t != null && t.getText().toUpperCase().indexOf('F')>=0) {					_ttype = NUM_FLOAT;				}				else {					_ttype = NUM_DOUBLE; // assume double				}				})				|				// JDK 1.5 token for variable length arguments				(".." {_ttype = TRIPLE_DOT;})			)?	|	(	'0' {isDecimal = true;} // special case for just '0'			(	('x'|'X')				(											// hex					// the 'e'|'E' and float suffix stuff look					// like hex digits, hence the (...)+ doesn't					// know when to stop: ambig. ANTLR resolves					// it correctly by matching immediately. It					// is therefor ok to hush warning.					options {						warnWhenFollowAmbig=false;					}				:	HEX_DIGIT				)+			|	//float or double with leading zero				(('0'..'9')+ ('.'|EXPONENT|FLOAT_SUFFIX)) => ('0'..'9')+			|	('0'..'7')+									// octal			)?		|	('1'..'9') ('0'..'9')*  {isDecimal=true;}		// non-zero decimal		)		(	('l'|'L') { _ttype = NUM_LONG; }		// only check to see if it's a float if looks like decimal so far		|	{isDecimal}?			(	'.' ('0'..'9')* (EXPONENT)? (f2:FLOAT_SUFFIX {t=f2;})?			|	EXPONENT (f3:FLOAT_SUFFIX {t=f3;})?			|	f4:FLOAT_SUFFIX {t=f4;}			)			{			if (t != null && t.getText().toUpperCase() .indexOf('F') >= 0) {				_ttype = NUM_FLOAT;			}			else {				_ttype = NUM_DOUBLE; // assume double			}			}		)?	;// JDK 1.5 token for annotations and their declarationsAT	:	'@'	;// a couple protected methods to assist in matching floating point numbersprotectedEXPONENT	:	('e'|'E') ('+'|'-')? ('0'..'9')+	;protectedFLOAT_SUFFIX	:	'f'|'F'|'d'|'D'	;			//			 Note: Please don't use physical tabs.  Logical tabs for indent are width 4.//			 Here's a little hint for you, Emacs://			 Local Variables://			 tab-width: 4//			 mode: antlr-mode//			 indent-tabs-mode: nil//			 End:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -