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

📄 java.g

📁 jboss规则引擎
💻 G
📖 第 1 页 / 共 2 页
字号:
grammar JavaParser;/*tokens {	BLOCK; MODIFIERS; OBJBLOCK; SLIST; CTOR_DEF; METHOD_DEF; VARIABLE_DEF;	INSTANCE_INIT; STATIC_INIT; TYPE; CLASS_DEF; INTERFACE_DEF;	PACKAGE_DEF; ARRAY_DECLARATOR; EXTENDS_CLAUSE; IMPLEMENTS_CLAUSE;	PARAMETERS; PARAMETER_DEF; LABELED_STAT; TYPECAST; INDEX_OP;	POST_INC; POST_DEC; METHOD_CALL; EXPR; ARRAY_INIT;	UNARY_MINUS; UNARY_PLUS; CASE_GROUP; ELIST; FOR_INIT; FOR_CONDITION;	FOR_ITERATOR; EMPTY_STAT; SUPER_CTOR_CALL; CTOR_CALL;}*/@parser::header {	package org.drools.semantics.java.parser;	import java.util.Iterator;}@parser::members {	private List identifiers = new ArrayList();	public List getIdentifiers() { return identifiers; }	public static final CommonToken IGNORE_TOKEN = new CommonToken(null,0,99,0,0);	private List errors = new ArrayList();		private String source = "unknown";		public void setSource(String source) {		this.source = source;	}		public String getSource() {		return this.source;	}			public void reportError(RecognitionException ex) {	        // if we've already reported an error and have not matched a token                // yet successfully, don't report any errors.                if ( errorRecovery ) {                        //System.err.print("[SPURIOUS] ");                        return;                }                errorRecovery = true;		errors.add( ex ); 	}     	     	/** return the raw RecognitionException errors */     	public List getErrors() {     		return errors;     	}     	     	/** Return a list of pretty strings summarising the errors */     	public List getErrorMessages() {     		List messages = new ArrayList(); 		for ( Iterator errorIter = errors.iterator() ; errorIter.hasNext() ; ) {     	     		messages.add( createErrorMessage( (RecognitionException) errorIter.next() ) );     	     	}     	     	return messages;     	}     	     	/** return true if any parser errors were accumulated */     	public boolean hasErrors() {  		return ! errors.isEmpty();     	}     	     	/** This will take a RecognitionException, and create a sensible error message out of it */     	public String createErrorMessage(RecognitionException e)        {		StringBuffer message = new StringBuffer();		                message.append( source + ":"+e.line+":"+e.charPositionInLine+" ");                if ( e instanceof MismatchedTokenException ) {                        MismatchedTokenException mte = (MismatchedTokenException)e;                        message.append("mismatched token: "+                                                           e.token+                                                           "; expecting type "+                                                           tokenNames[mte.expecting]);                }                else if ( e instanceof MismatchedTreeNodeException ) {                        MismatchedTreeNodeException mtne = (MismatchedTreeNodeException)e;                        message.append("mismatched tree node: "+                                                           mtne.foundNode+                                                           "; expecting type "+                                                           tokenNames[mtne.expecting]);                }                else if ( e instanceof NoViableAltException ) {                        NoViableAltException nvae = (NoViableAltException)e;			message.append( "Unexpected token '" + e.token.getText() + "'" );                        /*                        message.append("decision=<<"+nvae.grammarDecisionDescription+">>"+                                                           " state "+nvae.stateNumber+                                                           " (decision="+nvae.decisionNumber+                                                           ") no viable alt; token="+                                                           e.token);                                                           */                }                else if ( e instanceof EarlyExitException ) {                        EarlyExitException eee = (EarlyExitException)e;                        message.append("required (...)+ loop (decision="+                                                           eee.decisionNumber+                                                           ") did not match anything; token="+                                                           e.token);                }                else if ( e instanceof MismatchedSetException ) {                        MismatchedSetException mse = (MismatchedSetException)e;                        message.append("mismatched token '"+                                                           e.token+                                                           "' expecting set "+mse.expecting);                }                else if ( e instanceof MismatchedNotSetException ) {                        MismatchedNotSetException mse = (MismatchedNotSetException)e;                        message.append("mismatched token '"+                                                           e.token+                                                           "' expecting set "+mse.expecting);                }                else if ( e instanceof FailedPredicateException ) {                        FailedPredicateException fpe = (FailedPredicateException)e;                        message.append("rule "+fpe.ruleName+" failed predicate: {"+                                                           fpe.predicateText+"}?");		}               	return message.toString();        }   } @lexer::header {	package org.drools.semantics.java.parser;}@lexer::members {	public static final CommonToken IGNORE_TOKEN = new CommonToken(null,0,99,0,0);} /** A declaration is the creation of a reference or primitive-type variable *  Create a separate Type/Var tree for each var in the var list. */declaration	:	modifiers typeSpec variableDefinitions			;// A type specification is a type name with possible brackets afterwards//   (which would make it an array type).typeSpec	: classTypeSpec	| builtInTypeSpec	;// A class type specification is a class type with possible brackets afterwards//   (which would make it an array type).classTypeSpec	:	identifier (LBRACK  RBRACK)*	;// A builtin type specification is a builtin type with possible brackets// afterwards (which would make it an array type).builtInTypeSpec	:	builtInType (LBRACK  RBRACK)*	;// A type name. which is either a (possibly qualified) class name or//   a primitive (builtin) typetype	:	identifier	|	builtInType	;// The primitive types.builtInType	:	'void'	|	'boolean'	|	'byte'	|	'char'	|	'short'	|	'int'	|	'float'	|	'long'	|	'double'	;// A (possibly-qualified) java identifier.  We start with the first IDENT//   and expand its name by adding dots and following IDENTSidentifier	:	IDENT  ( DOT IDENT )*	;identifierStar	:	IDENT		( DOT IDENT )*		( DOT STAR  )?	;// A list of zero or more modifiers.  We could have used (modifier)* in//   place of a call to modifiers, but I thought it was a good idea to keep//   this rule separate so they can easily be collected in a Vector if//   someone so desiresmodifiers	:	( modifier )*			;// modifiers for Java classes, interfaces, class/instance vars and methodsmodifier	:	'private'	|	'public'	|	'protected'	|	'static'	|	'transient'	|	'final'	|	'abstract'	|	'native'	|	'threadsafe'	|	'synchronized'//	|	'const'			// reserved word, but not valid	|	'volatile'	|	'strictfp'	;// Definition of a Java classclassDefinition	:	'class' IDENT		// it _might_ have a superclass...		superClassClause		// it might implement some interfaces...		implementsClause		// now parse the body of the class		classBlock	;superClassClause	:	( 'extends' identifier )?			;// Definition of a Java InterfaceinterfaceDefinition	:	'interface' IDENT		// it might extend some other interfaces		interfaceExtends		// now parse the body of the interface (looks like a class...)		classBlock	;// This is the body of a class.  You can have fields and extra semicolons,// That's about it (until you see what a field is...)classBlock	:	LCURLY			( field | SEMI )*		RCURLY			;// An interface can extend several other interfaces...interfaceExtends	:	(		'extends'		identifier ( COMMA identifier )*		)?	;// A class can implement several interfaces...implementsClause	:	(			'implements' identifier ( COMMA identifier )*		)?	;// Now the various things that can be defined inside a class or interface...// Note that not all of these are really valid in an interface (constructors,//   for example), and if this grammar were used for a compiler there would//   need to be some semantic checks to make sure we're doing the right thing...field	:	// method, constructor, or variable declaration		modifiers		(	ctorHead constructorBody // constructor					|	classDefinition       // inner class					|	interfaceDefinition   // inner interface					|	typeSpec  // method or variable declaration(s)			(	IDENT  // the name of the method				// parse the formal parameter declarations.				LPAREN parameterDeclarationList RPAREN				declaratorBrackets				// get the list of exceptions that this method is				// declared to throw				(throwsClause)?				( compoundStatement | SEMI )			|	variableDefinitions SEMI//											)		)    // 'static { ... }' class initializer	|	'static' compoundStatement		    // '{ ... }' instance initializer	|	compoundStatement			;constructorBody    :   LCURLY             ( options {greedy=true;} : explicitConstructorInvocation)?            (statement)*        RCURLY    ;/** Catch obvious constructor calls, but not the expr.super(...) calls */explicitConstructorInvocation    :   'this' LPAREN argList RPAREN SEMI		    |   'super' LPAREN argList RPAREN SEMI		    ;variableDefinitions	:	variableDeclarator		(	COMMA			variableDeclarator		)*	;/** Declaration of a variable.  This can be a class/instance variable, *   or a local variable in a method * It can also include possible initialization. */variableDeclarator	:	IDENT declaratorBrackets varInitializer			;declaratorBrackets	:			(LBRACK  RBRACK)*	;varInitializer	:	( ASSIGN initializer )?	;// This is an initializer used to set up an array.arrayInitializer	:	LCURLY 			(	initializer				(					// CONFLICT: does a COMMA after an initializer start a new					//           initializer or start the option ',' at end?					//           ANTLR generates proper code by matching					//			 the comma as soon as possible.					COMMA initializer				)*				(COMMA)?			)?		RCURLY	;// The two 'things' that can initialize an array element are an expression//   and another (nested) array initializer.initializer	:	expression	|	arrayInitializer	;// This is the header of a method.  It includes the name and parameters//   for the method.//   This also watches for a list of exception classes in a 'throws' clause.ctorHead	:	IDENT  // the name of the method		// parse the formal parameter declarations.		LPAREN parameterDeclarationList RPAREN		// get the list of exceptions that this method is declared to throw		(throwsClause)?	;// This is a list of exception classes that the method is declared to throwthrowsClause	:	'throws' identifier ( COMMA identifier )*	;// A list of formal parametersparameterDeclarationList	:	( parameterDeclaration ( COMMA parameterDeclaration )* )?	;// A formal parameter.parameterDeclaration	:	parameterModifier typeSpec IDENT		declaratorBrackets	;parameterModifier	:	('final')?			;// Compound statement.  This is used in many contexts://   Inside a class definition prefixed with 'static'://      it is a class initializer//   Inside a class definition without 'static'://      it is an instance initializer//   As the body of a method//   As a completely indepdent braced block of code inside a method//      it starts a new scope for variable definitionscompoundStatement	:	LCURLY 			// include the (possibly-empty) list of statements			(statement)*		RCURLY	;statement	// A list of statements in curly braces -- start a new scope	:	compoundStatement	// declarations are ambiguous with 'ID DOT' relative to expression	// statements.  Must backtrack to be sure.  Could use a semantic	// predicate to test symbol table to see what the type was coming	// up, but that's pretty hard without a symbol table ;)	// remove (declaration)=>	|	declaration SEMI	// An expression statement.  This could be a method call,	// assignment statement, or any other expression evaluated for	// side-effects.	|	expression SEMI	// class definition	|	modifiers classDefinition	// Attach a label to the front of a statement	|	IDENT COLON  statement	// If-else statement	|	'if' LPAREN expression RPAREN statement		(			// CONFLICT: the old "dangling-else" problem...			//           ANTLR generates proper code matching			//			 as soon as possible.  Hush warning.			'else' statement		)?	// For statement	|	'for'			LPAREN				forInit SEMI   // initializer				forCond	SEMI   // condition test				forIter         // updater			RPAREN			statement                     // statement to loop over	// While statement	|	'while' LPAREN expression RPAREN statement	// do-while statement	|	'do' statement 'while' LPAREN expression RPAREN SEMI	// get out of a loop (or switch)	|	'break' (IDENT)? SEMI	// do next iteration of a loop	|	'continue' (IDENT)? SEMI	// Return an expression	|	'return' (expression)? SEMI	// switch/case statement	|	'switch' LPAREN expression RPAREN LCURLY			( casesGroup )*		RCURLY	// exception try-catch block	|	tryBlock	// throw an exception	|	'throw' expression SEMI	// synchronize a statement	|	'synchronized' LPAREN expression RPAREN compoundStatement	// asserts (uncomment if you want 1.4 compatibility)	// |	'assert' expression ( COLON expression )? SEMI	// empty statement	|	SEMI 	;casesGroup	:	(	// CONFLICT: to which case group do the statements bind?			//           ANTLR generates proper code: it groups the			//           many 'case'/'default' labels together then			//           follows them with the statements				options {greedy=true;}			:			aCase		)+		caseSList			;aCase	:	('case' expression | 'default') COLON	;caseSList	:	(statement)*			;// The initializer for a for loopforInit		// if it looks like a declaration, it is        // remove (declaration)=> 	:	(	declaration		// otherwise it could be an expression list...		|	expressionList		)?			;forCond	:	(expression)?			;forIter	:	(expressionList)?			;// an exception handler try/catch blocktryBlock	:	'try' compoundStatement		(handler)*		( finallyClause )?	;finallyClause	:	'finally' compoundStatement	;// an exception handlerhandler	:	'catch' LPAREN parameterDeclaration RPAREN compoundStatement	;// expressions// Note that most of these expressions follow the pattern//   thisLevelExpression ://       nextHigherPrecedenceExpression//           (OPERATOR nextHigherPrecedenceExpression)*// which is a standard recursive definition for a parsing an expression.// The operators in java have the following precedences://    lowest  (13)  = *= /= %= += -= <<= >>= >>>= &= = |=//            (12)  ?://            (11)  ||//            (10)  &&//            ( 9)  |//            ( 8)  //            ( 7)  &//            ( 6)  == =//            ( 5)  < <= > >=//            ( 4)  << >>//            ( 3)  +(binary) -(binary)//            ( 2)  * / %

⌨️ 快捷键说明

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