Example #1
0
  public void testActions(String templates, String actionName, String action, String expected)
      throws org.antlr.runtime.RecognitionException {
    int lp = templates.indexOf('(');
    String name = templates.substring(0, lp);
    STGroup group = new STGroupString(templates);
    ST st = group.getInstanceOf(name);
    st.add(actionName, action);
    String grammar = st.render();
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(grammar, equeue);
    if (g.ast != null && !g.ast.hasErrors) {
      SemanticPipeline sem = new SemanticPipeline(g);
      sem.process();

      ATNFactory factory = new ParserATNFactory(g);
      if (g.isLexer()) factory = new LexerATNFactory((LexerGrammar) g);
      g.atn = factory.createATN();

      CodeGenerator gen = new CodeGenerator(g);
      ST outputFileST = gen.generateParser();
      String output = outputFileST.render();
      // System.out.println(output);
      String b = "#" + actionName + "#";
      int start = output.indexOf(b);
      String e = "#end-" + actionName + "#";
      int end = output.indexOf(e);
      String snippet = output.substring(start + b.length(), end);
      assertEquals(expected, snippet);
    }
    if (equeue.size() > 0) {
      System.err.println(equeue.toString());
    }
  }
  public List<String> getEvalInfoForString(String grammarString, String pattern)
      throws RecognitionException {
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(grammarString);
    List<String> evals = new ArrayList<String>();
    if (g.ast != null && !g.ast.hasErrors) {
      SemanticPipeline sem = new SemanticPipeline(g);
      sem.process();

      ATNFactory factory = new ParserATNFactory(g);
      if (g.isLexer()) factory = new LexerATNFactory((LexerGrammar) g);
      g.atn = factory.createATN();

      CodeGenerator gen = new CodeGenerator(g);
      ST outputFileST = gen.generateParser();

      //			STViz viz = outputFileST.inspect();
      //			try {
      //				viz.waitForClose();
      //			}
      //			catch (Exception e) {
      //				e.printStackTrace();
      //			}

      boolean debug = false;
      DebugInterpreter interp =
          new DebugInterpreter(
              outputFileST.groupThatCreatedThisInstance,
              outputFileST.impl.nativeGroup.errMgr,
              debug);
      InstanceScope scope = new InstanceScope(null, outputFileST);
      StringWriter sw = new StringWriter();
      AutoIndentWriter out = new AutoIndentWriter(sw);
      interp.exec(out, scope);

      for (String e : interp.evals) {
        if (e.contains(pattern)) {
          evals.add(e);
        }
      }
    }
    if (equeue.size() > 0) {
      System.err.println(equeue.toString());
    }
    return evals;
  }
Example #3
0
  public void testErrors(String[] pairs, boolean printTree) {
    for (int i = 0; i < pairs.length; i += 2) {
      String input = pairs[i];
      String expect = pairs[i + 1];

      String[] lines = input.split("\n");
      String fileName = getFilenameFromFirstLineOfGrammar(lines[0]);
      ErrorQueue equeue = antlr(fileName, fileName, input, false);

      String actual = equeue.toString(true);
      actual = actual.replace(tmpdir + File.separator, "");
      System.err.println(actual);
      String msg = input;
      msg = msg.replace("\n", "\\n");
      msg = msg.replace("\r", "\\r");
      msg = msg.replace("\t", "\\t");

      assertEquals("error in: " + msg, expect, actual);
    }
  }
Example #4
0
 protected void checkGrammarSemanticsWarning(
     ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception {
   ANTLRMessage foundMsg = null;
   for (int i = 0; i < equeue.warnings.size(); i++) {
     ANTLRMessage m = equeue.warnings.get(i);
     if (m.getErrorType() == expectedMessage.getErrorType()) {
       foundMsg = m;
     }
   }
   assertNotNull("no error; " + expectedMessage.getErrorType() + " expected", foundMsg);
   assertTrue(
       "error is not a GrammarSemanticsMessage", foundMsg instanceof GrammarSemanticsMessage);
   assertEquals(Arrays.toString(expectedMessage.getArgs()), Arrays.toString(foundMsg.getArgs()));
   if (equeue.size() != 1) {
     System.err.println(equeue);
   }
 }