1 module TokenStreamRewriterTest;
2 
3 import antlr.v4.runtime.ANTLRInputStream;
4 import antlr.v4.runtime.CommonToken;
5 import antlr.v4.runtime.CommonTokenStream;
6 import antlr.v4.runtime.DiagnosticErrorListener;
7 import antlr.v4.runtime.LexerNoViableAltException;
8 import antlr.v4.runtime.Token;
9 import antlr.v4.runtime.TokenStream;
10 import antlr.v4.runtime.TokenStreamRewriter : TokenStreamRewriter;
11 import antlr.v4.runtime.atn.ParserATNSimulator;
12 import antlr.v4.runtime.tree.ParseTreeWalker;
13 import dshould;
14 import RuleTranslatorBaseListener : RuleTranslatorBaseListener;
15 import RuleTranslatorLexer : RuleTranslatorLexer;
16 import RuleTranslatorParser: RuleTranslatorParser;
17 import std.array : join;
18 import std.conv : to;
19 import std.file;
20 import std.variant;
21 
22 auto input = [
23     `# Text definition DEFAS`,
24     `# for automatic announcements`,
25     `# Version 2018-02-21`,
26 
27     `rule Delay as DELAY de`,
28     `base de.Phrases`,
29 
30     `"Information zu"` ~ "\n"
31     ];
32 
33 string toString (in string[] source)
34 {
35     return source.join("\n");
36 }
37 
38 string getInput ()
39 {
40     return(toString(input));
41 }
42 
43 @("replaceAll")
44 unittest
45 {
46     class ReplaceAll : RuleTranslatorBaseListener
47     {
48 
49         TokenStreamRewriter rewriter;
50         Token firstToken;
51 
52         this(TokenStream tokens)
53             {
54                 rewriter = new TokenStreamRewriter(tokens);
55             }
56 
57         override public void enterFile_input(RuleTranslatorParser.File_inputContext ctx)
58             {
59                 firstToken = ctx.start;
60             }
61 
62         override public void exitStmt(RuleTranslatorParser.StmtContext ctx)
63             {
64                 Variant parameter = "alpha";
65                 rewriter.replace(firstToken, ctx.stop, parameter);
66             }
67     }
68 
69     auto expected = "alpha";
70 
71     auto antlrInput = new ANTLRInputStream(getInput);
72     auto lexer = new RuleTranslatorLexer(antlrInput);
73     auto cts = new CommonTokenStream(lexer);
74     cts.fill;
75     auto parser = new RuleTranslatorParser(cts);
76     // Specify entry point
77     auto rootContext = parser.file_input;
78     parser.numberOfSyntaxErrors.should.equal(0);
79     auto extractor = new ReplaceAll(cts);
80     auto walker = new ParseTreeWalker;
81     walker.walk(extractor, rootContext);
82     auto str = extractor.rewriter.getText.get!(string);
83     str.should.equal(expected);
84 }
85 
86 @("replace_and_delete")
87 unittest
88 {
89     class InsertTestListenerReplace : RuleTranslatorBaseListener
90     {
91         TokenStreamRewriter rewriter;
92 
93         this(TokenStream tokens)
94             {
95                 rewriter = new TokenStreamRewriter(tokens);
96             }
97 
98         override public void exitStmt(RuleTranslatorParser.StmtContext ctx)
99             {
100                 rewriter.replace(ctx.start, ctx.stop, Variant("gamma"));
101             }
102     }
103 
104     auto expected = [
105         "\nruleDelayasDELAYde",
106         `basede.Phrases`,
107         "gamma"
108         ];
109 
110     auto antlrInput = new ANTLRInputStream(getInput);
111     auto lexer = new RuleTranslatorLexer(antlrInput);
112     auto cts = new CommonTokenStream(lexer);
113     cts.fill;
114     auto tokens = cts.getTokens;
115     cts.getNumberOfOnChannelTokens.should.equal(15);
116     auto parser = new RuleTranslatorParser(cts);
117     // Specify entry point
118     auto rootContext = parser.file_input;
119     parser.numberOfSyntaxErrors.should.equal(0);
120     auto extractor = new InsertTestListenerReplace(cts);
121     auto walker = new ParseTreeWalker;
122     walker.walk(extractor, rootContext);
123     auto str = extractor.rewriter.getText.get!(string);
124 
125     str.should.equal(toString(expected));
126     extractor.rewriter.deleteProgram;
127     str = extractor.rewriter.getText.get!(string);
128     expected = [
129         "\nruleDelayasDELAYde",
130         `basede.Phrases`,
131         `"Information zu"` ~ "\n"
132         ];
133     str.should.equal(toString(expected));
134 }
135 
136 @("deleteT")
137 unittest
138 {
139     class InsertTestListenerDelete : RuleTranslatorBaseListener
140     {
141         TokenStreamRewriter rewriter;
142 
143         this(TokenStream tokens)
144             {
145                 rewriter = new TokenStreamRewriter(tokens);
146             }
147 
148         override public void exitStmt(RuleTranslatorParser.StmtContext ctx)
149             {
150                 rewriter.deleteT(ctx.start, ctx.stop);
151             }
152     }
153 
154     auto expected = [
155         "\nruleDelayasDELAYde",
156         "basede.Phrases\n"
157         ];
158 
159     auto antlrInput = new ANTLRInputStream(getInput);
160     auto lexer = new RuleTranslatorLexer(antlrInput);
161     auto cts = new CommonTokenStream(lexer);
162     cts.fill;
163     auto tokens = cts.getTokens;
164     cts.getNumberOfOnChannelTokens.should.equal(15);
165     auto parser = new RuleTranslatorParser(cts);
166     // Specify entry point
167     auto rootContext = parser.file_input;
168     parser.numberOfSyntaxErrors.should.equal(0);
169     auto extractor = new InsertTestListenerDelete(cts);
170     auto walker = new ParseTreeWalker;
171     walker.walk(extractor, rootContext);
172     auto str = extractor.rewriter.getText.get!(string);
173     str.should.equal(toString(expected));
174 }
175 
176 @("insert after and before")
177 unittest
178 {
179     class InsertTestListener : RuleTranslatorBaseListener
180     {
181         TokenStreamRewriter rewriter;
182 
183         this(TokenStream tokens)
184             {
185                 rewriter = new TokenStreamRewriter(tokens);
186             }
187 
188         override public void exitStmt(RuleTranslatorParser.StmtContext ctx)
189             {
190                 Variant str_a = "alpha";
191                 Variant str_b = "beta";
192                 rewriter.insertBefore(ctx.start, str_a);
193                 rewriter.insertAfter(ctx.start, str_b);
194             }
195     }
196 
197     auto expected = [
198         "\nruleDelayasDELAYde",
199         `basede.Phrases`,
200         `alpha"Information zu"beta` ~"\n",
201         ];
202 
203     auto antlrInput = new ANTLRInputStream(getInput);
204     auto lexer = new RuleTranslatorLexer(antlrInput);
205     auto cts = new CommonTokenStream(lexer);
206     cts.fill;
207     auto tokens = cts.getTokens;
208     cts.getNumberOfOnChannelTokens.should.equal(15);
209     auto parser = new RuleTranslatorParser(cts);
210     // Specify entry point
211     auto rootContext = parser.file_input;
212     parser.numberOfSyntaxErrors.should.equal(0);
213     auto extractor = new InsertTestListener(cts);
214     auto walker = new ParseTreeWalker;
215     walker.walk(extractor, rootContext);
216     auto str = extractor.rewriter.getText.get!(string);
217     str.should.equal(toString(expected));
218 }