From 118d22506651e0b2f2c5746f13c18796064cf8ef Mon Sep 17 00:00:00 2001 From: parrt Date: Mon, 17 May 2010 13:28:04 -0800 Subject: [PATCH] got actions into lexer; reorg'd [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6872] --- .../Java/src/org/antlr/v4/runtime/Lexer.java | 32 +-- .../src/org/antlr/v4/runtime/pda/PDA.java | 42 ++-- .../v4/tool/templates/codegen/Java/Java.stg | 74 ++++-- .../org/antlr/v4/codegen/CodeGenerator.java | 1 - .../src/org/antlr/v4/codegen/CompiledPDA.java | 24 ++ .../org/antlr/v4/codegen/LexerFactory.java | 27 ++- ...nerator.java => PDABytecodeGenerator.java} | 95 ++++---- ...tecodeTriggers.g => PDABytecodeTriggers.g} | 26 +-- ...Triggers.java => PDABytecodeTriggers.java} | 216 +++++++++--------- ...gers.tokens => PDABytecodeTriggers.tokens} | 0 .../antlr/v4/codegen/SourceGenTriggers.tokens | 99 ++++++++ .../v4/codegen/{nfa => pda}/AcceptInstr.java | 6 +- .../v4/codegen/{nfa => pda}/ActionInstr.java | 8 +- .../v4/codegen/{nfa => pda}/CallInstr.java | 6 +- .../antlr/v4/codegen/{nfa => pda}/Instr.java | 6 +- .../v4/codegen/{nfa => pda}/JumpInstr.java | 6 +- .../v4/codegen/{nfa => pda}/LabelInstr.java | 6 +- .../v4/codegen/{nfa => pda}/MatchInstr.java | 6 +- .../v4/codegen/{nfa => pda}/RangeInstr.java | 8 +- .../v4/codegen/{nfa => pda}/RetInstr.java | 2 +- .../v4/codegen/{nfa => pda}/SaveInstr.java | 6 +- .../v4/codegen/{nfa => pda}/SemPredInstr.java | 8 +- .../v4/codegen/{nfa => pda}/SplitInstr.java | 8 +- .../codegen/{nfa => pda}/WildcardInstr.java | 2 +- tool/src/org/antlr/v4/misc/DoubleKeyMap.java | 32 ++- ...on.java => TestPDABytecodeGeneration.java} | 6 +- ...Interp.java => TestPDABytecodeInterp.java} | 8 +- 27 files changed, 480 insertions(+), 280 deletions(-) create mode 100644 tool/src/org/antlr/v4/codegen/CompiledPDA.java rename tool/src/org/antlr/v4/codegen/{NFABytecodeGenerator.java => PDABytecodeGenerator.java} (59%) rename tool/src/org/antlr/v4/codegen/{NFABytecodeTriggers.g => PDABytecodeTriggers.g} (90%) rename tool/src/org/antlr/v4/codegen/{NFABytecodeTriggers.java => PDABytecodeTriggers.java} (90%) rename tool/src/org/antlr/v4/codegen/{NFABytecodeTriggers.tokens => PDABytecodeTriggers.tokens} (100%) create mode 100644 tool/src/org/antlr/v4/codegen/SourceGenTriggers.tokens rename tool/src/org/antlr/v4/codegen/{nfa => pda}/AcceptInstr.java (73%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/ActionInstr.java (69%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/CallInstr.java (76%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/Instr.java (78%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/JumpInstr.java (72%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/LabelInstr.java (77%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/MatchInstr.java (79%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/RangeInstr.java (80%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/RetInstr.java (83%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/SaveInstr.java (77%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/SemPredInstr.java (69%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/SplitInstr.java (75%) rename tool/src/org/antlr/v4/codegen/{nfa => pda}/WildcardInstr.java (89%) rename tool/test/org/antlr/v4/test/{TestNFABytecodeGeneration.java => TestPDABytecodeGeneration.java} (97%) rename tool/test/org/antlr/v4/test/{TestNFABytecodeInterp.java => TestPDABytecodeInterp.java} (96%) diff --git a/runtime/Java/src/org/antlr/v4/runtime/Lexer.java b/runtime/Java/src/org/antlr/v4/runtime/Lexer.java index 3b1820314..f437356cd 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/Lexer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/Lexer.java @@ -30,6 +30,7 @@ package org.antlr.v4.runtime; import org.antlr.runtime.CharStream; import org.antlr.runtime.Token; import org.antlr.runtime.TokenSource; +import org.antlr.v4.runtime.pda.PDA; /** A lexer is recognizer that draws input symbols from a character stream. * lexer grammars result in a subclass of this object. A Lexer object @@ -37,8 +38,14 @@ import org.antlr.runtime.TokenSource; * of speed. */ public abstract class Lexer extends BaseRecognizer implements TokenSource { + public static final int DEFAULT_MODE = 0; + /** Where is the lexer drawing characters from? */ - protected CharStream input; + public CharStream input; + + public int _mode = DEFAULT_MODE; + + public static PDA[] modeToPDA; public Lexer() { } @@ -89,8 +96,8 @@ public abstract class Lexer extends BaseRecognizer implements TokenSource { eof.setCharPositionInLine(getCharPositionInLine()); return eof; } - try { - _nextToken(); + { + state.type = modeToPDA[_mode].execThompson(input); if ( state.token==null ) { emit(); } @@ -99,14 +106,14 @@ public abstract class Lexer extends BaseRecognizer implements TokenSource { } return state.token; } - catch (NoViableAltException nva) { - reportError(nva); - recover(nva); // throw out current char and try again - } - catch (RecognitionException re) { - reportError(re); - // match() routine has already called recover() - } +// catch (NoViableAltException nva) { +// reportError(nva); +// recover(nva); // throw out current char and try again +// } +// catch (RecognitionException re) { +// reportError(re); +// // match() routine has already called recover() +// } } } @@ -120,9 +127,6 @@ public abstract class Lexer extends BaseRecognizer implements TokenSource { state.token = Token.SKIP_TOKEN; } - /** This is the lexer entry point that sets instance var 'token' */ - public abstract void _nextToken() throws RecognitionException; - /** Set the char stream and reset the lexer */ public void setCharStream(CharStream input) { this.input = null; diff --git a/runtime/Java/src/org/antlr/v4/runtime/pda/PDA.java b/runtime/Java/src/org/antlr/v4/runtime/pda/PDA.java index c9d447587..dfe3800c4 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/pda/PDA.java +++ b/runtime/Java/src/org/antlr/v4/runtime/pda/PDA.java @@ -12,8 +12,14 @@ import java.util.Map; /** A (nondeterministic) pushdown bytecode machine for lexing and LL prediction. * Derived partially from Cox' description of Thompson's 1960s work: * http://swtch.com/~rsc/regexp/regexp2.html + * + * Primary difference is that I've extended to have actions, semantic predicates + * and a stack for rule invocation. */ public class PDA { + public interface action_fptr { void exec(int action); } + public interface sempred_fptr { boolean eval(int predIndex); } + public byte[] code; public Map ruleToAddr; public int[] tokenTypeToAddr; @@ -163,12 +169,12 @@ processOneChar: // then, move to next char, looking for longer match // (we continue processing if there are states in reach) break; - case Bytecode.JMP : // ignore - case Bytecode.SPLIT : - case Bytecode.CALL : - case Bytecode.RET : - case Bytecode.SEMPRED : - break; +// case Bytecode.JMP : // ignore +// case Bytecode.SPLIT : +// case Bytecode.CALL : +// case Bytecode.RET : +// case Bytecode.SEMPRED : +// break; default : throw new RuntimeException("invalid instruction @ "+ip+": "+opcode); } @@ -248,9 +254,9 @@ processOneChar: case Bytecode.SEMPRED : // add next instruction only if sempred succeeds int ruleIndex = getShort(code, ip); - int actionIndex = getShort(code, ip+2); - System.out.println("eval sempred "+ ruleIndex+", "+actionIndex); - if ( sempred(ruleIndex, actionIndex) ) { + int predIndex = getShort(code, ip+2); + System.out.println("eval sempred "+ ruleIndex+", "+predIndex); + if ( sempred(ruleIndex, predIndex) ) { addToClosure(closure, ip+4, alt, context); } break; @@ -425,15 +431,6 @@ processOneChar: } } - void trace(int ip) { - String instr = Bytecode.disassembleInstruction(code, ip); - System.out.println(instr); - } - - public static int getShort(byte[] memory, int index) { - return (memory[index]&0xFF) <<(8*1) | (memory[index+1]&0xFF); // prevent sign extension with mask - } - // subclass needs to override these if there are sempreds or actions in lexer rules public boolean sempred(int ruleIndex, int actionIndex) { @@ -442,6 +439,15 @@ processOneChar: public void action(int ruleIndex, int actionIndex) { } + + void trace(int ip) { + String instr = Bytecode.disassembleInstruction(code, ip); + System.out.println(instr); + } + + public static int getShort(byte[] memory, int index) { + return (memory[index]&0xFF) <<(8*1) | (memory[index+1]&0xFF); // prevent sign extension with mask + } /* public int exec(CharStream input, String ruleName) { diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index 5aae1a8a4..ee47ea617 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -229,10 +229,9 @@ import org.antlr.runtime.*; >> -Lexer(lexerName,modes,pdas) ::= << +Lexer(lexerName,modes,pdas, actions, sempreds) ::= << public class extends Lexer { - public static enum Mode { ()}; separator=", ">; int mode; Mode(int m) {mode=m;\}} - public Mode _mode = Mode.DEFAULT_MODE; + = ;}; separator="\n"> public () {;} public (CharStream input) { @@ -243,27 +242,68 @@ public class extends Lexer { } public String getGrammarFileName() { return ""; } - public void _nextToken() throws RecognitionException { - state.type = modeToPDA[_mode.ordinal()].execThompson(input); - } - + + - public static PDA[] modeToPDA = { _PDA()}; separator=", "> }; + static { + modeToPDA = new PDA[] { _PDA()}; separator=", "> }; + } } >> -PDA(name, model) ::= << -public static final byte[] _code = { - -}; -public static final int[] _tokenTypeToAddr = { - -}; +PDA(name, model, actions, sempreds) ::= << public static final class _PDA extends PDA { - ruleToAddr, int[] tokenTypeToAddr, int nLabels!> + public static final byte[] code = { + + }; + public static final int[] tokenTypeToAddr = { + + }; + + public void action(int r, int a) { + : _actions(a); break; + }> + } + } + + + public void sempred(int r, int a) { + : return _sempreds(a); + }> + } + } + public _PDA() { - super(_code, _tokenTypeToAddr, ); + super(code, tokenTypeToAddr, ); + } +} +>> + +actionMethod(name, actions) ::= << +public void _actions(int action) { + switch ( action ) { + : + + break; + }> + } +} +>> + +sempredMethod(name, preds) ::= << +public boolean _sempreds(int pred) { + switch ( pred ) { + : + return

; + }> + default : return false; } } >> diff --git a/tool/src/org/antlr/v4/codegen/CodeGenerator.java b/tool/src/org/antlr/v4/codegen/CodeGenerator.java index 20a6f9d6b..491e2a957 100644 --- a/tool/src/org/antlr/v4/codegen/CodeGenerator.java +++ b/tool/src/org/antlr/v4/codegen/CodeGenerator.java @@ -111,7 +111,6 @@ public class CodeGenerator { public void write(ST code, String fileName) throws IOException { long start = System.currentTimeMillis(); Writer w = g.tool.getOutputFile(g, fileName); - // Write the output to a StringWriter STWriter wr = new AutoIndentWriter(w); wr.setLineWidth(lineWidth); code.write(wr); diff --git a/tool/src/org/antlr/v4/codegen/CompiledPDA.java b/tool/src/org/antlr/v4/codegen/CompiledPDA.java new file mode 100644 index 000000000..881965009 --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/CompiledPDA.java @@ -0,0 +1,24 @@ +package org.antlr.v4.codegen; + +import org.antlr.runtime.Token; +import org.antlr.v4.codegen.pda.Instr; +import org.antlr.v4.misc.DoubleKeyMap; +import org.antlr.v4.tool.Rule; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** */ +public class CompiledPDA { public List instrs = new ArrayList(); + public byte[] code; // instrs in bytecode form + public int ip = 0; // where to write next + public Map ruleToAddr = new HashMap(); + public int[] tokenTypeToAddr; + + public DoubleKeyMap ruleLabels = new DoubleKeyMap(); + public DoubleKeyMap ruleActions = new DoubleKeyMap(); + public DoubleKeyMap ruleSempreds = new DoubleKeyMap(); + public int nLabels; +} diff --git a/tool/src/org/antlr/v4/codegen/LexerFactory.java b/tool/src/org/antlr/v4/codegen/LexerFactory.java index e22a28a4b..2b27b9bc7 100644 --- a/tool/src/org/antlr/v4/codegen/LexerFactory.java +++ b/tool/src/org/antlr/v4/codegen/LexerFactory.java @@ -1,9 +1,12 @@ package org.antlr.v4.codegen; -import org.antlr.v4.runtime.pda.PDA; +import org.antlr.runtime.Token; import org.antlr.v4.tool.LexerGrammar; +import org.antlr.v4.tool.Rule; import org.stringtemplate.v4.ST; +import java.util.Set; + /** */ public class LexerFactory { public CodeGenerator gen; @@ -20,8 +23,28 @@ public class LexerFactory { fileST.add("fileName", gen.getRecognizerFileName()); fileST.add("lexer", lexerST); for (String modeName : lg.modes.keySet()) { // for each mode - PDA pda = NFABytecodeGenerator.getBytecode(lg, modeName); + CompiledPDA pda = PDABytecodeGenerator.compileLexerMode(lg, modeName); ST pdaST = gen.templates.getInstanceOf("PDA"); + for (Rule r : pda.ruleActions.keySet()) { + Set actionTokens = pda.ruleActions.keySet(r); + ST actionST = gen.templates.getInstanceOf("actionMethod"); + actionST.add("name", r.name); + for (Token t : actionTokens) { + actionST.add("actions", t.getText()); + } + pdaST.add("actions", actionST); + lexerST.add("actions", actionST); + } + for (Rule r : pda.ruleSempreds.keySet()) { + Set sempredTokens = pda.ruleSempreds.keySet(r); + ST sempredST = gen.templates.getInstanceOf("sempredMethod"); + sempredST.add("name", r.name); + for (Token t : sempredTokens) { + sempredST.add("preds", t.getText()); + } + pdaST.add("sempreds", sempredST); + lexerST.add("sempreds", sempredST); + } pdaST.add("name", modeName); pdaST.add("model", pda); lexerST.add("pdas", pdaST); diff --git a/tool/src/org/antlr/v4/codegen/NFABytecodeGenerator.java b/tool/src/org/antlr/v4/codegen/PDABytecodeGenerator.java similarity index 59% rename from tool/src/org/antlr/v4/codegen/NFABytecodeGenerator.java rename to tool/src/org/antlr/v4/codegen/PDABytecodeGenerator.java index 9efac977d..97e88fd34 100644 --- a/tool/src/org/antlr/v4/codegen/NFABytecodeGenerator.java +++ b/tool/src/org/antlr/v4/codegen/PDABytecodeGenerator.java @@ -4,9 +4,8 @@ import org.antlr.runtime.RecognizerSharedState; import org.antlr.runtime.Token; import org.antlr.runtime.tree.CommonTreeNodeStream; import org.antlr.runtime.tree.TreeNodeStream; -import org.antlr.v4.codegen.nfa.*; +import org.antlr.v4.codegen.pda.*; import org.antlr.v4.misc.CharSupport; -import org.antlr.v4.misc.DoubleKeyMap; import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.parse.GrammarASTAdaptor; import org.antlr.v4.runtime.pda.Bytecode; @@ -16,64 +15,47 @@ import org.antlr.v4.tool.GrammarAST; import org.antlr.v4.tool.LexerGrammar; import org.antlr.v4.tool.Rule; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; import java.util.Map; /** http://swtch.com/~rsc/regexp/regexp2.html */ -public class NFABytecodeGenerator extends TreeParser { - LexerGrammar lg; - public List instrs = new ArrayList(); - public int ip = 0; // where to write next - Map ruleToAddr = new HashMap(); - int[] tokenTypeToAddr; - - DoubleKeyMap ruleLabels = new DoubleKeyMap(); - DoubleKeyMap ruleActions = new DoubleKeyMap(); - DoubleKeyMap ruleSempreds = new DoubleKeyMap(); - +public class PDABytecodeGenerator extends TreeParser { public Rule currentRule; + CompiledPDA pda = new CompiledPDA(); + public int labelIndex = 0; // first time we ask for labels we index -// public abstract class LabelMaker { -// Map labels = new HashMap(); -// public LabelMaker(Collection keys) { -// for (Key k : keys) labels.put(k, computeLabel(k)); -// } -// public abstract Label computeLabel(Key k); -// } - - public NFABytecodeGenerator(TreeNodeStream input, RecognizerSharedState state) { + public PDABytecodeGenerator(TreeNodeStream input, RecognizerSharedState state) { super(input, state); } public void emit(Instr I) { - I.addr = ip; + I.addr = pda.ip; I.rule = currentRule; I.gen = this; - ip += I.nBytes(); - instrs.add(I); + pda.ip += I.nBytes(); + pda.instrs.add(I); } // indexed from 0 per rule public int getActionIndex(Rule r, Token actionToken) { - Integer I = ruleActions.get(r, actionToken); + Integer I = pda.ruleActions.get(r, actionToken); if ( I!=null ) return I; // already got its label - Map labels = ruleActions.get(r); - int i = labels.size(); - ruleActions.put(r, actionToken, i); + Map labels = pda.ruleActions.get(r); + int i = 0; + if ( labels!=null ) i = labels.size(); + pda.ruleActions.put(r, actionToken, i); return i; } // indexed from 0 per rule public int getSempredIndex(Rule r, Token actionToken) { - Integer I = ruleSempreds.get(r, actionToken); + Integer I = pda.ruleSempreds.get(r, actionToken); if ( I!=null ) return I; // already got its label - Map labels = ruleSempreds.get(r); - int i = labels.size(); - ruleSempreds.put(r, actionToken, i); + Map labels = pda.ruleSempreds.get(r); + int i = 0; + if ( labels!=null ) i = labels.size(); + pda.ruleSempreds.put(r, actionToken, i); return i; } @@ -82,10 +64,10 @@ public class NFABytecodeGenerator extends TreeParser { * to an index in an action. */ public int getLabelIndex(Rule r, String labelName) { - Integer I = ruleLabels.get(r, labelName); + Integer I = pda.ruleLabels.get(r, labelName); if ( I!=null ) return I; // already got its label int i = labelIndex++; - ruleLabels.put(r, labelName, i); + pda.ruleLabels.put(r, labelName, i); return i; } @@ -96,30 +78,29 @@ public class NFABytecodeGenerator extends TreeParser { } } - public byte[] getBytecode() { - Instr last = instrs.get(instrs.size() - 1); + public byte[] convertInstrsToBytecode() { + Instr last = pda.instrs.get(pda.instrs.size() - 1); int size = last.addr + last.nBytes(); byte[] code = new byte[size]; // resolve CALL instruction targets before generating code - for (Instr I : instrs) { + for (Instr I : pda.instrs) { if ( I instanceof CallInstr ) { CallInstr C = (CallInstr) I; String ruleName = C.token.getText(); - C.target = ruleToAddr.get(ruleName); + C.target = pda.ruleToAddr.get(ruleName); } } - for (Instr I : instrs) { + for (Instr I : pda.instrs) { I.write(code); } return code; } - public static PDA getBytecode(LexerGrammar lg, String modeName) { + public static CompiledPDA compileLexerMode(LexerGrammar lg, String modeName) { GrammarASTAdaptor adaptor = new GrammarASTAdaptor(); - NFABytecodeTriggers gen = new NFABytecodeTriggers(null); - gen.lg = lg; - gen.tokenTypeToAddr = new int[lg.getMaxTokenType()+1]; + PDABytecodeTriggers gen = new PDABytecodeTriggers(null); + gen.pda.tokenTypeToAddr = new int[lg.getMaxTokenType()+1]; // add split for s0 to hook up rules (fill in operands as we gen rules) int numRules = lg.modes.get(modeName).size(); @@ -135,10 +116,10 @@ public class NFABytecodeGenerator extends TreeParser { CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor,blk); gen.setTreeNodeStream(nodes); int ttype = lg.getTokenType(r.name); - gen.ruleToAddr.put(r.name, gen.ip); + gen.pda.ruleToAddr.put(r.name, gen.pda.ip); if ( !r.isFragment() ) { - s0.addrs.add(gen.ip); - gen.tokenTypeToAddr[ttype] = gen.ip; + s0.addrs.add(gen.pda.ip); + gen.pda.tokenTypeToAddr[ttype] = gen.pda.ip; } try { gen.block(); // GEN Instr OBJECTS @@ -154,11 +135,17 @@ public class NFABytecodeGenerator extends TreeParser { e.printStackTrace(System.err); } } - byte[] code = gen.getBytecode(); - System.out.println(Bytecode.disassemble(code)); - System.out.println("rule addrs="+gen.ruleToAddr); + gen.pda.code = gen.convertInstrsToBytecode(); + gen.pda.nLabels = gen.labelIndex; + System.out.println(Bytecode.disassemble(gen.pda.code)); + System.out.println("rule addrs="+gen.pda.ruleToAddr); + return gen.pda; + } - return new PDA(code, gen.ruleToAddr, gen.tokenTypeToAddr, gen.labelIndex); + // testing + public static PDA getPDA(LexerGrammar lg, String modeName) { + CompiledPDA info = compileLexerMode(lg, modeName); + return new PDA(info.code, info.ruleToAddr, info.tokenTypeToAddr, info.nLabels); } /** Write value at index into a byte array highest to lowest byte, diff --git a/tool/src/org/antlr/v4/codegen/NFABytecodeTriggers.g b/tool/src/org/antlr/v4/codegen/PDABytecodeTriggers.g similarity index 90% rename from tool/src/org/antlr/v4/codegen/NFABytecodeTriggers.g rename to tool/src/org/antlr/v4/codegen/PDABytecodeTriggers.g index 3c203c233..6d75695c8 100644 --- a/tool/src/org/antlr/v4/codegen/NFABytecodeTriggers.g +++ b/tool/src/org/antlr/v4/codegen/PDABytecodeTriggers.g @@ -1,14 +1,14 @@ -tree grammar NFABytecodeTriggers; +tree grammar PDABytecodeTriggers; options { language = Java; tokenVocab = ANTLRParser; ASTLabelType = GrammarAST; - superClass = NFABytecodeGenerator; + superClass = PDABytecodeGenerator; } @header { package org.antlr.v4.codegen; -import org.antlr.v4.codegen.nfa.*; +import org.antlr.v4.codegen.pda.*; import org.antlr.v4.tool.GrammarAST; import org.antlr.v4.tool.GrammarASTWithOptions; import org.antlr.v4.tool.LexerGrammar; @@ -29,7 +29,7 @@ block if ( nAlts>1 ) { S = new SplitInstr(nAlts); emit(S); - S.addrs.add(ip); + S.addrs.add(pda.ip); } int alt = 1; } @@ -39,13 +39,13 @@ block JumpInstr J = new JumpInstr(); jumps.add(J); emit(J); - S.addrs.add(ip); + S.addrs.add(pda.ip); } alt++; } )+ { - int END = ip; + int END = pda.ip; for (JumpInstr J : jumps) J.target = END; } ) @@ -87,17 +87,17 @@ ebnf | { SplitInstr S = new SplitInstr(2); emit(S); - S.addrs.add(ip); + S.addrs.add(pda.ip); } ^(OPTIONAL block) { - S.addrs.add(ip); + S.addrs.add(pda.ip); } | { - int start=ip; + int start=pda.ip; SplitInstr S = new SplitInstr(2); emit(S); - int blkStart = ip; + int blkStart = pda.ip; } ^(CLOSURE block) { @@ -105,14 +105,14 @@ ebnf emit(J); J.target = start; S.addrs.add(blkStart); - S.addrs.add(ip); + S.addrs.add(pda.ip); if ( greedyOption!=null && greedyOption.equals("false") ) Collections.reverse(S.addrs); } - | {int start=ip;} ^(POSITIVE_CLOSURE block) + | {int start=pda.ip;} ^(POSITIVE_CLOSURE block) { SplitInstr S = new SplitInstr(2); emit(S); - int stop = ip; + int stop = pda.ip; S.addrs.add(start); S.addrs.add(stop); if ( greedyOption!=null && greedyOption.equals("false") ) Collections.reverse(S.addrs); diff --git a/tool/src/org/antlr/v4/codegen/NFABytecodeTriggers.java b/tool/src/org/antlr/v4/codegen/PDABytecodeTriggers.java similarity index 90% rename from tool/src/org/antlr/v4/codegen/NFABytecodeTriggers.java rename to tool/src/org/antlr/v4/codegen/PDABytecodeTriggers.java index 4cb610063..cb9a6d637 100644 --- a/tool/src/org/antlr/v4/codegen/NFABytecodeTriggers.java +++ b/tool/src/org/antlr/v4/codegen/PDABytecodeTriggers.java @@ -1,11 +1,11 @@ -// $ANTLR ${project.version} ${buildNumber} NFABytecodeTriggers.g 2010-05-03 15:53:49 +// $ANTLR ${project.version} ${buildNumber} PDABytecodeTriggers.g 2010-05-17 12:41:45 package org.antlr.v4.codegen; import org.antlr.runtime.*; import org.antlr.runtime.tree.TreeNodeStream; import org.antlr.runtime.tree.TreeRuleReturnScope; -import org.antlr.v4.codegen.nfa.*; +import org.antlr.v4.codegen.pda.*; import org.antlr.v4.tool.GrammarAST; import org.antlr.v4.tool.GrammarASTWithOptions; @@ -13,7 +13,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -public class NFABytecodeTriggers extends NFABytecodeGenerator { +public class PDABytecodeTriggers extends PDABytecodeGenerator { public static final String[] tokenNames = new String[] { "", "", "", "", "SEMPRED", "FORCED_ACTION", "DOC_COMMENT", "SRC", "NLCHARS", "COMMENT", "DOUBLE_QUOTE_STRING_LITERAL", "DOUBLE_ANGLE_STRING_LITERAL", "ACTION_STRING_LITERAL", "ACTION_CHAR_LITERAL", "ARG_ACTION", "NESTED_ACTION", "ACTION", "ACTION_ESC", "WSNLCHARS", "OPTIONS", "TOKENS", "SCOPE", "IMPORT", "FRAGMENT", "LEXER", "PARSER", "TREE", "GRAMMAR", "PROTECTED", "PUBLIC", "PRIVATE", "RETURNS", "THROWS", "CATCH", "FINALLY", "TEMPLATE", "MODE", "COLON", "COLONCOLON", "COMMA", "SEMI", "LPAREN", "RPAREN", "IMPLIES", "LT", "GT", "ASSIGN", "QUESTION", "BANG", "STAR", "PLUS", "PLUS_ASSIGN", "OR", "ROOT", "DOLLAR", "DOT", "RANGE", "ETC", "RARROW", "TREE_BEGIN", "AT", "NOT", "RBRACE", "TOKEN_REF", "RULE_REF", "INT", "WSCHARS", "ESC_SEQ", "STRING_LITERAL", "HEX_DIGIT", "UNICODE_ESC", "WS", "ERRCHAR", "RULE", "RULES", "RULEMODIFIERS", "RULEACTIONS", "BLOCK", "REWRITE_BLOCK", "OPTIONAL", "CLOSURE", "POSITIVE_CLOSURE", "SYNPRED", "CHAR_RANGE", "EPSILON", "ALT", "ALTLIST", "ID", "ARG", "ARGLIST", "RET", "COMBINED", "INITACTION", "LABEL", "GATED_SEMPRED", "SYN_SEMPRED", "BACKTRACK_SEMPRED", "WILDCARD", "LIST", "ELEMENT_OPTIONS", "ST_RESULT", "RESULT", "ALT_REWRITE" }; @@ -122,36 +122,36 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // delegators - public NFABytecodeTriggers(TreeNodeStream input) { + public PDABytecodeTriggers(TreeNodeStream input) { this(input, new RecognizerSharedState()); } - public NFABytecodeTriggers(TreeNodeStream input, RecognizerSharedState state) { + public PDABytecodeTriggers(TreeNodeStream input, RecognizerSharedState state) { super(input, state); } - public String[] getTokenNames() { return NFABytecodeTriggers.tokenNames; } - public String getGrammarFileName() { return "NFABytecodeTriggers.g"; } + public String[] getTokenNames() { return PDABytecodeTriggers.tokenNames; } + public String getGrammarFileName() { return "PDABytecodeTriggers.g"; } public static class block_return extends TreeRuleReturnScope { }; // $ANTLR start "block" - // NFABytecodeTriggers.g:20:1: block : ^( BLOCK ( ^( OPTIONS ( . )+ ) )? ( alternative )+ ) ; - public final NFABytecodeTriggers.block_return block() throws RecognitionException { - NFABytecodeTriggers.block_return retval = new NFABytecodeTriggers.block_return(); + // PDABytecodeTriggers.g:20:1: block : ^( BLOCK ( ^( OPTIONS ( . )+ ) )? ( alternative )+ ) ; + public final PDABytecodeTriggers.block_return block() throws RecognitionException { + PDABytecodeTriggers.block_return retval = new PDABytecodeTriggers.block_return(); retval.start = input.LT(1); try { - // NFABytecodeTriggers.g:21:5: ( ^( BLOCK ( ^( OPTIONS ( . )+ ) )? ( alternative )+ ) ) - // NFABytecodeTriggers.g:21:7: ^( BLOCK ( ^( OPTIONS ( . )+ ) )? ( alternative )+ ) + // PDABytecodeTriggers.g:21:5: ( ^( BLOCK ( ^( OPTIONS ( . )+ ) )? ( alternative )+ ) ) + // PDABytecodeTriggers.g:21:7: ^( BLOCK ( ^( OPTIONS ( . )+ ) )? ( alternative )+ ) { match(input,BLOCK,FOLLOW_BLOCK_in_block68); match(input, Token.DOWN, null); - // NFABytecodeTriggers.g:21:16: ( ^( OPTIONS ( . )+ ) )? + // PDABytecodeTriggers.g:21:16: ( ^( OPTIONS ( . )+ ) )? int alt2=2; int LA2_0 = input.LA(1); @@ -160,12 +160,12 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } switch (alt2) { case 1 : - // NFABytecodeTriggers.g:21:17: ^( OPTIONS ( . )+ ) + // PDABytecodeTriggers.g:21:17: ^( OPTIONS ( . )+ ) { match(input,OPTIONS,FOLLOW_OPTIONS_in_block72); match(input, Token.DOWN, null); - // NFABytecodeTriggers.g:21:27: ( . )+ + // PDABytecodeTriggers.g:21:27: ( . )+ int cnt1=0; loop1: do { @@ -182,7 +182,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { switch (alt1) { case 1 : - // NFABytecodeTriggers.g:21:27: . + // PDABytecodeTriggers.g:21:27: . { matchAny(input); @@ -216,11 +216,11 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { if ( nAlts>1 ) { S = new SplitInstr(nAlts); emit(S); - S.addrs.add(ip); + S.addrs.add(pda.ip); } int alt = 1; - // NFABytecodeTriggers.g:36:7: ( alternative )+ + // PDABytecodeTriggers.g:36:7: ( alternative )+ int cnt3=0; loop3: do { @@ -234,7 +234,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { switch (alt3) { case 1 : - // NFABytecodeTriggers.g:36:9: alternative + // PDABytecodeTriggers.g:36:9: alternative { pushFollow(FOLLOW_alternative_in_block96); alternative(); @@ -246,7 +246,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { JumpInstr J = new JumpInstr(); jumps.add(J); emit(J); - S.addrs.add(ip); + S.addrs.add(pda.ip); } alt++; @@ -264,7 +264,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } while (true); - int END = ip; + int END = pda.ip; for (JumpInstr J : jumps) J.target = END; @@ -285,10 +285,10 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // $ANTLR start "alternative" - // NFABytecodeTriggers.g:54:1: alternative : ( ^( ALT_REWRITE a= alternative . ) | ^( ALT EPSILON ) | ^( ALT (e= element )+ ) ); + // PDABytecodeTriggers.g:54:1: alternative : ( ^( ALT_REWRITE a= alternative . ) | ^( ALT EPSILON ) | ^( ALT (e= element )+ ) ); public final void alternative() throws RecognitionException { try { - // NFABytecodeTriggers.g:55:5: ( ^( ALT_REWRITE a= alternative . ) | ^( ALT EPSILON ) | ^( ALT (e= element )+ ) ) + // PDABytecodeTriggers.g:55:5: ( ^( ALT_REWRITE a= alternative . ) | ^( ALT EPSILON ) | ^( ALT (e= element )+ ) ) int alt5=3; int LA5_0 = input.LA(1); @@ -329,7 +329,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } switch (alt5) { case 1 : - // NFABytecodeTriggers.g:55:7: ^( ALT_REWRITE a= alternative . ) + // PDABytecodeTriggers.g:55:7: ^( ALT_REWRITE a= alternative . ) { match(input,ALT_REWRITE,FOLLOW_ALT_REWRITE_in_alternative147); @@ -346,7 +346,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 2 : - // NFABytecodeTriggers.g:56:7: ^( ALT EPSILON ) + // PDABytecodeTriggers.g:56:7: ^( ALT EPSILON ) { match(input,ALT,FOLLOW_ALT_in_alternative164); @@ -358,12 +358,12 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 3 : - // NFABytecodeTriggers.g:57:9: ^( ALT (e= element )+ ) + // PDABytecodeTriggers.g:57:9: ^( ALT (e= element )+ ) { match(input,ALT,FOLLOW_ALT_in_alternative183); match(input, Token.DOWN, null); - // NFABytecodeTriggers.g:57:15: (e= element )+ + // PDABytecodeTriggers.g:57:15: (e= element )+ int cnt4=0; loop4: do { @@ -377,7 +377,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { switch (alt4) { case 1 : - // NFABytecodeTriggers.g:57:16: e= element + // PDABytecodeTriggers.g:57:16: e= element { pushFollow(FOLLOW_element_in_alternative188); element(); @@ -417,19 +417,19 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // $ANTLR start "element" - // NFABytecodeTriggers.g:60:1: element : ( labeledElement | atom | ebnf | ACTION | SEMPRED | GATED_SEMPRED | treeSpec ); + // PDABytecodeTriggers.g:60:1: element : ( labeledElement | atom | ebnf | ACTION | SEMPRED | GATED_SEMPRED | treeSpec ); public final void element() throws RecognitionException { GrammarAST ACTION1=null; GrammarAST SEMPRED2=null; GrammarAST GATED_SEMPRED3=null; try { - // NFABytecodeTriggers.g:61:2: ( labeledElement | atom | ebnf | ACTION | SEMPRED | GATED_SEMPRED | treeSpec ) + // PDABytecodeTriggers.g:61:2: ( labeledElement | atom | ebnf | ACTION | SEMPRED | GATED_SEMPRED | treeSpec ) int alt6=7; alt6 = dfa6.predict(input); switch (alt6) { case 1 : - // NFABytecodeTriggers.g:61:4: labeledElement + // PDABytecodeTriggers.g:61:4: labeledElement { pushFollow(FOLLOW_labeledElement_in_element219); labeledElement(); @@ -440,7 +440,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 2 : - // NFABytecodeTriggers.g:62:4: atom + // PDABytecodeTriggers.g:62:4: atom { pushFollow(FOLLOW_atom_in_element228); atom(); @@ -451,7 +451,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 3 : - // NFABytecodeTriggers.g:63:4: ebnf + // PDABytecodeTriggers.g:63:4: ebnf { pushFollow(FOLLOW_ebnf_in_element239); ebnf(); @@ -462,7 +462,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 4 : - // NFABytecodeTriggers.g:64:6: ACTION + // PDABytecodeTriggers.g:64:6: ACTION { ACTION1=(GrammarAST)match(input,ACTION,FOLLOW_ACTION_in_element252); emit(new ActionInstr(ACTION1.token)); @@ -470,7 +470,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 5 : - // NFABytecodeTriggers.g:65:6: SEMPRED + // PDABytecodeTriggers.g:65:6: SEMPRED { SEMPRED2=(GrammarAST)match(input,SEMPRED,FOLLOW_SEMPRED_in_element266); emit(new SemPredInstr(SEMPRED2.token)); @@ -478,7 +478,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 6 : - // NFABytecodeTriggers.g:66:4: GATED_SEMPRED + // PDABytecodeTriggers.g:66:4: GATED_SEMPRED { GATED_SEMPRED3=(GrammarAST)match(input,GATED_SEMPRED,FOLLOW_GATED_SEMPRED_in_element277); emit(new SemPredInstr(GATED_SEMPRED3.token)); @@ -486,7 +486,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 7 : - // NFABytecodeTriggers.g:67:4: treeSpec + // PDABytecodeTriggers.g:67:4: treeSpec { pushFollow(FOLLOW_treeSpec_in_element284); treeSpec(); @@ -511,17 +511,17 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // $ANTLR start "labeledElement" - // NFABytecodeTriggers.g:70:1: labeledElement : ( ^( ASSIGN ID atom ) | ^( ASSIGN ID block ) | ^( PLUS_ASSIGN ID atom ) | ^( PLUS_ASSIGN ID block ) ); + // PDABytecodeTriggers.g:70:1: labeledElement : ( ^( ASSIGN ID atom ) | ^( ASSIGN ID block ) | ^( PLUS_ASSIGN ID atom ) | ^( PLUS_ASSIGN ID block ) ); public final void labeledElement() throws RecognitionException { GrammarAST ID4=null; try { - // NFABytecodeTriggers.g:71:2: ( ^( ASSIGN ID atom ) | ^( ASSIGN ID block ) | ^( PLUS_ASSIGN ID atom ) | ^( PLUS_ASSIGN ID block ) ) + // PDABytecodeTriggers.g:71:2: ( ^( ASSIGN ID atom ) | ^( ASSIGN ID block ) | ^( PLUS_ASSIGN ID atom ) | ^( PLUS_ASSIGN ID block ) ) int alt7=4; alt7 = dfa7.predict(input); switch (alt7) { case 1 : - // NFABytecodeTriggers.g:71:4: ^( ASSIGN ID atom ) + // PDABytecodeTriggers.g:71:4: ^( ASSIGN ID atom ) { match(input,ASSIGN,FOLLOW_ASSIGN_in_labeledElement302); @@ -540,7 +540,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 2 : - // NFABytecodeTriggers.g:72:4: ^( ASSIGN ID block ) + // PDABytecodeTriggers.g:72:4: ^( ASSIGN ID block ) { match(input,ASSIGN,FOLLOW_ASSIGN_in_labeledElement318); @@ -557,7 +557,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 3 : - // NFABytecodeTriggers.g:73:4: ^( PLUS_ASSIGN ID atom ) + // PDABytecodeTriggers.g:73:4: ^( PLUS_ASSIGN ID atom ) { match(input,PLUS_ASSIGN,FOLLOW_PLUS_ASSIGN_in_labeledElement332); @@ -574,7 +574,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 4 : - // NFABytecodeTriggers.g:74:4: ^( PLUS_ASSIGN ID block ) + // PDABytecodeTriggers.g:74:4: ^( PLUS_ASSIGN ID block ) { match(input,PLUS_ASSIGN,FOLLOW_PLUS_ASSIGN_in_labeledElement345); @@ -605,16 +605,16 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // $ANTLR start "treeSpec" - // NFABytecodeTriggers.g:77:1: treeSpec : ^( TREE_BEGIN (e= element )+ ) ; + // PDABytecodeTriggers.g:77:1: treeSpec : ^( TREE_BEGIN (e= element )+ ) ; public final void treeSpec() throws RecognitionException { try { - // NFABytecodeTriggers.g:78:5: ( ^( TREE_BEGIN (e= element )+ ) ) - // NFABytecodeTriggers.g:78:7: ^( TREE_BEGIN (e= element )+ ) + // PDABytecodeTriggers.g:78:5: ( ^( TREE_BEGIN (e= element )+ ) ) + // PDABytecodeTriggers.g:78:7: ^( TREE_BEGIN (e= element )+ ) { match(input,TREE_BEGIN,FOLLOW_TREE_BEGIN_in_treeSpec367); match(input, Token.DOWN, null); - // NFABytecodeTriggers.g:78:21: (e= element )+ + // PDABytecodeTriggers.g:78:21: (e= element )+ int cnt8=0; loop8: do { @@ -628,7 +628,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { switch (alt8) { case 1 : - // NFABytecodeTriggers.g:78:22: e= element + // PDABytecodeTriggers.g:78:22: e= element { pushFollow(FOLLOW_element_in_treeSpec373); element(); @@ -668,9 +668,9 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { }; // $ANTLR start "ebnf" - // NFABytecodeTriggers.g:81:1: ebnf : ( ^( astBlockSuffix block ) | ^( OPTIONAL block ) | ^( CLOSURE block ) | ^( POSITIVE_CLOSURE block ) | block ); - public final NFABytecodeTriggers.ebnf_return ebnf() throws RecognitionException { - NFABytecodeTriggers.ebnf_return retval = new NFABytecodeTriggers.ebnf_return(); + // PDABytecodeTriggers.g:81:1: ebnf : ( ^( astBlockSuffix block ) | ^( OPTIONAL block ) | ^( CLOSURE block ) | ^( POSITIVE_CLOSURE block ) | block ); + public final PDABytecodeTriggers.ebnf_return ebnf() throws RecognitionException { + PDABytecodeTriggers.ebnf_return retval = new PDABytecodeTriggers.ebnf_return(); retval.start = input.LT(1); @@ -678,7 +678,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { String greedyOption = blk.getOption("greedy"); try { - // NFABytecodeTriggers.g:86:2: ( ^( astBlockSuffix block ) | ^( OPTIONAL block ) | ^( CLOSURE block ) | ^( POSITIVE_CLOSURE block ) | block ) + // PDABytecodeTriggers.g:86:2: ( ^( astBlockSuffix block ) | ^( OPTIONAL block ) | ^( CLOSURE block ) | ^( POSITIVE_CLOSURE block ) | block ) int alt9=5; switch ( input.LA(1) ) { case IMPLIES: @@ -717,7 +717,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { switch (alt9) { case 1 : - // NFABytecodeTriggers.g:86:4: ^( astBlockSuffix block ) + // PDABytecodeTriggers.g:86:4: ^( astBlockSuffix block ) { pushFollow(FOLLOW_astBlockSuffix_in_ebnf398); astBlockSuffix(); @@ -737,12 +737,12 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 2 : - // NFABytecodeTriggers.g:87:4: ^( OPTIONAL block ) + // PDABytecodeTriggers.g:87:4: ^( OPTIONAL block ) { SplitInstr S = new SplitInstr(2); emit(S); - S.addrs.add(ip); + S.addrs.add(pda.ip); match(input,OPTIONAL,FOLLOW_OPTIONAL_in_ebnf413); @@ -755,19 +755,19 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { match(input, Token.UP, null); - S.addrs.add(ip); + S.addrs.add(pda.ip); } break; case 3 : - // NFABytecodeTriggers.g:96:4: ^( CLOSURE block ) + // PDABytecodeTriggers.g:96:4: ^( CLOSURE block ) { - int start=ip; + int start=pda.ip; SplitInstr S = new SplitInstr(2); emit(S); - int blkStart = ip; + int blkStart = pda.ip; match(input,CLOSURE,FOLLOW_CLOSURE_in_ebnf433); @@ -784,16 +784,16 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { emit(J); J.target = start; S.addrs.add(blkStart); - S.addrs.add(ip); + S.addrs.add(pda.ip); if ( greedyOption!=null && greedyOption.equals("false") ) Collections.reverse(S.addrs); } break; case 4 : - // NFABytecodeTriggers.g:111:4: ^( POSITIVE_CLOSURE block ) + // PDABytecodeTriggers.g:111:4: ^( POSITIVE_CLOSURE block ) { - int start=ip; + int start=pda.ip; match(input,POSITIVE_CLOSURE,FOLLOW_POSITIVE_CLOSURE_in_ebnf451); match(input, Token.DOWN, null); @@ -807,7 +807,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { SplitInstr S = new SplitInstr(2); emit(S); - int stop = ip; + int stop = pda.ip; S.addrs.add(start); S.addrs.add(stop); if ( greedyOption!=null && greedyOption.equals("false") ) Collections.reverse(S.addrs); @@ -816,7 +816,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 5 : - // NFABytecodeTriggers.g:120:5: block + // PDABytecodeTriggers.g:120:5: block { pushFollow(FOLLOW_block_in_ebnf464); block(); @@ -841,11 +841,11 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // $ANTLR start "astBlockSuffix" - // NFABytecodeTriggers.g:123:1: astBlockSuffix : ( ROOT | IMPLIES | BANG ); + // PDABytecodeTriggers.g:123:1: astBlockSuffix : ( ROOT | IMPLIES | BANG ); public final void astBlockSuffix() throws RecognitionException { try { - // NFABytecodeTriggers.g:124:5: ( ROOT | IMPLIES | BANG ) - // NFABytecodeTriggers.g: + // PDABytecodeTriggers.g:124:5: ( ROOT | IMPLIES | BANG ) + // PDABytecodeTriggers.g: { if ( input.LA(1)==IMPLIES||input.LA(1)==BANG||input.LA(1)==ROOT ) { input.consume(); @@ -872,18 +872,18 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // $ANTLR start "atom" - // NFABytecodeTriggers.g:129:1: atom : ( ^( ROOT range ) | ^( BANG range ) | ^( ROOT notSet ) | ^( BANG notSet ) | notSet | range | ^( DOT ID terminal ) | ^( DOT ID ruleref ) | ^( WILDCARD . ) | WILDCARD | terminal | ruleref ); + // PDABytecodeTriggers.g:129:1: atom : ( ^( ROOT range ) | ^( BANG range ) | ^( ROOT notSet ) | ^( BANG notSet ) | notSet | range | ^( DOT ID terminal ) | ^( DOT ID ruleref ) | ^( WILDCARD . ) | WILDCARD | terminal | ruleref ); public final void atom() throws RecognitionException { GrammarAST WILDCARD5=null; GrammarAST WILDCARD6=null; try { - // NFABytecodeTriggers.g:130:2: ( ^( ROOT range ) | ^( BANG range ) | ^( ROOT notSet ) | ^( BANG notSet ) | notSet | range | ^( DOT ID terminal ) | ^( DOT ID ruleref ) | ^( WILDCARD . ) | WILDCARD | terminal | ruleref ) + // PDABytecodeTriggers.g:130:2: ( ^( ROOT range ) | ^( BANG range ) | ^( ROOT notSet ) | ^( BANG notSet ) | notSet | range | ^( DOT ID terminal ) | ^( DOT ID ruleref ) | ^( WILDCARD . ) | WILDCARD | terminal | ruleref ) int alt10=12; alt10 = dfa10.predict(input); switch (alt10) { case 1 : - // NFABytecodeTriggers.g:130:4: ^( ROOT range ) + // PDABytecodeTriggers.g:130:4: ^( ROOT range ) { match(input,ROOT,FOLLOW_ROOT_in_atom518); @@ -899,7 +899,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 2 : - // NFABytecodeTriggers.g:131:4: ^( BANG range ) + // PDABytecodeTriggers.g:131:4: ^( BANG range ) { match(input,BANG,FOLLOW_BANG_in_atom530); @@ -915,7 +915,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 3 : - // NFABytecodeTriggers.g:132:4: ^( ROOT notSet ) + // PDABytecodeTriggers.g:132:4: ^( ROOT notSet ) { match(input,ROOT,FOLLOW_ROOT_in_atom542); @@ -931,7 +931,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 4 : - // NFABytecodeTriggers.g:133:4: ^( BANG notSet ) + // PDABytecodeTriggers.g:133:4: ^( BANG notSet ) { match(input,BANG,FOLLOW_BANG_in_atom554); @@ -947,7 +947,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 5 : - // NFABytecodeTriggers.g:134:4: notSet + // PDABytecodeTriggers.g:134:4: notSet { pushFollow(FOLLOW_notSet_in_atom565); notSet(); @@ -958,7 +958,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 6 : - // NFABytecodeTriggers.g:135:4: range + // PDABytecodeTriggers.g:135:4: range { pushFollow(FOLLOW_range_in_atom575); range(); @@ -969,7 +969,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 7 : - // NFABytecodeTriggers.g:136:4: ^( DOT ID terminal ) + // PDABytecodeTriggers.g:136:4: ^( DOT ID terminal ) { match(input,DOT,FOLLOW_DOT_in_atom586); @@ -986,7 +986,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 8 : - // NFABytecodeTriggers.g:137:4: ^( DOT ID ruleref ) + // PDABytecodeTriggers.g:137:4: ^( DOT ID ruleref ) { match(input,DOT,FOLLOW_DOT_in_atom599); @@ -1003,7 +1003,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 9 : - // NFABytecodeTriggers.g:138:7: ^( WILDCARD . ) + // PDABytecodeTriggers.g:138:7: ^( WILDCARD . ) { WILDCARD5=(GrammarAST)match(input,WILDCARD,FOLLOW_WILDCARD_in_atom615); @@ -1016,7 +1016,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 10 : - // NFABytecodeTriggers.g:139:7: WILDCARD + // PDABytecodeTriggers.g:139:7: WILDCARD { WILDCARD6=(GrammarAST)match(input,WILDCARD,FOLLOW_WILDCARD_in_atom631); emit(new WildcardInstr(WILDCARD6.token)); @@ -1024,7 +1024,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 11 : - // NFABytecodeTriggers.g:140:9: terminal + // PDABytecodeTriggers.g:140:9: terminal { pushFollow(FOLLOW_terminal_in_atom646); terminal(); @@ -1035,7 +1035,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 12 : - // NFABytecodeTriggers.g:141:9: ruleref + // PDABytecodeTriggers.g:141:9: ruleref { pushFollow(FOLLOW_ruleref_in_atom660); ruleref(); @@ -1060,10 +1060,10 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // $ANTLR start "notSet" - // NFABytecodeTriggers.g:144:1: notSet : ( ^( NOT terminal ) | ^( NOT block ) ); + // PDABytecodeTriggers.g:144:1: notSet : ( ^( NOT terminal ) | ^( NOT block ) ); public final void notSet() throws RecognitionException { try { - // NFABytecodeTriggers.g:145:5: ( ^( NOT terminal ) | ^( NOT block ) ) + // PDABytecodeTriggers.g:145:5: ( ^( NOT terminal ) | ^( NOT block ) ) int alt11=2; int LA11_0 = input.LA(1); @@ -1101,7 +1101,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } switch (alt11) { case 1 : - // NFABytecodeTriggers.g:145:7: ^( NOT terminal ) + // PDABytecodeTriggers.g:145:7: ^( NOT terminal ) { match(input,NOT,FOLLOW_NOT_in_notSet683); @@ -1117,7 +1117,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 2 : - // NFABytecodeTriggers.g:146:7: ^( NOT block ) + // PDABytecodeTriggers.g:146:7: ^( NOT block ) { match(input,NOT,FOLLOW_NOT_in_notSet697); @@ -1147,10 +1147,10 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // $ANTLR start "ruleref" - // NFABytecodeTriggers.g:149:1: ruleref : ( ^( ROOT ^( RULE_REF ( ARG_ACTION )? ) ) | ^( BANG ^( RULE_REF ( ARG_ACTION )? ) ) | ^( RULE_REF ( ARG_ACTION )? ) ); + // PDABytecodeTriggers.g:149:1: ruleref : ( ^( ROOT ^( RULE_REF ( ARG_ACTION )? ) ) | ^( BANG ^( RULE_REF ( ARG_ACTION )? ) ) | ^( RULE_REF ( ARG_ACTION )? ) ); public final void ruleref() throws RecognitionException { try { - // NFABytecodeTriggers.g:150:5: ( ^( ROOT ^( RULE_REF ( ARG_ACTION )? ) ) | ^( BANG ^( RULE_REF ( ARG_ACTION )? ) ) | ^( RULE_REF ( ARG_ACTION )? ) ) + // PDABytecodeTriggers.g:150:5: ( ^( ROOT ^( RULE_REF ( ARG_ACTION )? ) ) | ^( BANG ^( RULE_REF ( ARG_ACTION )? ) ) | ^( RULE_REF ( ARG_ACTION )? ) ) int alt15=3; switch ( input.LA(1) ) { case ROOT: @@ -1177,7 +1177,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { switch (alt15) { case 1 : - // NFABytecodeTriggers.g:150:7: ^( ROOT ^( RULE_REF ( ARG_ACTION )? ) ) + // PDABytecodeTriggers.g:150:7: ^( ROOT ^( RULE_REF ( ARG_ACTION )? ) ) { match(input,ROOT,FOLLOW_ROOT_in_ruleref721); @@ -1186,7 +1186,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { if ( input.LA(1)==Token.DOWN ) { match(input, Token.DOWN, null); - // NFABytecodeTriggers.g:150:25: ( ARG_ACTION )? + // PDABytecodeTriggers.g:150:25: ( ARG_ACTION )? int alt12=2; int LA12_0 = input.LA(1); @@ -1195,7 +1195,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } switch (alt12) { case 1 : - // NFABytecodeTriggers.g:150:25: ARG_ACTION + // PDABytecodeTriggers.g:150:25: ARG_ACTION { match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_ruleref726); @@ -1213,7 +1213,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 2 : - // NFABytecodeTriggers.g:151:7: ^( BANG ^( RULE_REF ( ARG_ACTION )? ) ) + // PDABytecodeTriggers.g:151:7: ^( BANG ^( RULE_REF ( ARG_ACTION )? ) ) { match(input,BANG,FOLLOW_BANG_in_ruleref739); @@ -1222,7 +1222,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { if ( input.LA(1)==Token.DOWN ) { match(input, Token.DOWN, null); - // NFABytecodeTriggers.g:151:25: ( ARG_ACTION )? + // PDABytecodeTriggers.g:151:25: ( ARG_ACTION )? int alt13=2; int LA13_0 = input.LA(1); @@ -1231,7 +1231,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } switch (alt13) { case 1 : - // NFABytecodeTriggers.g:151:25: ARG_ACTION + // PDABytecodeTriggers.g:151:25: ARG_ACTION { match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_ruleref744); @@ -1249,13 +1249,13 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 3 : - // NFABytecodeTriggers.g:152:7: ^( RULE_REF ( ARG_ACTION )? ) + // PDABytecodeTriggers.g:152:7: ^( RULE_REF ( ARG_ACTION )? ) { match(input,RULE_REF,FOLLOW_RULE_REF_in_ruleref757); if ( input.LA(1)==Token.DOWN ) { match(input, Token.DOWN, null); - // NFABytecodeTriggers.g:152:18: ( ARG_ACTION )? + // PDABytecodeTriggers.g:152:18: ( ARG_ACTION )? int alt14=2; int LA14_0 = input.LA(1); @@ -1264,7 +1264,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } switch (alt14) { case 1 : - // NFABytecodeTriggers.g:152:18: ARG_ACTION + // PDABytecodeTriggers.g:152:18: ARG_ACTION { match(input,ARG_ACTION,FOLLOW_ARG_ACTION_in_ruleref759); @@ -1294,14 +1294,14 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // $ANTLR start "range" - // NFABytecodeTriggers.g:155:1: range : ^( RANGE a= STRING_LITERAL b= STRING_LITERAL ) ; + // PDABytecodeTriggers.g:155:1: range : ^( RANGE a= STRING_LITERAL b= STRING_LITERAL ) ; public final void range() throws RecognitionException { GrammarAST a=null; GrammarAST b=null; try { - // NFABytecodeTriggers.g:156:5: ( ^( RANGE a= STRING_LITERAL b= STRING_LITERAL ) ) - // NFABytecodeTriggers.g:156:7: ^( RANGE a= STRING_LITERAL b= STRING_LITERAL ) + // PDABytecodeTriggers.g:156:5: ( ^( RANGE a= STRING_LITERAL b= STRING_LITERAL ) ) + // PDABytecodeTriggers.g:156:7: ^( RANGE a= STRING_LITERAL b= STRING_LITERAL ) { match(input,RANGE,FOLLOW_RANGE_in_range782); @@ -1327,7 +1327,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { // $ANTLR start "terminal" - // NFABytecodeTriggers.g:160:1: terminal : ( ^( STRING_LITERAL . ) | STRING_LITERAL | ^( TOKEN_REF ARG_ACTION . ) | ^( TOKEN_REF . ) | TOKEN_REF | ^( ROOT terminal ) | ^( BANG terminal ) ); + // PDABytecodeTriggers.g:160:1: terminal : ( ^( STRING_LITERAL . ) | STRING_LITERAL | ^( TOKEN_REF ARG_ACTION . ) | ^( TOKEN_REF . ) | TOKEN_REF | ^( ROOT terminal ) | ^( BANG terminal ) ); public final void terminal() throws RecognitionException { GrammarAST STRING_LITERAL7=null; GrammarAST STRING_LITERAL8=null; @@ -1336,12 +1336,12 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { GrammarAST TOKEN_REF11=null; try { - // NFABytecodeTriggers.g:161:5: ( ^( STRING_LITERAL . ) | STRING_LITERAL | ^( TOKEN_REF ARG_ACTION . ) | ^( TOKEN_REF . ) | TOKEN_REF | ^( ROOT terminal ) | ^( BANG terminal ) ) + // PDABytecodeTriggers.g:161:5: ( ^( STRING_LITERAL . ) | STRING_LITERAL | ^( TOKEN_REF ARG_ACTION . ) | ^( TOKEN_REF . ) | TOKEN_REF | ^( ROOT terminal ) | ^( BANG terminal ) ) int alt16=7; alt16 = dfa16.predict(input); switch (alt16) { case 1 : - // NFABytecodeTriggers.g:161:8: ^( STRING_LITERAL . ) + // PDABytecodeTriggers.g:161:8: ^( STRING_LITERAL . ) { STRING_LITERAL7=(GrammarAST)match(input,STRING_LITERAL,FOLLOW_STRING_LITERAL_in_terminal817); @@ -1354,7 +1354,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 2 : - // NFABytecodeTriggers.g:162:7: STRING_LITERAL + // PDABytecodeTriggers.g:162:7: STRING_LITERAL { STRING_LITERAL8=(GrammarAST)match(input,STRING_LITERAL,FOLLOW_STRING_LITERAL_in_terminal832); emitString(STRING_LITERAL8.token); @@ -1362,7 +1362,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 3 : - // NFABytecodeTriggers.g:163:7: ^( TOKEN_REF ARG_ACTION . ) + // PDABytecodeTriggers.g:163:7: ^( TOKEN_REF ARG_ACTION . ) { TOKEN_REF9=(GrammarAST)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_terminal846); @@ -1376,7 +1376,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 4 : - // NFABytecodeTriggers.g:164:7: ^( TOKEN_REF . ) + // PDABytecodeTriggers.g:164:7: ^( TOKEN_REF . ) { TOKEN_REF10=(GrammarAST)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_terminal862); @@ -1389,7 +1389,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 5 : - // NFABytecodeTriggers.g:165:7: TOKEN_REF + // PDABytecodeTriggers.g:165:7: TOKEN_REF { TOKEN_REF11=(GrammarAST)match(input,TOKEN_REF,FOLLOW_TOKEN_REF_in_terminal878); emit(new CallInstr(TOKEN_REF11.token)); @@ -1397,7 +1397,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 6 : - // NFABytecodeTriggers.g:166:7: ^( ROOT terminal ) + // PDABytecodeTriggers.g:166:7: ^( ROOT terminal ) { match(input,ROOT,FOLLOW_ROOT_in_terminal893); @@ -1413,7 +1413,7 @@ public class NFABytecodeTriggers extends NFABytecodeGenerator { } break; case 7 : - // NFABytecodeTriggers.g:167:7: ^( BANG terminal ) + // PDABytecodeTriggers.g:167:7: ^( BANG terminal ) { match(input,BANG,FOLLOW_BANG_in_terminal908); diff --git a/tool/src/org/antlr/v4/codegen/NFABytecodeTriggers.tokens b/tool/src/org/antlr/v4/codegen/PDABytecodeTriggers.tokens similarity index 100% rename from tool/src/org/antlr/v4/codegen/NFABytecodeTriggers.tokens rename to tool/src/org/antlr/v4/codegen/PDABytecodeTriggers.tokens diff --git a/tool/src/org/antlr/v4/codegen/SourceGenTriggers.tokens b/tool/src/org/antlr/v4/codegen/SourceGenTriggers.tokens new file mode 100644 index 000000000..d21b1abba --- /dev/null +++ b/tool/src/org/antlr/v4/codegen/SourceGenTriggers.tokens @@ -0,0 +1,99 @@ +COMBINED=91 +LT=44 +STAR=49 +BACKTRACK_SEMPRED=96 +DOUBLE_ANGLE_STRING_LITERAL=11 +FORCED_ACTION=5 +ARGLIST=89 +ALTLIST=86 +NOT=61 +SEMPRED=4 +ACTION=16 +TOKEN_REF=63 +RULEMODIFIERS=75 +ST_RESULT=100 +RPAREN=42 +RET=90 +IMPORT=22 +STRING_LITERAL=68 +ARG=88 +ARG_ACTION=14 +DOUBLE_QUOTE_STRING_LITERAL=10 +COMMENT=9 +ACTION_CHAR_LITERAL=13 +GRAMMAR=27 +RULEACTIONS=76 +WSCHARS=66 +INITACTION=92 +ALT_REWRITE=102 +IMPLIES=43 +RULE=73 +RBRACE=62 +ACTION_ESC=17 +PRIVATE=30 +SRC=7 +THROWS=32 +CHAR_RANGE=83 +INT=65 +EPSILON=84 +LIST=98 +COLONCOLON=38 +WSNLCHARS=18 +WS=71 +LEXER=24 +OR=52 +GT=45 +CATCH=33 +CLOSURE=80 +PARSER=25 +DOLLAR=54 +PROTECTED=28 +ELEMENT_OPTIONS=99 +NESTED_ACTION=15 +FRAGMENT=23 +ID=87 +TREE_BEGIN=59 +LPAREN=41 +AT=60 +ESC_SEQ=67 +ALT=85 +TREE=26 +SCOPE=21 +ETC=57 +COMMA=39 +WILDCARD=97 +DOC_COMMENT=6 +PLUS=50 +REWRITE_BLOCK=78 +DOT=55 +MODE=36 +RETURNS=31 +RULES=74 +RARROW=58 +UNICODE_ESC=70 +HEX_DIGIT=69 +RANGE=56 +TOKENS=20 +RESULT=101 +GATED_SEMPRED=94 +BANG=48 +ACTION_STRING_LITERAL=12 +ROOT=53 +SEMI=40 +RULE_REF=64 +NLCHARS=8 +OPTIONAL=79 +SYNPRED=82 +COLON=37 +QUESTION=47 +FINALLY=34 +TEMPLATE=35 +LABEL=93 +SYN_SEMPRED=95 +ERRCHAR=72 +BLOCK=77 +ASSIGN=46 +PLUS_ASSIGN=51 +PUBLIC=29 +POSITIVE_CLOSURE=81 +OPTIONS=19 diff --git a/tool/src/org/antlr/v4/codegen/nfa/AcceptInstr.java b/tool/src/org/antlr/v4/codegen/pda/AcceptInstr.java similarity index 73% rename from tool/src/org/antlr/v4/codegen/nfa/AcceptInstr.java rename to tool/src/org/antlr/v4/codegen/pda/AcceptInstr.java index 1dbedded2..fbf44c093 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/AcceptInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/AcceptInstr.java @@ -1,6 +1,6 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.Bytecode; /** */ @@ -13,7 +13,7 @@ public class AcceptInstr extends Instr { public int nBytes() { return 1+2; } public void write(byte[] code) { super.write(code); - NFABytecodeGenerator.writeShort(code, addr+1, (short)ruleIndex); + PDABytecodeGenerator.writeShort(code, addr+1, (short)ruleIndex); } public String toString() { return addr+":AcceptInstr "+ruleIndex; } } diff --git a/tool/src/org/antlr/v4/codegen/nfa/ActionInstr.java b/tool/src/org/antlr/v4/codegen/pda/ActionInstr.java similarity index 69% rename from tool/src/org/antlr/v4/codegen/nfa/ActionInstr.java rename to tool/src/org/antlr/v4/codegen/pda/ActionInstr.java index c2d63bb48..95178dd7e 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/ActionInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/ActionInstr.java @@ -1,7 +1,7 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; import org.antlr.runtime.Token; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.Bytecode; /** */ @@ -15,8 +15,8 @@ public class ActionInstr extends Instr { public int nBytes() { return 1+2*2; } public void write(byte[] code) { super.write(code); - NFABytecodeGenerator.writeShort(code, addr+1, (short)rule.index); - NFABytecodeGenerator.writeShort(code, addr+1+2, (short)gen.getActionIndex(rule, token)); + PDABytecodeGenerator.writeShort(code, addr+1, (short)rule.index); + PDABytecodeGenerator.writeShort(code, addr+1+2, (short)gen.getActionIndex(rule, token)); } public String toString() { return addr+":ActionInstr "+actionIndex; } } diff --git a/tool/src/org/antlr/v4/codegen/nfa/CallInstr.java b/tool/src/org/antlr/v4/codegen/pda/CallInstr.java similarity index 76% rename from tool/src/org/antlr/v4/codegen/nfa/CallInstr.java rename to tool/src/org/antlr/v4/codegen/pda/CallInstr.java index b8a02fc0a..45157843c 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/CallInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/CallInstr.java @@ -1,7 +1,7 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; import org.antlr.runtime.Token; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.Bytecode; /** */ @@ -13,7 +13,7 @@ public class CallInstr extends Instr { public int nBytes() { return 1+Bytecode.ADDR_SIZE; } public void write(byte[] code) { super.write(code); - NFABytecodeGenerator.writeShort(code, addr+1, (short)target); + PDABytecodeGenerator.writeShort(code, addr+1, (short)target); } @Override diff --git a/tool/src/org/antlr/v4/codegen/nfa/Instr.java b/tool/src/org/antlr/v4/codegen/pda/Instr.java similarity index 78% rename from tool/src/org/antlr/v4/codegen/nfa/Instr.java rename to tool/src/org/antlr/v4/codegen/pda/Instr.java index 2707375d0..fed848fa0 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/Instr.java +++ b/tool/src/org/antlr/v4/codegen/pda/Instr.java @@ -1,13 +1,13 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.tool.Rule; /** */ public abstract class Instr { public int addr; public Rule rule; - public NFABytecodeGenerator gen; + public PDABytecodeGenerator gen; public abstract short opcode(); public abstract int nBytes(); diff --git a/tool/src/org/antlr/v4/codegen/nfa/JumpInstr.java b/tool/src/org/antlr/v4/codegen/pda/JumpInstr.java similarity index 72% rename from tool/src/org/antlr/v4/codegen/nfa/JumpInstr.java rename to tool/src/org/antlr/v4/codegen/pda/JumpInstr.java index ca0c50851..d116dd2e7 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/JumpInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/JumpInstr.java @@ -1,6 +1,6 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.Bytecode; /** */ @@ -10,7 +10,7 @@ public class JumpInstr extends Instr { public int nBytes() { return 1+Bytecode.ADDR_SIZE; } public void write(byte[] code) { super.write(code); - NFABytecodeGenerator.writeShort(code, addr+1, (short)target); + PDABytecodeGenerator.writeShort(code, addr+1, (short)target); } @Override diff --git a/tool/src/org/antlr/v4/codegen/nfa/LabelInstr.java b/tool/src/org/antlr/v4/codegen/pda/LabelInstr.java similarity index 77% rename from tool/src/org/antlr/v4/codegen/nfa/LabelInstr.java rename to tool/src/org/antlr/v4/codegen/pda/LabelInstr.java index a5fd3ab4a..496eb7175 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/LabelInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/LabelInstr.java @@ -1,7 +1,7 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; import org.antlr.runtime.Token; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.Bytecode; /** */ @@ -16,7 +16,7 @@ public class LabelInstr extends Instr { public void write(byte[] code) { super.write(code); labelIndex = gen.getLabelIndex(rule, token.getText()); - NFABytecodeGenerator.writeShort(code, addr+1, (short)labelIndex); + PDABytecodeGenerator.writeShort(code, addr+1, (short)labelIndex); } public String toString() { return addr+":LabelInstr "+ labelIndex; } } diff --git a/tool/src/org/antlr/v4/codegen/nfa/MatchInstr.java b/tool/src/org/antlr/v4/codegen/pda/MatchInstr.java similarity index 79% rename from tool/src/org/antlr/v4/codegen/nfa/MatchInstr.java rename to tool/src/org/antlr/v4/codegen/pda/MatchInstr.java index cd51e4e39..9f1237296 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/MatchInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/MatchInstr.java @@ -1,7 +1,7 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; import org.antlr.runtime.Token; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.Bytecode; /** */ @@ -14,7 +14,7 @@ public class MatchInstr extends Instr { public void write(byte[] code) { super.write(code); if ( charSize(c)==1 ) code[addr+1] = (byte)(c&0xFF); - else NFABytecodeGenerator.writeShort(code, addr+1, (short)c); + else PDABytecodeGenerator.writeShort(code, addr+1, (short)c); } @Override diff --git a/tool/src/org/antlr/v4/codegen/nfa/RangeInstr.java b/tool/src/org/antlr/v4/codegen/pda/RangeInstr.java similarity index 80% rename from tool/src/org/antlr/v4/codegen/nfa/RangeInstr.java rename to tool/src/org/antlr/v4/codegen/pda/RangeInstr.java index 58576b4ea..f959a5033 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/RangeInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/RangeInstr.java @@ -1,7 +1,7 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; import org.antlr.runtime.Token; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.misc.CharSupport; import org.antlr.v4.runtime.pda.Bytecode; @@ -24,8 +24,8 @@ public class RangeInstr extends Instr { code[addr+2] = (byte)(b&0xFF); } else { - NFABytecodeGenerator.writeShort(code, addr+1, (short)a); - NFABytecodeGenerator.writeShort(code, addr+1+charSize(a,b), (short)b); + PDABytecodeGenerator.writeShort(code, addr+1, (short)a); + PDABytecodeGenerator.writeShort(code, addr+1+charSize(a,b), (short)b); } } diff --git a/tool/src/org/antlr/v4/codegen/nfa/RetInstr.java b/tool/src/org/antlr/v4/codegen/pda/RetInstr.java similarity index 83% rename from tool/src/org/antlr/v4/codegen/nfa/RetInstr.java rename to tool/src/org/antlr/v4/codegen/pda/RetInstr.java index bd1fba703..9ef559e3d 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/RetInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/RetInstr.java @@ -1,4 +1,4 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; import org.antlr.v4.runtime.pda.Bytecode; diff --git a/tool/src/org/antlr/v4/codegen/nfa/SaveInstr.java b/tool/src/org/antlr/v4/codegen/pda/SaveInstr.java similarity index 77% rename from tool/src/org/antlr/v4/codegen/nfa/SaveInstr.java rename to tool/src/org/antlr/v4/codegen/pda/SaveInstr.java index 12f920f8c..7176eab50 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/SaveInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/SaveInstr.java @@ -1,7 +1,7 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; import org.antlr.runtime.Token; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.Bytecode; /** */ @@ -16,7 +16,7 @@ public class SaveInstr extends Instr { public void write(byte[] code) { super.write(code); labelIndex = gen.getLabelIndex(rule, token.getText()); - NFABytecodeGenerator.writeShort(code, addr+1, (short) labelIndex); + PDABytecodeGenerator.writeShort(code, addr+1, (short) labelIndex); } public String toString() { return addr+":SaveInstr "+ labelIndex; } } diff --git a/tool/src/org/antlr/v4/codegen/nfa/SemPredInstr.java b/tool/src/org/antlr/v4/codegen/pda/SemPredInstr.java similarity index 69% rename from tool/src/org/antlr/v4/codegen/nfa/SemPredInstr.java rename to tool/src/org/antlr/v4/codegen/pda/SemPredInstr.java index fd4957106..6591a18df 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/SemPredInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/SemPredInstr.java @@ -1,7 +1,7 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; import org.antlr.runtime.Token; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.Bytecode; /** */ @@ -15,8 +15,8 @@ public class SemPredInstr extends Instr { public int nBytes() { return 1+2*2; } public void write(byte[] code) { super.write(code); - NFABytecodeGenerator.writeShort(code, addr+1, (short)rule.index); - NFABytecodeGenerator.writeShort(code, addr+1+2, (short)gen.getSempredIndex(rule, token)); + PDABytecodeGenerator.writeShort(code, addr+1, (short)rule.index); + PDABytecodeGenerator.writeShort(code, addr+1+2, (short)gen.getSempredIndex(rule, token)); } public String toString() { return addr+":SemPredInstr "+ predIndex; } } diff --git a/tool/src/org/antlr/v4/codegen/nfa/SplitInstr.java b/tool/src/org/antlr/v4/codegen/pda/SplitInstr.java similarity index 75% rename from tool/src/org/antlr/v4/codegen/nfa/SplitInstr.java rename to tool/src/org/antlr/v4/codegen/pda/SplitInstr.java index 1ce2d166a..1c707b94c 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/SplitInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/SplitInstr.java @@ -1,6 +1,6 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.Bytecode; import java.util.ArrayList; @@ -16,10 +16,10 @@ public class SplitInstr extends Instr { public void write(byte[] code) { super.write(code); int a = addr + 1; - NFABytecodeGenerator.writeShort(code, a, (short)addrs.size()); + PDABytecodeGenerator.writeShort(code, a, (short)addrs.size()); a += 2; for (int x : addrs) { - NFABytecodeGenerator.writeShort(code, a, (short)x); + PDABytecodeGenerator.writeShort(code, a, (short)x); a += Bytecode.ADDR_SIZE; } } diff --git a/tool/src/org/antlr/v4/codegen/nfa/WildcardInstr.java b/tool/src/org/antlr/v4/codegen/pda/WildcardInstr.java similarity index 89% rename from tool/src/org/antlr/v4/codegen/nfa/WildcardInstr.java rename to tool/src/org/antlr/v4/codegen/pda/WildcardInstr.java index af223f264..5415bf33b 100644 --- a/tool/src/org/antlr/v4/codegen/nfa/WildcardInstr.java +++ b/tool/src/org/antlr/v4/codegen/pda/WildcardInstr.java @@ -1,4 +1,4 @@ -package org.antlr.v4.codegen.nfa; +package org.antlr.v4.codegen.pda; import org.antlr.runtime.Token; import org.antlr.v4.runtime.pda.Bytecode; diff --git a/tool/src/org/antlr/v4/misc/DoubleKeyMap.java b/tool/src/org/antlr/v4/misc/DoubleKeyMap.java index 5619ec143..183028567 100644 --- a/tool/src/org/antlr/v4/misc/DoubleKeyMap.java +++ b/tool/src/org/antlr/v4/misc/DoubleKeyMap.java @@ -1,20 +1,22 @@ package org.antlr.v4.misc; -import java.util.HashMap; +import java.util.Collection; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; /** Sometimes we need to map a key to a value but key is two pieces of data. * This nested hash table saves creating a single key each time we access * map; avoids mem creation. */ public class DoubleKeyMap { - Map> data = new HashMap>(); + Map> data = new LinkedHashMap>(); public Value put(Key1 k1, Key2 k2, Value v) { Map data2 = data.get(k1); Value prev = null; if ( data2==null ) { - data2 = new HashMap(); + data2 = new LinkedHashMap(); data.put(k1, data2); } else { @@ -26,12 +28,28 @@ public class DoubleKeyMap { public Value get(Key1 k1, Key2 k2) { Map data2 = data.get(k1); - if ( data2==null ) { - data2 = new HashMap(); - data.put(k1, data2); - } + if ( data2==null ) return null; return data2.get(k2); } public Map get(Key1 k1) { return data.get(k1); } + + /** Get all values associated with primary key */ + public Collection values(Key1 k1) { + Map data2 = data.get(k1); + if ( data2==null ) return null; + return data2.values(); + } + + /** get all primary keys */ + public Set keySet() { + return data.keySet(); + } + + /** get all secondary keys associated with a primary key */ + public Set keySet(Key1 k1) { + Map data2 = data.get(k1); + if ( data2==null ) return null; + return data2.keySet(); + } } diff --git a/tool/test/org/antlr/v4/test/TestNFABytecodeGeneration.java b/tool/test/org/antlr/v4/test/TestPDABytecodeGeneration.java similarity index 97% rename from tool/test/org/antlr/v4/test/TestNFABytecodeGeneration.java rename to tool/test/org/antlr/v4/test/TestPDABytecodeGeneration.java index 617251e49..ae097d3a8 100644 --- a/tool/test/org/antlr/v4/test/TestNFABytecodeGeneration.java +++ b/tool/test/org/antlr/v4/test/TestPDABytecodeGeneration.java @@ -1,7 +1,7 @@ package org.antlr.v4.test; import org.antlr.v4.Tool; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.Bytecode; import org.antlr.v4.runtime.pda.PDA; import org.antlr.v4.semantics.SemanticPipeline; @@ -9,7 +9,7 @@ import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.LexerGrammar; import org.junit.Test; -public class TestNFABytecodeGeneration extends BaseTest { +public class TestPDABytecodeGeneration extends BaseTest { @Test public void testString() throws Exception { LexerGrammar g = new LexerGrammar( "lexer grammar L;\n"+ @@ -215,7 +215,7 @@ public class TestNFABytecodeGeneration extends BaseTest { } } } - PDA PDA = NFABytecodeGenerator.getBytecode(g, LexerGrammar.DEFAULT_MODE_NAME); + PDA PDA = PDABytecodeGenerator.getPDA(g, LexerGrammar.DEFAULT_MODE_NAME); assertEquals(expecting, Bytecode.disassemble(PDA.code)); } } diff --git a/tool/test/org/antlr/v4/test/TestNFABytecodeInterp.java b/tool/test/org/antlr/v4/test/TestPDABytecodeInterp.java similarity index 96% rename from tool/test/org/antlr/v4/test/TestNFABytecodeInterp.java rename to tool/test/org/antlr/v4/test/TestPDABytecodeInterp.java index 9fecd0e54..900bc9bb8 100644 --- a/tool/test/org/antlr/v4/test/TestNFABytecodeInterp.java +++ b/tool/test/org/antlr/v4/test/TestPDABytecodeInterp.java @@ -3,7 +3,7 @@ package org.antlr.v4.test; import org.antlr.runtime.ANTLRStringStream; import org.antlr.runtime.Token; import org.antlr.v4.Tool; -import org.antlr.v4.codegen.NFABytecodeGenerator; +import org.antlr.v4.codegen.PDABytecodeGenerator; import org.antlr.v4.runtime.pda.PDA; import org.antlr.v4.semantics.SemanticPipeline; import org.antlr.v4.tool.Grammar; @@ -15,7 +15,7 @@ import java.util.Arrays; import java.util.List; /** */ -public class TestNFABytecodeInterp extends BaseTest { +public class TestPDABytecodeInterp extends BaseTest { @Test public void testString() throws Exception { LexerGrammar g = new LexerGrammar( "lexer grammar L;\n"+ @@ -202,7 +202,7 @@ public class TestNFABytecodeInterp extends BaseTest { } } - PDA PDA = NFABytecodeGenerator.getBytecode(g, LexerGrammar.DEFAULT_MODE_NAME); + PDA PDA = PDABytecodeGenerator.getPDA(g, LexerGrammar.DEFAULT_MODE_NAME); ANTLRStringStream in = new ANTLRStringStream(input); List tokenTypes = new ArrayList(); int ttype = 0; @@ -236,7 +236,7 @@ public class TestNFABytecodeInterp extends BaseTest { } } - PDA PDA = NFABytecodeGenerator.getBytecode(g, LexerGrammar.DEFAULT_MODE_NAME); + PDA PDA = PDABytecodeGenerator.getPDA(g, LexerGrammar.DEFAULT_MODE_NAME); ANTLRStringStream in = new ANTLRStringStream(input); List tokenTypes = new ArrayList(); int ttype = PDA.execThompson(in);