use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6StrictParser method fromAccumulate.
/**
* fromAccumulate := ACCUMULATE LEFT_PAREN lhsAnd (COMMA|SEMICOLON)
* ( INIT chunk_(_) COMMA ACTION chunk_(_) COMMA
* ( REVERSE chunk_(_) COMMA)? RESULT chunk_(_)
* | accumulateFunction
* ) RIGHT_PAREN
*
* @param pattern
* @throws org.antlr.runtime.RecognitionException
*/
private void fromAccumulate(PatternDescrBuilder<?> pattern) throws RecognitionException {
AccumulateDescrBuilder<?> accumulate = helper.start(pattern, AccumulateDescrBuilder.class, null);
try {
if (helper.validateIdentifierKey(DroolsSoftKeywords.ACCUMULATE)) {
match(input, DRL6Lexer.ID, DroolsSoftKeywords.ACCUMULATE, null, DroolsEditorType.KEYWORD);
} else {
// might be using the short mnemonic
match(input, DRL6Lexer.ID, DroolsSoftKeywords.ACC, null, DroolsEditorType.KEYWORD);
}
if (state.failed)
return;
if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE);
}
match(input, DRL6Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
CEDescrBuilder<?, AndDescr> source = accumulate.source();
try {
helper.start(source, CEDescrBuilder.class, null);
lhsAnd(source, false);
if (state.failed)
return;
if (source.getDescr() != null && source.getDescr() instanceof ConditionalElementDescr) {
ConditionalElementDescr root = (ConditionalElementDescr) source.getDescr();
BaseDescr[] descrs = root.getDescrs().toArray(new BaseDescr[root.getDescrs().size()]);
root.getDescrs().clear();
for (int i = 0; i < descrs.length; i++) {
root.addOrMerge(descrs[i]);
}
}
} finally {
helper.end(CEDescrBuilder.class, source);
}
if (input.LA(1) == DRL6Lexer.COMMA) {
match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
} else if (input.LA(-1) != DRL6Lexer.SEMICOLON) {
match(input, DRL6Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
}
if (helper.validateIdentifierKey(DroolsSoftKeywords.INIT)) {
// custom code, inline accumulate
// initBlock
match(input, DRL6Lexer.ID, DroolsSoftKeywords.INIT, null, DroolsEditorType.KEYWORD);
if (state.failed)
return;
if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_INIT);
}
String init = chunk(DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_INIT_INSIDE);
if (state.failed)
return;
if (state.backtracking == 0)
accumulate.init(init);
if (input.LA(1) == DRL6Lexer.COMMA) {
match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
}
// actionBlock
match(input, DRL6Lexer.ID, DroolsSoftKeywords.ACTION, null, DroolsEditorType.KEYWORD);
if (state.failed)
return;
if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_ACTION);
}
String action = chunk(DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_ACTION_INSIDE);
if (state.failed)
return;
if (state.backtracking == 0)
accumulate.action(action);
if (input.LA(1) == DRL6Lexer.COMMA) {
match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
}
// reverseBlock
if (helper.validateIdentifierKey(DroolsSoftKeywords.REVERSE)) {
match(input, DRL6Lexer.ID, DroolsSoftKeywords.REVERSE, null, DroolsEditorType.KEYWORD);
if (state.failed)
return;
if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_REVERSE);
}
String reverse = chunk(DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_REVERSE_INSIDE);
if (state.failed)
return;
if (state.backtracking == 0)
accumulate.reverse(reverse);
if (input.LA(1) == DRL6Lexer.COMMA) {
match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
}
}
// resultBlock
match(input, DRL6Lexer.ID, DroolsSoftKeywords.RESULT, null, DroolsEditorType.KEYWORD);
if (state.failed)
return;
if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_RESULT);
}
String result = chunk(DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_RESULT_INSIDE);
if (state.failed)
return;
if (state.backtracking == 0)
accumulate.result(result);
} else {
// accumulate functions
accumulateFunction(accumulate, false, null);
if (state.failed)
return;
}
match(input, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
} finally {
helper.end(AccumulateDescrBuilder.class, accumulate);
if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
}
}
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6StrictParser method lhsAnd.
/**
* lhsAnd := LEFT_PAREN AND lhsUnary+ RIGHT_PAREN
* | lhsUnary (AND lhsUnary)*
*
* @param ce
* @throws org.antlr.runtime.RecognitionException
*/
private BaseDescr lhsAnd(final CEDescrBuilder<?, ?> ce, boolean allowOr) throws RecognitionException {
BaseDescr result = null;
if (input.LA(1) == DRL6Lexer.LEFT_PAREN && helper.validateLT(2, DroolsSoftKeywords.AND)) {
// prefixed AND
CEDescrBuilder<?, AndDescr> and = null;
if (state.backtracking == 0) {
and = ce.and();
result = ce.getDescr();
helper.start(and, CEDescrBuilder.class, null);
}
try {
match(input, DRL6Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
match(input, DRL6Lexer.ID, DroolsSoftKeywords.AND, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
}
while (input.LA(1) != DRL6Lexer.RIGHT_PAREN) {
lhsUnary(and, allowOr);
if (state.failed)
return null;
}
match(input, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
} finally {
if (state.backtracking == 0) {
helper.end(CEDescrBuilder.class, and);
}
}
} else {
// infix AND
// create an AND anyway, since if it is not an AND we remove it later
CEDescrBuilder<?, AndDescr> and = null;
if (state.backtracking == 0) {
and = ce.and();
result = and.getDescr();
helper.start(and, CEDescrBuilder.class, null);
}
try {
lhsUnary(and, allowOr);
if (state.failed)
return null;
if (helper.validateIdentifierKey(DroolsSoftKeywords.AND) || input.LA(1) == DRL6Lexer.DOUBLE_AMPER) {
while (helper.validateIdentifierKey(DroolsSoftKeywords.AND) || input.LA(1) == DRL6Lexer.DOUBLE_AMPER) {
if (input.LA(1) == DRL6Lexer.DOUBLE_AMPER) {
match(input, DRL6Lexer.DOUBLE_AMPER, null, null, DroolsEditorType.SYMBOL);
} else {
match(input, DRL6Lexer.ID, DroolsSoftKeywords.AND, null, DroolsEditorType.KEYWORD);
}
if (state.failed)
return null;
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
}
lhsUnary(and, allowOr);
if (state.failed)
return null;
}
} else {
if (state.backtracking == 0 && and.getDescr().getDescrs().size() < 2) {
// if no AND present, then remove it and add children to parent
((ConditionalElementDescr) ce.getDescr()).getDescrs().remove(and.getDescr());
for (BaseDescr base : and.getDescr().getDescrs()) {
((ConditionalElementDescr) ce.getDescr()).addDescr(base);
}
result = ce.getDescr();
}
}
} finally {
if (state.backtracking == 0) {
helper.end(CEDescrBuilder.class, and);
}
}
}
return result;
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6StrictParser method lhsPatternBind.
/**
* lhsPatternBind := label?
* ( LEFT_PAREN lhsPattern (OR lhsPattern)* RIGHT_PAREN
* | lhsPattern )
*
* @param ce
* @return
* @throws org.antlr.runtime.RecognitionException
*/
@SuppressWarnings("unchecked")
private BaseDescr lhsPatternBind(PatternContainerDescrBuilder<?, ?> ce, final boolean allowOr) throws RecognitionException {
PatternDescrBuilder<?> pattern = null;
CEDescrBuilder<?, OrDescr> or = null;
BaseDescr result = null;
Token first = input.LT(1);
pattern = helper.start((DescrBuilder<?, ?>) ce, PatternDescrBuilder.class, null);
if (pattern != null) {
result = pattern.getDescr();
}
String label = null;
boolean isUnification = false;
if (input.LA(1) == DRL6Lexer.ID && input.LA(2) == DRL6Lexer.COLON && !helper.validateCEKeyword(1)) {
label = label(DroolsEditorType.IDENTIFIER_PATTERN);
if (state.failed)
return null;
} else if (input.LA(1) == DRL6Lexer.ID && input.LA(2) == DRL6Lexer.UNIFY && !helper.validateCEKeyword(1)) {
label = unif(DroolsEditorType.IDENTIFIER_PATTERN);
if (state.failed)
return null;
isUnification = true;
}
if (input.LA(1) == DRL6Lexer.LEFT_PAREN) {
try {
match(input, DRL6Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
if (helper.validateCEKeyword(1)) {
failMismatchedTokenException();
// in case it is backtracking
return null;
}
lhsPattern(pattern, label, isUnification);
if (state.failed)
return null;
if (allowOr && helper.validateIdentifierKey(DroolsSoftKeywords.OR) && ce instanceof CEDescrBuilder) {
if (state.backtracking == 0) {
// this is necessary because of the crappy bind with multi-pattern OR syntax
or = ((CEDescrBuilder<DescrBuilder<?, ?>, OrDescr>) ce).or();
result = or.getDescr();
helper.end(PatternDescrBuilder.class, pattern);
helper.start(or, CEDescrBuilder.class, null);
// adjust real or starting token:
helper.setStart(or, first);
// remove original pattern from the parent CE child list:
((ConditionalElementDescr) ce.getDescr()).getDescrs().remove(pattern.getDescr());
// add pattern to the OR instead
or.getDescr().addDescr(pattern.getDescr());
}
while (helper.validateIdentifierKey(DroolsSoftKeywords.OR)) {
match(input, DRL6Lexer.ID, DroolsSoftKeywords.OR, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
pattern = helper.start(or, PatternDescrBuilder.class, null);
// new pattern, same binding
lhsPattern(pattern, label, isUnification);
if (state.failed)
return null;
helper.end(PatternDescrBuilder.class, pattern);
}
}
match(input, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
} finally {
if (or != null) {
helper.end(CEDescrBuilder.class, or);
} else {
helper.end(PatternDescrBuilder.class, pattern);
}
}
} else {
try {
lhsPattern(pattern, label, isUnification);
if (state.failed)
return null;
} finally {
helper.end(PatternDescrBuilder.class, pattern);
}
}
return result;
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6Parser method lhsParen.
/**
* lhsParen := LEFT_PAREN lhsOr RIGHT_PAREN
*
* @param ce
* @return
* @throws org.antlr.runtime.RecognitionException
*/
private BaseDescr lhsParen(CEDescrBuilder<?, ?> ce, boolean allowOr) throws RecognitionException {
match(input, DRL6Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
}
BaseDescr descr = lhsOr(ce, allowOr);
if (state.failed)
return null;
match(input, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
return descr;
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6Parser method constraint.
/**
* constraint := nestedConstraint | conditionalOrExpression
* @param pattern
* @throws org.antlr.runtime.RecognitionException
*/
private void constraint(PatternDescrBuilder<?> pattern, boolean positional, String prefix) throws RecognitionException {
if (speculateNestedConstraint()) {
nestedConstraint(pattern, prefix);
return;
}
if (state.backtracking == 0) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_START);
}
int first = input.index();
// resetting
exprParser.getHelper().setHasOperator(false);
try {
exprParser.conditionalOrExpression();
} finally {
if (state.backtracking == 0) {
if (input.LA(1) == DRL6Lexer.ID && input.LA(2) == DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
} else if (input.LA(1) != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_END);
} else if (!lastTokenWasWhiteSpace()) {
int location = getCurrentLocation();
if (location == Location.LOCATION_LHS_INSIDE_CONDITION_END) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
} else if (input.get(input.index()).getType() != DRL6Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_START);
}
} else if (getCurrentLocation() == Location.LOCATION_LHS_INSIDE_CONDITION_START && !exprParser.getHelper().getHasOperator() && lastTokenWasWhiteSpace() && input.LA(1) == DRL6Lexer.EOF && input.LA(-1) == DRL6Lexer.ID) {
helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_OPERATOR);
}
}
}
if (state.failed)
return;
if (state.backtracking == 0 && input.index() > first) {
// expression consumed something
int last = input.LT(-1).getTokenIndex();
String expr = toExpression(prefix, first, last);
pattern.constraint(expr, positional);
BaseDescr constrDescr = pattern.getDescr().getDescrs().get(pattern.getDescr().getDescrs().size() - 1);
constrDescr.setLocation(input.get(first).getLine(), input.get(first).getCharPositionInLine());
constrDescr.setEndLocation(input.get(last).getLine(), input.get(last).getCharPositionInLine());
constrDescr.setStartCharacter(((CommonToken) input.get(first)).getStartIndex());
constrDescr.setEndCharacter(((CommonToken) input.get(last)).getStopIndex());
}
}
Aggregations