类org.antlr.runtime.tree.CommonTree源码实例Demo

下面列出了怎么用org.antlr.runtime.tree.CommonTree的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: cuba   文件: IdVarSelector.java
@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;
}
 
源代码2 项目: alfresco-data-model   文件: FTSQueryParser.java
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;
    }
}
 
源代码3 项目: alfresco-data-model   文件: CMISQueryParser.java
/**
 * @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);
    }
}
 
源代码4 项目: cuba   文件: JoinVariableNode.java
@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;
}
 
源代码5 项目: alfresco-remote-api   文件: WhereTests.java
/**
 * 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));
		}
		
	});
}
 
/**
 * 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();
}
 
源代码7 项目: alfresco-remote-api   文件: WhereTests.java
/**
 * 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));
		}
		
	});
}
 
源代码8 项目: alfresco-remote-api   文件: WhereTests.java
/**
 * 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]));				
			}
		}
		
	});
}
 
源代码9 项目: codebuff   文件: CompilationState.java
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);
        }
    }
}
 
源代码10 项目: tracecompass   文件: EventIDParser.java
@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);
}
 
源代码11 项目: codebuff   文件: CompilationState.java
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);
        }
    }
}
 
源代码12 项目: codebuff   文件: CompilationState.java
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);
        }
    }
}
 
源代码13 项目: codebuff   文件: CompilationState.java
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);
        }
    }
}
 
源代码14 项目: legstar-core2   文件: AbstractCobolTester.java
/**
 * 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);
    }
}
 
源代码15 项目: codebuff   文件: CompilationState.java
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);
        }
    }
}
 
源代码16 项目: tracecompass   文件: CtfParserTest.java
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));
            }
        }
    }
}
 
源代码17 项目: codebuff   文件: CompilationState.java
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;
}
 
源代码18 项目: netbeans   文件: VmOptionsGrammarTest.java
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;
}
 
源代码19 项目: Elasticsearch   文件: SqlParser.java
@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
    }
}
 
源代码20 项目: Elasticsearch   文件: SqlParser.java
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
    }
}
 
源代码21 项目: tracecompass   文件: EnvironmentParser.java
@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();
}
 
源代码22 项目: Elasticsearch   文件: SqlParser.java
private static CommonTree parseIdentifier(String expression) {
    try {
        return (CommonTree) getParser(expression).ident().getTree();
    } catch (RecognitionException e) {
        throw new AssertionError(e);
    }
}
 
源代码23 项目: codebuff   文件: CompilationState.java
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;
}
 
源代码24 项目: alfresco-data-model   文件: FTSQueryParser.java
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;
}
 
源代码25 项目: alfresco-data-model   文件: FTSQueryParser.java
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;
}
 
源代码26 项目: cuba   文件: EntitiesFinder.java
@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;
}
 
源代码27 项目: alfresco-data-model   文件: FTSQueryParser.java
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;
}
 
源代码28 项目: alfresco-data-model   文件: CMISQueryParser.java
/**
 * @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);
    }
}
 
源代码29 项目: alfresco-data-model   文件: CMISFTSQueryParser.java
@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;
    }

}
 
源代码30 项目: alfresco-data-model   文件: CMISFTSQueryParser.java
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);
}