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(T) : RewriteOperation!T
18 {
19 
20     public size_t lastIndex;
21 
22     public this(size_t from, size_t to, T text)
23     {
24         super(from, text);
25         lastIndex = to;
26     }
27 
28     /**
29      * @uml
30      * @override
31      */
32     public override size_t execute(ref T buf)
33     {
34         if (text) {
35             buf ~= text;
36         }
37         return lastIndex+1;
38     }
39 
40     /**
41      * @uml
42      * @override
43      */
44     public override string toString()
45     {
46         if (text is null)
47             {
48                 return format("<DeleteOp@%s..%s>", tokens.get(to!int(index)),
49                               tokens.get(to!int(lastIndex)));
50             }
51         return format("<ReplaceOp@%s..%s:\"%s\">", tokens.get(to!int(index)),
52                       tokens.get(to!int(lastIndex)), text);
53     }
54 
55 }