org.antlr.v4.runtime.tree.ParseTree Java Examples

The following examples show how to use org.antlr.v4.runtime.tree.ParseTree. 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: AntlrUtils.java    From sonar-tsql-plugin with GNU General Public License v3.0 7 votes vote down vote up
public static void print(final ParseTree node, final int level, CommonTokenStream stream) {
	final Interval sourceInterval = node.getSourceInterval();

	final Token firstToken = stream.get(sourceInterval.a);

	int line = firstToken.getLine();
	int charStart = firstToken.getCharPositionInLine();

	int endLine = line;
	int endChar = charStart + firstToken.getText().length();

	String data = "@(" + line + ":" + charStart + "," + endLine + ":" + endChar + ") with text: "
			+ firstToken.getText();
	final int tmp = level + 1;
	final StringBuilder sb = new StringBuilder();
	sb.append(StringUtils.repeat("\t", level));
	sb.append(node.getClass().getSimpleName() + ": " + data + " :" + node.getText());
	System.out.println(sb.toString());
	final int n = node.getChildCount();
	for (int i = 0; i < n; i++) {

		final ParseTree c = node.getChild(i);
		print(c, tmp, stream);

	}
}
 
Example #2
Source File: StepCleanVersion.java    From yauaa with Apache License 2.0 6 votes vote down vote up
@Override
public WalkResult walk(ParseTree tree, String value) {
    String actualValue = getActualValue(tree, value);
    if (actualValue != null) {
        // Sanitize the provided value
        actualValue = replaceString(actualValue, "_", ".");
        actualValue = replaceString(actualValue, "/", " ");
        actualValue = replaceString(actualValue, ", ", ".");
    }

    return walkNextStep(tree, actualValue);
}
 
Example #3
Source File: FishParserFactory.java    From antsdb with GNU Lesser General Public License v3.0 6 votes vote down vote up
private Object run(GeneratorContext gctx, VdmContext ctx, Parameters params, StmtContext rule) {
    ParseTree stmt = rule.getChild(0);
    Generator<ParseTree> generator = InstructionGenerator.getGenerator(stmt);
    Instruction code = generator.gen(gctx, stmt);
    if (code == null) {
        return null;
    }
    try {
        Object result = code.run(ctx, params, 0);
        return result;
    }
    finally {
        if (generator instanceof DdlGenerator<?>) {
            // mysql commits after every ddl statement
            ctx.getSession().commit();
        }
    }
}
 
Example #4
Source File: Trans.java    From cs652 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args) {
	String code =
		"int x;\n" +
		"A b;\n";
	ANTLRInputStream input = new ANTLRInputStream(code);
	LangLexer lexer = new LangLexer(input);
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	LangParser parser = new LangParser(tokens);
	ParseTree tree = parser.file(); // start up

	System.out.println(tree.toStringTree(parser));

	ParseTreeWalker walker = new ParseTreeWalker();
	Gen listener = new Gen();
	walker.walk(listener, tree);

	ST output = listener.file.getTemplate();
	System.out.println(output.render());
}
 
Example #5
Source File: Test.java    From cs652 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args) {
		String prop = "id=\"parrt\"\n";
		ANTLRInputStream input = new ANTLRInputStream(prop);
		PropertyFileLexer lexer = new PropertyFileLexer(input);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		PropertyFileParser parser = new PropertyFileParser(tokens);
		ParseTree tree = parser.file();
		System.out.println(tree.toStringTree(parser));
//		Trees.inspect(tree, parser);

		ParseTreeWalker walker = new ParseTreeWalker();
		PropertyFileLoader loader = new PropertyFileLoader();
		walker.walk(loader, tree);

		System.out.println(loader.props);
	}
 
Example #6
Source File: Tool.java    From bookish with MIT License 6 votes vote down vote up
public String translateString(Translator trans, String markdown, String startRule) throws Exception {
	CharStream input = CharStreams.fromString(markdown);
	BookishLexer lexer = new BookishLexer(input);
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	BookishParser parser = new BookishParser(tokens,null, 0);
	parser.removeErrorListeners();
	parser.addErrorListener(new ConsoleErrorListener() {
		@Override
		public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
			msg = "Parsing author string: "+msg;
			super.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
		}
	});
	Method startMethod = BookishParser.class.getMethod(startRule, (Class[])null);
	ParseTree doctree = (ParseTree)startMethod.invoke(parser, (Object[])null);

	OutputModelObject omo = trans.visit(doctree); // get single chapter

	ModelConverter converter = new ModelConverter(trans.templates);
	ST outputST = converter.walk(omo);
	return outputST.render();
}
 
Example #7
Source File: ASTGenerator.java    From ASTGenerator with MIT License 6 votes vote down vote up
private static void generateAST(RuleContext ctx, boolean verbose, int indentation) {
       boolean toBeIgnored = !verbose && ctx.getChildCount() == 1 && ctx.getChild(0) instanceof ParserRuleContext;

       if (!toBeIgnored) {
           String ruleName = Java8Parser.ruleNames[ctx.getRuleIndex()];
    LineNum.add(Integer.toString(indentation));
           Type.add(ruleName);
           Content.add(ctx.getText());
}
       for (int i = 0; i < ctx.getChildCount(); i++) {
           ParseTree element = ctx.getChild(i);
           if (element instanceof RuleContext) {
               generateAST((RuleContext) element, verbose, indentation + (toBeIgnored ? 0 : 1));
           }
       }
   }
 
Example #8
Source File: DefaultLinesProviderTest.java    From sonar-tsql-plugin with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void test() {

	final CharStream charStream = CharStreams.fromString("\r\nSELECT\r\n 1");

	final TSqlLexer lexer = new TSqlLexer(charStream);

	final CommonTokenStream stream = new CommonTokenStream(lexer);

	stream.fill();
	TSqlParser parser = new TSqlParser(stream);
	ParseTree child = parser.tsql_file().getChild(0);
	DefaultLinesProvider lines = new DefaultLinesProvider(stream);
	int line = lines.getLine(new ParsedNode(child));
	Assert.assertEquals(2, line);

}
 
Example #9
Source File: InstanceIdentifierParser.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
private Collection<YangExpr> parsePredicate(final PredicateContext expr) {
    final ParseTree first = expr.getChild(0);
    if (first instanceof LeafListPredicateContext) {
        return ImmutableSet.of(YangBinaryOperator.EQUALS.exprWith(YangLocationPath.self(),
            parseEqStringValue(getChild(((LeafListPredicateContext) first)
                .getChild(LeafListPredicateExprContext.class, 0), EqQuotedStringContext.class, 1))));
    } else if (first instanceof PosContext) {
        return ImmutableSet.of(YangBinaryOperator.EQUALS.exprWith(FunctionSupport.POSITION,
            mathSupport.createNumber(((PosContext) first).getToken(instanceIdentifierParser.PositiveIntegerValue, 0)
                .getText())));
    }

    final int length = expr.getChildCount();
    final List<YangExpr> ret = new ArrayList<>(length);
    for (int i = 0; i < length; ++i) {
        final KeyPredicateExprContext pred = getChild(expr, KeyPredicateContext.class, i)
                .getChild(KeyPredicateExprContext.class, 0);
        ret.add(YangBinaryOperator.EQUALS.exprWith(
            createChildExpr(getChild(pred, NodeIdentifierContext.class, 0)),
            parseEqStringValue(getChild(pred, EqQuotedStringContext.class, 1))));

    }

    return ret;
}
 
Example #10
Source File: ANTLRv4GrammarParser.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
protected ParseTree parse(Parser parser, IElementType root) {
	int startRule;
	if (root instanceof IFileElementType) {
		startRule = ANTLRv4Parser.RULE_grammarSpec;
	}
	else if (root == ANTLRv4TokenTypes.TOKEN_ELEMENT_TYPES.get(ANTLRv4Lexer.TOKEN_REF)
		|| root == ANTLRv4TokenTypes.TOKEN_ELEMENT_TYPES.get(ANTLRv4Lexer.RULE_REF)) {
		startRule = ANTLRv4Parser.RULE_atom;
	}
	else {
		startRule = Token.INVALID_TYPE;
	}

	switch (startRule) {
	case ANTLRv4Parser.RULE_grammarSpec:
		return ((ANTLRv4Parser) parser).grammarSpec();

	case ANTLRv4Parser.RULE_atom:
		return ((ANTLRv4Parser) parser).atom();

	default:
		String ruleName = ANTLRv4Parser.ruleNames[startRule];
		throw new UnsupportedOperationException(String.format("cannot start parsing using root element %s", root));
	}
}
 
Example #11
Source File: BdsCompiler.java    From BigDataScript with Apache License 2.0 6 votes vote down vote up
/**
 * BdsCompiler program
 */
public ProgramUnit compile() {
	if (debug) log("Loading file: '" + programFileName + "'");

	// Convert to AST
	ParseTree tree = parseProgram();
	if (tree == null) return null;

	// Convert to BdsNodes
	programUnit = createModel(tree);
	if (programUnit == null) return null;

	CompilerMessages.reset();

	// Add local symbols
	if (addSymbols()) return null;

	// Type-checking
	if (typeChecking()) return null;

	// Cleanup: Free some memory by reseting structure we won't use any more
	TypeCheckedNodes.get().reset();

	// OK
	return programUnit;
}
 
Example #12
Source File: SerpentCompileTest.java    From ethereumj with MIT License 6 votes vote down vote up
@Test    // expression test 3
  public void test8() {

      String code = "a = 2 | 2 xor 2 * 2";
      String expected = "2 2 MUL 2 XOR 2 OR 0 MSTORE";

SerpentParser parser = ParserUtils.getParser(SerpentLexer.class,
		SerpentParser.class, code);
      ParseTree tree = parser.parse();

      String result = new SerpentToAssemblyCompiler().visit(tree);
      result = result.replaceAll("\\s+", " ");
      result = result.trim();

      assertEquals(expected, result);
  }
 
Example #13
Source File: AntlrParserDriver.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ParseTree parseTokenStream(TokenSubStream tokens)
    throws ParserException {
  ParseTree returnTree = parseTokenStreamImpl(tokens);
  if (returnTree == null) {
    throw new ParserException("");
  }
  return returnTree;
}
 
Example #14
Source File: AssignmentTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testPrivateInName() {
	String input = "struct acpi_battery *battery = m->private;";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains("simple_decl"));
}
 
Example #15
Source File: ExpressionComp.java    From BigDataScript with Apache License 2.0 5 votes vote down vote up
@Override
protected void parse(ParseTree tree) {
	String op = tree.getChild(1).getText();
	switch (op) {
	case "<":
		expr = new ExpressionLt(this, tree);
		break;

	case "<=":
		expr = new ExpressionLe(this, tree);
		break;

	case "==":
		expr = new ExpressionEq(this, tree);
		break;

	case "!=":
		expr = new ExpressionNe(this, tree);
		break;

	case ">=":
		expr = new ExpressionGe(this, tree);
		break;

	case ">":
		expr = new ExpressionGt(this, tree);
		break;

	default:
		throw new RuntimeException("Unsuported operator '" + op + "'. This should never happen!");
	}
}
 
Example #16
Source File: ProgramParser.java    From vespa with Apache License 2.0 5 votes vote down vote up
private List<String> readName(Namespaced_nameContext node) {
    List<String> path = Lists.newArrayList();
    for (ParseTree elt:node.children) {
        if (!(getParseTreeIndex(elt) == yqlplusParser.DOT)) {
            path.add(elt.getText());
        }
     }
    return path;
}
 
Example #17
Source File: TestFaLaLa.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	ANTLRInputStream input = new ANTLRFileStream("test.falala");
	FaLaLaLexer lexer = new FaLaLaLexer(input);
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	FaLaLaParser parser = new FaLaLaParser(tokens);
	ParseTree tree = parser.prog();
	System.out.println(tree.toStringTree(parser));

	ParseTreeWalker walker = new ParseTreeWalker();
	DefSymbols def = new DefSymbols();
	walker.walk(def, tree);
}
 
Example #18
Source File: AstBuilder.java    From kalang with MIT License 5 votes vote down vote up
@Override
public Object visitInterpolationExpr(KalangParser.InterpolationExprContext ctx) {
    List<ParseTree> children = ctx.children;
    ExprNode[] exprs = new ExprNode[children.size()];
    for(int i=0;i<exprs.length;i++){
        ParseTree c = children.get(i);
        if(c instanceof TerminalNode){
            Token token = ((TerminalNode) c).getSymbol();
            int t = token.getType();
            String rawText = c.getText();
            String text;
            switch(t){
                case KalangLexer.InterpolationPreffixString:
                    text = rawText.substring(1,rawText.length()-2);
                    break;
                case KalangLexer.INTERPOLATION_STRING:
                    text = rawText;
                    break;
                case KalangLexer.RBRACE:
                case KalangLexer.INTERPOLATION_END:
                case KalangLexer.INTERPOLATION_INTERUPT:
                    //TODO optimize empty string
                    text = "";
                    break;
                default :
                    throw Exceptions.unexpectedValue(t);
            }
            exprs[i]=new ConstExpr(StringLiteralUtil.parse(text));
        }else if(c instanceof ExpressionContext){
            exprs[i] = this.visitExpression((ExpressionContext) c);
        }else{
            throw Exceptions.unexpectedValue(c);
        }
    }
    return this.concatExpressionsToStringExpr(exprs);
}
 
Example #19
Source File: StepNext.java    From yauaa with Apache License 2.0 5 votes vote down vote up
@Override
public WalkResult walk(ParseTree tree, String value) {
    ParseTree nextTree = next(tree);
    if (nextTree == null) {
        return null;
    }

    return walkNextStep(nextTree, null);
}
 
Example #20
Source File: AntlrParserDriver.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
public ParseTree parseString(String input) throws ParserException {
  CharStream inputStream = CharStreams.fromString(input);
  Lexer lex = createLexer(inputStream);
  TokenSubStream tokens = new TokenSubStream(lex);
  ParseTree tree = parseTokenStream(tokens);
  return tree;
}
 
Example #21
Source File: FunctionCallTests.java    From fuzzyc2cpg with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testCallViaPtr()
{
	String input = "ptr->foo(x);";
	AntlrParserDriver functionParser = createFunctionDriver();
	ParseTree tree = functionParser.parseString(input);
	String output = tree.toStringTree(functionParser.getAntlrParser());
	assertTrue(output.contains("function_argument_list"));
}
 
Example #22
Source File: Cache.java    From swift-js-transpiler with MIT License 5 votes vote down vote up
public CacheBlockAndObject find(String varName, ParseTree node) {
    varName = varName.trim();

    do {
        Map<String, Object> blockTypeCache = cache.get(node);
        if(blockTypeCache == null) continue;
        if(blockTypeCache.containsKey(varName)) return new CacheBlockAndObject(node, blockTypeCache.get(varName));
        if(node instanceof SwiftParser.Top_levelContext) break;
    }
    while((node = findNearestAncestorBlock(node.getParent())) != null);

    return null;
}
 
Example #23
Source File: TypeMap.java    From BigDataScript with Apache License 2.0 5 votes vote down vote up
@Override
protected void parse(ParseTree tree) {
	// Value type
	primitiveType = PrimitiveType.MAP;
	valueType = (Type) factory(tree, 0);

	// Key type
	if (tree.getChildCount() > 3) {
		keyType = (Type) factory(tree, 2);
	} else {
		// Default key type: string
		keyType = Types.STRING;
	}
}
 
Example #24
Source File: CQNAntlrListener.java    From cqengine with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void exitVerboseBetweenQuery(CQNGrammarParser.VerboseBetweenQueryContext ctx) {
    Attribute<O, Comparable> attribute = queryParser.getAttribute(ctx.attributeName(), Comparable.class);
    List<? extends ParseTree> queryParameters = ctx.queryParameter(), booleanParameters = ctx.BooleanLiteral();
    Comparable lowerValue = queryParser.parseValue(attribute, queryParameters.get(0));
    boolean lowerInclusive = queryParser.parseValue(Boolean.class, booleanParameters.get(0));
    Comparable upperValue = queryParser.parseValue(attribute, queryParameters.get(1));
    boolean upperInclusive = queryParser.parseValue(Boolean.class, booleanParameters.get(1));
    addParsedQuery(ctx, QueryFactory.between(attribute, lowerValue, lowerInclusive, upperValue, upperInclusive));
}
 
Example #25
Source File: Translator.java    From bookish with MIT License 5 votes vote down vote up
@Override
public OutputModelObject visitParagraph_content(BookishParser.Paragraph_contentContext ctx) {
	List<OutputModelObject> elements = new ArrayList<>();
	for (ParseTree el : ctx.children) {
		OutputModelObject c = visit(el);
		if ( c!=null ) {
			elements.add(c);
		}
	}
	// find all REFs within paragraph
	Collection<ParseTree> refNodes =
		XPath.findAll(ctx, "//REF", new BookishParser(null));
	List<EntityDef> entitiesRefd = new ArrayList<>();
	for (ParseTree t : refNodes) {
		String label = stripQuotes(t.getText());
		EntityDef def = document.getEntity(label);
		if ( def!=null ) {
			if ( !book.entitiesRendered.contains(def) &&
				 !document.entitiesRendered.contains(def) )
			{
				entitiesRefd.add(def); // Nobody has shown it yet
				if ( document.entitiesRendered.contains(def) ) {
					document.entitiesRendered.add(def);
				}
				if ( book.entitiesRendered.contains(def) ) {
					book.entitiesRendered.add(def);
				}
			}
		}
		else {
			System.err.printf("line %d: Unknown label '%s'\n", ctx.start.getLine(), label);
		}
	}
	return new Paragraph(elements, entitiesRefd);
}
 
Example #26
Source File: Translator.java    From bookish with MIT License 5 votes vote down vote up
@Override
public OutputModelObject visitSection_content(BookishParser.Section_contentContext ctx) {
	if ( ctx.children==null ) {
		return null;
	}

	List<OutputModelObject> elements = new ArrayList<>();
	for (ParseTree el : ctx.children) {
		OutputModelObject m = visit(el);
		elements.add(m);
	}
	return new Join(elements);
}
 
Example #27
Source File: Translator.java    From bookish with MIT License 5 votes vote down vote up
@Override
public OutputModelObject visitAbstract_(BookishParser.Abstract_Context ctx) {
	List<OutputModelObject> paras = new ArrayList<>();
	paras.add(visit(ctx.paragraph_optional_blank_line()));
	for (ParseTree p : ctx.paragraph()) {
		Paragraph para = (Paragraph) visit(p);
		paras.add(para);
	}
	return new Abstract(paras);
}
 
Example #28
Source File: Gen.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public OutputModelObject visitFile(LangParser.FileContext ctx) {
	OutputFile file = new OutputFile();
	for (ParseTree child : ctx.children) {
		OutputModelObject m = visit(child);
		file.add(m);
	}
	return file;
}
 
Example #29
Source File: TestParsing.java    From yql-plus with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "parsetrees")
public void testParseTree(Unit unit, String input, String expectedOutput) throws IOException, RecognitionException {
    yqlplusParser parser = prepareParser(input);
    ParseTree output;
    switch (unit) {
        case PROGRAM:
            output = parser.program();
            break;
        case EXPRESSION:
            output = parser.expression(true);
            break;
        default:
            throw new IllegalArgumentException();
    }
}
 
Example #30
Source File: AntlrXPathParser.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
private YangLocationPath parseLocationPath(final LocationPathContext expr) {
    verifyChildCount(expr, 1);
    final ParseTree first = expr.getChild(0);
    if (first instanceof RelativeLocationPathContext) {
        return YangLocationPath.relative(parseLocationPathSteps((RelativeLocationPathContext) first));
    }

    final AbsoluteLocationPathNorootContext abs = verifyTree(AbsoluteLocationPathNorootContext.class, first);
    verifyChildCount(abs, 2);

    final Deque<Step> steps = parseLocationPathSteps(getChild(abs, RelativeLocationPathContext.class, 1));
    parseStepShorthand(abs.getChild(0)).ifPresent(steps::addFirst);

    return YangLocationPath.absolute(steps);
}