org.antlr.runtime.ANTLRStringStream Java Examples

The following examples show how to use org.antlr.runtime.ANTLRStringStream. 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: STGroupString.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void load() {
    if ( alreadyLoaded ) return;
    alreadyLoaded = true;
    GroupParser parser;
    try {
        ANTLRStringStream fs = new ANTLRStringStream(text);
        fs.name = sourceName;
        GroupLexer lexer = new GroupLexer(fs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new GroupParser(tokens);
        // no prefix since this group file is the entire group, nothing lives
        // beneath it.
        parser.group(this, "/");
    }
    catch (Exception e) {
        errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, "<string>");
    }
}
 
Example #2
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void test2ReplaceMiddleIndex1InsertBefore() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
       tokens.insertBefore(0, "_");
       tokens.replace(1, "x");
	tokens.replace(1, "y");
	String result = tokens.toString();
	String expecting = "_ayc";
	assertEquals(expecting, result);
}
 
Example #3
Source File: TreeTest.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
private LogicalExpression parseExpression(String expr) throws RecognitionException, IOException{

    ExprLexer lexer = new ExprLexer(new ANTLRStringStream(expr));
    CommonTokenStream tokens = new CommonTokenStream(lexer);

//    tokens.fill();
//    for(Token t : (List<Token>) tokens.getTokens()){
//      System.out.println(t + "" + t.getType());
//    }
//    tokens.rewind();

    ExprParser parser = new ExprParser(tokens);
    parse_return ret = parser.parse();

    return ret.e;

  }
 
Example #4
Source File: ParseDependencies.java    From BART with MIT License 6 votes vote down vote up
public void generateDependencies(String text, EGTask task) throws Exception {
    try {
        this.task = task;
        DependenciesLexer lex = new DependenciesLexer(new ANTLRStringStream(text));
        CommonTokenStream tokens = new CommonTokenStream(lex);
        DependenciesParser g = new DependenciesParser(tokens);
        try {
            g.setGenerator(this);
            g.prog();
        } catch (RecognitionException ex) {
            logger.error("Unable to load mapping task: " + ex.getMessage());
            throw new ParserException(ex);
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getLocalizedMessage());
        throw new ParserException(e);
    }
}
 
Example #5
Source File: CqlParserTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveErrorListener() throws Exception
{
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();

    CharStream stream = new ANTLRStringStream("SELECT * FORM test;");
    CqlLexer lexer = new CqlLexer(stream);

    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(firstCounter);
    parser.addErrorListener(secondCounter);
    parser.removeErrorListener(secondCounter);

    parser.query();

    assertTrue(firstCounter.count > 0);
    assertEquals(0, secondCounter.count);
}
 
Example #6
Source File: STGroupString.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void load() {
    if ( alreadyLoaded ) return;
    alreadyLoaded = true;
    GroupParser parser;
    try {
        ANTLRStringStream fs = new ANTLRStringStream(text);
        fs.name = sourceName;
        GroupLexer lexer = new GroupLexer(fs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new GroupParser(tokens);
        // no prefix since this group file is the entire group, nothing lives
        // beneath it.
        parser.group(this, "/");
    }
    catch (Exception e) {
        errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, "<string>");
    }
}
 
Example #7
Source File: CqlParserTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddErrorListener() throws Exception
{
    SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
    SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();

    CharStream stream = new ANTLRStringStream("SELECT * FORM test;");
    CqlLexer lexer = new CqlLexer(stream);

    TokenStream tokenStream = new CommonTokenStream(lexer);
    CqlParser parser = new CqlParser(tokenStream);
    parser.addErrorListener(firstCounter);
    parser.addErrorListener(secondCounter);

    parser.query();

    // ANTLR 3.5 reports 2 errors in the sentence above (missing FROM and missing EOF).
    assertTrue(firstCounter.count > 0);
    assertTrue(secondCounter.count > 0);
}
 
Example #8
Source File: STGroupString.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void load() {
    if ( alreadyLoaded ) return;
    alreadyLoaded = true;
    GroupParser parser;
    try {
        ANTLRStringStream fs = new ANTLRStringStream(text);
        fs.name = sourceName;
        GroupLexer lexer = new GroupLexer(fs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new GroupParser(tokens);
        // no prefix since this group file is the entire group, nothing lives
        // beneath it.
        parser.group(this, "/");
    }
    catch (Exception e) {
        errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, "<string>");
    }
}
 
Example #9
Source File: STGroupString.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void load() {
    if ( alreadyLoaded ) return;
    alreadyLoaded = true;
    GroupParser parser;
    try {
        ANTLRStringStream fs = new ANTLRStringStream(text);
        fs.name = sourceName;
        GroupLexer lexer = new GroupLexer(fs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new GroupParser(tokens);
        // no prefix since this group file is the entire group, nothing lives
        // beneath it.
        parser.group(this, "/");
    }
    catch (Exception e) {
        errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, "<string>");
    }
}
 
Example #10
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testInsertThenReplaceLastIndex() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.insertBefore(2, "y");
	tokens.replace(2, "x");
	String result = tokens.toString();
	String expecting = "abx";
	assertEquals(expecting, result);
}
 
Example #11
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testReplaceSingleMiddleThenOverlappingSuperset() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcba");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(2, 2, "xyz");
	tokens.replace(0, 3, "foo");
	String result = tokens.toString();
	String expecting = "fooa";
	assertEquals(expecting, result);
}
 
Example #12
Source File: STGroupString.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void load() {
    if ( alreadyLoaded ) return;
    alreadyLoaded = true;
    GroupParser parser;
    try {
        ANTLRStringStream fs = new ANTLRStringStream(text);
        fs.name = sourceName;
        GroupLexer lexer = new GroupLexer(fs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new GroupParser(tokens);
        // no prefix since this group file is the entire group, nothing lives
        // beneath it.
        parser.group(this, "/");
    }
    catch (Exception e) {
        errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, "<string>");
    }
}
 
Example #13
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testReplaceThenReplaceLowerIndexedSuperset() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcccba");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(2, 4, "xyz");
	tokens.replace(1, 3, "foo"); // overlap, error
	Exception exc = null;
	try {
		tokens.toString();
	}
	catch (IllegalArgumentException iae) {
		exc = iae;
	}
	String expecting = "replace op boundaries of <[email protected]:\"foo\"> overlap with previous <[email protected]:\"xyz\">";
	assertNotNull(exc);
	assertEquals(expecting, exc.getMessage());
}
 
Example #14
Source File: STGroupString.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void load() {
    if ( alreadyLoaded ) return;
    alreadyLoaded = true;
    GroupParser parser;
    try {
        ANTLRStringStream fs = new ANTLRStringStream(text);
        fs.name = sourceName;
        GroupLexer lexer = new GroupLexer(fs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new GroupParser(tokens);
        // no prefix since this group file is the entire group, nothing lives
        // beneath it.
        parser.group(this, "/");
    }
    catch (Exception e) {
        errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, "<string>");
    }
}
 
Example #15
Source File: STGroupString.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void load() {
    if ( alreadyLoaded ) return;
    alreadyLoaded = true;
    GroupParser parser;
    try {
        ANTLRStringStream fs = new ANTLRStringStream(text);
        fs.name = sourceName;
        GroupLexer lexer = new GroupLexer(fs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new GroupParser(tokens);
        // no prefix since this group file is the entire group, nothing lives
        // beneath it.
        parser.group(this, "/");
    }
    catch (Exception e) {
        errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, "<string>");
    }
}
 
Example #16
Source File: DrlExprParser.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
/** Parse an expression from text */
public ConstraintConnectiveDescr parse( final String text ) {
    ConstraintConnectiveDescr constraint = null;
    try {
        DRLLexer lexer = DRLFactory.getDRLLexer(new ANTLRStringStream(text), languageLevel);
        CommonTokenStream input = new CommonTokenStream( lexer );
        RecognizerSharedState state = new RecognizerSharedState();
        helper = new ParserHelper( input, state, languageLevel );
        DRLExpressions parser = DRLFactory.getDRLExpressions(input, state, helper, languageLevel);
        parser.setBuildDescr( true );
        parser.setLeftMostExpr( null ); // setting initial value just in case
        BaseDescr expr = parser.conditionalOrExpression();
        if ( expr != null && !parser.hasErrors() ) {
            constraint = ConstraintConnectiveDescr.newAnd();
            constraint.addOrMerge( expr );
        }
    } catch ( RecognitionException e ) {
        helper.reportError( e );
    }
    return constraint;
}
 
Example #17
Source File: InputParser.java    From tuffylite with Apache License 2.0 6 votes vote down vote up
public void parseEvidenceString(String chunk, long lineOffset){
	ANTLRStringStream input = new ANTLRStringStream(chunk);
	MLNLexer lexer = new MLNLexer(input);
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	MLNParser parser = new MLNParser(tokens);
	parser.lineOffset = lineOffset;
	parser.ml = this.mln;
	try {
		parser.evidenceList();
		parser.reset();
		tokens.reset();
		lexer.reset();
		input.reset();
		parser.ml = null;
		parser = null;
	} catch (Exception e) {
		mln.closeFiles();
		ExceptionMan.handle(e);
	}
}
 
Example #18
Source File: STGroupString.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void load() {
    if ( alreadyLoaded ) return;
    alreadyLoaded = true;
    GroupParser parser;
    try {
        ANTLRStringStream fs = new ANTLRStringStream(text);
        fs.name = sourceName;
        GroupLexer lexer = new GroupLexer(fs);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        parser = new GroupParser(tokens);
        // no prefix since this group file is the entire group, nothing lives
        // beneath it.
        parser.group(this, "/");
    }
    catch (Exception e) {
        errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, "<string>");
    }
}
 
Example #19
Source File: GrammarTreeTest.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Test
public void selectGeo() throws RecognitionException {
    String queryString = "select * where a within .1 of -40.343666, 175.630917";

    ANTLRStringStream in = new ANTLRStringStream( queryString );
    CpQueryFilterLexer lexer = new CpQueryFilterLexer( in );
    TokenRewriteStream tokens = new TokenRewriteStream( lexer );
    CpQueryFilterParser parser = new CpQueryFilterParser( tokens );

    ParsedQuery query = parser.ql().parsedQuery;

    WithinOperand operand = ( WithinOperand ) query.getRootOperand();

    assertEquals( "a", operand.getProperty().getValue() );
    assertEquals( .1f, operand.getDistance().getFloatValue(), 0 );
    assertEquals( -40.343666f, operand.getLatitude().getFloatValue(), 0 );
    assertEquals( 175.630917f, operand.getLongitude().getFloatValue(), 0 );
}
 
Example #20
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testDisjointInserts() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.insertBefore(1, "x");
	tokens.insertBefore(2, "y");
	tokens.insertBefore(0, "z");
	String result = tokens.toString();
	String expecting = "zaxbyc";
	assertEquals(expecting, result);
}
 
Example #21
Source File: STGroupString.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
   public void load() {
	if (alreadyLoaded) return;
	alreadyLoaded = true;
	GroupParser parser;
	try {
		ANTLRStringStream fs = new ANTLRStringStream(text);
		fs.name = sourceName;
		GroupLexer lexer = new GroupLexer(fs);
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		parser = new GroupParser(tokens);
		// no prefix since this group file is the entire group, nothing lives
		// beneath it.
		parser.group(this, "/");
	}
	catch (Exception e) {
		errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, "<string>");
	}
}
 
Example #22
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testOverlappingReplace3() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abcc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(1, 2, "foo");
	tokens.replace(0, 2, "bar"); // wipes prior nested replace
	String result = tokens.toString();
	String expecting = "barc";
	assertEquals(expecting, result);
}
 
Example #23
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testCombine3Inserts() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.insertBefore(1, "x");
	tokens.insertBefore(0, "y");
	tokens.insertBefore(1, "z");
	String result = tokens.toString();
	String expecting = "yazxbc";
	assertEquals(expecting, result);
}
 
Example #24
Source File: GrammarTreeTest.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Test
public void uuidParse() throws RecognitionException {
    String queryString = "select * where  title = c6ee8a1c-3ef4-11e2-8861-02e81adcf3d0";

    ANTLRStringStream in = new ANTLRStringStream( queryString );
    CpQueryFilterLexer lexer = new CpQueryFilterLexer( in );
    TokenRewriteStream tokens = new TokenRewriteStream( lexer );
    CpQueryFilterParser parser = new CpQueryFilterParser( tokens );

    ParsedQuery query = parser.ql().parsedQuery;

    Equal rootNode = ( Equal ) query.getRootOperand();

    assertEquals( "title", rootNode.getProperty().getValue() );
    assertEquals( UUID.fromString( "c6ee8a1c-3ef4-11e2-8861-02e81adcf3d0" ),
            ( ( UUIDLiteral ) rootNode.getLiteral() ).getValue() );
}
 
Example #25
Source File: TestTokenRewriteStream.java    From jFuzzyLogic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void testReplaceThenInsertBeforeLastIndex() throws Exception {
	Grammar g = new Grammar(
		"lexer grammar t;\n"+
		"A : 'a';\n" +
		"B : 'b';\n" +
		"C : 'c';\n");
	CharStream input = new ANTLRStringStream("abc");
	Interpreter lexEngine = new Interpreter(g, input);
	TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
	tokens.LT(1); // fill buffer
	tokens.replace(2, "x");
	tokens.insertBefore(2, "y");
	String result = tokens.toString();
	String expecting = "abyx";
	assertEquals(expecting, result);
}
 
Example #26
Source File: PartialContentAssistContextFactory.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected void createContextsForLastCompleteNode(EObject previousModel, boolean strict) {
	String currentNodePrefix = getPrefix(currentNode);
	if (!Strings.isEmpty(currentNodePrefix) && !currentNode.getText().equals(currentNodePrefix)) {
		lexer.setCharStream(new ANTLRStringStream(currentNodePrefix));
		Token token = lexer.nextToken();
		if (token == Token.EOF_TOKEN) {
			return;
		}
		while (token != Token.EOF_TOKEN) {
			if (isErrorToken(token)) {
				return;
			}
			token = lexer.nextToken();
		}
	}
	String prefix = "";
	Collection<FollowElement> followElements = parseFollowElements(completionOffset, strict);
	doCreateContexts(lastCompleteNode, currentNode, prefix, previousModel, followElements);
}
 
Example #27
Source File: GrammarTreeTest.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Test
public void selectDistance() throws RecognitionException {
    String queryString = "select * where a contains 'foo'";

    ANTLRStringStream in = new ANTLRStringStream( queryString );
    CpQueryFilterLexer lexer = new CpQueryFilterLexer( in );
    TokenRewriteStream tokens = new TokenRewriteStream( lexer );
    CpQueryFilterParser parser = new CpQueryFilterParser( tokens );

    ParsedQuery query = parser.ql().parsedQuery;

    ContainsOperand operand = ( ContainsOperand ) query.getRootOperand();

    assertEquals( "a", operand.getProperty().getValue() );
    assertEquals( "foo", operand.getString().getValue() );
}
 
Example #28
Source File: BreakpointConditionParser.java    From binnavi with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a breakpoint condition string.
 *
 * @param conditionString The condition string to parse.
 *
 * @return The parsed breakpoint condition tree.
 *
 * @throws RecognitionException Thrown if the condition string could not be parsed.
 * @throws MaybeNullException Thrown if an empty condition string is passed to the function.
 */
public static ConditionNode parse(final String conditionString) throws RecognitionException,
    MaybeNullException {
  if (conditionString.trim().isEmpty()) {
    throw new MaybeNullException();
  }
  final CharStream charStream = new ANTLRStringStream(conditionString);
  final ConditionLexer lexer = new ConditionLexer(charStream);
  final CommonTokenStream tokens = new CommonTokenStream();
  tokens.setTokenSource(lexer);
  final ConditionParser parser = new ConditionParser(tokens);
  parser.setTreeAdaptor(adaptor);
  try {
    final ConditionParser.prog_return parserResult = parser.prog();
    final CommonTree ast = (CommonTree) parserResult.getTree();

    if (parser.input.index() < parser.input.size()) {
      throw new RecognitionException();
    }
    return convert(ast);
  } catch (final IllegalArgumentException e) {
    throw new RecognitionException();
  }
}
 
Example #29
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 #30
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);
	}
}