001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.test;
029:
030: import org.antlr.runtime.ANTLRStringStream;
031: import org.antlr.runtime.CharStream;
032: import org.antlr.runtime.TokenRewriteStream;
033: import org.antlr.tool.Grammar;
034: import org.antlr.tool.Interpreter;
035:
036: public class TestTokenRewriteStream extends BaseTest {
037:
038: /** Public default constructor used by TestRig */
039: public TestTokenRewriteStream() {
040: }
041:
042: public void testInsertBeforeIndex0() throws Exception {
043: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
044: + "B : 'b';\n" + "C : 'c';\n");
045: CharStream input = new ANTLRStringStream("abc");
046: Interpreter lexEngine = new Interpreter(g, input);
047: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
048: tokens.LT(1); // fill buffer
049: tokens.insertBefore(0, "0");
050: String result = tokens.toString();
051: String expecting = "0abc";
052: assertEquals(result, expecting);
053: }
054:
055: public void testInsertAfterLastIndex() throws Exception {
056: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
057: + "B : 'b';\n" + "C : 'c';\n");
058: CharStream input = new ANTLRStringStream("abc");
059: Interpreter lexEngine = new Interpreter(g, input);
060: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
061: tokens.LT(1); // fill buffer
062: tokens.insertAfter(2, "x");
063: String result = tokens.toString();
064: String expecting = "abcx";
065: assertEquals(result, expecting);
066: }
067:
068: public void test2InsertBeforeAfterMiddleIndex() throws Exception {
069: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
070: + "B : 'b';\n" + "C : 'c';\n");
071: CharStream input = new ANTLRStringStream("abc");
072: Interpreter lexEngine = new Interpreter(g, input);
073: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
074: tokens.LT(1); // fill buffer
075: tokens.insertBefore(1, "x");
076: tokens.insertAfter(1, "x");
077: String result = tokens.toString();
078: String expecting = "axbxc";
079: assertEquals(result, expecting);
080: }
081:
082: public void testReplaceIndex0() throws Exception {
083: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
084: + "B : 'b';\n" + "C : 'c';\n");
085: CharStream input = new ANTLRStringStream("abc");
086: Interpreter lexEngine = new Interpreter(g, input);
087: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
088: tokens.LT(1); // fill buffer
089: tokens.replace(0, "x");
090: String result = tokens.toString();
091: String expecting = "xbc";
092: assertEquals(result, expecting);
093: }
094:
095: public void testReplaceLastIndex() throws Exception {
096: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
097: + "B : 'b';\n" + "C : 'c';\n");
098: CharStream input = new ANTLRStringStream("abc");
099: Interpreter lexEngine = new Interpreter(g, input);
100: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
101: tokens.LT(1); // fill buffer
102: tokens.replace(2, "x");
103: String result = tokens.toString();
104: String expecting = "abx";
105: assertEquals(result, expecting);
106: }
107:
108: public void testReplaceMiddleIndex() throws Exception {
109: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
110: + "B : 'b';\n" + "C : 'c';\n");
111: CharStream input = new ANTLRStringStream("abc");
112: Interpreter lexEngine = new Interpreter(g, input);
113: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
114: tokens.LT(1); // fill buffer
115: tokens.replace(1, "x");
116: String result = tokens.toString();
117: String expecting = "axc";
118: assertEquals(result, expecting);
119: }
120:
121: public void test2ReplaceMiddleIndex() throws Exception {
122: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
123: + "B : 'b';\n" + "C : 'c';\n");
124: CharStream input = new ANTLRStringStream("abc");
125: Interpreter lexEngine = new Interpreter(g, input);
126: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
127: tokens.LT(1); // fill buffer
128: tokens.replace(1, "x");
129: tokens.replace(1, "y");
130: String result = tokens.toString();
131: String expecting = "ayc";
132: assertEquals(result, expecting);
133: }
134:
135: public void testReplaceThenDeleteMiddleIndex() throws Exception {
136: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
137: + "B : 'b';\n" + "C : 'c';\n");
138: CharStream input = new ANTLRStringStream("abc");
139: Interpreter lexEngine = new Interpreter(g, input);
140: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
141: tokens.LT(1); // fill buffer
142: tokens.replace(1, "x");
143: tokens.delete(1);
144: String result = tokens.toString();
145: String expecting = "ac";
146: assertEquals(result, expecting);
147: }
148:
149: public void testReplaceThenInsertSameIndex() throws Exception {
150: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
151: + "B : 'b';\n" + "C : 'c';\n");
152: CharStream input = new ANTLRStringStream("abc");
153: Interpreter lexEngine = new Interpreter(g, input);
154: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
155: tokens.LT(1); // fill buffer
156: tokens.replace(0, "x");
157: tokens.insertBefore(0, "0");
158: String result = tokens.toString();
159: String expecting = "0xbc";
160: assertEquals(result, expecting);
161: }
162:
163: public void testReplaceThen2InsertSameIndex() throws Exception {
164: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
165: + "B : 'b';\n" + "C : 'c';\n");
166: CharStream input = new ANTLRStringStream("abc");
167: Interpreter lexEngine = new Interpreter(g, input);
168: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
169: tokens.LT(1); // fill buffer
170: tokens.replace(0, "x");
171: tokens.insertBefore(0, "y");
172: tokens.insertBefore(0, "z");
173: String result = tokens.toString();
174: String expecting = "zyxbc";
175: assertEquals(result, expecting);
176: }
177:
178: public void testInsertThenReplaceSameIndex() throws Exception {
179: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
180: + "B : 'b';\n" + "C : 'c';\n");
181: CharStream input = new ANTLRStringStream("abc");
182: Interpreter lexEngine = new Interpreter(g, input);
183: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
184: tokens.LT(1); // fill buffer
185: tokens.insertBefore(0, "0");
186: tokens.replace(0, "x");
187: String result = tokens.toString();
188: String expecting = "0xbc";
189: assertEquals(result, expecting);
190: }
191:
192: public void test2InsertMiddleIndex() throws Exception {
193: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
194: + "B : 'b';\n" + "C : 'c';\n");
195: CharStream input = new ANTLRStringStream("abc");
196: Interpreter lexEngine = new Interpreter(g, input);
197: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
198: tokens.LT(1); // fill buffer
199: tokens.insertBefore(1, "x");
200: tokens.insertBefore(1, "y");
201: String result = tokens.toString();
202: String expecting = "ayxbc";
203: assertEquals(result, expecting);
204: }
205:
206: public void test2InsertThenReplaceIndex0() throws Exception {
207: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
208: + "B : 'b';\n" + "C : 'c';\n");
209: CharStream input = new ANTLRStringStream("abc");
210: Interpreter lexEngine = new Interpreter(g, input);
211: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
212: tokens.LT(1); // fill buffer
213: tokens.insertBefore(0, "x");
214: tokens.insertBefore(0, "y");
215: tokens.replace(0, "z");
216: String result = tokens.toString();
217: String expecting = "yxzbc";
218: assertEquals(result, expecting);
219: }
220:
221: public void testReplaceThenInsertBeforeLastIndex() throws Exception {
222: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
223: + "B : 'b';\n" + "C : 'c';\n");
224: CharStream input = new ANTLRStringStream("abc");
225: Interpreter lexEngine = new Interpreter(g, input);
226: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
227: tokens.LT(1); // fill buffer
228: tokens.replace(2, "x");
229: tokens.insertBefore(2, "y");
230: String result = tokens.toString();
231: String expecting = "abyx";
232: assertEquals(result, expecting);
233: }
234:
235: public void testInsertThenReplaceLastIndex() throws Exception {
236: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
237: + "B : 'b';\n" + "C : 'c';\n");
238: CharStream input = new ANTLRStringStream("abc");
239: Interpreter lexEngine = new Interpreter(g, input);
240: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
241: tokens.LT(1); // fill buffer
242: tokens.insertBefore(2, "y");
243: tokens.replace(2, "x");
244: String result = tokens.toString();
245: String expecting = "abyx";
246: assertEquals(result, expecting);
247: }
248:
249: public void testReplaceThenInsertAfterLastIndex() throws Exception {
250: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
251: + "B : 'b';\n" + "C : 'c';\n");
252: CharStream input = new ANTLRStringStream("abc");
253: Interpreter lexEngine = new Interpreter(g, input);
254: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
255: tokens.LT(1); // fill buffer
256: tokens.replace(2, "x");
257: tokens.insertAfter(2, "y");
258: String result = tokens.toString();
259: String expecting = "abxy";
260: assertEquals(result, expecting);
261: }
262:
263: public void testReplaceRangeThenInsertInMiddle() throws Exception {
264: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
265: + "B : 'b';\n" + "C : 'c';\n");
266: CharStream input = new ANTLRStringStream("abcccba");
267: Interpreter lexEngine = new Interpreter(g, input);
268: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
269: tokens.LT(1); // fill buffer
270: tokens.replace(2, 4, "x");
271: tokens.insertBefore(3, "y"); // no effect; can't insert in middle of replaced region
272: String result = tokens.toString();
273: String expecting = "abxba";
274: assertEquals(result, expecting);
275: }
276:
277: public void testReplaceRangeThenInsertAtLeftEdge() throws Exception {
278: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
279: + "B : 'b';\n" + "C : 'c';\n");
280: CharStream input = new ANTLRStringStream("abcccba");
281: Interpreter lexEngine = new Interpreter(g, input);
282: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
283: tokens.LT(1); // fill buffer
284: tokens.replace(2, 4, "x");
285: tokens.insertBefore(2, "y");
286: String result = tokens.toString();
287: String expecting = "abyxba";
288: assertEquals(result, expecting);
289: }
290:
291: public void testReplaceRangeThenInsertAtRightEdge()
292: throws Exception {
293: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
294: + "B : 'b';\n" + "C : 'c';\n");
295: CharStream input = new ANTLRStringStream("abcccba");
296: Interpreter lexEngine = new Interpreter(g, input);
297: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
298: tokens.LT(1); // fill buffer
299: tokens.replace(2, 4, "x");
300: tokens.insertBefore(4, "y"); // no effect; within range of a replace
301: String result = tokens.toString();
302: String expecting = "abxba";
303: assertEquals(result, expecting);
304: }
305:
306: public void testReplaceRangeThenInsertAfterRightEdge()
307: throws Exception {
308: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
309: + "B : 'b';\n" + "C : 'c';\n");
310: CharStream input = new ANTLRStringStream("abcccba");
311: Interpreter lexEngine = new Interpreter(g, input);
312: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
313: tokens.LT(1); // fill buffer
314: tokens.replace(2, 4, "x");
315: tokens.insertAfter(4, "y");
316: String result = tokens.toString();
317: String expecting = "abxyba";
318: assertEquals(result, expecting);
319: }
320:
321: public void testReplaceAll() throws Exception {
322: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
323: + "B : 'b';\n" + "C : 'c';\n");
324: CharStream input = new ANTLRStringStream("abcccba");
325: Interpreter lexEngine = new Interpreter(g, input);
326: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
327: tokens.LT(1); // fill buffer
328: tokens.replace(0, 6, "x");
329: String result = tokens.toString();
330: String expecting = "x";
331: assertEquals(result, expecting);
332: }
333:
334: public void testReplaceSubsetThenFetch() throws Exception {
335: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
336: + "B : 'b';\n" + "C : 'c';\n");
337: CharStream input = new ANTLRStringStream("abcccba");
338: Interpreter lexEngine = new Interpreter(g, input);
339: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
340: tokens.LT(1); // fill buffer
341: tokens.replace(2, 4, "xyz");
342: String result = tokens.toString(0, 6);
343: String expecting = "abxyzba";
344: assertEquals(result, expecting);
345: }
346:
347: public void testReplaceThenReplaceSuperset() throws Exception {
348: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
349: + "B : 'b';\n" + "C : 'c';\n");
350: CharStream input = new ANTLRStringStream("abcccba");
351: Interpreter lexEngine = new Interpreter(g, input);
352: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
353: tokens.LT(1); // fill buffer
354: tokens.replace(2, 4, "xyz");
355: tokens.replace(2, 5, "foo"); // kills previous replace
356: String result = tokens.toString();
357: String expecting = "abfooa";
358: assertEquals(result, expecting);
359: }
360:
361: public void testReplaceThenReplaceLowerIndexedSuperset()
362: throws Exception {
363: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
364: + "B : 'b';\n" + "C : 'c';\n");
365: CharStream input = new ANTLRStringStream("abcccba");
366: Interpreter lexEngine = new Interpreter(g, input);
367: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
368: tokens.LT(1); // fill buffer
369: tokens.replace(2, 4, "xyz");
370: tokens.replace(1, 3, "foo"); // executes first since 1<2; then ignores replace@2 as it skips over 1..3
371: String result = tokens.toString();
372: String expecting = "afoocba";
373: assertEquals(result, expecting);
374: }
375:
376: public void testReplaceSingleMiddleThenOverlappingSuperset()
377: throws Exception {
378: Grammar g = new Grammar("lexer grammar t;\n" + "A : 'a';\n"
379: + "B : 'b';\n" + "C : 'c';\n");
380: CharStream input = new ANTLRStringStream("abcba");
381: Interpreter lexEngine = new Interpreter(g, input);
382: TokenRewriteStream tokens = new TokenRewriteStream(lexEngine);
383: tokens.LT(1); // fill buffer
384: tokens.replace(2, 2, "xyz");
385: tokens.replace(0, 3, "foo");
386: String result = tokens.toString();
387: String expecting = "fooa";
388: assertEquals(result, expecting);
389: }
390:
391: }
|