Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continued reading of DRLParser.java #5

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 36 additions & 78 deletions drools-compiler/src/main/java/org/drools/lang/DRLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.drools.lang.api.QueryDescrBuilder;
import org.drools.lang.api.RuleDescrBuilder;
import org.drools.lang.descr.AndDescr;
import org.drools.lang.descr.AnnotationDescr;
import org.drools.lang.descr.AttributeDescr;
import org.drools.lang.descr.BaseDescr;
import org.drools.lang.descr.ConditionalElementDescr;
Expand Down Expand Up @@ -472,29 +473,17 @@ public TypeDeclarationDescr declare() throws RecognitionException {
}

/**
* field := ID COLON type (EQUALS_ASSIGN expression)? annotation* SEMICOLON?
* field := label type (EQUALS_ASSIGN expression)? annotation* SEMICOLON?
*/
private void field( DeclareDescrBuilder declare ) {
FieldDescrBuilder field = null;
try {
// ID
Token id = match( input,
DRLLexer.ID,
null,
null,
DroolsEditorType.IDENTIFIER );
String fname = label( DroolsEditorType.IDENTIFIER );
if ( state.failed ) return;

field = helper.start( FieldDescrBuilder.class,
id.getText(),
null );

match( input,
DRLLexer.COLON,
null,
null,
DroolsEditorType.SYMBOL );
if ( state.failed ) return;
fname,
null );

// type
String type = qualifiedIdentifier();
Expand Down Expand Up @@ -942,7 +931,7 @@ private String stringId() throws RecognitionException {
}

/**
* attributes := (ATTRIBUTES COMMA)? attribute ( COMMA? attribute )*
* attributes := (ATTRIBUTES COLON)? attribute ( COMMA? attribute )*
* @param rule
* @throws RecognitionException
*/
Expand Down Expand Up @@ -1424,8 +1413,7 @@ private AttributeDescr intOrChunkAttribute( String[] key ) throws RecognitionExc
-1 );
if ( state.failed ) return null;
if ( state.backtracking == 0 ) {
attribute.value( safeStripDelimiters( value,
new String[]{"(", ")"} ) );
attribute.value( safeStripDelimiters( value, "(", ")" ) );
attribute.type( AttributeDescr.Type.EXPRESSION );
}
} else {
Expand Down Expand Up @@ -1469,7 +1457,7 @@ private AttributeDescr intOrChunkAttribute( String[] key ) throws RecognitionExc
}

/**
* lhs := WHEN COLON? lhsStatement*
* lhs := WHEN COLON? lhsStatement
* @param rule
* @throws RecognitionException
*/
Expand Down Expand Up @@ -1652,8 +1640,8 @@ private BaseDescr lhsOr( final CEDescrBuilder< ? , ? > ce,
}

/**
* lhsAnd:= LEFT_PAREN AND lhsUnary+ RIGHT_PAREN
* | lhsUnary (AND lhsUnary)*
* lhsAnd := LEFT_PAREN AND lhsUnary+ RIGHT_PAREN
* | lhsUnary (AND lhsUnary)*
*
* @param ce
* @throws RecognitionException
Expand Down Expand Up @@ -2136,7 +2124,7 @@ private BaseDescr lhsPatternBind( PatternContainerDescrBuilder< ? , ? > ce,

String label = null;
if ( input.LA( 1 ) == DRLLexer.ID && input.LA( 2 ) == DRLLexer.COLON && !helper.validateCEKeyword( 1 ) ) {
label = label();
label = label( DroolsEditorType.IDENTIFIER_PATTERN );
if ( state.failed ) return null;
}

Expand Down Expand Up @@ -2263,7 +2251,6 @@ private void failMissingTokenException() throws MissingTokenException {
*/
private void lhsPattern( PatternDescrBuilder< ? > pattern,
String label ) throws RecognitionException {
// String type = unadornedType();
String type = this.qualifiedIdentifier();
if ( state.failed ) return;

Expand Down Expand Up @@ -2310,12 +2297,12 @@ private void lhsPattern( PatternDescrBuilder< ? > pattern,
* @return
* @throws RecognitionException
*/
private String label() throws RecognitionException {
private String label( DroolsEditorType edType ) throws RecognitionException {
Token label = match( input,
DRLLexer.ID,
null,
null,
DroolsEditorType.IDENTIFIER_PATTERN );
edType );
if ( state.failed ) return null;

match( input,
Expand Down Expand Up @@ -2396,7 +2383,7 @@ private void constraints( PatternDescrBuilder< ? > pattern ) throws RecognitionE
}

/**
* constraint := conditionalExpression
* constraint := label? conditionalExpression
* @param pattern
* @throws RecognitionException
*/
Expand All @@ -2406,21 +2393,7 @@ private void constraint( PatternDescrBuilder< ? > pattern, boolean positional )
}
String bind = null;
if ( input.LA( 1 ) == DRLLexer.ID && input.LA( 2 ) == DRLLexer.COLON ) {
// bind
Token id = match( input,
DRLLexer.ID,
null,
null,
DroolsEditorType.IDENTIFIER_VARIABLE );
if ( state.failed ) return;

bind = id.getText();

match( input,
DRLLexer.COLON,
null,
null,
DroolsEditorType.SYMBOL );
bind = label( DroolsEditorType.IDENTIFIER_VARIABLE );
if ( state.failed ) return;
}

Expand Down Expand Up @@ -2482,7 +2455,7 @@ private void patternBehavior( PatternDescrBuilder< ? > pattern ) throws Recognit
}

/**
* behaviorDef := ID COLON ID LEFT_PAREN expression RIGHT_PAREN
* behaviorDef := label ID LEFT_PAREN expression RIGHT_PAREN
* @param pattern
* @throws RecognitionException
*/
Expand All @@ -2491,18 +2464,7 @@ private void behaviorDef( PatternDescrBuilder< ? > pattern ) throws RecognitionE
null,
null );
try {
Token type = match( input,
DRLLexer.ID,
null,
null,
DroolsEditorType.IDENTIFIER_PATTERN );
if ( state.failed ) return;

match( input,
DRLLexer.COLON,
null,
null,
DroolsEditorType.SYMBOL );
String bName = label( DroolsEditorType.IDENTIFIER_PATTERN );
if ( state.failed ) return;

Token subtype = match( input,
Expand All @@ -2513,7 +2475,7 @@ private void behaviorDef( PatternDescrBuilder< ? > pattern ) throws RecognitionE
if ( state.failed ) return;

if ( state.backtracking == 0 ) {
behavior.type( type.getText(),
behavior.type( bName,
subtype.getText() );
}

Expand All @@ -2529,6 +2491,7 @@ private void behaviorDef( PatternDescrBuilder< ? > pattern ) throws RecognitionE
}
}


/**
* patternSource := FROM
* ( accumulate
Expand Down Expand Up @@ -2987,18 +2950,9 @@ private void rhs( RuleDescrBuilder rule ) throws RecognitionException {
chunk = chunk.substring( 0,
chunk.length() - DroolsSoftKeywords.END.length() );
}
// removing the "then" keyword any any subsequent space and line break
int index = 4;
while ( index < chunk.length() && Character.isWhitespace( chunk.charAt( index ) ) ) {
index++;
if ( chunk.charAt( index - 1 ) == '\r' || chunk.charAt( index - 1 ) == '\n' ) {
if ( index < chunk.length() && chunk.charAt( index - 1 ) == '\r' && chunk.charAt( index ) == '\n' ) {
index++;
}
break;
}
}
chunk = chunk.substring( index );
// remove the "then" keyword and any subsequent spaces and line breaks
// keep indendation of 1st non-blank line
chunk = chunk.replaceFirst( "^then\\s*[\\r\\n]", "" );
}
rule.rhs( chunk );

Expand Down Expand Up @@ -3164,8 +3118,13 @@ private void elementValuePair( AnnotationDescrBuilder annotation ) {
if ( state.failed ) return;

if ( state.backtracking == 0 ) {
annotation.keyValue( key != null ? key : "value",
value );
String actKey = key != null ? key : "value";
String actVal = annotation.getDescr().getValue( actKey );
if( actVal != null ){
// TODO: error message?
value = "\"" + AnnotationDescr.unquote( actVal ) + AnnotationDescr.unquote( value ) + "\"";
}
annotation.keyValue( actKey, value );
}

} catch ( RecognitionException re ) {
Expand Down Expand Up @@ -3666,12 +3625,13 @@ public boolean mismatchIsMissingToken( TokenStream input,
}

private String safeStripDelimiters( String value,
String[] delimiters ) {
String left, String right ) {
if ( value != null ) {
value = value.trim();
if ( value.length() > 1 && value.startsWith( delimiters[0] ) && value.endsWith( delimiters[1] ) ) {
value = value.substring( 1,
value.length() - 1 );
if ( value.length() >= left.length() + right.length() &&
value.startsWith( left ) && value.endsWith( right ) ) {
value = value.substring( left.length(),
value.length() - right.length() );
}
}
return value;
Expand All @@ -3680,9 +3640,7 @@ private String safeStripDelimiters( String value,
private String safeStripStringDelimiters( String value ) {
if ( value != null ) {
value = value.trim();
if ( value.length() > 1 &&
((value.startsWith( "\"" ) && value.endsWith( "\"" )) ||
(value.startsWith( "'" ) && value.endsWith( "'" ))) ) {
if ( value.length() >= 2 && value.startsWith( "\"" ) && value.endsWith( "\"" ) ) {
value = value.substring( 1,
value.length() - 1 );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class AnnotationDescr extends BaseDescr {
private final String name;
private Map<String, String> values;

private static String unquote( String s ){
// '' and 'a' are passed through as
public static String unquote( String s ){
if( s.startsWith( "\"" ) && s.endsWith( "\"" ) ||
s.startsWith( "'" ) && s.endsWith( "'" ) ) {
return s.substring( 1, s.length() - 1 );
Expand Down