From d51784a250522ed0c416f4e9aa868b5ea43c8c18 Mon Sep 17 00:00:00 2001 From: parrt Date: Sat, 23 Jul 2011 12:58:09 -0800 Subject: [PATCH] got lexer actions working again [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8901] --- .../v4/tool/templates/codegen/Java/Java.stg | 30 +++++++------------ .../v4/codegen/OutputModelController.java | 22 ++++++++++++++ .../src/org/antlr/v4/codegen/model/Lexer.java | 15 +++------- .../org/antlr/v4/codegen/model/Parser.java | 11 ------- 4 files changed, 36 insertions(+), 42 deletions(-) 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 094541992..c689dc2a6 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 @@ -58,10 +58,16 @@ public class extends Parser { @Override public ATN getATN() { return _ATN; } + + +} +>> + +dumpActions(recog, actionFuncs, sempredFuncs) ::= << public void _action(RuleContext _localctx, int ruleIndex, int predIndex) { switch ( ruleIndex ) { - : _action(()_localctx, predIndex);}; separator="\n"> } } @@ -70,31 +76,15 @@ case : _action(()_localctx, predIndex);}; separ public boolean _sempred(RuleContext _localctx, int ruleIndex, int predIndex) { switch ( ruleIndex ) { - : return _sempred(()_localctx, predIndex);}; separator="\n"> } return true; } - - -} >> -/* -dumpActions(actions) ::= << - - public void action(int ruleIndex, int actionIndex) { - switch ( actionIndex ) { - : break;}; separator="\n"> - } - } - ->> -*/ - ctor(p) ::= << public (TokenStream input) { super(input); @@ -553,7 +543,7 @@ import org.antlr.v4.runtime.misc.*; >> -Lexer(lexer, atn, actions, sempreds) ::= << +Lexer(lexer, atn, actionFuncs, sempredFuncs) ::= << public class extends Lexer { public static final int =}; separator=", ", wrap, anchor>; @@ -585,7 +575,7 @@ public class extends Lexer { - !> + } >> diff --git a/tool/src/org/antlr/v4/codegen/OutputModelController.java b/tool/src/org/antlr/v4/codegen/OutputModelController.java index 4b134361f..9d7b9e46e 100644 --- a/tool/src/org/antlr/v4/codegen/OutputModelController.java +++ b/tool/src/org/antlr/v4/codegen/OutputModelController.java @@ -93,6 +93,28 @@ public class OutputModelController { LexerFile file = lexerFile(gen.getRecognizerFileName()); setRoot(file); file.lexer = lexer(file); + + Grammar g = delegate.getGrammar(); + for (Rule r : g.rules.values()) { + String ctxType = gen.target.getRuleFunctionContextStructName(r); + for (ActionAST a : r.actions) { + if ( a instanceof PredAST ) { + PredAST p = (PredAST)a; + RuleSempredFunction rsf = new RuleSempredFunction(delegate, r, ctxType); + file.lexer.sempredFuncs.add(rsf); + rsf.actions.put(g.sempreds.get(p), new Action(delegate, p)); + } + else if ( a.getType()==ANTLRParser.ACTION || + a.getType()==ANTLRParser.FORCED_ACTION ) + { + // lexer sees {{...}} and {..} as same; neither are done until accept + RuleActionFunction raf = new RuleActionFunction(delegate, r, ctxType); + file.lexer.actionFuncs.add(raf); + raf.actions.put(g.actions.get(a), new ForcedAction(delegate, a)); + } + } + } + return file; } diff --git a/tool/src/org/antlr/v4/codegen/model/Lexer.java b/tool/src/org/antlr/v4/codegen/model/Lexer.java index ab25928ea..c30f69d0f 100644 --- a/tool/src/org/antlr/v4/codegen/model/Lexer.java +++ b/tool/src/org/antlr/v4/codegen/model/Lexer.java @@ -43,8 +43,10 @@ public class Lexer extends OutputModelObject { public Collection modes; @ModelElement public SerializedATN atn; - @ModelElement public LinkedHashMap actions; - @ModelElement public LinkedHashMap sempreds; + @ModelElement public List actionFuncs = + new ArrayList(); + @ModelElement public List sempredFuncs = + new ArrayList(); public Lexer(OutputModelFactory factory, LexerFile file) { this.factory = factory; @@ -63,15 +65,6 @@ public class Lexer extends OutputModelObject { tokenNames = g.getTokenDisplayNames(); ruleNames = g.rules.keySet(); - - sempreds = new LinkedHashMap(); - for (PredAST p : g.sempreds.keySet()) { - sempreds.put(g.sempreds.get(p), new Action(factory, p)); - } - actions = new LinkedHashMap(); - for (ActionAST a : g.actions.keySet()) { - actions.put(g.actions.get(a), new Action(factory, a)); - } } } diff --git a/tool/src/org/antlr/v4/codegen/model/Parser.java b/tool/src/org/antlr/v4/codegen/model/Parser.java index bf9e556b6..c46384484 100644 --- a/tool/src/org/antlr/v4/codegen/model/Parser.java +++ b/tool/src/org/antlr/v4/codegen/model/Parser.java @@ -62,16 +62,5 @@ public class Parser extends OutputModelObject { tokenNames = g.getTokenDisplayNames(); ruleNames = g.rules.keySet(); atn = new SerializedATN(factory, g.atn); - - /* - sempreds = new LinkedHashMap(); - for (PredAST p : g.sempreds.keySet()) { - sempreds.put(g.sempreds.get(p), new Action(factory, p)); - } - actions = new LinkedHashMap(); - for (ActionAST a : g.actions.keySet()) { - actions.put(g.actions.get(a), new ForcedAction(factory, a)); - } - */ } }