Fix issue with tests

This commit is contained in:
Peter Boyer 2016-01-05 09:30:28 -05:00
parent f2852686a7
commit 977a1c3ae7
5 changed files with 49 additions and 29 deletions

View File

@ -770,7 +770,7 @@ public abstract class BaseTest {
+")\n"
+ "\n"
+ "type TreeShapeListener struct {\n"
+ " *antlr4.BaseParseTreeListener\n"
+ " *parser.Base<listenerName>\n"
+ "}\n"
+ "\n"
+ "func NewTreeShapeListener() *TreeShapeListener {\n"
@ -780,9 +780,8 @@ public abstract class BaseTest {
+ "func (this *TreeShapeListener) EnterEveryRule(ctx antlr4.ParserRuleContext) {\n"
+ " for i := 0; i\\<ctx.GetChildCount(); i++ {\n"
+ " child := ctx.GetChild(i)\n"
+ " parent := child.GetParent()\n"
+ " parentR,ok := child.GetParent().(antlr4.RuleNode)\n"
+ " if parent.GetRuleContext() != ctx || !ok {\n"
+ " if !ok || parentR.GetBaseRuleContext() != ctx.GetBaseRuleContext() {\n"
+ " panic(\"Invalid parse tree shape detected.\")\n"
+ " }\n"
+ " }\n"
@ -810,10 +809,12 @@ public abstract class BaseTest {
outputFileST.add("lexerName", lexerName);
outputFileST.add("listenerName", listenerName);
outputFileST.add("visitorName", visitorName);
outputFileST.add("parserStartRuleName", parserStartRuleName);
outputFileST.add("parserStartRuleName", parserStartRuleName.substring(0, 1).toUpperCase() + parserStartRuleName.substring(1) );
writeFile(tmpdir, "Test.go", outputFileST.render());
}
protected void writeLexerTestFile(String lexerName, boolean showDFA) {
ST outputFileST = new ST(
"package main\n" +

View File

@ -3,7 +3,6 @@ package antlr4
import (
"reflect"
"strconv"
"fmt"
)
type ParserRuleContext interface {
@ -91,11 +90,9 @@ func (this *BaseParserRuleContext) GetText() string {
// Double dispatch methods for listeners
func (prc *BaseParserRuleContext) EnterRule(listener ParseTreeListener) {
fmt.Println("Do nothing enter")
}
func (prc *BaseParserRuleContext) ExitRule(listener ParseTreeListener) {
fmt.Println("Do nothing exit")
}
// * Does not set parent link other add methods do that///
@ -181,8 +178,8 @@ func (prc *BaseParserRuleContext) GetRuleContext() RuleContext {
return prc
}
func (this *BaseParserRuleContext) Accept(Visitor ParseTreeVisitor) interface{} {
return Visitor.VisitChildren(this)
func (this *BaseParserRuleContext) Accept(visitor ParseTreeVisitor) interface{} {
return visitor.VisitChildren(this)
}
func (prc *BaseParserRuleContext) SetStart(t Token) {

View File

@ -60,6 +60,10 @@ func NewBaseRuleContext(parent RuleContext, invokingState int) *BaseRuleContext
return rn
}
func (this *BaseRuleContext) GetBaseRuleContext() *BaseRuleContext {
return this
}
func (this *BaseRuleContext) SetParent(v Tree) {
this.parentCtx = v.(RuleContext)
}

View File

@ -35,6 +35,7 @@ type RuleNode interface {
ParseTree
GetRuleContext() RuleContext
GetBaseRuleContext() *BaseRuleContext
}
type TerminalNode interface {
@ -48,13 +49,19 @@ type ErrorNode interface {
}
type ParseTreeVisitor interface {
// NOTE: removed type arguments
Visit(tree ParseTree) interface{}
VisitChildren(node RuleNode) interface{}
VisitTerminal(node TerminalNode) interface{}
VisitErrorNode(node ErrorNode) interface{}
}
type BaseParseTreeVisitor struct {}
func (v *BaseParseTreeVisitor) Visit(tree ParseTree) interface{} { return nil }
func (v *BaseParseTreeVisitor) VisitChildren(node RuleNode) interface{} { return nil }
func (v *BaseParseTreeVisitor) VisitTerminal(node TerminalNode) interface{} { return nil }
func (v *BaseParseTreeVisitor) VisitErrorNode(node ErrorNode) interface{} { return nil }
// TODO
//func (this ParseTreeVisitor) Visit(ctx) {
// if (Utils.isArray(ctx)) {
@ -144,8 +151,8 @@ func (this *TerminalNodeImpl) GetChildCount() int {
return 0
}
func (this *TerminalNodeImpl) Accept(Visitor ParseTreeVisitor) interface{} {
return Visitor.VisitTerminal(this)
func (this *TerminalNodeImpl) Accept(v ParseTreeVisitor) interface{} {
return v.VisitTerminal(this)
}
func (this *TerminalNodeImpl) GetText() string {
@ -180,8 +187,8 @@ func (this *ErrorNodeImpl) IsErrorNode() bool {
return true
}
func (this *ErrorNodeImpl) Accept(Visitor ParseTreeVisitor) interface{} {
return Visitor.VisitErrorNode(this)
func (this *ErrorNodeImpl) Accept(v ParseTreeVisitor) interface{} {
return v.VisitErrorNode(this)
}
type ParseTreeWalker struct {

View File

@ -14,9 +14,10 @@ import (
"fmt"
)
// suppress unused import error, many tests
// require fmt.
// suppress unused import error, we aren't guaranteed
// to have reflect or fmt used in the generated code below
var _ = fmt.Printf
var _ = reflect.Copy
<namedActions.header>
@ -84,13 +85,29 @@ import "antlr4"
type <file.grammarName>Visitor interface {
antlr4.ParseTreeVisitor
<file.VisitorNames:{lname |
<file.visitorNames:{lname |
// Visit a parse tree produced by <file.parserName>#<lname>.
Visit<lname; format="cap">(ctx antlr4.ParserRuleContext)
Visit<lname; format="cap">(ctx *<lname; format="cap">Context) interface{\}
}; separator="\n">
}
>>
BaseVisitorFile(file, header) ::= <<
<fileHeader(file.grammarFileName, file.ANTLRVersion)>
package parser // <file.grammarName>
import "antlr4"
type Base<file.grammarName>Visitor struct {
*antlr4.BaseParseTreeVisitor
}
<file.visitorNames:{lname |
func (v *Base<file.grammarName>Visitor) Visit<lname; format="cap">(ctx *<lname; format="cap">Context) interface{\} {
return v.VisitChildren(ctx)
\}}; separator="\n">
>>
Parser(parser, funcs, atn, sempredFuncs, superClass) ::= <<
<if(superClass)>
@ -126,12 +143,13 @@ func New<parser.name>(input antlr4.TokenStream) *<parser.name> {
parser.RuleNames = ruleNames
parser.LiteralNames = literalNames
parser.SymbolicNames = symbolicNames
<namedActions.members>
parser.GrammarFileName = "<parser.grammarFileName; format="java-escape">"
return parser
}
<namedActions.members>
const(
<parser.name>EOF = antlr4.TokenEOF
<if(parser.tokens)>
@ -713,33 +731,26 @@ func New<struct.name>(parser antlr4.Parser, ctx antlr4.ParserRuleContext) *<stru
>>
ListenerDispatchMethod(method) ::= <<
func (s *<struct.name>) <if(method.isEnter)>Enter<else>Exit<endif>Rule(listener antlr4.ParseTreeListener) {
listener.(<parser.grammarName>Listener).<if(method.isEnter)>Enter<else>Exit<endif><struct.derivedFromName; format="cap">(s)
}
>>
VisitorDispatchMethod(method) ::= <<
func (s *<struct.name>) Accept(Visitor antlr4.ParseTreeVisitor) interface{} {
func (s *<struct.name>) Accept(visitor antlr4.ParseTreeVisitor) interface{} {
switch t := listener.(type) {
case <parser.grammarName>Listener:
switch t := visitor.(type) {
case <parser.grammarName>Visitor:
return t.Visit<struct.derivedFromName; format="cap">(s)
default:
return t.VisitChildren(s)
}
}
>>
AttributeDecl(d) ::= "p.<d.name> = <if(d.InitValue)><d.InitValue><else>null<endif>"
/** If we don't know location of label def x, use this template */