-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOutput.cs
More file actions
146 lines (134 loc) · 5.29 KB
/
Output.cs
File metadata and controls
146 lines (134 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Template generated code from Antlr4BuildTasks.Template v 3.0
namespace Java
{
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
internal static class Output
{
private static int changed = 0;
private static bool first_time = true;
public static StringBuilder OutputTokens(this CommonTokenStream stream)
{
StringBuilder sb = new StringBuilder();
foreach (IToken token in stream.GetTokens())
{
sb.AppendLine("Token " + token.TokenIndex + " " + token.Type + " " + "channel " + ((Lexer)stream.TokenSource).ChannelNames[token.Channel] + " " + Output.PerformEscapes(token.Text));
}
return sb;
}
public static StringBuilder OutputTree(this IParseTree tree, CommonTokenStream stream)
{
StringBuilder sb = new StringBuilder();
tree.ParenthesizedAST(sb, stream);
return sb;
}
private static void ParenthesizedAST(this IParseTree tree, StringBuilder sb, CommonTokenStream stream, int level = 0)
{
// Antlr always names a non-terminal with first letter lowercase,
// but renames it when creating the type in C#. So, remove the prefix,
// lowercase the first letter, and remove the trailing "Context" part of
// the name. Saves big time on output!
if (tree as TerminalNodeImpl != null)
{
TerminalNodeImpl tok = tree as TerminalNodeImpl;
Interval interval = tok.SourceInterval;
IList<IToken> inter = null;
if (tok.Symbol.TokenIndex >= 0)
{
inter = stream.GetHiddenTokensToLeft(tok.Symbol.TokenIndex);
}
if (inter != null)
{
foreach (IToken t in inter)
{
StartLine(sb, tree, stream, level);
sb.AppendLine("( " + ((Lexer)stream.TokenSource).ChannelNames[t.Channel] + " text=" + t.Text.PerformEscapes());
}
}
StartLine(sb, tree, stream, level);
sb.AppendLine("( " + ((Lexer)stream.TokenSource).ChannelNames[tok.Symbol.Channel] + " i=" + tree.SourceInterval.a
+ " txt=" + tree.GetText().PerformEscapes()
+ " tt=" + tok.Symbol.Type);
}
else
{
string fixed_name = tree.GetType().ToString()
.Replace("Antlr4.Runtime.Tree.", "");
fixed_name = Regex.Replace(fixed_name, "^.*[+]", "");
fixed_name = fixed_name.Substring(0, fixed_name.Length - "Context".Length);
fixed_name = fixed_name[0].ToString().ToLower()
+ fixed_name.Substring(1);
StartLine(sb, tree, stream, level);
sb.Append("( " + fixed_name);
sb.AppendLine();
}
for (int i = 0; i < tree.ChildCount; ++i)
{
IParseTree c = tree.GetChild(i);
c.ParenthesizedAST(sb, stream, level + 1);
}
if (level == 0)
{
for (int k = 0; k < 1 + changed - level; ++k)
{
sb.Append(") ");
}
sb.AppendLine();
changed = 0;
}
}
private static void StartLine(StringBuilder sb, IParseTree tree, CommonTokenStream stream, int level = 0)
{
if (changed - level >= 0)
{
if (!first_time)
{
for (int j = 0; j < level; ++j)
{
sb.Append(" ");
}
for (int k = 0; k < 1 + changed - level; ++k)
{
sb.Append(") ");
}
sb.AppendLine();
}
changed = 0;
first_time = false;
}
changed = level;
for (int j = 0; j < level; ++j)
{
sb.Append(" ");
}
}
private static string ToLiteral(this string input)
{
using (StringWriter writer = new StringWriter())
{
string literal = input;
literal = literal.Replace("\\", "\\\\");
literal = literal.Replace("\b", "\\b");
literal = literal.Replace("\n", "\\n");
literal = literal.Replace("\t", "\\t");
literal = literal.Replace("\r", "\\r");
literal = literal.Replace("\f", "\\f");
literal = literal.Replace("\"", "\\\"");
literal = literal.Replace(string.Format("\" +{0}\t\"", Environment.NewLine), "");
return literal;
}
}
public static string PerformEscapes(this string s)
{
StringBuilder new_s = new StringBuilder();
new_s.Append(s.ToLiteral());
return new_s.ToString();
}
}
}