diff --git a/contributors.txt b/contributors.txt index b249c21b75..d71c0a820c 100644 --- a/contributors.txt +++ b/contributors.txt @@ -319,3 +319,4 @@ YYYY/MM/DD, github id, Full name, email 2021/10/10, tools4origins, Erwan Guyomarc'h, contact@erwan-guyomarch.fr 2021/10/19, jcking, Justin King, jcking@google.com 2021/10/31, skef, Skef Iterum, github@skef.org +2021/12/25, Tinker1024, Tinker1024, tinker@huawei.com diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/InterpDataReaderTest.interp b/runtime-testsuite/resources/org/antlr/v4/test/runtime/InterpDataReaderTest.interp new file mode 100644 index 0000000000..c8e9bb2d25 --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/InterpDataReaderTest.interp @@ -0,0 +1,50 @@ +token literal names: +null +'=' +'*' +'/' +'+' +'-' +'(' +')' +null +null +null +null + +token symbolic names: +null +null +null +null +null +null +null +null +ID +INT +NEWLINE +WS + +rule names: +T__0 +T__1 +T__2 +T__3 +T__4 +T__5 +T__6 +ID +INT +NEWLINE +WS + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 13, 61, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 6, 9, 41, 10, 9, 13, 9, 14, 9, 42, 3, 10, 6, 10, 46, 10, 10, 13, 10, 14, 10, 47, 3, 11, 5, 11, 51, 10, 11, 3, 11, 3, 11, 3, 12, 6, 12, 56, 10, 12, 13, 12, 14, 12, 57, 3, 12, 3, 12, 2, 2, 13, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 3, 2, 5, 4, 2, 67, 92, 99, 124, 3, 2, 50, 59, 4, 2, 11, 11, 34, 34, 2, 64, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 3, 25, 3, 2, 2, 2, 5, 27, 3, 2, 2, 2, 7, 29, 3, 2, 2, 2, 9, 31, 3, 2, 2, 2, 11, 33, 3, 2, 2, 2, 13, 35, 3, 2, 2, 2, 15, 37, 3, 2, 2, 2, 17, 40, 3, 2, 2, 2, 19, 45, 3, 2, 2, 2, 21, 50, 3, 2, 2, 2, 23, 55, 3, 2, 2, 2, 25, 26, 7, 63, 2, 2, 26, 4, 3, 2, 2, 2, 27, 28, 7, 44, 2, 2, 28, 6, 3, 2, 2, 2, 29, 30, 7, 49, 2, 2, 30, 8, 3, 2, 2, 2, 31, 32, 7, 45, 2, 2, 32, 10, 3, 2, 2, 2, 33, 34, 7, 47, 2, 2, 34, 12, 3, 2, 2, 2, 35, 36, 7, 42, 2, 2, 36, 14, 3, 2, 2, 2, 37, 38, 7, 43, 2, 2, 38, 16, 3, 2, 2, 2, 39, 41, 9, 2, 2, 2, 40, 39, 3, 2, 2, 2, 41, 42, 3, 2, 2, 2, 42, 40, 3, 2, 2, 2, 42, 43, 3, 2, 2, 2, 43, 18, 3, 2, 2, 2, 44, 46, 9, 3, 2, 2, 45, 44, 3, 2, 2, 2, 46, 47, 3, 2, 2, 2, 47, 45, 3, 2, 2, 2, 47, 48, 3, 2, 2, 2, 48, 20, 3, 2, 2, 2, 49, 51, 7, 15, 2, 2, 50, 49, 3, 2, 2, 2, 50, 51, 3, 2, 2, 2, 51, 52, 3, 2, 2, 2, 52, 53, 7, 12, 2, 2, 53, 22, 3, 2, 2, 2, 54, 56, 9, 4, 2, 2, 55, 54, 3, 2, 2, 2, 56, 57, 3, 2, 2, 2, 57, 55, 3, 2, 2, 2, 57, 58, 3, 2, 2, 2, 58, 59, 3, 2, 2, 2, 59, 60, 8, 12, 2, 2, 60, 24, 3, 2, 2, 2, 7, 2, 42, 47, 50, 57, 3, 8, 2, 2] diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/InterpDataReaderTest2.interp b/runtime-testsuite/resources/org/antlr/v4/test/runtime/InterpDataReaderTest2.interp new file mode 100644 index 0000000000..8e4b1c2137 --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/InterpDataReaderTest2.interp @@ -0,0 +1,50 @@ +token symbolic names: +null +null +null +null +null +null +null +null +ID +INT +NEWLINE +WS + +token literal names: +null +'=' +'*' +'/' +'+' +'-' +'(' +')' +null +null +null +null + +rule names: +T__0 +T__1 +T__2 +T__3 +T__4 +T__5 +T__6 +ID +INT +NEWLINE +WS + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 13, 61, 8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7, 9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12, 3, 2, 3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8, 3, 8, 3, 9, 6, 9, 41, 10, 9, 13, 9, 14, 9, 42, 3, 10, 6, 10, 46, 10, 10, 13, 10, 14, 10, 47, 3, 11, 5, 11, 51, 10, 11, 3, 11, 3, 11, 3, 12, 6, 12, 56, 10, 12, 13, 12, 14, 12, 57, 3, 12, 3, 12, 2, 2, 13, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17, 10, 19, 11, 21, 12, 23, 13, 3, 2, 5, 4, 2, 67, 92, 99, 124, 3, 2, 50, 59, 4, 2, 11, 11, 34, 34, 2, 64, 2, 3, 3, 2, 2, 2, 2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2, 2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2, 2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 3, 25, 3, 2, 2, 2, 5, 27, 3, 2, 2, 2, 7, 29, 3, 2, 2, 2, 9, 31, 3, 2, 2, 2, 11, 33, 3, 2, 2, 2, 13, 35, 3, 2, 2, 2, 15, 37, 3, 2, 2, 2, 17, 40, 3, 2, 2, 2, 19, 45, 3, 2, 2, 2, 21, 50, 3, 2, 2, 2, 23, 55, 3, 2, 2, 2, 25, 26, 7, 63, 2, 2, 26, 4, 3, 2, 2, 2, 27, 28, 7, 44, 2, 2, 28, 6, 3, 2, 2, 2, 29, 30, 7, 49, 2, 2, 30, 8, 3, 2, 2, 2, 31, 32, 7, 45, 2, 2, 32, 10, 3, 2, 2, 2, 33, 34, 7, 47, 2, 2, 34, 12, 3, 2, 2, 2, 35, 36, 7, 42, 2, 2, 36, 14, 3, 2, 2, 2, 37, 38, 7, 43, 2, 2, 38, 16, 3, 2, 2, 2, 39, 41, 9, 2, 2, 2, 40, 39, 3, 2, 2, 2, 41, 42, 3, 2, 2, 2, 42, 40, 3, 2, 2, 2, 42, 43, 3, 2, 2, 2, 43, 18, 3, 2, 2, 2, 44, 46, 9, 3, 2, 2, 45, 44, 3, 2, 2, 2, 46, 47, 3, 2, 2, 2, 47, 45, 3, 2, 2, 2, 47, 48, 3, 2, 2, 2, 48, 20, 3, 2, 2, 2, 49, 51, 7, 15, 2, 2, 50, 49, 3, 2, 2, 2, 50, 51, 3, 2, 2, 2, 51, 52, 3, 2, 2, 2, 52, 53, 7, 12, 2, 2, 53, 22, 3, 2, 2, 2, 54, 56, 9, 4, 2, 2, 55, 54, 3, 2, 2, 2, 56, 57, 3, 2, 2, 2, 57, 55, 3, 2, 2, 2, 57, 58, 3, 2, 2, 2, 58, 59, 3, 2, 2, 2, 59, 60, 8, 12, 2, 2, 60, 24, 3, 2, 2, 2, 7, 2, 42, 47, 50, 57, 3, 8, 2, 2] diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/java/TestInterpreterDataReader.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/java/TestInterpreterDataReader.java new file mode 100644 index 0000000000..c370d2f61d --- /dev/null +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/java/TestInterpreterDataReader.java @@ -0,0 +1,75 @@ +package org.antlr.v4.test.runtime.java; + +import org.antlr.v4.runtime.Vocabulary; +import org.antlr.v4.runtime.atn.ATN; +import org.antlr.v4.runtime.atn.ATNSerializer; +import org.antlr.v4.runtime.misc.InterpreterDataReader; +import org.junit.Assert; +import org.junit.Test; + +import java.lang.reflect.Field; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class TestInterpreterDataReader { + + @Test + public void testParseFile() throws NoSuchFieldException, IllegalAccessException { + final ClassLoader loader = Thread.currentThread().getContextClassLoader(); + // pay attention to the path where the InterpDataReaderTest.interp is stored. + final URL stuff = loader.getResource("org/antlr/v4/test/runtime/InterpDataReaderTest.interp"); + Assert.assertNotNull(stuff); + + InterpreterDataReader.InterpreterData interpreterData = InterpreterDataReader.parseFile(stuff.getPath()); + Field atnField = interpreterData.getClass().getDeclaredField("atn"); + Field vocabularyField = interpreterData.getClass().getDeclaredField("vocabulary"); + Field ruleNamesField = interpreterData.getClass().getDeclaredField("ruleNames"); + Field channelsField = interpreterData.getClass().getDeclaredField("channels"); + Field modesField = interpreterData.getClass().getDeclaredField("modes"); + + atnField.setAccessible(true); + vocabularyField.setAccessible(true); + ruleNamesField.setAccessible(true); + channelsField.setAccessible(true); + modesField.setAccessible(true); + + ATN atn = (ATN) atnField.get(interpreterData); + Vocabulary vocabulary = (Vocabulary) vocabularyField.get(interpreterData); + List ruleNames = castList(ruleNamesField.get(interpreterData), String.class); + List channels = castList(channelsField.get(interpreterData), String.class); + List modes = castList(modesField.get(interpreterData), String.class); + + char[] atnChars = ATNSerializer.getSerializedAsChars(atn); + Assert.assertTrue(atnChars.length > 0); + Assert.assertNotNull(vocabulary); + Assert.assertEquals(11, ruleNames.size()); + Assert.assertEquals(2, channels.size()); + Assert.assertEquals(1, modes.size()); + } + + @Test + public void testParseFileError() { + final ClassLoader loader = Thread.currentThread().getContextClassLoader(); + final URL stuff = loader.getResource("org/antlr/v4/test/runtime/InterpDataReaderTest2.interp"); + Assert.assertNotNull(stuff); + + try { + InterpreterDataReader.InterpreterData interpreterData = InterpreterDataReader.parseFile(stuff.getPath()); + } catch (Exception e) { + Assert.assertEquals(e.getClass(), RuntimeException.class); + Assert.assertEquals(e.getMessage(), "Unexpected data entry"); + } + } + + private List castList(Object obj, Class clazz) { + List result = new ArrayList(); + if (obj instanceof List) { + for (Object o : (List) obj) { + result.add(clazz.cast(o)); + } + return result; + } + return null; + } +} diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/InterpreterDataReader.java b/runtime/Java/src/org/antlr/v4/runtime/misc/InterpreterDataReader.java index 221fa55d3e..953bc6b757 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/InterpreterDataReader.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/InterpreterDataReader.java @@ -90,6 +90,7 @@ public static InterpreterData parseFile(String fileName) { result.ruleNames.add(line); } + line = br.readLine(); if ( line.equals("channel names:") ) { // Additional lexer data. result.channels = new ArrayList(); while ((line = br.readLine()) != null) {