org.antlr.runtime.tree.CommonTreeNodeStream Java Examples

The following examples show how to use org.antlr.runtime.tree.CommonTreeNodeStream. 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: DSLTokenizedMappingFile.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private DSLMapWalker buildFileMappingWalker(final List<ParserError> errors, CharStream stream) throws RecognitionException{
    DSLMapLexer lexer = new DSLMapLexer(stream);
    CommonTokenStream tokens = new CommonTokenStream();
    tokens.setTokenSource(lexer);
    DSLMapParser parser = new DSLMapParser(tokens);
    DSLMapParser.mapping_file_return example = parser.mapping_file();
    CommonTree tree = (CommonTree) example.getTree();
    //        logger.info(tree.toStringTree());

    CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
    DSLMapWalker walker = new DSLMapWalker(nodes);

    errors.addAll( lexer.getErrors() );
    errors.addAll( parser.getErrors() );
    return walker;
}
 
Example #2
Source File: ParserTestingUtils.java    From spork with Apache License 2.0 6 votes vote down vote up
public static LogicalPlan generateLogicalPlan(String query)
throws RecognitionException, ParsingFailureException, IOException {
    Tree ast = validateAst( query );
    
    CommonTreeNodeStream input = new CommonTreeNodeStream( ast );
    LogicalPlanBuilder builder = new LogicalPlanBuilder( input );
    LogicalPlanGenerator walker = new LogicalPlanGenerator( input, builder );
    walker.query();
    
    if( 0 < walker.getNumberOfSyntaxErrors() ) 
        throw new ParsingFailureException( LogicalPlanGenerator.class );
    
    LogicalPlan plan = walker.getLogicalPlan();
    System.out.println( "Generated logical plan: " + plan.toString() );
    
    return plan;
}
 
Example #3
Source File: AbstractCobolTester.java    From legstar-core2 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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 #4
Source File: Cob2Xsd.java    From legstar-core2 with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Generates a model from an Abstract Syntax Tree.
 * 
 * @param ast the abstract syntax tree produced by parser
 * @return a list of root COBOL data items
 * @throws RecognizerException if tree cannot be walked
 */
public List < CobolDataItem > emitModel(final CommonTree ast)
        throws RecognizerException {
    List < CobolDataItem > cobolDataItems = new ArrayList < CobolDataItem >();
    if (_log.isDebugEnabled()) {
        _log.debug("4. Emitting Model from AST: {}",
                ((ast == null) ? "null" : ast.toStringTree()));
    }
    if (ast == null) {
        return cobolDataItems;
    }
    try {
        TreeNodeStream nodes = new CommonTreeNodeStream(ast);
        CobolStructureEmitter emitter = new CobolStructureEmitterImpl(
                nodes, getErrorHandler());
        emitter.cobdata(cobolDataItems);
        return cobolDataItems;
    } catch (RecognitionException e) {
        throw new RecognizerException(e);
    }
}
 
Example #5
Source File: MapleCfgVisitor_optAllInIf.java    From Gaalop with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Parses a snippet of maple code and returns a list of CFG nodes that implement the returned maple expressions.
 * 
 * @param graph The control flow graph the new nodes should be created in.
 * @param mapleCode The code returned by Maple.
 * @return A list of control flow nodes modeling the returned code.
 */
List<AssignmentNode> parseMapleCode(ControlFlowGraph graph, String mapleCode) {
	oldMinVal = new HashMap<String, String>();
	oldMaxVal = new HashMap<String, String>();

	/* fill the Maps with the min and maxvalues from the nodes */
	for (Variable v : graph.getInputVariables()) {
		if (v.getMinValue() != null)
			oldMinVal.put(v.getName(), v.getMinValue());
		if (v.getMaxValue() != null)
			oldMaxVal.put(v.getName(), v.getMaxValue());
	}

	MapleLexer lexer = new MapleLexer(new ANTLRStringStream(mapleCode));
	MapleParser parser = new MapleParser(new CommonTokenStream(lexer));
	try {
		MapleParser.program_return result = parser.program();
		MapleTransformer transformer = new MapleTransformer(new CommonTreeNodeStream(result.getTree()));
		return transformer.script(graph, oldMinVal, oldMaxVal);
	} catch (RecognitionException e) {
		throw new RuntimeException(e);
	}
}
 
Example #6
Source File: MapleCfgVisitor.java    From Gaalop with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Parses a snippet of maple code and returns a list of CFG nodes that implement the returned maple expressions.
 * 
 * @param graph The control flow graph the new nodes should be created in.
 * @param mapleCode The code returned by Maple.
 * @return A list of control flow nodes modeling the returned code.
 */
List<AssignmentNode> parseMapleCode(ControlFlowGraph graph, String mapleCode) {
	oldMinVal = new HashMap<String, String>();
	oldMaxVal = new HashMap<String, String>();

	/* fill the Maps with the min and maxvalues from the nodes */
	for (Variable v : graph.getInputVariables()) {
		if (v.getMinValue() != null)
			oldMinVal.put(v.getName(), v.getMinValue());
		if (v.getMaxValue() != null)
			oldMaxVal.put(v.getName(), v.getMaxValue());
	}

	MapleLexer lexer = new MapleLexer(new ANTLRStringStream(mapleCode));
	MapleParser parser = new MapleParser(new CommonTokenStream(lexer));
	try {
		MapleParser.program_return result = parser.program();
		MapleTransformer transformer = new MapleTransformer(new CommonTreeNodeStream(result.getTree()));
		return transformer.script(graph, oldMinVal, oldMaxVal);
	} catch (RecognitionException e) {
		throw new RuntimeException(e);
	}
}
 
Example #7
Source File: OutputModelController.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public void buildNormalRuleFunction(Rule r, RuleFunction function) {
	CodeGenerator gen = delegate.getGenerator();
	// TRIGGER factory functions for rule alts, elements
	GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream());
	GrammarAST blk = (GrammarAST)r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
	CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk);
	walker = new SourceGenTriggers(nodes, this);
	try {
		// walk AST of rule alts/elements
		function.code = DefaultOutputModelFactory.list(walker.block(null, null));
		function.hasLookaheadBlock = walker.hasLookaheadBlock;
	}
	catch (org.antlr.runtime.RecognitionException e){
		e.printStackTrace(System.err);
	}

	function.ctxType = gen.getTarget().getRuleFunctionContextStructName(function);

	function.postamble = rulePostamble(function, r);
}
 
Example #8
Source File: RuleFunction.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Given list of X and r refs in alt, compute how many of each there are */
protected FrequencySet<String> getElementFrequenciesForAlt(AltAST ast) {
	try {
		ElementFrequenciesVisitor visitor = new ElementFrequenciesVisitor(new CommonTreeNodeStream(new GrammarASTAdaptor(), ast));
		visitor.outerAlternative();
		if (visitor.frequencies.size() != 1) {
			factory.getGrammar().tool.errMgr.toolError(ErrorType.INTERNAL_ERROR);
			return new FrequencySet<String>();
		}

		return visitor.frequencies.peek();
	}
	catch (RecognitionException ex) {
		factory.getGrammar().tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, ex);
		return new FrequencySet<String>();
	}
}
 
Example #9
Source File: ParserATNFactory.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void _createATN(Collection<Rule> rules) {
	createRuleStartAndStopATNStates();

	GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
	for (Rule r : rules) {
		// find rule's block
		GrammarAST blk = (GrammarAST)r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
		CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk);
		ATNBuilder b = new ATNBuilder(nodes,this);
		try {
			setCurrentRuleName(r.name);
			Handle h = b.ruleBlock(null);
			rule(r.ast, r.name, h);
		}
		catch (RecognitionException re) {
			ErrorManager.fatalInternalError("bad grammar AST structure", re);
		}
	}
}
 
Example #10
Source File: GrammarAST.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public String toTokenString() {
	CharStream input = this.token.getInputStream();
	GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input);
	CommonTreeNodeStream nodes =
		new CommonTreeNodeStream(adaptor, this);
	StringBuilder buf = new StringBuilder();
	GrammarAST o = (GrammarAST)nodes.LT(1);
	int type = adaptor.getType(o);
	while ( type!=Token.EOF ) {
		buf.append(" ");
		buf.append(o.getText());
		nodes.consume();
		o = (GrammarAST)nodes.LT(1);
		type = adaptor.getType(o);
	}
	return buf.toString();
}
 
Example #11
Source File: LeftRecursiveRuleAnalyzer.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public LeftRecursiveRuleAnalyzer(GrammarAST ruleAST,
								 Tool tool, String ruleName, String language)
{
	super(new CommonTreeNodeStream(new GrammarASTAdaptor(ruleAST.token.getInputStream()), ruleAST));
	this.tool = tool;
	this.ruleName = ruleName;
	this.language = language;
	this.tokenStream = ruleAST.g.tokenStream;
	if (this.tokenStream == null) {
		throw new NullPointerException("grammar must have a token stream");
	}

	loadPrecRuleTemplates();
}
 
Example #12
Source File: CFAssignmentExpression.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
private CFExpression reparse( cfStringData _string, CFContext _context )
    throws cfmRunTimeException {
	// note, the fact that calling leftVal.getString() will not include the
	// pound signs is what's req'd
	// note addition of ';' at end of expression to make it parsable
	try {
		ANTLRNoCaseReaderStream input = new ANTLRNoCaseReaderStream(
		    new poundSignFilterStream(new StringReader(_string.getString())));

		CFMLLexer lexer = new CFMLLexer(input);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		CFMLParser parser = new CFMLParser(tokens);
		parser.scriptMode = false;
		CFMLParser.expression_return r = parser.expression();
		CommonTree tree = (CommonTree) r.getTree();

		CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
		nodes.setTokenStream(tokens);
		CFMLTree p2 = new CFMLTree(nodes);
		p2.scriptMode = false;
		return p2.expression();
	} catch (IOException ioe) { // shouldn't happen
		throw new CFException("Invalid expression : " + left.Decompile(0),
		    _context);
	} catch (RecognitionException pe) {
		throw new CFException("Invalid expression : " + left.Decompile(0),
		    _context);
	} catch (poundSignFilterStreamException e) {
		throw new CFException("Invalid expression : " + left.Decompile(0),
		    _context);
	}

}
 
Example #13
Source File: GrammarTransformPipeline.java    From codebuff with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void reduceBlocksToSets(GrammarAST root) {
	CommonTreeNodeStream nodes = new CommonTreeNodeStream(new GrammarASTAdaptor(), root);
	GrammarASTAdaptor adaptor = new GrammarASTAdaptor();
	BlockSetTransformer transformer = new BlockSetTransformer(nodes, g);
	transformer.setTreeAdaptor(adaptor);
	transformer.downup(root);
}
 
Example #14
Source File: HTMLParser.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * The only method that should be called to initiate the process
 * 
 * @param is
 *            The input stream from where to get the data
 * @param os
 *            The output stream to write the processed fragment/document to
 * @param htmlFilter
 *            An interface called during the processing of the document. Can
 *            be used to modify elements
 * @param convertIntoValidXML
 *            Converts the output into valid XML for XSL processing for
 *            example
 */
public static void process(Reader reader, Writer writer, IHTMLFilter htmlFilter, boolean convertIntoValidXML)
		throws HandlingException {
	try {
		// Open a char stream input for the document
		ANTLRStringStream input = new ANTLRReaderStream(reader);

		// Start lexing the input
		htmlLexerLexer lex = new htmlLexerLexer(input);

		// Tokenstream for the parser.
		CommonTokenStream tokens = new CommonTokenStream(lex);
		htmlParserParser parser = new htmlParserParser(tokens);
		htmlParserParser.document_return root = parser.document();

		// Set up the tree parser
		CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree) root.getTree());
		htmlTreeParser walker = new htmlTreeParser(nodes);

		// Initialize data structures
		topNode = new ThreadLocal<>();
		currentNode = new ThreadLocal<>();
		attrNode = new ThreadLocal<>();

		// Walk in the entire document using the tree parser.
		walker.document();

		// Get the top node
		TagNode top = (TagNode) topNode.get();

		// Write the clean document out.
		top.writeAll(writer, htmlFilter, convertIntoValidXML, false);
	} catch (IOException ioe) {
		throw new HandlingException("Could not parse document");
	} catch (RecognitionException re) {
		throw new HandlingException("Could not parse document");
	}
}
 
Example #15
Source File: QueryParserDriver.java    From spork with Apache License 2.0 5 votes vote down vote up
private static Tree validateAst(Tree ast) throws RecognitionException, ParserException {
    CommonTreeNodeStream nodes = new CommonTreeNodeStream( ast );
    AstValidator walker = new AstValidator( nodes );
    AstValidator.query_return newResult = walker.query();
    Tree newAst = (Tree)newResult.getTree();

    checkError( walker );

    return newAst;
}
 
Example #16
Source File: ParserTestingUtils.java    From spork with Apache License 2.0 5 votes vote down vote up
public static Tree validateAst(String query)
throws RecognitionException, ParsingFailureException, IOException {
    Tree ast = parse( query );
    CommonTreeNodeStream nodes = new CommonTreeNodeStream( ast );
    AstValidator walker = new AstValidator( nodes );
    AstValidator.query_return newResult = walker.query();
    Tree newAst = (Tree)newResult.getTree();
    TreePrinter.printTree( (CommonTree)newAst, 0 );
    
    if( 0 < walker.getNumberOfSyntaxErrors() ) 
        throw new ParsingFailureException( AstValidator.class );
    
    return newAst;
}
 
Example #17
Source File: TestCommonTreeNodeStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void testStackStretch() throws Exception {
	// make more than INITIAL_CALL_STACK_SIZE pushes
	Tree r0 = new CommonTree(new CommonToken(101));
	CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
	// go 1 over initial size
	for (int i=1; i<=CommonTreeNodeStream.INITIAL_CALL_STACK_SIZE+1; i++) {
		stream.push(i);
	}
	assertEquals(10, stream.pop());
	assertEquals(9, stream.pop());
}
 
Example #18
Source File: FluxCompiler.java    From metafacture-core with Apache License 2.0 4 votes vote down vote up
private static CommonTreeNodeStream compileAst(final InputStream flowDef) throws IOException, RecognitionException {
    final FluxParser parser = new FluxParser(new CommonTokenStream(new FluxLexer(new ANTLRInputStream(flowDef))));
    return new CommonTreeNodeStream(parser.flux().getTree());
}
 
Example #19
Source File: FluxCompiler.java    From metafacture-core with Apache License 2.0 4 votes vote down vote up
private static FluxProgramm compileFlow(final CommonTreeNodeStream treeNodes, final Map<String, String> vars)
        throws RecognitionException {
    final FlowBuilder flowBuilder = new FlowBuilder(treeNodes);
    flowBuilder.addVaribleAssignements(vars);
    return flowBuilder.flux();
}
 
Example #20
Source File: TestCommonTreeNodeStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testPushPopFromEOF() throws Exception {
	// ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
	// stream has 9 real + 8 nav nodes
	// Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
	Tree r0 = new CommonTree(new CommonToken(101));
	Tree r1 = new CommonTree(new CommonToken(102));
	r1.addChild(new CommonTree(new CommonToken(103)));
	r0.addChild(r1);
	Tree r2 = new CommonTree(new CommonToken(104));
	r2.addChild(new CommonTree(new CommonToken(105)));
	r0.addChild(r2);
	Tree r3 = new CommonTree(new CommonToken(106));
	r3.addChild(new CommonTree(new CommonToken(107)));
	r0.addChild(r3);
	r0.addChild(new CommonTree(new CommonToken(108)));
	r0.addChild(new CommonTree(new CommonToken(109)));

	CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);

	while ( stream.LA(1)!=Token.EOF ) {
		stream.consume();
	}
	int indexOf102 = 2;
	int indexOf104 = 6;
	assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());

	// CALL 102
	stream.push(indexOf102);
	assertEquals(102, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 102
	assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume DN
	assertEquals(103, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 103
	assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
	// RETURN (to empty stack)
	stream.pop();
	assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());

	// CALL 104
	stream.push(indexOf104);
	assertEquals(104, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 102
	assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume DN
	assertEquals(105, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 103
	assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
	// RETURN (to empty stack)
	stream.pop();
	assertEquals(Token.EOF, ((Tree)stream.LT(1)).getType());
}
 
Example #21
Source File: TestCommonTreeNodeStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testNestedPushPop() throws Exception {
	// ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
	// stream has 9 real + 8 nav nodes
	// Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
	Tree r0 = new CommonTree(new CommonToken(101));
	Tree r1 = new CommonTree(new CommonToken(102));
	r1.addChild(new CommonTree(new CommonToken(103)));
	r0.addChild(r1);
	Tree r2 = new CommonTree(new CommonToken(104));
	r2.addChild(new CommonTree(new CommonToken(105)));
	r0.addChild(r2);
	Tree r3 = new CommonTree(new CommonToken(106));
	r3.addChild(new CommonTree(new CommonToken(107)));
	r0.addChild(r3);
	r0.addChild(new CommonTree(new CommonToken(108)));
	r0.addChild(new CommonTree(new CommonToken(109)));

	CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);

	// Assume we want to hit node 107 and then "call 102", which
	// calls 104, then return

	int indexOf102 = 2;
	int indexOf107 = 12;
	for (int k=1; k<=indexOf107; k++) { // consume til 107 node
		stream.consume();
	}
	assertEquals(107, ((Tree)stream.LT(1)).getType());
	// CALL 102
	stream.push(indexOf102);
	assertEquals(102, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 102
	assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume DN
	assertEquals(103, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 103

	// CALL 104
	int indexOf104 = 6;
	stream.push(indexOf104);
	assertEquals(104, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 102
	assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume DN
	assertEquals(105, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 103
	assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
	// RETURN (to UP node in 102 subtree)
	stream.pop();

	assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
	// RETURN (to empty stack)
	stream.pop();
	assertEquals(107, ((Tree)stream.LT(1)).getType());
}
 
Example #22
Source File: TestCommonTreeNodeStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void testPushPop() throws Exception {
	// ^(101 ^(102 103) ^(104 105) ^(106 107) 108 109)
	// stream has 9 real + 8 nav nodes
	// Sequence of types: 101 DN 102 DN 103 UP 104 DN 105 UP 106 DN 107 UP 108 109 UP
	Tree r0 = new CommonTree(new CommonToken(101));
	Tree r1 = new CommonTree(new CommonToken(102));
	r1.addChild(new CommonTree(new CommonToken(103)));
	r0.addChild(r1);
	Tree r2 = new CommonTree(new CommonToken(104));
	r2.addChild(new CommonTree(new CommonToken(105)));
	r0.addChild(r2);
	Tree r3 = new CommonTree(new CommonToken(106));
	r3.addChild(new CommonTree(new CommonToken(107)));
	r0.addChild(r3);
	r0.addChild(new CommonTree(new CommonToken(108)));
	r0.addChild(new CommonTree(new CommonToken(109)));

	CommonTreeNodeStream stream = new CommonTreeNodeStream(r0);
	String expecting = " 101 2 102 2 103 3 104 2 105 3 106 2 107 3 108 109 3";
	String found = stream.toString();
	assertEquals(expecting, found);

	// Assume we want to hit node 107 and then "call 102" then return

	int indexOf102 = 2;
	int indexOf107 = 12;
	for (int k=1; k<=indexOf107; k++) { // consume til 107 node
		stream.consume();
	}
	// CALL 102
	assertEquals(107, ((Tree)stream.LT(1)).getType());
	stream.push(indexOf102);
	assertEquals(102, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 102
	assertEquals(Token.DOWN, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume DN
	assertEquals(103, ((Tree)stream.LT(1)).getType());
	stream.consume(); // consume 103
	assertEquals(Token.UP, ((Tree)stream.LT(1)).getType());
	// RETURN
	stream.pop();
	assertEquals(107, ((Tree)stream.LT(1)).getType());
}
 
Example #23
Source File: DryRunGruntParser.java    From spork with Apache License 2.0 4 votes vote down vote up
@Override
protected void processPig(String cmd) throws IOException {
    
    int start = getLineNumber();
   
    StringBuilder blder = new StringBuilder();
    for (int i = 1; i < start; i++) {
        blder.append("\n");            
    }
    
    if (cmd.charAt(cmd.length() - 1) != ';') {
        cmd += ";";
    }
    
    blder.append(cmd);
    cmd = blder.toString();
    
    CommonTokenStream tokenStream = QueryParserDriver.tokenize(cmd, source);
    Tree ast = null;

    try {
        ast = QueryParserDriver.parse( tokenStream );
    } catch(RuntimeException ex) {
        throw new ParserException( ex.getMessage() );
    }    

    if (!hasMacro) {
        List<CommonTree> importNodes = new ArrayList<CommonTree>();
        List<CommonTree> macroNodes = new ArrayList<CommonTree>();
        List<CommonTree> inlineNodes = new ArrayList<CommonTree>();
        
        QueryParserDriver.traverseImport(ast, importNodes);
        QueryParserDriver.traverse(ast, macroNodes, inlineNodes);
        
        if (!importNodes.isEmpty() || !macroNodes.isEmpty()
                        || !inlineNodes.isEmpty()) {
            hasMacro = true;
        }
    }

    if (parserTree == null) {
        parserTree = ast;
    } else {
        int n = ast.getChildCount();
        for (int i = 0; i < n; i++) {
            parserTree.addChild(ast.getChild(i));
        }
    }
 
    CommonTree dup = (CommonTree)parserTree.dupNode();
    dup.addChildren(((CommonTree)parserTree).getChildren());

    QueryParserDriver driver = new QueryParserDriver(context, "0",
            new HashMap<String, String>());
    Tree newAst = driver.expandMacro(dup);
    
    CommonTreeNodeStream nodes = new CommonTreeNodeStream( newAst );
    AstPrinter walker = new AstPrinter( nodes );
    try {
        walker.query();
    } catch (RecognitionException e) {
        throw new ParserException("Failed to print AST for command " + cmd,
                e);
    }

    String result = walker.getResult().trim();
   
    if (!result.isEmpty()) {
        String[] lines = result.split("\n");
        for (int i = toSkip; i < lines.length; i++) {
            sb.append(lines[i]).append("\n");
            toSkip++;
        }   
    }
}
 
Example #24
Source File: SmaliMod.java    From ratel with Apache License 2.0 4 votes vote down vote up
public static boolean assembleSmaliFile(File smaliFile,DexBuilder dexBuilder, boolean verboseErrors,
                                        boolean printTokens) throws IOException, RecognitionException {

    CommonTokenStream tokens;
    LexerErrorInterface lexer;

    InputStream is = new FileInputStream(smaliFile);
    InputStreamReader reader = new InputStreamReader(is, "UTF-8");

    lexer = new smaliFlexLexer(reader);
    ((smaliFlexLexer)lexer).setSourceFile(smaliFile);
    tokens = new CommonTokenStream((TokenSource) lexer);

    if (printTokens) {
        tokens.getTokens();

        for (int i=0; i<tokens.size(); i++) {
            Token token = tokens.get(i);
            if (token.getChannel() == smaliParser.HIDDEN) {
                continue;
            }

            System.out.println(smaliParser.tokenNames[token.getType()] + ": " + token.getText());
        }
    }

    smaliParser parser = new smaliParser(tokens);
    parser.setVerboseErrors(verboseErrors);

    smaliParser.smali_file_return result = parser.smali_file();

    if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfSyntaxErrors() > 0) {
        is.close();
        reader.close();
        return false;
    }

    CommonTree t = (CommonTree) result.getTree();

    CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
    treeStream.setTokenStream(tokens);

    smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);

    dexGen.setVerboseErrors(verboseErrors);
    dexGen.setDexBuilder(dexBuilder);
    dexGen.smali_file();

    is.close();
    reader.close();

    return dexGen.getNumberOfSyntaxErrors() == 0;
}