1 /*
2  * Copyright (c) 2012-2019 The ANTLR Project. All rights reserved.
3  * Use of this file is governed by the BSD 3-clause license that
4  * can be found in the LICENSE.txt file in the project root.
5  */
6 
7 module antlr.v4.runtime.ReplaceOp;
8 
9 import antlr.v4.runtime.RewriteOperation;
10 import std.conv;
11 import std.format;
12 
13 /**
14  * I'm going to try replacing range from x..y with (y-x)+1 ReplaceOp
15  *  instructions.
16  */
17 class ReplaceOp : RewriteOperation
18 {
19 
20     public size_t lastIndex;
21 
22     public this(size_t from, size_t to, string text)
23     {
24         super(from, text);
25         lastIndex = to;
26     }
27 
28     /**
29      * @uml
30      * @override
31      */
32     public override size_t execute(ref string buf)
33     {
34            debug(TokenStreamRewriter) {
35                 import std.stdio : writefln;
36                 writefln("ReplaceOp: execute buf = %s", buf);
37             }
38         if (text) {
39             buf ~= to!string(text);
40         }
41         return lastIndex+1;
42     }
43 
44     /**
45      * @uml
46      * @override
47      */
48     public override string toString()
49     {
50         if (text is null)
51             {
52                 return format("<DeleteOp@%s..%s>", tokens.get(to!int(index)),
53                               tokens.get(to!int(lastIndex)));
54             }
55         return format("<ReplaceOp@%s..%s:\"%s\">", tokens.get(to!int(index)),
56                       tokens.get(to!int(lastIndex)), text);
57     }
58 
59 }