Java Code Examples for org.antlr.runtime.tree.CommonTree

The following examples show how to use org.antlr.runtime.tree.CommonTree. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: cuba   Source File: IdVarSelector.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object pre(Object t) {
    if (!(t instanceof CommonTree))
        return t;

    if (t instanceof CommonErrorNode) {
        return t;
    }

    CommonTree node = (CommonTree) t;

    if (node instanceof QueryNode) {
        QueryVariableContext newCurrent = new QueryVariableContext(model, (QueryNode) node);
        if (root == null) {
            root = newCurrent;
        }
        QueryVariableContext last = stack.peekLast();
        if (last != null) {
            last.addChild(newCurrent);
        }
        stack.addLast(newCurrent);
    }
    return t;
}
 
Example 2
private static boolean hasOptionalFieldReference(CommonTree node)
{
    switch (node.getType())
    {
    case FTSParser.TERM:
    case FTSParser.EXACT_TERM:
    case FTSParser.PHRASE:
    case FTSParser.EXACT_PHRASE:
    case FTSParser.SYNONYM:
    case FTSParser.PROXIMITY:
    case FTSParser.RANGE:
        return true;
    default:
        return false;
    }
}
 
Example 3
/**
 * @param orNode CommonTree
 * @param factory QueryModelFactory
 * @param functionEvaluationContext FunctionEvaluationContext
 * @param selectors Map<String, Selector>
 * @param columnMap HashMap<String, Column>
 * @return Constraint
 */
private Constraint buildDisjunction(CommonTree orNode, QueryModelFactory factory,
        FunctionEvaluationContext functionEvaluationContext, Map<String, Selector> selectors,
        HashMap<String, Column> columnMap)
{
    List<Constraint> constraints = new ArrayList<Constraint>(orNode.getChildCount());
    for (int i = 0; i < orNode.getChildCount(); i++)
    {
        CommonTree andNode = (CommonTree) orNode.getChild(i);
        Constraint constraint = buildConjunction(andNode, factory, functionEvaluationContext, selectors, columnMap);
        constraints.add(constraint);
    }
    if (constraints.size() == 1)
    {
        return constraints.get(0);
    } else
    {
        return factory.createDisjunction(constraints);
    }
}
 
Example 4
Source Project: cuba   Source File: JoinVariableNode.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public CommonTree treeToQueryPre(QueryBuilder sb, List<ErrorRec> invalidNodes) {
    int childCount = getChildCount();
    if (childCount == 0) {
        invalidNodes.add(new ErrorRec(this, "No children found"));
        return null;
    }
    sb.appendSpace();
    sb.appendString(joinSpec);
    sb.appendSpace();
    sb.appendString(toQuery(getChild(0)));
    sb.appendSpace();
    sb.appendString(variableName);
    if (childCount > 1) {
        sb.appendSpace();
        sb.appendString("on");
        for (int idx = 1; idx < childCount; idx++) {
            sb.appendSpace();
            sb.appendString(toQuery(getChild(idx)));
        }
    }
    return null;
}
 
Example 5
/**
 * Used by ComparisonClauseTest, validates the clause
 * @param theQuery Query
 * @param comparisonOperator One of EQUALS LESSTHAN GREATERTHAN LESSTHANOREQUALS GREATERTHANOREQUALS
    * @param propName String
    * @param propVal String
    */
private void comparisonChecks(Query theQuery, final int comparisonOperator, final String propName, final String propVal) {
    assertNotNull(theQuery);
    CommonTree tree = theQuery.getTree();
    assertNotNull(tree);
	assertEquals(comparisonOperator, tree.getType());
	assertEquals(WhereClauseParser.PROPERTYNAME, tree.getChild(0).getType());
	assertTrue(propName.equals(tree.getChild(0).getText()));
	assertEquals(WhereClauseParser.PROPERTYVALUE, tree.getChild(1).getType());
	QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){
		@Override
		public void comparison(int comparisonType, String propertyName, String propertyValue, boolean negated) {
			assertTrue("Property name should be "+propName,propName.equals(propertyName));
			assertTrue(comparisonOperator == comparisonType);
			assertTrue("Property value should be "+propVal,propVal.equals(propertyValue));
		}
		
	});
}
 
Example 6
/**
 * Creates the string representation of a type specifier.
 *
 * @param typeSpecifierList
 *            A TYPE_SPECIFIER node.
 *
 * @return A StringBuilder to which will be appended the string.
 * @throws ParseException
 *             invalid node
 */
@Override
public String parse(CommonTree typeSpecifierList, ICommonTreeParserParameter param) throws ParseException {
    if (!(param instanceof Param)) {
        throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
    }
    List<CommonTree> pointers = ((Param) param).fList;
    StringBuilder sb = new StringBuilder();
    sb.append(TypeSpecifierListStringParser.INSTANCE.parse(typeSpecifierList, null));
    if (pointers != null) {
        CommonTree temp = new CommonTree();
        for (CommonTree pointer : pointers) {
            temp.addChild(pointer);
        }
        sb.append(PointerListStringParser.INSTANCE.parse(temp, null));
    }
    return sb.toString();
}
 
Example 7
/**
 * Used by the matchesClauseTest
 * @param theQuery Query
 * @param propName String
 * @param propVal String
 */
private void matchesChecks(Query theQuery, final String propName, final String propVal) {
    assertNotNull(theQuery);
    CommonTree tree = theQuery.getTree();
    assertNotNull(tree);
	assertEquals(WhereClauseParser.PROPERTYNAME, tree.getChild(0).getType());
	assertTrue(propName.equals(tree.getChild(0).getText()));
	assertEquals(WhereClauseParser.PROPERTYVALUE, tree.getChild(1).getType());
	QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){
		@Override
		public void matches(String propertyName, String propertyValue, boolean negated) {
			assertTrue("Property name should be "+propName,propName.equals(propertyName));
			assertTrue("Property value should be "+propVal,propVal.equals(propertyValue));
		}
		
	});
}
 
Example 8
/**
 * Use by the inClauseTest
 * @param theQuery Query
 * @param propName String
 * @param values String...
 */
private void inChecks(Query theQuery, final String propName, final String... values) {
    assertNotNull(theQuery);
    CommonTree tree = theQuery.getTree();
    assertNotNull(tree);
	assertEquals(WhereClauseParser.IN, tree.getType());
	assertEquals(WhereClauseParser.PROPERTYNAME, tree.getChild(0).getType());
	assertTrue(propName.equals(tree.getChild(0).getText()));
	QueryHelper.walk(theQuery, new WalkerCallbackAdapter(){
		@Override
		public void in(String property, boolean negated, String... propertyValues) {
			assertTrue("Property name should be "+propName,propName.equals(property));
			for (int i = 0; i < values.length; i++) {
				assertTrue("Value must match:"+values[i],values[i].equals(propertyValues[i]));				
			}
		}
		
	});
}
 
Example 9
Source Project: codebuff   Source File: CompilationState.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void refAttr(Token templateToken, CommonTree id) {
    String name = id.getText();
    if ( impl.formalArguments!=null && impl.formalArguments.get(name)!=null ) {
        FormalArgument arg = impl.formalArguments.get(name);
        int index = arg.index;
        emit1(id, Bytecode.INSTR_LOAD_LOCAL, index);
    }
    else {
        if ( Interpreter.predefinedAnonSubtemplateAttributes.contains(name) ) {
            errMgr.compileTimeError(ErrorType.REF_TO_IMPLICIT_ATTRIBUTE_OUT_OF_SCOPE, templateToken, id.token);
            emit(id, Bytecode.INSTR_NULL);
        }
        else {
            emit1(id, Bytecode.INSTR_LOAD_ATTR, name);
        }
    }
}
 
Example 10
Source Project: tracecompass   Source File: EventIDParser.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Long parse(CommonTree tree, ICommonTreeParserParameter param) throws ParseException {

    CommonTree firstChild = (CommonTree) tree.getChild(0);

    if (isUnaryInteger(firstChild)) {
        if (tree.getChildCount() > 1) {
            throw new ParseException(INVALID_VALUE_ERROR);
        }
        long intval = UnaryIntegerParser.INSTANCE.parse(firstChild, null);
        if (intval > Integer.MAX_VALUE) {
            throw new ParseException("Event id larger than int.maxvalue, something is amiss"); //$NON-NLS-1$
        }
        return intval;
    }
    throw new ParseException(INVALID_VALUE_ERROR);
}
 
Example 11
Source Project: codebuff   Source File: CompilationState.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void refAttr(Token templateToken, CommonTree id) {
    String name = id.getText();
    if ( impl.formalArguments !=null && impl.formalArguments.get(name)!=null ) {
        FormalArgument arg = impl.formalArguments.get(name);
        int index = arg.index;
        emit1(id, Bytecode.INSTR_LOAD_LOCAL, index);
    }
    else {
        if ( Interpreter.predefinedAnonSubtemplateAttributes.contains(name) ) {
            errMgr.compileTimeError(ErrorType.REF_TO_IMPLICIT_ATTRIBUTE_OUT_OF_SCOPE,
                                    templateToken,
                                    id.token);
            emit(id, Bytecode.INSTR_NULL);
        }
        else {
            emit1(id, Bytecode.INSTR_LOAD_ATTR, name);
        }
    }
}
 
Example 12
Source Project: codebuff   Source File: CompilationState.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void refAttr(Token templateToken, CommonTree id) {
    String name = id.getText();
    if ( impl.formalArguments!=null && impl.formalArguments.get(name) !=null ) {
        FormalArgument arg = impl.formalArguments.get(name);
        int index = arg.index;
        emit1(id, Bytecode.INSTR_LOAD_LOCAL, index);
    }
    else {
        if ( Interpreter.predefinedAnonSubtemplateAttributes.contains(name) ) {
            errMgr.compileTimeError(ErrorType.REF_TO_IMPLICIT_ATTRIBUTE_OUT_OF_SCOPE, templateToken, id.token);
            emit(id, Bytecode.INSTR_NULL);
        } else {
            emit1(id, Bytecode.INSTR_LOAD_ATTR, name);
        }
    }
}
 
Example 13
Source Project: codebuff   Source File: CompilationState.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void refAttr(Token templateToken, CommonTree id) {
    String name = id.getText();
    if ( impl.formalArguments!=null && impl.formalArguments.get(name) !=null ) {
        FormalArgument arg = impl.formalArguments.get(name);
        int index = arg.index;
        emit1(id, Bytecode.INSTR_LOAD_LOCAL, index);
    }
    else {
        if ( Interpreter.predefinedAnonSubtemplateAttributes.contains(name) ) {
            errMgr.compileTimeError(ErrorType.REF_TO_IMPLICIT_ATTRIBUTE_OUT_OF_SCOPE, templateToken, id.token);
            emit(id, Bytecode.INSTR_NULL);
        } else {
            emit1(id, Bytecode.INSTR_LOAD_ATTR, name);
        }
    }
}
 
Example 14
/**
 * Starting from a COBOL source fragment translates to XML Schema.
 * @param source COBOL source fragment.
 * @return an XML Schema
 * @throws RecognizerException if emit fails
 */
public String emit(final String source)  throws RecognizerException {
    try {
        CommonTree ast = parse(source);
        if (_log.isDebugEnabled()) {
            _log.debug(ast.toStringTree());
        }
        TreeNodeStream nodes = new CommonTreeNodeStream(ast);
        CobolStructureEmitter emitter = new CobolStructureEmitterImpl(
                nodes, getErrorHandler());
        List < CobolDataItem > dataEntries = new ArrayList < CobolDataItem >();
        emitter.cobdata(dataEntries);
        return dataEntries.toString();
    } catch (RecognitionException e) {
        throw new RecognizerException(e);
    }
}
 
Example 15
Source Project: codebuff   Source File: CompilationState.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void refAttr(Token templateToken, CommonTree id) {
    String name = id.getText();
    if ( impl.formalArguments!=null && impl.formalArguments.get(name)!=null ) {
        FormalArgument arg = impl.formalArguments.get(name);
        int index = arg.index;
        emit1(id, Bytecode.INSTR_LOAD_LOCAL, index);
    }
    else {
        if ( Interpreter.predefinedAnonSubtemplateAttributes.contains(name) ) {
            errMgr.compileTimeError(ErrorType.REF_TO_IMPLICIT_ATTRIBUTE_OUT_OF_SCOPE, templateToken, id.token);
            emit(id, Bytecode.INSTR_NULL);
        }
        else {
            emit1(id, Bytecode.INSTR_LOAD_ATTR, name);
        }
    }
}
 
Example 16
Source Project: tracecompass   Source File: CtfParserTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
void matches(CommonTree tree) {
    if (fType == -1) {
        return;
    }
    if (tree.getType() != fType) {
        fail("Type mismatch!" +
                " expected:" + fType +
                " actual:" + tree.getType());
    }

    if (fText != null) {
        if (!fText.equals(tree.getText())) {
            fail("Text mismatch!" +
                    " expected:" + fText +
                    " actual:" + tree.getText());
        }
    }

    if (fChild != null) {
        int size = fChild.length;
        if (tree.getChildren() == null) {
            if (size != 0) {
                fail("Invalid children!"
                        + "Expect: " + size + "child");
            }
        } else {
            if (tree.getChildren().size() != size) {
                fail("Invalid number of childs!"
                        + " expected:" + size
                        + " actual:" + tree.getChildren().size());
            }

            for (int i = 0; i < size; ++i) {
                fChild[i].matches((CommonTree) tree.getChild(i));
            }
        }
    }
}
 
Example 17
Source Project: codebuff   Source File: CompilationState.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void emit(CommonTree opAST, short opcode) {
    ensureCapacity(1);
    if ( opAST!=null ) {
        int i = opAST.getTokenStartIndex();
        int j = opAST.getTokenStopIndex();
        int p = ((CommonToken)tokens.get(i)).getStartIndex();
        int q = ((CommonToken)tokens.get(j)).getStopIndex();
        if ( !(p<0 || q<0) ) impl.sourceMap[ip] = new Interval(p, q);
    }
    impl.instrs[ip++] = (byte)opcode;
}
 
Example 18
Source Project: netbeans   Source File: VmOptionsGrammarTest.java    License: Apache License 2.0 5 votes vote down vote up
private JavaVMOption<?> extractOption(CommandLineParser.vmOptions_return options_return) {
    CommonTree root = (CommonTree) options_return.getTree();
    if (root instanceof JavaVMOption<?>) {
        return (JavaVMOption<?>) root;
    } else if (root != null) {
        return (JavaVMOption<?>) root.getChildren().get(0);
    }
    return null;
}
 
Example 19
Source Project: Elasticsearch   Source File: SqlParser.java    License: Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Statement createStatement(CommonTree tree) {
    TreeNodeStream stream = new BufferedTreeNodeStream(tree);
    StatementBuilder builder = new StatementBuilder(stream);
    try {
        return builder.statement().value;
    } catch (RecognitionException e) {
        throw new AssertionError(e); // RecognitionException is not thrown
    }
}
 
Example 20
Source Project: Elasticsearch   Source File: SqlParser.java    License: Apache License 2.0 5 votes vote down vote up
private static Expression createExpression(CommonTree tree) {
    TreeNodeStream stream = new BufferedTreeNodeStream(tree);
    StatementBuilder builder = new StatementBuilder(stream);
    try {
        return builder.singleExpression().value;
    } catch (RecognitionException e) {
        throw new AssertionError(e); // RecognitionException is not thrown
    }
}
 
Example 21
Source Project: tracecompass   Source File: EnvironmentParser.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Map<String, String> parse(CommonTree environment, ICommonTreeParserParameter param) {

    ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
    List<CommonTree> children = environment.getChildren();
    for (CommonTree child : children) {
        String left;
        String right;
        left = child.getChild(0).getChild(0).getChild(0).getText();
        right = child.getChild(1).getChild(0).getChild(0).getText();
        builder.put(left, right);
    }
    return builder.build();
}
 
Example 22
Source Project: Elasticsearch   Source File: SqlParser.java    License: Apache License 2.0 5 votes vote down vote up
private static CommonTree parseIdentifier(String expression) {
    try {
        return (CommonTree) getParser(expression).ident().getTree();
    } catch (RecognitionException e) {
        throw new AssertionError(e);
    }
}
 
Example 23
Source Project: codebuff   Source File: CompilationState.java    License: BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void emit(CommonTree opAST, short opcode) {
    ensureCapacity(1);
    if ( opAST!=null ) {
        int i = opAST.getTokenStartIndex();
        int j = opAST.getTokenStopIndex();
        int p = ((CommonToken)tokens.get(i)).getStartIndex();
        int q = ((CommonToken)tokens.get(j)).getStopIndex();
        if ( !(p<0 || q<0) ) impl.sourceMap[ip] = new Interval(p, q);
    }
    impl.instrs[ip++] = (byte)opcode;
}
 
Example 24
static private CommonTree copy(CommonTree source)
{
    CommonTree newNode = new CommonTree(source);
    if (source.getChildCount() > 0)
    {
        for (Object current : source.getChildren())
        {
            CommonTree child = (CommonTree) current;
            CommonTree newChild = copy(child);
            newNode.addChild(newChild);
        }
    }
    return newNode;
}
 
Example 25
private static boolean isStarWithNoField(CommonTree testNode)
{
    if (testNode.getType() == FTSParser.TERM && testNode.getChildCount() == 1)
    {
        CommonTree child = (CommonTree) testNode.getChild(0);
        if (child.getType() == FTSParser.STAR)
        {
            return true;
        }
    }
    return false;
}
 
Example 26
Source Project: cuba   Source File: EntitiesFinder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object pre(Object t) {
    if (t instanceof PathNode) {
        filteredNodes.add((CommonTree) t);
    }
    if (t instanceof IdentificationVariableNode) {
        filteredNodes.add((CommonTree) t);
    }
    if (t instanceof JoinVariableNode) {
        filteredNodes.add((CommonTree) t);
    }
    return t;
}
 
Example 27
static private Float findFuzzy(Tree node)
{
    for (int i = 0, l = node.getChildCount(); i < l; i++)
    {
        CommonTree child = (CommonTree) node.getChild(i);
        if (child.getType() == FTSParser.FUZZY)
        {
            String fuzzyString = child.getChild(0).getText();
            float fuzzy = Float.parseFloat(fuzzyString);
            return Float.valueOf(fuzzy);
        }
    }
    return null;
}
 
Example 28
/**
 * @param testNode CommonTree
 * @param factory QueryModelFactory
 * @param functionEvaluationContext FunctionEvaluationContext
 * @param selectors Map<String, Selector>
 * @param columnMap HashMap<String, Column>
 * @return Constraint
 */
private Constraint buildTest(CommonTree testNode, QueryModelFactory factory,
        FunctionEvaluationContext functionEvaluationContext, Map<String, Selector> selectors,
        HashMap<String, Column> columnMap)
{
    if (testNode.getType() == CMISParser.DISJUNCTION)
    {
        return buildDisjunction(testNode, factory, functionEvaluationContext, selectors, columnMap);
    } else
    {
        return buildPredicate(testNode, factory, functionEvaluationContext, selectors, columnMap);
    }
}
 
Example 29
@SuppressWarnings("unused")
static public Constraint buildFTS(String ftsExpression, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext, Selector selector,
        Map<String, Column> columnMap, String defaultField)
{
    // TODO: Decode sql escape for '' should do in CMIS layer

    // parse templates to trees ...

    CMIS_FTSParser parser = null;
    try
    {
        CharStream cs = new ANTLRStringStream(ftsExpression);
        CMIS_FTSLexer lexer = new CMIS_FTSLexer(cs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new CMIS_FTSParser(tokens);
        CommonTree ftsNode = (CommonTree) parser.cmisFtsQuery().getTree();
        return buildFTSConnective(ftsNode, factory, functionEvaluationContext, selector, columnMap, defaultField);
    }
    catch (RecognitionException e)
    {
        if (parser != null)
        {
            String[] tokenNames = parser.getTokenNames();
            String hdr = parser.getErrorHeader(e);
            String msg = parser.getErrorMessage(e, tokenNames);
            throw new FTSQueryException(hdr + "\n" + msg, e);
        }
        return null;
    }

}
 
Example 30
static private Constraint buildPhrase(CommonTree testNode, QueryModelFactory factory,
        FunctionEvaluationContext functionEvaluationContext, Selector selector, Map<String, Column> columnMap)
{
    String functionName = FTSPhrase.NAME;
    Function function = factory.getFunction(functionName);
    Map<String, Argument> functionArguments = new LinkedHashMap<String, Argument>();
    LiteralArgument larg = factory.createLiteralArgument(FTSPhrase.ARG_PHRASE, DataTypeDefinition.TEXT, getText(testNode.getChild(0)));
    functionArguments.put(larg.getName(), larg);
    larg = factory.createLiteralArgument(FTSPhrase.ARG_TOKENISATION_MODE, DataTypeDefinition.ANY, AnalysisMode.DEFAULT);
    functionArguments.put(larg.getName(), larg);
    return factory.createFunctionalConstraint(function, functionArguments);
}