1 /*
2  * Copyright (c) 2012-2017 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 package org.antlr.v4.test.tool;
8 
9 import org.antlr.v4.runtime.atn.ATN;
10 import org.antlr.v4.runtime.atn.ATNDeserializer;
11 import org.antlr.v4.runtime.atn.ATNSerializer;
12 import org.antlr.v4.runtime.misc.Utils;
13 import org.antlr.v4.tool.Grammar;
14 import org.antlr.v4.tool.LexerGrammar;
15 import org.junit.Before;
16 import org.junit.Test;
17 
18 import java.util.Arrays;
19 
20 import static org.junit.Assert.assertEquals;
21 
22 public class TestATNDeserialization extends BaseJavaToolTest {
23 	@Before
24 	@Override
testSetUp()25 	public void testSetUp() throws Exception {
26 		super.testSetUp();
27 	}
28 
testSimpleNoBlock()29 	@Test public void testSimpleNoBlock() throws Exception {
30 		Grammar g = new Grammar(
31 			"parser grammar T;\n"+
32 			"a : A B ;");
33 		checkDeserializationIsStable(g);
34 	}
35 
testEOF()36 	@Test public void testEOF() throws Exception {
37 		Grammar g = new Grammar(
38 			"parser grammar T;\n"+
39 			"a : EOF ;");
40 		checkDeserializationIsStable(g);
41 	}
42 
testEOFInSet()43 	@Test public void testEOFInSet() throws Exception {
44 		Grammar g = new Grammar(
45 			"parser grammar T;\n"+
46 			"a : (EOF|A) ;");
47 		checkDeserializationIsStable(g);
48 	}
49 
testNot()50 	@Test public void testNot() throws Exception {
51 		Grammar g = new Grammar(
52 			"parser grammar T;\n"+
53 			"tokens {A, B, C}\n" +
54 			"a : ~A ;");
55 		checkDeserializationIsStable(g);
56 	}
57 
testWildcard()58 	@Test public void testWildcard() throws Exception {
59 		Grammar g = new Grammar(
60 			"parser grammar T;\n"+
61 			"tokens {A, B, C}\n" +
62 			"a : . ;");
63 		checkDeserializationIsStable(g);
64 	}
65 
testPEGAchillesHeel()66 	@Test public void testPEGAchillesHeel() throws Exception {
67 		Grammar g = new Grammar(
68 			"parser grammar T;\n"+
69 			"a : A | A B ;");
70 		checkDeserializationIsStable(g);
71 	}
72 
test3Alts()73 	@Test public void test3Alts() throws Exception {
74 		Grammar g = new Grammar(
75 			"parser grammar T;\n"+
76 			"a : A | A B | A B C ;");
77 		checkDeserializationIsStable(g);
78 	}
79 
testSimpleLoop()80 	@Test public void testSimpleLoop() throws Exception {
81 		Grammar g = new Grammar(
82 			"parser grammar T;\n"+
83 			"a : A+ B ;");
84 		checkDeserializationIsStable(g);
85 	}
86 
testRuleRef()87 	@Test public void testRuleRef() throws Exception {
88 		Grammar g = new Grammar(
89 			"parser grammar T;\n"+
90 			"a : e ;\n" +
91 			"e : E ;\n");
92 		checkDeserializationIsStable(g);
93 	}
94 
testLexerTwoRules()95 	@Test public void testLexerTwoRules() throws Exception {
96 		LexerGrammar lg = new LexerGrammar(
97 			"lexer grammar L;\n"+
98 			"A : 'a' ;\n" +
99 			"B : 'b' ;\n");
100 		checkDeserializationIsStable(lg);
101 	}
102 
testLexerEOF()103 	@Test public void testLexerEOF() throws Exception {
104 		LexerGrammar lg = new LexerGrammar(
105 			"lexer grammar L;\n"+
106 			"A : 'a' EOF ;\n");
107 		checkDeserializationIsStable(lg);
108 	}
109 
testLexerEOFInSet()110 	@Test public void testLexerEOFInSet() throws Exception {
111 		LexerGrammar lg = new LexerGrammar(
112 			"lexer grammar L;\n"+
113 			"A : 'a' (EOF|'\\n') ;\n");
114 		checkDeserializationIsStable(lg);
115 	}
116 
testLexerRange()117 	@Test public void testLexerRange() throws Exception {
118 		LexerGrammar lg = new LexerGrammar(
119 			"lexer grammar L;\n"+
120 			"INT : '0'..'9' ;\n");
121 		checkDeserializationIsStable(lg);
122 	}
123 
testLexerLoops()124 	@Test public void testLexerLoops() throws Exception {
125 		LexerGrammar lg = new LexerGrammar(
126 			"lexer grammar L;\n"+
127 			"INT : '0'..'9'+ ;\n");
128 		checkDeserializationIsStable(lg);
129 	}
130 
testLexerNotSet()131 	@Test public void testLexerNotSet() throws Exception {
132 		LexerGrammar lg = new LexerGrammar(
133 			"lexer grammar L;\n"+
134 			"ID : ~('a'|'b')\n ;");
135 		checkDeserializationIsStable(lg);
136 	}
137 
testLexerNotSetWithRange()138 	@Test public void testLexerNotSetWithRange() throws Exception {
139 		LexerGrammar lg = new LexerGrammar(
140 			"lexer grammar L;\n"+
141 			"ID : ~('a'|'b'|'e'|'p'..'t')\n ;");
142 		checkDeserializationIsStable(lg);
143 	}
144 
testLexerNotSetWithRange2()145 	@Test public void testLexerNotSetWithRange2() throws Exception {
146 		LexerGrammar lg = new LexerGrammar(
147 			"lexer grammar L;\n"+
148 			"ID : ~('a'|'b') ~('e'|'p'..'t')\n ;");
149 		checkDeserializationIsStable(lg);
150 	}
151 
test2ModesInLexer()152 	@Test public void test2ModesInLexer() throws Exception {
153 		LexerGrammar lg = new LexerGrammar(
154 			"lexer grammar L;\n"+
155 			"A : 'a'\n ;\n" +
156 			"mode M;\n" +
157 			"B : 'b';\n" +
158 			"mode M2;\n" +
159 			"C : 'c';\n");
160 		checkDeserializationIsStable(lg);
161 	}
162 
checkDeserializationIsStable(Grammar g)163 	protected void checkDeserializationIsStable(Grammar g) {
164 		ATN atn = createATN(g, false);
165 		char[] data = Utils.toCharArray(ATNSerializer.getSerialized(atn));
166 		String atnData = ATNSerializer.getDecoded(atn, Arrays.asList(g.getTokenNames()));
167 		ATN atn2 = new ATNDeserializer().deserialize(data);
168 		String atn2Data = ATNSerializer.getDecoded(atn2, Arrays.asList(g.getTokenNames()));
169 
170 		assertEquals(atnData, atn2Data);
171 	}
172 }
173