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