Java Code Examples for org.antlr.runtime.RecognitionException#printStackTrace()

The following examples show how to use org.antlr.runtime.RecognitionException#printStackTrace() . 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: DRL6StrictParser.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
/**
 * Invokes the expression parser, trying to parse the annotation
 * as a full java-style annotation
 *
 * @return true if the sequence of tokens will match the
 *         elementValuePairs() syntax. false otherwise.
 */
private boolean speculateFullAnnotation() {
    state.backtracking++;
    int start = input.mark();
    try {
        exprParser.fullAnnotation(null);
    } catch (RecognitionException re) {
        System.err.println("impossible: " + re);
        re.printStackTrace();
    }
    boolean success = !state.failed;
    input.rewind(start);
    state.backtracking--;
    state.failed = false;
    return success;

}
 
Example 2
Source File: DRL6Parser.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
/**
 * Invokes the expression parser, trying to parse the annotation
 * as a full java-style annotation
 *
 * @return true if the sequence of tokens will match the
 *         elementValuePairs() syntax. false otherwise.
 */
private boolean speculateFullAnnotation() {
    state.backtracking++;
    int start = input.mark();
    try {
        exprParser.fullAnnotation(null);
    } catch (RecognitionException re) {
        System.err.println("impossible: " + re);
        re.printStackTrace();
    }
    boolean success = !state.failed;
    input.rewind(start);
    state.backtracking--;
    state.failed = false;
    return success;

}
 
Example 3
Source File: DRL5Parser.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private boolean speculateParameters( boolean requiresType ) {
    state.backtracking++;
    int start = input.mark();
    try {
        parameters( null,
                    requiresType ); // can never throw exception
    } catch ( RecognitionException re ) {
        System.err.println( "impossible: " + re );
        re.printStackTrace();
    }
    boolean success = !state.failed;
    input.rewind( start );
    state.backtracking--;
    state.failed = false;
    return success;
}
 
Example 4
Source File: DRL5Parser.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
/**
 * Invokes the expression parser, trying to parse the annotation
 * as a full java-style annotation
 *
 * @return true if the sequence of tokens will match the
 *         elementValuePairs() syntax. false otherwise.
 */
private boolean speculateFullAnnotation() {
    state.backtracking++;
    int start = input.mark();
    try {
        exprParser.fullAnnotation( null );
    } catch ( RecognitionException re ) {
        System.err.println( "impossible: " + re );
        re.printStackTrace();
    }
    boolean success = ! state.failed;
    input.rewind( start );
    state.backtracking--;
    state.failed = false;
    return success;

}
 
Example 5
Source File: DRL6StrictParser.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
private boolean speculateParameters(boolean requiresType) {
    state.backtracking++;
    int start = input.mark();
    try {
        parameters(null,
                requiresType); // can never throw exception
    } catch (RecognitionException re) {
        System.err.println("impossible: " + re);
        re.printStackTrace();
    }
    boolean success = !state.failed;
    input.rewind(start);
    state.backtracking--;
    state.failed = false;
    return success;
}
 
Example 6
Source File: JavaConsequenceBuilderTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
    public void testFixExitPointsReferences() {
        String consequence = 
            " System.out.println(\"this is a test\");\n " + 
            " exitPoints[\"foo\"].insert( new Cheese() );\n " + 
            " System.out.println(\"we are done with exitPoints\");\n ";
        setupTest( consequence, new HashMap<String, Object>() );
        try {
            JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
            JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock( (String) ruleDescr.getConsequence(),
                                                                                      new BoundIdentifiers( new HashMap<String, Class<?>>(), null ) );

            String fixed = fixBlockDescr(context, analysis, new HashMap<String, Declaration>());

            String expected = " System.out.println(\"this is a test\");\n " + 
                              " drools.getExitPoint(\"foo\").insert( new Cheese() );\n " + 
                              " System.out.println(\"we are done with exitPoints\");\n ";

//            System.out.println( "=============================" );
//            System.out.println( ruleDescr.getConsequence() );
//            System.out.println( "=============================" );
//            System.out.println( fixed );

            assertNotNull(fixed, context.getErrors().toString());
            assertEqualsIgnoreSpaces( expected,
                                      fixed );
        } catch ( RecognitionException e ) {
            e.printStackTrace();
        }

    }
 
Example 7
Source File: CluCalcCppTest.java    From Gaalop with GNU Lesser General Public License v3.0 5 votes vote down vote up
private double[] parseString(String string) {
	ANTLRStringStream input = new ANTLRStringStream(string);
	TestbenchLexer lexer = new TestbenchLexer(input);
	CommonTokenStream tokenStream = new CommonTokenStream(lexer);
	TestbenchParser parser = new TestbenchParser(tokenStream);
	try {
		CoeffReader reader = parser.line();
		return reader.getCoeffs();
	} catch (RecognitionException e) {
		e.printStackTrace();
	}
	throw new IllegalStateException("Line " + string + " could not be parsed.");
}
 
Example 8
Source File: HorizonJUnitTest.java    From Gaalop with GNU Lesser General Public License v3.0 5 votes vote down vote up
private double[] parseString(String string) {
	ANTLRStringStream input = new ANTLRStringStream(string);
	TestbenchLexer lexer = new TestbenchLexer(input);
	CommonTokenStream tokenStream = new CommonTokenStream(lexer);
	TestbenchParser parser = new TestbenchParser(tokenStream);
	try {
		CoeffReader reader = parser.line();
		return reader.getCoeffs();
	} catch (RecognitionException e) {
		e.printStackTrace();
	}
	throw new IllegalStateException("Line " + string + " could not be parsed.");
}
 
Example 9
Source File: TestbenchParserTest.java    From Gaalop with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {		
	String string = "0.9e-009^e1 + 0.98e+010^e2 - 0.189039^e3 + 0.98^e + 1^e0";
	ANTLRStringStream input = new ANTLRStringStream(string);
	TestbenchLexer lexer = new TestbenchLexer(input);
	CommonTokenStream tokenStream = new CommonTokenStream(lexer);
	TestbenchParser parser = new TestbenchParser(tokenStream);
	try {
		CoeffReader reader = parser.line();
		System.out.println(Arrays.toString(reader.getCoeffs()));
	} catch (RecognitionException e) {
		e.printStackTrace();
	}
}
 
Example 10
Source File: FuzzyEdgeOrchestrator.java    From EdgeCloudSim with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void initialize() {
	numberOfHost=SimSettings.getInstance().getNumOfEdgeHosts();
	
	try {
		fis1 = FIS.createFromString(FCL_definition.fclDefinition1, false);
		fis2 = FIS.createFromString(FCL_definition.fclDefinition2, false);
		fis3 = FIS.createFromString(FCL_definition.fclDefinition3, false);
	} catch (RecognitionException e) {
		SimLogger.printLine("Cannot generate FIS! Terminating simulation...");
		e.printStackTrace();
		System.exit(0);
	}
}
 
Example 11
Source File: JavaConsequenceBuilderPRAlwaysTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
    public void testFixEntryPointsReferences() {
        String consequence = 
            " System.out.println(\"this is a test\");\n " + 
            " entryPoints[\"foo\"].insert( new Cheese() );\n " + 
            " System.out.println(\"we are done with entryPoints\");\n ";
        setupTest( "", new HashMap<String, Object>() );
        try {
            ruleDescr.setConsequence( consequence );
            JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
            JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock( (String) ruleDescr.getConsequence(),
                                                                                      new BoundIdentifiers( new HashMap<String, Class<?>>(), null ) );

            String fixed = fixBlockDescr( context, analysis, new HashMap<String,Declaration>() );

            String expected = " System.out.println(\"this is a test\");\n " + 
                              " drools.getEntryPoint(\"foo\").insert( new Cheese() );\n " + 
                              " System.out.println(\"we are done with entryPoints\");\n ";

//            System.out.println( "=============================" );
//            System.out.println( ruleDescr.getConsequence() );
//            System.out.println( "=============================" );
//            System.out.println( fixed );

            assertNotNull( context.getErrors().toString(),
                           fixed );
            assertEqualsIgnoreSpaces( expected,
                                      fixed );
        } catch ( RecognitionException e ) {
            e.printStackTrace();
        }

    }
 
Example 12
Source File: JavaConsequenceBuilderPRAlwaysTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
    public void testFixExitPointsReferences() {
        String consequence = 
            " System.out.println(\"this is a test\");\n " + 
            " exitPoints[\"foo\"].insert( new Cheese() );\n " + 
            " System.out.println(\"we are done with exitPoints\");\n ";
        setupTest( consequence, new HashMap<String, Object>() );
        try {
            JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
            JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock( (String) ruleDescr.getConsequence(),
                                                                                      new BoundIdentifiers( new HashMap<String, Class<?>>(), null ) );

            String fixed = fixBlockDescr(context, analysis, new HashMap<String, Declaration>());

            String expected = " System.out.println(\"this is a test\");\n " + 
                              " drools.getExitPoint(\"foo\").insert( new Cheese() );\n " + 
                              " System.out.println(\"we are done with exitPoints\");\n ";

//            System.out.println( "=============================" );
//            System.out.println( ruleDescr.getConsequence() );
//            System.out.println( "=============================" );
//            System.out.println( fixed );

            assertNotNull(fixed, context.getErrors().toString());
            assertEqualsIgnoreSpaces( expected,
                                      fixed );
        } catch ( RecognitionException e ) {
            e.printStackTrace();
        }

    }
 
Example 13
Source File: JavaConsequenceBuilderTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
    public void testFixEntryPointsReferences() {
        String consequence = 
            " System.out.println(\"this is a test\");\n " + 
            " entryPoints[\"foo\"].insert( new Cheese() );\n " + 
            " System.out.println(\"we are done with entryPoints\");\n ";
        setupTest( "", new HashMap<String, Object>() );
        try {
            ruleDescr.setConsequence( consequence );
            JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
            JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock( (String) ruleDescr.getConsequence(),
                                                                                      new BoundIdentifiers( new HashMap<String, Class<?>>(), null ) );

            String fixed = fixBlockDescr( context, analysis, new HashMap<String,Declaration>() );

            String expected = " System.out.println(\"this is a test\");\n " + 
                              " drools.getEntryPoint(\"foo\").insert( new Cheese() );\n " + 
                              " System.out.println(\"we are done with entryPoints\");\n ";

//            System.out.println( "=============================" );
//            System.out.println( ruleDescr.getConsequence() );
//            System.out.println( "=============================" );
//            System.out.println( fixed );

            assertNotNull( context.getErrors().toString(),
                           fixed );
            assertEqualsIgnoreSpaces( expected,
                                      fixed );
        } catch ( RecognitionException e ) {
            e.printStackTrace();
        }

    }
 
Example 14
Source File: DRL5Parser.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private boolean speculatePositionalConstraints() {
    state.backtracking++;
    int start = input.mark();
    try {
        positionalConstraints( null ); // can never throw exception
    } catch ( RecognitionException re ) {
        System.err.println( "impossible: " + re );
        re.printStackTrace();
    }
    boolean success = !state.failed;
    input.rewind( start );
    state.backtracking--;
    state.failed = false;
    return success;
}
 
Example 15
Source File: DRL6Parser.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private boolean speculatePositionalConstraints() {
    state.backtracking++;
    int start = input.mark();
    try {
        positionalConstraints(null); // can never throw exception
    } catch (RecognitionException re) {
        System.err.println("impossible: " + re);
        re.printStackTrace();
    }
    boolean success = !state.failed;
    input.rewind(start);
    state.backtracking--;
    state.failed = false;
    return success;
}
 
Example 16
Source File: Owl2FunctionalStyleParserTest.java    From elk-reasoner with Apache License 2.0 5 votes vote down vote up
public void testClazz() throws InterruptedException, ExecutionException {
	try {
		ElkClass clazz = parseElkClass("owl:Thing");
		assertNotNull(clazz);
		ElkObjectFactory objectFactory = new ElkObjectFactoryImpl();
		assertSame(objectFactory.getOwlThing(), clazz);
	} catch (RecognitionException e) {
		assertFalse(true);
		e.printStackTrace();
	}

}
 
Example 17
Source File: DRL6StrictParser.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private boolean speculatePositionalConstraints() {
    state.backtracking++;
    int start = input.mark();
    try {
        positionalConstraints(null); // can never throw exception
    } catch (RecognitionException re) {
        System.err.println("impossible: " + re);
        re.printStackTrace();
    }
    boolean success = !state.failed;
    input.rewind(start);
    state.backtracking--;
    state.failed = false;
    return success;
}
 
Example 18
Source File: JavaConsequenceBuilderTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testFixThrows() {
    String consequence =
        " modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + 
        " throw new java.lang.RuntimeException(\"xxx\");\n " +
        " Cheese c1 = $cheese;\n" +
        " modify( c1 ) { setPrice( 10 ), setOldPrice( age ) }\n ";
    setupTest( "", new HashMap<String, Object>() );
    try {
        ruleDescr.setConsequence( consequence );
        JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
        Map<String, Class<?>> declrCls = new HashMap<String, Class<?>>();
        declrCls.put( "$cheese", Cheese.class );
        
        JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock( (String) ruleDescr.getConsequence(),
                                                                                  new BoundIdentifiers(declrCls, null ) );
        
        BoundIdentifiers bindings = new BoundIdentifiers( new HashMap(), null );
        bindings.getDeclrClasses().put( "$cheese", Cheese.class );
        bindings.getDeclrClasses().put( "age", int.class );
        
        // Set the inputs for each container, this is needed for modifes when the target context is the result of an expression
        List<JavaBlockDescr> descrs = new ArrayList<JavaBlockDescr>();
        setContainerBlockInputs(context,
                                        descrs,
                                        analysis.getBlockDescrs(), 
                                        consequence,
                                        bindings,
                                        new HashMap(),
                                        0);

        context.getKnowledgeBuilder().getTypeDeclaration(Cheese.class).setPropertyReactive(true);
        String fixed = fixBlockDescr(context, analysis, context.getDeclarationResolver().getDeclarations( context.getRule() ) );

        String expected = 
                " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" +
        		"  throw new java.lang.RuntimeException(\"xxx\");\r\n" + 
        		"  Cheese c1 = $cheese;\r\n" + 
        		" { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" +
        		" \r\n" + 
        		"";

        assertNotNull( context.getErrors().toString(),
                       fixed );
        assertEqualsIgnoreSpaces( expected,
                                  fixed );
    } catch ( RecognitionException e ) {
        e.printStackTrace();
    }

}
 
Example 19
Source File: JavaConsequenceBuilderPRAlwaysTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testFixThrows() {
    String consequence =
        " modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + 
        " throw new java.lang.RuntimeException(\"xxx\");\n " +
        " Cheese c1 = $cheese;\n" +
        " modify( c1 ) { setPrice( 10 ), setOldPrice( age ) }\n ";
    setupTest( "", new HashMap<String, Object>() );
    try {
        ruleDescr.setConsequence( consequence );
        JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
        Map<String, Class<?>> declrCls = new HashMap<String, Class<?>>();
        declrCls.put( "$cheese", Cheese.class );
        
        JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock( (String) ruleDescr.getConsequence(),
                                                                                  new BoundIdentifiers(declrCls, null ) );
        
        BoundIdentifiers bindings = new BoundIdentifiers( new HashMap(), null );
        bindings.getDeclrClasses().put( "$cheese", Cheese.class );
        bindings.getDeclrClasses().put( "age", int.class );
        
        // Set the inputs for each container, this is needed for modifes when the target context is the result of an expression
        List<JavaBlockDescr> descrs = new ArrayList<JavaBlockDescr>();
        setContainerBlockInputs(context,
                                        descrs,
                                        analysis.getBlockDescrs(), 
                                        consequence,
                                        bindings,
                                        new HashMap(),
                                        0);

        context.getKnowledgeBuilder().getTypeDeclaration(Cheese.class).setPropertyReactive(true);
        String fixed = fixBlockDescr(context, analysis, context.getDeclarationResolver().getDeclarations( context.getRule() ) );

        String expected = 
                " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" +
        		"  throw new java.lang.RuntimeException(\"xxx\");\r\n" + 
        		"  Cheese c1 = $cheese;\r\n" + 
        		" { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, new org.drools.core.util.bitmask.LongBitMask(12L), org.drools.compiler.Cheese.class ); }\r\n" +
        		" \r\n" + 
        		"";

        assertNotNull( context.getErrors().toString(),
                       fixed );
        assertEqualsIgnoreSpaces( expected,
                                  fixed );
    } catch ( RecognitionException e ) {
        e.printStackTrace();
    }

}
 
Example 20
Source File: ParsedModule.java    From swift-t with Apache License 2.0 4 votes vote down vote up
/**
   Use ANTLR to parse the input and get the Tree
 * @throws IOException
 */
private static SwiftAST runANTLR(ANTLRInputStream input, LineMapping lineMap) {

  ExMLexer lexer = new ExMLexer(input);
  lexer.lineMap = lineMap;
  CommonTokenStream tokens = new CommonTokenStream(lexer);
  ExMParser parser = new ExMParser(tokens);
  parser.lineMap = lineMap;
  parser.setTreeAdaptor(new SwTreeAdaptor());

  // Launch parsing
  ExMParser.program_return program = null;
  try
  {
    program = parser.program();
  }
  catch (RecognitionException e)
  {
    // This is an internal error
    e.printStackTrace();
    System.out.println("Parsing failed: internal error");
    throw new STCFatal(ExitCode.ERROR_INTERNAL.code());
  }

  /* NOTE: in some cases the antlr parser will actually recover from
   *    errors, print an error message and continue, generating the
   *    parse tree that it thinks is most plausible.  This is where
   *    we detect this case.
   */
  if (parser.parserError) {
    // This is a user error
    System.err.println("Error occurred during parsing.");
    throw new STCFatal(ExitCode.ERROR_USER.code());
  }

  // Do we actually need this check? -Justin (10/26/2011)
  if (program == null)
    throw new STCRuntimeError("PARSER FAILED!");


  SwiftAST tree = (SwiftAST) program.getTree();

  return tree;
}