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