Skip to content

Commit

Permalink
Fix for antlr#748 and antlr#2991
Browse files Browse the repository at this point in the history
  • Loading branch information
kaby76 committed Jul 23, 2024
1 parent 1e08bcb commit a05fefc
Show file tree
Hide file tree
Showing 2 changed files with 364 additions and 3 deletions.
81 changes: 79 additions & 2 deletions _scripts/templates/CSharp/st.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ static void DoParse(ICharStream str, string input_name, int row_number)
{
if (tee)
{
System.IO.File.WriteAllText(input_name + ".tree", tree.ToStringTree(parser));
System.IO.File.WriteAllText(input_name + ".tree", ToStringTree(tree, parser));
} else
{
System.Console.Error.WriteLine(tree.ToStringTree(parser));
System.Console.Error.WriteLine(ToStringTree(tree, parser));
}
}
if (show_profile)
Expand All @@ -300,6 +300,83 @@ static void DoParse(ICharStream str, string input_name, int row_number)
}
if (tee) output.Close();
}

public static string ToStringTree(ITree tree, Parser recog)
{
StringBuilder sb = new StringBuilder();
string[] ruleNames = recog != null ? recog.RuleNames : null;
IList\<string> ruleNamesList = ruleNames != null ? ruleNames.ToList() : null;
ToStringTree(sb, tree, 0, ruleNamesList);
return sb.ToString();
}

public static void ToStringTree(StringBuilder sb, ITree t, int indent, IList\<string> ruleNames)
{
string s = Antlr4.Runtime.Misc.Utils.EscapeWhitespace(GetNodeText(t, ruleNames), false);
if (t.ChildCount == 0)
{
for (int i = 0; i \< indent; ++i) sb.Append(" ");
sb.AppendLine(s);
return;
}
s = Antlr4.Runtime.Misc.Utils.EscapeWhitespace(GetNodeText(t, ruleNames), false);
for (int i = 0; i \< indent; ++i) sb.Append(' ');
sb.AppendLine(s);
for (int i = 0; i \< t.ChildCount; i++)
{
ToStringTree(sb, t.GetChild(i), indent+1, ruleNames);
}
}

public static string GetNodeText(ITree t, Parser recog)
{
string[] ruleNames = recog != null ? recog.RuleNames : null;
IList\<string> ruleNamesList = ruleNames != null ? ruleNames.ToList() : null;
return GetNodeText(t, ruleNamesList);
}

public static string GetNodeText(ITree t, IList\<string> ruleNames)
{
if (ruleNames != null)
{
if (t is RuleContext)
{
int ruleIndex = ((RuleContext)t).RuleIndex;
string ruleName = ruleNames[ruleIndex];
int altNumber = ((RuleContext)t).getAltNumber();
if ( altNumber!= Antlr4.Runtime.Atn.ATN.INVALID_ALT_NUMBER ) {
return ruleName+":"+altNumber;
}
return ruleName;
}
else
{
if (t is IErrorNode)
{
return t.ToString();
}
else
{
if (t is ITerminalNode)
{
IToken symbol = ((ITerminalNode)t).Symbol;
if (symbol != null)
{
string s = symbol.Text;
return s;
}
}
}
}
}
// no recog for rule names
object payload = t.Payload;
if (payload is IToken)
{
return ((IToken)payload).Text;
}
return t.Payload.ToString();
}
}

<if(has_name_space)>}<endif>
286 changes: 285 additions & 1 deletion abb/examples/robdata.sys.tree
Original file line number Diff line number Diff line change
@@ -1 +1,285 @@
(module_ (moduleData module (moduleName (procCall (procName robdata) (procParameter ( sysmodule )))) \n (dataList \n (declaration (init_ PERS) (type_ tooldata) tMain := (expression (array_ [ (expression (primitive TRUE)) , (expression (array_ [ (expression (array_ [ (expression (primitive 0.0)) , (expression (primitive 0.0)) , (expression (primitive 700.0)) ])) , (expression (array_ [ (expression (primitive 1.0)) , (expression (primitive 0.0)) , (expression (primitive 0.0)) , (expression (primitive 0.0)) ])) ])) , (expression (array_ [ (expression (primitive 0.0)) , (expression (array_ [ (expression (primitive 5.0)) , (expression (primitive 0)) , (expression (primitive 95.5)) ])) , (expression (array_ [ (expression (primitive 1)) , (expression (primitive 0)) , (expression (primitive 0)) , (expression (primitive 0)) ])) , (expression (primitive 0.228)) , (expression (primitive 0.094)) , (expression (primitive 0)) ])) ])) ;) \n (declaration (init_ PERS) (type_ wobjdata) wobjMain := (expression (array_ [ (expression (primitive FALSE)) , (expression (primitive TRUE)) , (expression (primitive "")) , (expression (array_ [ (expression (array_ [ (expression (primitive 0)) , (expression (primitive 0)) , (expression (primitive 0)) ])) , (expression (array_ [ (expression (primitive 1)) , (expression (primitive 0)) , (expression (primitive 0)) , (expression (primitive 0)) ])) ])) , (expression (array_ [ (expression (array_ [ (expression (primitive 0)) , (expression (primitive 0)) , (expression (primitive 0)) ])) , (expression (array_ [ (expression (primitive 1)) , (expression (primitive 0)) , (expression (primitive 0)) , (expression (primitive 0)) ])) ])) ])) ;) \n (declaration (init_ CONST) (type_ speeddata) v200 := (expression (array_ [ (expression (primitive 200)) , (expression (primitive 500)) , (expression (primitive 5000)) , (expression (primitive 1000)) ])) ;) \n \n (procedure PROC (procCall (procName ConfigOn) (procParameter ( ))) \n (functionCall ConfJ (functionParameter \On) ;) \n (functionCall ConfL (functionParameter \On) ;) \n ENDPROC) \n \n) endmodule) <EOF>)
module_
moduleData
module
moduleName
procCall
procName
robdata
procParameter
(
sysmodule
)
\n
dataList
\n
declaration
init_
PERS
type_
tooldata
tMain
:=
expression
array_
[
expression
primitive
TRUE
,
expression
array_
[
expression
array_
[
expression
primitive
0.0
,
expression
primitive
0.0
,
expression
primitive
700.0
]
,
expression
array_
[
expression
primitive
1.0
,
expression
primitive
0.0
,
expression
primitive
0.0
,
expression
primitive
0.0
]
]
,
expression
array_
[
expression
primitive
0.0
,
expression
array_
[
expression
primitive
5.0
,
expression
primitive
0
,
expression
primitive
95.5
]
,
expression
array_
[
expression
primitive
1
,
expression
primitive
0
,
expression
primitive
0
,
expression
primitive
0
]
,
expression
primitive
0.228
,
expression
primitive
0.094
,
expression
primitive
0
]
]
;
\n
declaration
init_
PERS
type_
wobjdata
wobjMain
:=
expression
array_
[
expression
primitive
FALSE
,
expression
primitive
TRUE
,
expression
primitive
""
,
expression
array_
[
expression
array_
[
expression
primitive
0
,
expression
primitive
0
,
expression
primitive
0
]
,
expression
array_
[
expression
primitive
1
,
expression
primitive
0
,
expression
primitive
0
,
expression
primitive
0
]
]
,
expression
array_
[
expression
array_
[
expression
primitive
0
,
expression
primitive
0
,
expression
primitive
0
]
,
expression
array_
[
expression
primitive
1
,
expression
primitive
0
,
expression
primitive
0
,
expression
primitive
0
]
]
]
;
\n
declaration
init_
CONST
type_
speeddata
v200
:=
expression
array_
[
expression
primitive
200
,
expression
primitive
500
,
expression
primitive
5000
,
expression
primitive
1000
]
;
\n
\n
procedure
PROC
procCall
procName
ConfigOn
procParameter
(
)
\n
functionCall
ConfJ
functionParameter
\On
;
\n
functionCall
ConfL
functionParameter
\On
;
\n
ENDPROC
\n
\n
endmodule
<EOF>

0 comments on commit a05fefc

Please sign in to comment.