got lexer actions working again

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8901]
This commit is contained in:
parrt 2011-07-23 12:58:09 -08:00
parent ba9f5d54e7
commit d51784a250
4 changed files with 36 additions and 42 deletions

View File

@ -58,10 +58,16 @@ public class <parser.name> extends Parser {
@Override
public ATN getATN() { return _ATN; }
<dumpActions(parser, actionFuncs, sempredFuncs)>
<atn>
}
>>
dumpActions(recog, actionFuncs, sempredFuncs) ::= <<
<if(actionFuncs)>
public void _action(RuleContext _localctx, int ruleIndex, int predIndex) {
switch ( ruleIndex ) {
<parser.actionFuncs:{f|
<recog.actionFuncs:{f|
case <f.ruleIndex> : <f.name>_action((<f.ctxType>)_localctx, predIndex);}; separator="\n">
}
}
@ -70,31 +76,15 @@ case <f.ruleIndex> : <f.name>_action((<f.ctxType>)_localctx, predIndex);}; separ
<if(sempredFuncs)>
public boolean _sempred(RuleContext _localctx, int ruleIndex, int predIndex) {
switch ( ruleIndex ) {
<parser.sempredFuncs:{f|
<recog.sempredFuncs:{f|
case <f.ruleIndex> : return <f.name>_sempred((<f.ctxType>)_localctx, predIndex);}; separator="\n">
}
return true;
}
<sempredFuncs; separator="\n">
<endif>
<atn>
}
>>
/*
dumpActions(actions) ::= <<
<if(actions)>
public void action(int ruleIndex, int actionIndex) {
switch ( actionIndex ) {
<actions:{index|
case <index> : <actions.(index)> break;}; separator="\n">
}
}
<endif>
>>
*/
ctor(p) ::= <<
public <p.name>(TokenStream input) {
super(input);
@ -553,7 +543,7 @@ import org.antlr.v4.runtime.misc.*;
<lexer>
>>
Lexer(lexer, atn, actions, sempreds) ::= <<
Lexer(lexer, atn, actionFuncs, sempredFuncs) ::= <<
public class <lexer.name> extends Lexer {
public static final int
<lexer.tokens:{k | <k>=<lexer.tokens.(k)>}; separator=", ", wrap, anchor>;
@ -585,7 +575,7 @@ public class <lexer.name> extends Lexer {
<lexer.namedActions.members>
<! <dumpActions(actions)> !>
<dumpActions(lexer, actionFuncs, sempredFuncs)>
<atn>
}
>>

View File

@ -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;
}

View File

@ -43,8 +43,10 @@ public class Lexer extends OutputModelObject {
public Collection<String> modes;
@ModelElement public SerializedATN atn;
@ModelElement public LinkedHashMap<Integer, Action> actions;
@ModelElement public LinkedHashMap<Integer, Action> sempreds;
@ModelElement public List<RuleActionFunction> actionFuncs =
new ArrayList<RuleActionFunction>();
@ModelElement public List<RuleSempredFunction> sempredFuncs =
new ArrayList<RuleSempredFunction>();
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<Integer, Action>();
for (PredAST p : g.sempreds.keySet()) {
sempreds.put(g.sempreds.get(p), new Action(factory, p));
}
actions = new LinkedHashMap<Integer, Action>();
for (ActionAST a : g.actions.keySet()) {
actions.put(g.actions.get(a), new Action(factory, a));
}
}
}

View File

@ -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<Integer, Action>();
for (PredAST p : g.sempreds.keySet()) {
sempreds.put(g.sempreds.get(p), new Action(factory, p));
}
actions = new LinkedHashMap<Integer, ForcedAction>();
for (ActionAST a : g.actions.keySet()) {
actions.put(g.actions.get(a), new ForcedAction(factory, a));
}
*/
}
}