From 2d5c4fd686bcafa48b44a3fd75d7c3fd75d6adfe Mon Sep 17 00:00:00 2001 From: parrt Date: Tue, 28 Jun 2011 16:15:31 -0800 Subject: [PATCH] got core ^ and ! ops in [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8791] --- tool/playground/T.g | 2 +- .../v4/tool/templates/codegen/Java/Java.stg | 12 +++++----- .../antlr/v4/codegen/ParserASTExtension.java | 22 ++++++++++++++++++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/tool/playground/T.g b/tool/playground/T.g index f0e252a9e..5f23b4e9d 100644 --- a/tool/playground/T.g +++ b/tool/playground/T.g @@ -1,6 +1,6 @@ parser grammar T; options {output=AST;} -a : A^ b ; +a : x+=A^ y+=b B! b!; b : B ; /* 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 3e86e5613..6da5b9dbb 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 @@ -104,17 +104,17 @@ public QStack\<\> _stk = new QStack\< + _localctx.stop = input.LT(-1); + } catch (RecognitionException re) { reportError(re); recover(); } finally { - _localctx.stop = input.LT(-1); - _stk.pop(); - _ctx = (ParserRuleContext)_ctx.parent; + //System.out.println("exit "+ruleNames[]); } return _localctx; @@ -266,12 +266,12 @@ cases(ttypes) ::= << >> InvokeRule(r) ::= << -_ctx.s = ; +_localctx.s = ; = }>(}; separator=",">); >> MatchToken(m) ::= << -_ctx.s = ; +_localctx.s = ; = }>(Token)match(); >> @@ -323,7 +323,7 @@ SetDynScopeAttr_negIndex(s, indexChunks, rhsChunks) ::= SetDynScopeAttr_index(s, indexChunks, rhsChunks) ::= ".get(). =;" -AddToLabelList(a) ::= "_localctx..add(_localctx.);" +AddToLabelList(a) ::= "_localctx..add();" TokenDecl(t) ::= "Token ;" TokenTypeDecl(t) ::= "int ;" diff --git a/tool/src/org/antlr/v4/codegen/ParserASTExtension.java b/tool/src/org/antlr/v4/codegen/ParserASTExtension.java index 88a4b02a4..21b06a434 100644 --- a/tool/src/org/antlr/v4/codegen/ParserASTExtension.java +++ b/tool/src/org/antlr/v4/codegen/ParserASTExtension.java @@ -1,8 +1,10 @@ package org.antlr.v4.codegen; +import org.antlr.runtime.tree.Tree; import org.antlr.v4.codegen.model.*; import org.antlr.v4.codegen.model.ast.*; import org.antlr.v4.misc.Utils; +import org.antlr.v4.parse.ANTLRParser; import java.util.List; @@ -18,15 +20,33 @@ public class ParserASTExtension extends CodeGeneratorExtension { } @Override - public List ruleRef(List ops) { + public List rootRule(List ops) { InvokeRule invokeOp = (InvokeRule)Utils.find(ops, InvokeRule.class); SrcOp treeOp = new BecomeRoot(factory, invokeOp.ast, invokeOp); return DefaultOutputModelFactory.list(ops, treeOp); } + @Override + public List rootToken(List ops) { + MatchToken matchOp = (MatchToken)Utils.find(ops, MatchToken.class); + SrcOp treeOp = new BecomeRoot(factory, matchOp.ast, matchOp); + return DefaultOutputModelFactory.list(ops, treeOp); + } + + @Override + public List ruleRef(List ops) { + InvokeRule invokeOp = (InvokeRule)Utils.find(ops, InvokeRule.class); + Tree parent = invokeOp.ast.getParent(); + if ( parent!=null && parent.getType()==ANTLRParser.BANG ) return ops; + SrcOp treeOp = new AddLeaf(factory, invokeOp.ast, invokeOp); + return DefaultOutputModelFactory.list(ops, treeOp); + } + @Override public List tokenRef(List ops) { MatchToken matchOp = (MatchToken)Utils.find(ops, MatchToken.class); + Tree parent = matchOp.ast.getParent(); + if ( parent!=null && parent.getType()==ANTLRParser.BANG ) return ops; SrcOp treeOp = new AddLeaf(factory, matchOp.ast, matchOp); return DefaultOutputModelFactory.list(ops, treeOp); }