📄 ruleparser.java
字号:
// $ANTLR 3.0ea8 /home/michael/projects/jboss-rules-3.0.x/drools-compiler/src/main/resources/org/drools/lang/drl.g 2006-08-16 14:21:20 package org.drools.lang; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.util.StringTokenizer; import org.drools.lang.descr.*;import org.antlr.runtime.*;import java.util.Stack;import java.util.List;import java.util.ArrayList;public class RuleParser extends Parser { public static final String[] tokenNames = new String[] { "<invalid>", "<EOR>", "<DOWN>", "<UP>", "EOL", "ID", "INT", "BOOL", "STRING", "FLOAT", "MISC", "WS", "SH_STYLE_SINGLE_LINE_COMMENT", "C_STYLE_SINGLE_LINE_COMMENT", "MULTI_LINE_COMMENT", "\';\'", "\'package\'", "\'import\'", "\'.\'", "\'.*\'", "\'expander\'", "\'global\'", "\'function\'", "\'(\'", "\',\'", "\')\'", "\'{\'", "\'}\'", "\'query\'", "\'end\'", "\'rule\'", "\'when\'", "\':\'", "\'then\'", "\'attributes\'", "\'salience\'", "\'no-loop\'", "\'auto-focus\'", "\'activation-group\'", "\'agenda-group\'", "\'duration\'", "\'or\'", "\'||\'", "\'==\'", "\'>\'", "\'>=\'", "\'<\'", "\'<=\'", "\'!=\'", "\'contains\'", "\'matches\'", "\'excludes\'", "\'null\'", "\'->\'", "\'and\'", "\'&&\'", "\'exists\'", "\'not\'", "\'eval\'", "\'[\'", "\']\'", "\'use\'" }; public static final int BOOL=7; public static final int INT=6; public static final int WS=11; public static final int EOF=-1; public static final int MISC=10; public static final int STRING=8; public static final int EOL=4; public static final int FLOAT=9; public static final int SH_STYLE_SINGLE_LINE_COMMENT=12; public static final int MULTI_LINE_COMMENT=14; public static final int C_STYLE_SINGLE_LINE_COMMENT=13; public static final int ID=5; public RuleParser(TokenStream input) { super(input); } public String[] getTokenNames() { return tokenNames; } private ExpanderResolver expanderResolver; private Expander expander; private boolean expanderDebug = false; private PackageDescr packageDescr; private List errors = new ArrayList(); private String source = "unknown"; private int lineOffset = 0; private boolean parserDebug = false; public void setParserDebug(boolean parserDebug) { this.parserDebug = parserDebug; } public void debug(String message) { if ( parserDebug ) System.err.println( "drl parser: " + message ); } public void setSource(String source) { this.source = source; } /** * This may be set to enable debuggin of DSLs/expanders. * If set to true, expander stuff will be sent to the Std out. */ public void setExpanderDebug(boolean status) { expanderDebug = status; } public String getSource() { return this.source; } public PackageDescr getPackageDescr() { return packageDescr; } private int offset(int line) { return line + lineOffset; } /** * This will set the offset to record when reparsing. Normally is zero of course */ public void setLineOffset(int i) { this.lineOffset = i; } public void setExpanderResolver(ExpanderResolver expanderResolver) { this.expanderResolver = expanderResolver; } public ExpanderResolver getExpanderResolver() { return expanderResolver; } /** Expand the LHS */ private String runWhenExpander(String text, int line) throws RecognitionException { String expanded = text.trim(); if (expanded.startsWith(">")) { expanded = expanded.substring(1); //escape !! } else { try { expanded = expander.expand( "when", text ); } catch (Exception e) { this.errors.add(new ExpanderException("Unable to expand: " + text + ". Due to " + e.getMessage(), line)); return ""; } } if (expanderDebug) { System.out.println("Expanding LHS: " + text + " ----> " + expanded + " --> from line: " + line); } return expanded; } /** This will apply a list of constraints to an LHS block */ private String applyConstraints(List constraints, String block) { //apply the constraints as a comma seperated list inside the previous block //the block will end in something like "foo()" and the constraint patterns will be put in the () if (constraints == null) { return block; } StringBuffer list = new StringBuffer(); for (Iterator iter = constraints.iterator(); iter.hasNext();) { String con = (String) iter.next(); list.append("\n\t\t"); list.append(con); if (iter.hasNext()) { list.append(","); } } if (block.endsWith("()")) { return block.substring(0, block.length() - 2) + "(" + list.toString() + ")"; } else { return block + "(" + list.toString() + ")"; } } /** Reparse the results of the expansion */ private void reparseLhs(String text, AndDescr descrs) throws RecognitionException { CharStream charStream = new ANTLRStringStream( text ); RuleParserLexer lexer = new RuleParserLexer( charStream ); TokenStream tokenStream = new CommonTokenStream( lexer ); RuleParser parser = new RuleParser( tokenStream ); parser.setLineOffset( descrs.getLine() ); parser.normal_lhs_block(descrs); if (parser.hasErrors()) { this.errors.addAll(parser.getErrors()); } if (expanderDebug) { System.out.println("Reparsing LHS: " + text + " --> successful:" + !parser.hasErrors()); } } /** Expand a line on the RHS */ private String runThenExpander(String text, int startLine) { //System.err.println( "expand THEN [" + text + "]" ); StringTokenizer lines = new StringTokenizer( text, "\n\r" ); StringBuffer expanded = new StringBuffer(); String eol = System.getProperty( "line.separator" ); while ( lines.hasMoreTokens() ) { startLine++; String line = lines.nextToken(); line = line.trim(); if ( line.length() > 0 ) { if ( line.startsWith( ">" ) ) { expanded.append( line.substring( 1 ) ); expanded.append( eol ); } else { try { expanded.append( expander.expand( "then", line ) ); expanded.append( eol ); } catch (Exception e) { this.errors.add(new ExpanderException("Unable to expand: " + line + ". Due to " + e.getMessage(), startLine)); } } } } if (expanderDebug) { System.out.println("Expanding RHS: " + text + " ----> " + expanded.toString() + " --> from line starting: " + startLine); } return expanded.toString(); } private String getString(Token token) { String orig = token.getText(); return orig.substring( 1, orig.length() -1 ); } 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 ) { return; } errorRecovery = true; ex.line = offset(ex.line); //add the offset if there is one 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+"}?"); } else if (e instanceof GeneralParseException) { message.append(" " + e.getMessage()); } return message.toString(); } void checkTrailingSemicolon(String text, int line) { if (text.trim().endsWith( ";" ) ) { this.errors.add( new GeneralParseException( "Trailing semi-colon not allowed", offset(line) ) ); } } // $ANTLR start opt_eol // /home/michael/projects/jboss-rules-3.0.x/drools-compiler/src/main/resources/org/drools/lang/drl.g:275:1: opt_eol : ( (';'|EOL))* ; public void opt_eol() throws RecognitionException { try { // /home/michael/projects/jboss-rules-3.0.x/drools-compiler/src/main/resources/org/drools/lang/drl.g:276:17: ( ( (';'|EOL))* ) // /home/michael/projects/jboss-rules-3.0.x/drools-compiler/src/main/resources/org/drools/lang/drl.g:276:17: ( (';'|EOL))* { // /home/michael/projects/jboss-rules-3.0.x/drools-compiler/src/main/resources/org/drools/lang/drl.g:276:17: ( (';'|EOL))* loop1: do { int alt1=2; int LA1_0 = input.LA(1); if ( LA1_0==EOL ) { alt1=1; } else if ( LA1_0==15 ) { alt1=1; } switch (alt1) { case 1 : // /home/michael/projects/jboss-rules-3.0.x/drools-compiler/src/main/resources/org/drools/lang/drl.g:276:18: (';'|EOL) { if ( input.LA(1)==EOL||input.LA(1)==15 ) { input.consume(); errorRecovery=false; } else { MismatchedSetException mse = new MismatchedSetException(null,input); recoverFromMismatchedSet(input,mse,FOLLOW_set_in_opt_eol41); throw mse; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -