Java Code Examples for org.antlr.runtime.tree.Tree#getType()

The following examples show how to use org.antlr.runtime.tree.Tree#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HL7Query.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private Evaluator<?> buildReferenceEvaluator(final Tree tree) {
    switch (tree.getType()) {
        case MESSAGE:
            return new MessageEvaluator();
        case SEGMENT_NAME:
            return new SegmentEvaluator(new StringLiteralEvaluator(tree.getText()));
        case IDENTIFIER:
            return new DeclaredReferenceEvaluator(new StringLiteralEvaluator(tree.getText()));
        case DOT:
            final Tree firstChild = tree.getChild(0);
            final Tree secondChild = tree.getChild(1);
            return new DotEvaluator(buildReferenceEvaluator(firstChild), buildIntegerEvaluator(secondChild));
        case STRING_LITERAL:
            return new StringLiteralEvaluator(tree.getText());
        case NUMBER:
            return new IntegerLiteralEvaluator(Integer.parseInt(tree.getText()));
        default:
            throw new HL7QueryParsingException("Failed to build evaluator for " + tree.getText());
    }
}
 
Example 2
Source File: IgniteQueryTreeRenderer.java    From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Flattens a subtree of kind "op[A, op[op[B, C], ...]]"
 * (or similar) into "op[A, B, C, ...]". Useful for nodes
 * representing multipart dis/conjunctions and arithmetic
 * expressions to skip unnecessary parentheses
 */
private static void flattenSubtree(Tree node) {
	if ( firstChildOfType( node, node.getType() ) == null ) {
		return;
	}
	Deque<Tree> s = new ArrayDeque<>();
	Deque<Tree> m = new ArrayDeque<>();
	s.add( node );
	Tree n;
	while ( ( n = s.pollLast() ) != null ) {
		if ( n.getType() == node.getType() ) {
			for ( int i = 0; i < n.getChildCount(); ++i ) {
				s.add( n.getChild( i ) );
			}
		}
		else {
			m.add( n );
		}
	}
	for ( int i = 0; i < node.getChildCount(); ++i ) {
		node.setChild( i, m.pollLast() );
	}
	while ( ( n = m.pollLast() ) != null ) {
		node.addChild( n );
	}
}
 
Example 3
Source File: CMISFTSQueryParser.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
static private String getText(Tree node)
{
    String text = node.getText();
    int index;
    switch (node.getType())
    {
    case CMIS_FTSParser.FTSWORD:
        index = text.indexOf('\\');
        if (index == -1)
        {
            return text;
        }
        else
        {
            return unescape(text);
        }
    case CMIS_FTSParser.FTSPHRASE:
        String phrase = text.substring(1, text.length() - 1);
        index = phrase.indexOf('\\');
        if (index == -1)
        {
            return phrase;
        }
        else
        {
            return unescape(phrase);
        }
    default:
        return text;
    }
}
 
Example 4
Source File: RecordPathCompiler.java    From nifi with Apache License 2.0 5 votes vote down vote up
public static RecordPathSegment compile(final Tree pathTree, final RecordPathSegment root, final boolean absolute) {
    if (pathTree.getType() == FUNCTION) {
        return buildPath(pathTree, null, absolute);
    }

    RecordPathSegment parent = root;
    for (int i = 0; i < pathTree.getChildCount(); i++) {
        final Tree child = pathTree.getChild(i);
        parent = RecordPathCompiler.buildPath(child, parent, absolute);
    }

    return parent;
}
 
Example 5
Source File: StreamDeclarationParser.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
private static DeclarationScope lookupStructName(CommonTree typeSpecifier, DeclarationScope scope) {
    /*
     * This needs a struct.struct_name.name to work, luckily, that is 99.99%
     * of traces we receive.
     */
    final Tree potentialStruct = typeSpecifier.getChild(0);
    DeclarationScope eventHeaderScope = null;
    if (potentialStruct.getType() == (CTFParser.STRUCT)) {
        final Tree potentialStructName = potentialStruct.getChild(0);
        if (potentialStructName.getType() == (CTFParser.STRUCT_NAME)) {
            final String name = potentialStructName.getChild(0).getText();
            eventHeaderScope = scope.lookupChildRecursive(name);
            if (eventHeaderScope == null) {
                eventHeaderScope = lookupScopeRecursiveStruct(name, scope);
            }
        }
    }
    /*
     * If that fails, maybe the struct is anonymous
     */
    if (eventHeaderScope == null) {
        eventHeaderScope = scope.lookupChildRecursive(MetadataStrings.STRUCT);
    }

    /*
     * This can still be null
     */
    return eventHeaderScope;
}
 
Example 6
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * {@code (BLOCK (ALT .))} or {@code (BLOCK (ALT 'a') (ALT .))}.
 */
public static boolean blockHasWildcardAlt(GrammarAST block) {
	for (Object alt : block.getChildren()) {
		if ( !(alt instanceof AltAST) ) continue;
		AltAST altAST = (AltAST)alt;
		if ( altAST.getChildCount()==1 || (altAST.getChildCount() == 2 && altAST.getChild(0).getType() == ANTLRParser.ELEMENT_OPTIONS) ) {
			Tree e = altAST.getChild(altAST.getChildCount() - 1);
			if ( e.getType()==ANTLRParser.WILDCARD ) {
				return true;
			}
		}
	}
	return false;
}
 
Example 7
Source File: RuleAST.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ActionAST getLexerAction() {
	Tree blk = getFirstChildWithType(ANTLRParser.BLOCK);
	if ( blk.getChildCount()==1 ) {
		Tree onlyAlt = blk.getChild(0);
		Tree lastChild = onlyAlt.getChild(onlyAlt.getChildCount()-1);
		if ( lastChild.getType()==ANTLRParser.ACTION ) {
			return (ActionAST)lastChild;
		}
	}
	return null;
}
 
Example 8
Source File: HL7Query.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void processDeclare(final Tree declare) {
    for (int i = 0; i < declare.getChildCount(); i++) {
        final Tree declarationTree = declare.getChild(i);

        final String identifier = declarationTree.getChild(0).getText();
        final Tree requiredOrOptionalTree = declarationTree.getChild(1);
        final boolean required = requiredOrOptionalTree.getType() == REQUIRED;

        final String segmentName = declarationTree.getChild(2).getText();

        final Declaration declaration = new Declaration() {
            @Override
            public String getAlias() {
                return identifier;
            }

            @Override
            public boolean isRequired() {
                return required;
            }

            @Override
            public Object getDeclaredValue(final HL7Message message) {
                if (message == null) {
                    return null;
                }

                return message.getSegments(segmentName);
            }
        };

        declarations.add(declaration);
    }
}
 
Example 9
Source File: HL7Query.java    From nifi with Apache License 2.0 5 votes vote down vote up
private HL7Query(final Tree tree, final String query) {
    this.tree = tree;
    this.query = query;

    List<Selection> select = null;
    BooleanEvaluator where = null;
    for (int i = 0; i < tree.getChildCount(); i++) {
        final Tree child = tree.getChild(i);

        switch (child.getType()) {
            case DECLARE:
                processDeclare(child);
                break;
            case SELECT:
                select = processSelect(child);
                break;
            case WHERE:
                where = processWhere(child);
                break;
            default:
                throw new HL7QueryParsingException("Found unexpected clause at root level: " + tree.getText());
        }
    }

    this.whereEvaluator = where;
    this.selections = select;
}
 
Example 10
Source File: HL7Query.java    From nifi with Apache License 2.0 5 votes vote down vote up
private BooleanEvaluator buildBooleanEvaluator(final Tree tree) {
    // TODO: add Date comparisons
    // LT/GT/GE/GE should allow for dates based on Field's Type
    // BETWEEN
    // DATE('2015/01/01')
    // DATE('2015/01/01 12:00:00')
    // DATE('24 HOURS AGO')
    // DATE('YESTERDAY')

    switch (tree.getType()) {
        case EQUALS:
            return new EqualsEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case NOT_EQUALS:
            return new NotEqualsEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case GT:
            return new GreaterThanEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case LT:
            return new LessThanEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case GE:
            return new GreaterThanOrEqualEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case LE:
            return new LessThanOrEqualEvaluator(buildReferenceEvaluator(tree.getChild(0)), buildReferenceEvaluator(tree.getChild(1)));
        case NOT:
            return new NotEvaluator(buildBooleanEvaluator(tree.getChild(0)));
        case AND:
            return new AndEvaluator(buildBooleanEvaluator(tree.getChild(0)), buildBooleanEvaluator(tree.getChild(1)));
        case OR:
            return new OrEvaluator(buildBooleanEvaluator(tree.getChild(0)), buildBooleanEvaluator(tree.getChild(1)));
        case IS_NULL:
            return new IsNullEvaluator(buildReferenceEvaluator(tree.getChild(0)));
        case NOT_NULL:
            return new NotNullEvaluator(buildReferenceEvaluator(tree.getChild(0)));
        default:
            throw new HL7QueryParsingException("Cannot build boolean evaluator for '" + tree.getText() + "'");
    }
}
 
Example 11
Source File: Query.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private static Evaluator<Boolean> buildBooleanEvaluator(final Tree tree) {
    switch (tree.getType()) {
        case TRUE:
            return addToken(new BooleanLiteralEvaluator(true), "true");
        case FALSE:
            return addToken(new BooleanLiteralEvaluator(false), "true");
    }
    throw new AttributeExpressionLanguageParsingException("Cannot build Boolean evaluator from tree " + tree.toString());
}
 
Example 12
Source File: EqualityComparisonBaseTreeNode.java    From suro with Apache License 2.0 5 votes vote down vote up
protected MessageFilter getEqualFilter() {
    String xpath = getXPath(getChild(0)); 
	
	Tree valueNode = getChild(1);
	
	switch(valueNode.getType()){
	case NUMBER:
		Number value = (Number)((ValueTreeNode)valueNode).getValue();
		return new PathValueMessageFilter(xpath, new NumericValuePredicate(value, "="));
	case STRING:
		String sValue = (String)((ValueTreeNode)valueNode).getValue();
		return new PathValueMessageFilter(xpath, new StringValuePredicate(sValue));
	case TRUE:
		return new PathValueMessageFilter(xpath, BooleanValuePredicate.TRUE);
	case FALSE:
		return new PathValueMessageFilter(xpath, BooleanValuePredicate.FALSE);
	case NULL:
		return new PathValueMessageFilter(xpath, NullValuePredicate.INSTANCE);
	case XPATH_FUN_NAME:
		String aPath = (String)((ValueTreeNode)valueNode).getValue();
		return new PathValueMessageFilter(xpath, new PathValuePredicate(aPath, xpath));
	case TIME_MILLIS_FUN_NAME:
		TimeMillisValueTreeNode timeNode = (TimeMillisValueTreeNode)valueNode;
		return new PathValueMessageFilter(xpath,
			new TimeMillisValuePredicate(
				timeNode.getValueFormat(), 
				timeNode.getValue(), 
				"="));
	case TIME_STRING_FUN_NAME:
		TimeStringValueTreeNode timeStringNode = (TimeStringValueTreeNode)valueNode;
		return new PathValueMessageFilter(xpath,
			new TimeStringValuePredicate(
				timeStringNode.getValueTimeFormat(), 
				timeStringNode.getInputTimeFormat(),
				timeStringNode.getValue(),
				"="));
	default:
		throw new UnexpectedTokenException(valueNode, "Number", "String", "TRUE", "FALSE");
	}
}
 
Example 13
Source File: HL7Query.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private IntegerEvaluator buildIntegerEvaluator(final Tree tree) {
    switch (tree.getType()) {
        case NUMBER:
            return new IntegerLiteralEvaluator(Integer.parseInt(tree.getText()));
        default:
            throw new HL7QueryParsingException("Failed to build Integer Evaluator for " + tree.getText());
    }
}
 
Example 14
Source File: HL7Query.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private String getSelectedName(final Tree selectable) {
    if (selectable.getChildCount() == 0) {
        return selectable.getText();
    } else if (selectable.getType() == DOT) {
        return getSelectedName(selectable.getChild(0)) + "." + getSelectedName(selectable.getChild(1));
    } else {
        return selectable.getChild(selectable.getChildCount() - 1).getText();
    }
}
 
Example 15
Source File: HL7Query.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void processDeclare(final Tree declare) {
    for (int i = 0; i < declare.getChildCount(); i++) {
        final Tree declarationTree = declare.getChild(i);

        final String identifier = declarationTree.getChild(0).getText();
        final Tree requiredOrOptionalTree = declarationTree.getChild(1);
        final boolean required = requiredOrOptionalTree.getType() == REQUIRED;

        final String segmentName = declarationTree.getChild(2).getText();

        final Declaration declaration = new Declaration() {
            @Override
            public String getAlias() {
                return identifier;
            }

            @Override
            public boolean isRequired() {
                return required;
            }

            @Override
            public Object getDeclaredValue(final HL7Message message) {
                if (message == null) {
                    return null;
                }

                return message.getSegments(segmentName);
            }
        };

        declarations.add(declaration);
    }
}
 
Example 16
Source File: HL7Query.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private HL7Query(final Tree tree, final String query) {
    this.tree = tree;
    this.query = query;

    List<Selection> select = null;
    BooleanEvaluator where = null;
    for (int i = 0; i < tree.getChildCount(); i++) {
        final Tree child = tree.getChild(i);

        switch (child.getType()) {
            case DECLARE:
                processDeclare(child);
                break;
            case SELECT:
                select = processSelect(child);
                break;
            case WHERE:
                where = processWhere(child);
                break;
            default:
                throw new HL7QueryParsingException("Found unexpected clause at root level: " + tree.getText());
        }
    }

    this.whereEvaluator = where;
    this.selections = select;
}
 
Example 17
Source File: TarsKey.java    From TarsJava with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void addChild(Tree child) {
    super.addChild(child);
    if (child.getType() == TARS_IDENTIFIER) {
        keyList.add(child.getText());
    }
}
 
Example 18
Source File: FTSQueryParser.java    From alfresco-data-model with GNU Lesser General Public License v3.0 4 votes vote down vote up
static private String getText(Tree node, boolean returnTextFromUnknownNodes)
{
    String text = node.getText();
    int index;
    switch (node.getType())
    {
    case FTSParser.FTSWORD:
    case FTSParser.FTSPRE:
    case FTSParser.FTSWILD:
        index = text.indexOf('\\');
        if (index == -1)
        {
            return text;
        }
        else
        {
            return unescape(text);
        }
    case FTSParser.FTSPHRASE:
        String phrase = text.substring(1, text.length() - 1);
        index = phrase.indexOf('\\');
        if (index == -1)
        {
            return phrase;
        }
        else
        {
            return unescape(phrase);
        }
    case FTSParser.ID:
        index = text.indexOf('\\');
        if (index == -1)
        {
            return ISO9075.decode(text);
        }
        else
        {
            return ISO9075.decode(unescape(text));
        }
    case FTSParser.URI:
    case FTSParser.OR:
    case FTSParser.AND:
    case FTSParser.NOT:
    case FTSParser.TILDA:
    case FTSParser.PLUS:
    case FTSParser.MINUS:
    case FTSParser.COLON:
    case FTSParser.STAR:
    case FTSParser.DOTDOT:
    case FTSParser.DOT:
    case FTSParser.AMP:
    case FTSParser.EXCLAMATION:
    case FTSParser.BAR:
    case FTSParser.EQUALS:
    case FTSParser.QUESTION_MARK:
    case FTSParser.TO:
    case FTSParser.COMMA:
    case FTSParser.CARAT:
    case FTSParser.DOLLAR:
    case FTSParser.AT:
    case FTSParser.PERCENT:
    case FTSParser.DECIMAL_INTEGER_LITERAL:
    case FTSParser.FLOATING_POINT_LITERAL:
    case FTSParser.DATETIME:
        return text;
    default:
        if(returnTextFromUnknownNodes)
        {
            return text;
        }
        else
        {
            return "";
        }
    }
}
 
Example 19
Source File: CliClient.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private ByteBuffer getColumnName(String columnFamily, Tree columnTree)
{
    return (columnTree.getType() == CliParser.FUNCTION_CALL)
                ? convertValueByFunction(columnTree, null, null)
                : columnNameAsBytes(CliUtils.unescapeSQLString(columnTree.getText()), columnFamily);
}
 
Example 20
Source File: CliClient.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
private ByteBuffer getSubColumnName(String columnFamily, Tree columnTree)
{
    return (columnTree.getType() == CliParser.FUNCTION_CALL)
                ? convertValueByFunction(columnTree, null, null)
                : subColumnNameAsBytes(CliUtils.unescapeSQLString(columnTree.getText()), columnFamily);
}