1 module TokenStreamRewriterTest;
2 
3 version(unittest) {
4 
5     import RuleTranslatorLexer;
6     import RuleTranslatorParser: RuleTranslatorParser;
7     import RuleTranslatorBaseListener;
8     import antlr.v4.runtime.ANTLRInputStream;
9     import antlr.v4.runtime.CommonToken;
10     import antlr.v4.runtime.CommonTokenStream;
11     import antlr.v4.runtime.TokenStream;
12     import antlr.v4.runtime.DiagnosticErrorListener;
13     import antlr.v4.runtime.LexerNoViableAltException;
14     import antlr.v4.runtime.Token;
15     import antlr.v4.runtime.TokenStreamRewriter : TokenStreamRewriter;
16     import antlr.v4.runtime.atn.ParserATNSimulator;
17     import antlr.v4.runtime.tree.ParseTreeWalker;
18     import dshould : be, equal, not, should;
19     import dshould.thrown;
20     import std.conv : to;
21     import std.stdio : File, writefln;
22     import std.file;
23     import std.variant;
24     import unit_threaded;
25 
26     public class InsertTestListenerReplace : RuleTranslatorBaseListener {
27 
28         TokenStreamRewriter rewriter;
29 
30         this(TokenStream tokens)
31         {
32             rewriter = new TokenStreamRewriter(tokens);
33         }
34         /**
35          * <p>The default implementation does nothing.</p>
36          */
37         override public void enterFile_input(RuleTranslatorParser.File_inputContext ctx) {
38             writefln("enterFile");
39         }
40         /**
41          * {@inheritDoc}
42          *
43          * <p>The default implementation does nothing.</p>
44          */
45         override public void exitStmt(RuleTranslatorParser.StmtContext ctx) {
46             debug(TokenStreamRewriter) {
47                 import std.stdio : writefln;
48                 writefln("exitStmt ctx.start = %s", ctx.start);
49             }
50             Variant parameter = "gamma";
51             rewriter.replace(ctx.start, ctx.stop, parameter);
52         }
53     }
54 
55     public class InsertTestListenerDelete : RuleTranslatorBaseListener {
56 
57         TokenStreamRewriter rewriter;
58 
59         this(TokenStream tokens)
60         {
61             rewriter = new TokenStreamRewriter(tokens);
62         }
63         /**
64          * <p>The default implementation does nothing.</p>
65          */
66         override public void enterFile_input(RuleTranslatorParser.File_inputContext ctx) {
67             writefln("enterFile");
68         }
69 
70         /**
71          * {@inheritDoc}
72          *
73          * <p>The default implementation does nothing.</p>
74          */
75         override public void exitStmt(RuleTranslatorParser.StmtContext ctx) {
76             debug(TokenStreamRewriter) {
77                 import std.stdio : writefln;
78                 writefln("exitStmt ctx.start = %s", ctx.start);
79             }
80             rewriter.deleteT(ctx.start, ctx.stop);
81         }
82     }
83 
84     public class InsertTestListenerResult : RuleTranslatorBaseListener {
85 
86         struct Result { ushort indent; string text;}
87         TokenStreamRewriter rewriter;
88 
89         this(TokenStream tokens)
90         {
91             rewriter = new TokenStreamRewriter(tokens);
92         }
93 
94         /**
95          * <p>The default implementation does nothing.</p>
96          */
97         override public void enterFile_input(RuleTranslatorParser.File_inputContext ctx) {
98             writefln("enterFile");
99         }
100         /**
101          * {@inheritDoc}
102          *
103          * <p>The default implementation does nothing.</p>
104          */
105         override public void exitStmt(RuleTranslatorParser.StmtContext ctx) {
106             //auto str = "alpha";
107             //rewriter.insertAfter(ctx.start, "beta");
108             //rewriter.insertBefore(ctx.start, str);
109         }
110     }
111 
112     public class InsertTestListener : RuleTranslatorBaseListener {
113 
114         TokenStreamRewriter rewriter;
115 
116         this(TokenStream tokens)
117         {
118             rewriter = new TokenStreamRewriter(tokens);
119         }
120         /**
121          * <p>The default implementation does nothing.</p>
122          */
123         override public void enterFile_input(RuleTranslatorParser.File_inputContext ctx) {
124             writefln("enterFile");
125         }
126         /**
127          * {@inheritDoc}struct Result { ushort indent; string text;}
128          *
129          * <p>The default implementation does nothing.</p>
130          */
131         override public void exitStmt(RuleTranslatorParser.StmtContext ctx) {
132             Variant str_a = "alpha";
133             Variant str_b = "beta";
134             rewriter.insertBefore(ctx.start, str_a);
135             rewriter.insertAfter(ctx.start, str_b);
136         }
137     }
138 
139     class Test {
140         @Tags("TokenStreamRewriter")
141         @("replace_and_delete")
142         unittest {
143             auto input =
144                 `# Text definition DEFAS
145 # for automatic announcements
146 # Version 2018-02-21
147 
148 rule Delay as DELAY de
149 base de.Phrases
150 
151 "Information zu"
152 `
153                 ;
154 
155             auto expected =
156                 `
157 ruleDelayasDELAYde
158 basede.Phrases
159 gamma`;
160 
161             auto antlrInput = new ANTLRInputStream(input);
162             auto lexer = new RuleTranslatorLexer(antlrInput);
163             auto cts = new CommonTokenStream(lexer);
164             cts.fill;
165             auto tokens = cts.getTokens;
166             foreach (i, t; tokens) {
167                 writefln("token %s: %s", i, t);
168             }
169             cts.getNumberOfOnChannelTokens.should.equal(15);
170             auto parser = new RuleTranslatorParser(cts);
171             // Specify entry point
172             auto rootContext = parser.file_input;
173             parser.numberOfSyntaxErrors.should.equal(0);
174             auto extractor = new InsertTestListenerReplace(cts);
175             auto walker = new ParseTreeWalker;
176             walker.walk(extractor, rootContext);
177             auto str = extractor.rewriter.getText.get!(string);
178             str.should.equal(expected);
179             extractor.rewriter.deleteProgram;
180             str = extractor.rewriter.getText.get!(string);
181             expected =
182                 `
183 ruleDelayasDELAYde
184 basede.Phrases
185 "Information zu"
186 `;
187             str.should.equal(expected);
188         }
189 
190         @Tags("TokenStreamRewriter")
191         @("insert after and before")
192         unittest {
193             auto input =
194                 `# Text definition DEFAS
195 # for automatic announcements
196 # Version 2018-02-21
197 
198 rule Delay as DELAY de
199 base de.Phrases
200 
201 "Information zu"
202 `
203                 ;
204 
205             auto expected =
206                 `
207 ruleDelayasDELAYde
208 basede.Phrases
209 alpha"Information zu"beta
210 `;
211 
212             auto antlrInput = new ANTLRInputStream(input);
213             auto lexer = new RuleTranslatorLexer(antlrInput);
214             auto cts = new CommonTokenStream(lexer);
215             cts.fill;
216             auto tokens = cts.getTokens;
217             foreach (i, t; tokens) {
218                 writefln("token %s: %s", i, t);
219             }
220             cts.getNumberOfOnChannelTokens.should.equal(15);
221             auto parser = new RuleTranslatorParser(cts);
222             // Specify entry point
223             auto rootContext = parser.file_input;
224             parser.numberOfSyntaxErrors.should.equal(0);
225             auto extractor = new InsertTestListener(cts);
226             auto walker = new ParseTreeWalker;
227             walker.walk(extractor, rootContext);
228             auto str = extractor.rewriter.getText.get!(string);
229             str.should.equal(expected);
230         }
231     }
232     @Tags("TokenStreamRewriter")
233         @("deleteT")
234         unittest {
235             auto input =
236                 `# Text definition DEFAS
237 # for automatic announcements
238 # Version 2018-02-21
239 
240 rule Delay as DELAY de
241 base de.Phrases
242 
243 "Information zu"
244 `
245                 ;
246 
247             auto expected =
248                 `
249 ruleDelayasDELAYde
250 basede.Phrases
251 `;
252 
253             auto antlrInput = new ANTLRInputStream(input);
254             auto lexer = new RuleTranslatorLexer(antlrInput);
255             auto cts = new CommonTokenStream(lexer);
256             cts.fill;
257             auto tokens = cts.getTokens;
258             foreach (i, t; tokens) {
259                 writefln("token %s: %s", i, t);
260             }
261             cts.getNumberOfOnChannelTokens.should.equal(15);
262             auto parser = new RuleTranslatorParser(cts);
263             // Specify entry point
264             auto rootContext = parser.file_input;
265             parser.numberOfSyntaxErrors.should.equal(0);
266             auto extractor = new InsertTestListenerDelete(cts);
267             auto walker = new ParseTreeWalker;
268             walker.walk(extractor, rootContext);
269             auto str = extractor.rewriter.getText.get!(string);
270             str.should.equal(expected);
271         }
272 }