public LL1PlusBlock(OutputModelFactory factory, GrammarAST plusRoot, List<CodeBlockForAlt> alts) { super(factory, plusRoot, alts); PlusBlockStartState blkStart = (PlusBlockStartState) plusRoot.atnState; stateNumber = blkStart.loopBackState.stateNumber; blockStartStateNumber = blkStart.stateNumber; this.decision = blkStart.decision; Grammar g = factory.getGrammar(); CodeGenerator gen = factory.getGenerator(); /** Lookahead for each alt 1..n */ IntervalSet[] altLookSets = g.decisionLOOK.get(decision); altLook = getAltLookaheadAsStringLists(altLookSets); IntervalSet all = new IntervalSet(); for (IntervalSet s : altLookSets) all.addAll(s); this.error = getThrowNoViableAlt(factory, plusRoot, all); loopExpr = addCodeForLoopLookaheadTempVar(all); loopLabel = gen.getTarget().getLoopLabel(plusRoot); loopCounterVar = gen.getTarget().getLoopCounter(plusRoot); IntervalSet exitLookSet = altLookSets[altLookSets.length - 1]; this.exitLook = gen.getTarget().getTokenTypesAsTargetLabels(g, exitLookSet.toArray()); }
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 LexerATNFactory(LexerGrammar g) { super(g); // use codegen to get correct language templates for lexer commands String language = g.getOptionString("language"); CodeGenerator gen = new CodeGenerator(g.tool, null, language); codegenTemplates = gen.getTemplates(); }
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; }