Print test arrays in java style (#50)

* Print string rule invocation stack array in java style

* regen runtime tests

* Another array print fix

* Fixes to print array of terminal nodes:

* regen tests
This commit is contained in:
Peter Boyer 2016-10-05 14:52:42 -04:00 committed by GitHub
parent 9b76bc6a71
commit 499d9d3f89
4 changed files with 36 additions and 11 deletions

View File

@ -220,7 +220,7 @@ ImportListener(X) ::= ""
GetExpectedTokenNames() ::= "p.GetExpectedTokens().StringVerbose(p.GetTokenNames(), nil, false)"
RuleInvocationStack() ::= "p.GetRuleInvocationStack(nil)"
RuleInvocationStack() ::= "antlr.PrintArrayJavaStyle(p.GetRuleInvocationStack(nil))"
LL_EXACT_AMBIG_DETECTION() ::= <<p.Interpreter.SetPredictionMode(antlr.PredictionModeLLExactAmbigDetection);>>
@ -360,7 +360,7 @@ func NewLeafListener() *LeafListener {
func (*LeafListener) ExitA(ctx *AContext) {
if ctx.GetChildCount() == 2 {
fmt.Printf("%s %s %s", ctx.INT(0).GetSymbol().GetText(), ctx.INT(1).GetSymbol().GetText(), ctx.AllINT())
fmt.Printf("%s %s %s", ctx.INT(0).GetSymbol().GetText(), ctx.INT(1).GetSymbol().GetText(), antlr.PrintArrayJavaStyle(antlr.TerminalNodeToStringArray(ctx.AllINT())))
} else {
fmt.Println(ctx.ID().GetSymbol())
}

View File

@ -285,7 +285,7 @@ public class TestListeners extends BaseTest {
@Test
public void testTokenGetters_1() throws Exception {
mkdir(parserpkgdir);
StringBuilder grammarBuilder = new StringBuilder(674);
StringBuilder grammarBuilder = new StringBuilder(734);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
@ -301,7 +301,7 @@ public class TestListeners extends BaseTest {
grammarBuilder.append("\n");
grammarBuilder.append("func (*LeafListener) ExitA(ctx *AContext) {\n");
grammarBuilder.append(" if ctx.GetChildCount() == 2 {\n");
grammarBuilder.append(" fmt.Printf(\"%s %s %s\", ctx.INT(0).GetSymbol().GetText(), ctx.INT(1).GetSymbol().GetText(), ctx.AllINT())\n");
grammarBuilder.append(" fmt.Printf(\"%s %s %s\", ctx.INT(0).GetSymbol().GetText(), ctx.INT(1).GetSymbol().GetText(), antlr.PrintArrayJavaStyle(antlr.TerminalNodeToStringArray(ctx.AllINT())))\n");
grammarBuilder.append(" } else {\n");
grammarBuilder.append(" fmt.Println(ctx.ID().GetSymbol())\n");
grammarBuilder.append(" }\n");
@ -338,7 +338,7 @@ public class TestListeners extends BaseTest {
@Test
public void testTokenGetters_2() throws Exception {
mkdir(parserpkgdir);
StringBuilder grammarBuilder = new StringBuilder(674);
StringBuilder grammarBuilder = new StringBuilder(734);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
@ -354,7 +354,7 @@ public class TestListeners extends BaseTest {
grammarBuilder.append("\n");
grammarBuilder.append("func (*LeafListener) ExitA(ctx *AContext) {\n");
grammarBuilder.append(" if ctx.GetChildCount() == 2 {\n");
grammarBuilder.append(" fmt.Printf(\"%s %s %s\", ctx.INT(0).GetSymbol().GetText(), ctx.INT(1).GetSymbol().GetText(), ctx.AllINT())\n");
grammarBuilder.append(" fmt.Printf(\"%s %s %s\", ctx.INT(0).GetSymbol().GetText(), ctx.INT(1).GetSymbol().GetText(), antlr.PrintArrayJavaStyle(antlr.TerminalNodeToStringArray(ctx.AllINT())))\n");
grammarBuilder.append(" } else {\n");
grammarBuilder.append(" fmt.Println(ctx.ID().GetSymbol())\n");
grammarBuilder.append(" }\n");

View File

@ -193,7 +193,7 @@ public class TestParseTrees extends BaseTest {
@Test
public void testTokenAndRuleContextString() throws Exception {
mkdir(parserpkgdir);
StringBuilder grammarBuilder = new StringBuilder(167);
StringBuilder grammarBuilder = new StringBuilder(194);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n");
@ -204,7 +204,7 @@ public class TestParseTrees extends BaseTest {
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' { \n");
grammarBuilder.append("fmt.Println(p.GetRuleInvocationStack(nil))\n");
grammarBuilder.append("fmt.Println(antlr.PrintArrayJavaStyle(p.GetRuleInvocationStack(nil)))\n");
grammarBuilder.append("} ;");
String grammar = grammarBuilder.toString();
String input ="x";

View File

@ -6,9 +6,7 @@ import (
"hash/fnv"
"sort"
"strings"
// "regexp"
// "bytes"
// "encoding/gob"
"bytes"
"strconv"
)
@ -343,6 +341,33 @@ func EscapeWhitespace(s string, escapeSpaces bool) string {
return s
}
func TerminalNodeToStringArray(sa []TerminalNode) []string {
st := make([]string, len(sa))
for i, s := range sa {
st[i] = fmt.Sprintf("%v", s)
}
return st
}
func PrintArrayJavaStyle(sa []string) string {
var buffer bytes.Buffer
buffer.WriteString("[")
for i, s := range sa {
buffer.WriteString(s)
if i != len(sa)-1 {
buffer.WriteString(", ")
}
}
buffer.WriteString("]")
return buffer.String()
}
func TitleCase(str string) string {
// func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string