From: <jbo...@li...> - 2006-01-24 08:20:08
|
Author: mic...@jb... Date: 2006-01-24 03:19:56 -0500 (Tue, 24 Jan 2006) New Revision: 2184 Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/lang/Parser.java trunk/labs/jbossrules/drools-core/src/test/java/org/drools/lang/one-rule.drl Log: trailing comments allowed Modified: trunk/labs/jbossrules/drools-core/src/main/java/org/drools/lang/Parser.java =================================================================== --- trunk/labs/jbossrules/drools-core/src/main/java/org/drools/lang/Parser.java 2006-01-24 07:02:40 UTC (rev 2183) +++ trunk/labs/jbossrules/drools-core/src/main/java/org/drools/lang/Parser.java 2006-01-24 08:19:56 UTC (rev 2184) @@ -22,7 +22,7 @@ */ public class Parser { - private static Pattern PACKAGE_DECL = Pattern.compile( "\\s*package\\s*([^;]+);?\\s*" ); + private static Pattern PACKAGE_DECL = Pattern.compile( "\\s*package\\s*([^;]+);?\\s*" ); private static Pattern IMPORT_STATEMENT = Pattern.compile( "\\s*import\\s*([^;]+);?\\s*" ); private static Pattern RULE_DECL = Pattern.compile( "\\s*rule\\s*([^\\s]+)\\s*" ); @@ -30,7 +30,11 @@ private static Pattern FACT_BINDING = Pattern.compile( "\\s*(\\w+)\\s*=>\\s*(.*)" ); private static Pattern FIELD_BINDING = Pattern.compile( "\\s*(\\w+):(\\w+)\\s*" ); - + + private static String COMMENT_2 = "//"; + private static String COMMENT_1 = "#"; + + private BufferedReader reader; private String packageDeclaration; @@ -165,7 +169,7 @@ line = laDiscard(); if ( ! line.trim().equals( "end" ) ) { - throw new ParseException( "end expected.", lineNumber); + throw new ParseException( "[end] expected. But instead got: [" + line.trim() + "]", lineNumber); } consumeDiscard(); @@ -263,7 +267,7 @@ String guts = pattern.substring( leftParen+1, rightParen ).trim(); - // TODO: Michael, should be also expand the guts? + // TODO: Michael, should be also expand the guts? Nope. Don't really think so. StringTokenizer tokens = new StringTokenizer( guts, "," ); @@ -281,7 +285,7 @@ String field = matcher.group( 2 ); System.err.println( "bind [" + bindTo + "] to field [" + field + "]" ); } else { - // TODO: Michael, want to jack in here also? + // TODO: Michael, want to jack in here also? Nope, not really... System.err.println( "further work required for [" + constraint + "]" ); } } @@ -333,13 +337,13 @@ String trimLine = line.trim(); - if ( trimLine.length() == 0 || trimLine.startsWith( "#" ) || trimLine.startsWith( "//" ) ) { + if ( trimLine.length() == 0 || trimLine.startsWith( COMMENT_1 ) || trimLine.startsWith( COMMENT_2 ) ) { line = null; } } reader.reset(); - return line; + return stripTrailingComments(line); } protected String consume() throws IOException { @@ -361,11 +365,24 @@ String trimLine = line.trim(); - if ( trimLine.length() == 0 || trimLine.startsWith( "#" ) || trimLine.startsWith( "//" ) ) { + if ( trimLine.length() == 0 || trimLine.startsWith( COMMENT_1 ) || trimLine.startsWith( COMMENT_2 ) ) { line = null; } } - return line; + return stripTrailingComments(line); } + + /** + * Removes "inline" comments from string. + */ + protected String stripTrailingComments(final String line) { + //BOB: is this more efficient with a single regex? + //Then can have a single definition of a comment? + int pos = line.lastIndexOf(COMMENT_1); + if (pos > 0) return line.substring(0, pos); + pos = line.lastIndexOf(COMMENT_2); + if (pos > 0) return line.substring(0, pos); + return line; + } } Modified: trunk/labs/jbossrules/drools-core/src/test/java/org/drools/lang/one-rule.drl =================================================================== --- trunk/labs/jbossrules/drools-core/src/test/java/org/drools/lang/one-rule.drl 2006-01-24 07:02:40 UTC (rev 2183) +++ trunk/labs/jbossrules/drools-core/src/test/java/org/drools/lang/one-rule.drl 2006-01-24 08:19:56 UTC (rev 2184) @@ -7,7 +7,7 @@ # semi colons are optional, we're line-centric -import java.util.List +import java.util.List//trailing comment import java.util.ArrayList; # use expanders for domain specific and pseudo natural language extensions @@ -15,7 +15,7 @@ rule find_seating - when + when #trailing comment context => Context( state == Context.ASSIGN_SEATS ) Seating( seatingId:id, seatingPid:pid, pathDone == true, seatingRightSeat:rightSeat, seatingRightGuestName:rightGuestName ) |