From c7fb7dfd794700bf6977a907a8612e9b644e4fe4 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 4 Jul 2016 21:53:55 +0200 Subject: o Improved parser. --- .../main/test/io/trygvis/ld/FullScriptsTest.java | 34 +++++++ .../test/io/trygvis/ld/InputSectionSpecTest.java | 38 ++++++++ .../src/main/test/io/trygvis/ld/NameTest.java | 50 ++++++++++ .../ld/SystemOutReportingErrorListener.java | 32 +++++++ .../main/test/io/trygvis/ld/test1/Test1Test.java | 102 +++++++++++++++++++++ 5 files changed, 256 insertions(+) create mode 100644 cmake/elfinfo/src/main/test/io/trygvis/ld/FullScriptsTest.java create mode 100644 cmake/elfinfo/src/main/test/io/trygvis/ld/InputSectionSpecTest.java create mode 100644 cmake/elfinfo/src/main/test/io/trygvis/ld/NameTest.java create mode 100644 cmake/elfinfo/src/main/test/io/trygvis/ld/SystemOutReportingErrorListener.java create mode 100644 cmake/elfinfo/src/main/test/io/trygvis/ld/test1/Test1Test.java (limited to 'cmake/elfinfo/src/main/test/io/trygvis') diff --git a/cmake/elfinfo/src/main/test/io/trygvis/ld/FullScriptsTest.java b/cmake/elfinfo/src/main/test/io/trygvis/ld/FullScriptsTest.java new file mode 100644 index 0000000..1452f1f --- /dev/null +++ b/cmake/elfinfo/src/main/test/io/trygvis/ld/FullScriptsTest.java @@ -0,0 +1,34 @@ +package io.trygvis.ld; + +import io.trygvis.ld.test1.Test1Test; +import org.junit.Test; + +import java.io.File; + +public class FullScriptsTest { + + @Test + public void testD2000() throws Exception { + fullScript2("lds/d2000.ld"); + } + + @Test + public void testStm32() throws Exception { + fullScript2("lds/stm32.ld"); + } + + private void fullScript2(String fileName) throws Exception { + try { + LdScript script = LdScript.parse(new File(fileName)); + + System.out.println("--------------------------------------------------------"); + for (LdScript.MemoryArea area : script.memoryAreas) { + System.out.println(" " + area.name + "(" + area.prettyAttributes() + ") : ORIGIN = " + area.prettyOrigin() + ", LENGTH=" + area.prettyLength()); + } + } catch (LdScript.ParseErrorException e) { + Test1Test.showTokens(e.tokens, e.parser); + System.out.println("Got " + e.errors.size() + " errors:"); + e.errors.forEach(System.out::println); + } + } +} diff --git a/cmake/elfinfo/src/main/test/io/trygvis/ld/InputSectionSpecTest.java b/cmake/elfinfo/src/main/test/io/trygvis/ld/InputSectionSpecTest.java new file mode 100644 index 0000000..2950061 --- /dev/null +++ b/cmake/elfinfo/src/main/test/io/trygvis/ld/InputSectionSpecTest.java @@ -0,0 +1,38 @@ +package io.trygvis.ld; + +import io.trygvis.ld.antlr.GnuLdLexer; +import io.trygvis.ld.antlr.GnuLdParser; +import org.antlr.v4.runtime.ANTLRInputStream; +import org.antlr.v4.runtime.CommonTokenStream; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class InputSectionSpecTest { + @Test + public void testName1() throws Exception { + a("*(.text)", "(input_section_spec (input_section_spec_no_keep (wildcard_spec (wildcard_name *)) ( (file_name_list (wildcard_spec (wildcard_name .text))) )))"); + } + + // This doesn't feel right + @Test + public void testName2() throws Exception { + a("*(.text.*)", "(input_section_spec (input_section_spec_no_keep (wildcard_spec (wildcard_name *)) ( (file_name_list (file_name_list (wildcard_spec (wildcard_name .text.))) opt_comma (wildcard_spec (wildcard_name *))) )))"); + } + + @Test + public void testName3() throws Exception { + a("KEEP(*/init_high.cpp.obj(.isr_vectors))", "(input_section_spec KEEP ( (input_section_spec_no_keep (wildcard_spec (wildcard_name *)) / init_high.cpp.obj ( .isr_vectors) ))"); + } + + private void a(String input, String expected) { + GnuLdParser parser = new GnuLdParser(new CommonTokenStream(new GnuLdLexer(new ANTLRInputStream(input)))); + parser.setBuildParseTree(true); + parser.removeErrorListeners(); + parser.addErrorListener(new SystemOutReportingErrorListener(input)); + GnuLdParser.Input_section_specContext input_section_spec = parser.input_section_spec(); + + assertEquals(input, expected, input_section_spec.toStringTree(parser)); + assertEquals(0, parser.getNumberOfSyntaxErrors()); + } +} diff --git a/cmake/elfinfo/src/main/test/io/trygvis/ld/NameTest.java b/cmake/elfinfo/src/main/test/io/trygvis/ld/NameTest.java new file mode 100644 index 0000000..8d7502f --- /dev/null +++ b/cmake/elfinfo/src/main/test/io/trygvis/ld/NameTest.java @@ -0,0 +1,50 @@ +package io.trygvis.ld; + +import io.trygvis.ld.antlr.GnuLdLexer; +import io.trygvis.ld.antlr.GnuLdParser; +import org.antlr.v4.runtime.ANTLRInputStream; +import org.antlr.v4.runtime.CommonTokenStream; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class NameTest { + @Test + public void testName1() throws Exception { + a("foo", "foo"); + } + + @Test + public void testName2() throws Exception { + a("\"foo bar\"", "foo bar"); + } + + // TODO: not really expected + @Test + public void testName3() throws Exception { + a("\"foo bar\"", "foo bar"); + } + + private void a(String input, String expected) { + /* + CommonTokenStream tokens = new CommonTokenStream(new GnuLdLexer(new ANTLRInputStream(input))); + tokens.fill(); + for (Object tok : tokens.getTokens()) { + System.out.println(tok); + } + GnuLdParser parser = new GnuLdParser(tokens); + +// StringGnuLdVisitor visitor = new StringGnuLdVisitor(); + GnuLdParser.NameContext name = parser.name(); + + System.out.println("Input : |" + input + "|"); + System.out.println("String tree: " + name.toStringTree(parser)); + +// visitor.visit(name); + String actual = StringGnuLdVisitor.parseName(name); + + parser.setTrace(true); + assertEquals(input + "\n" + name.toStringTree(), expected, actual); + */ + } +} diff --git a/cmake/elfinfo/src/main/test/io/trygvis/ld/SystemOutReportingErrorListener.java b/cmake/elfinfo/src/main/test/io/trygvis/ld/SystemOutReportingErrorListener.java new file mode 100644 index 0000000..c21a2e2 --- /dev/null +++ b/cmake/elfinfo/src/main/test/io/trygvis/ld/SystemOutReportingErrorListener.java @@ -0,0 +1,32 @@ +package io.trygvis.ld; + +import org.antlr.v4.runtime.BaseErrorListener; +import org.antlr.v4.runtime.RecognitionException; +import org.antlr.v4.runtime.Recognizer; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class SystemOutReportingErrorListener extends BaseErrorListener { + @Nullable + private final String input; + + public SystemOutReportingErrorListener(@Nonnull String input) { + this.input = input; + } + + public SystemOutReportingErrorListener() { + input = null; + } + + public void syntaxError(Recognizer recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { + System.out.println("line " + line + ":" + charPositionInLine + " " + msg); + if (input != null) { + System.out.println(input); + for (int i = 1; i < charPositionInLine; i++) { + System.out.print(' '); + } + } + System.out.println('^'); + } +} diff --git a/cmake/elfinfo/src/main/test/io/trygvis/ld/test1/Test1Test.java b/cmake/elfinfo/src/main/test/io/trygvis/ld/test1/Test1Test.java new file mode 100644 index 0000000..ca3f49a --- /dev/null +++ b/cmake/elfinfo/src/main/test/io/trygvis/ld/test1/Test1Test.java @@ -0,0 +1,102 @@ +package io.trygvis.ld.test1; + +import org.antlr.v4.runtime.ANTLRInputStream; +import org.antlr.v4.runtime.BufferedTokenStream; +import org.antlr.v4.runtime.CommonToken; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.Parser; +import org.antlr.v4.runtime.Token; +import org.junit.Test; + +import javax.annotation.Nonnull; + +import static org.junit.Assert.assertEquals; + +public class Test1Test { + + @Test + public void test1() { + name("foo", "foo"); + } + + @Test + public void test2() { + name("\"foo bar\"", "foo bar"); + } + + private void name(String input, String expected) { + Test1Parser parser = load(input); + + Test1Parser.NameContext name = parser.name(); + System.out.println("String tree: " + name.toStringTree(parser)); + + String actual = name.getText(); + assertEquals(input + "\n" + name.toStringTree(parser), expected, actual); + } + + @Test + public void expr1() { + expr("a + b", "(expr (name a) + (name b))"); + } + + @Test + public void expr2() { + expr("\"foo bar\" + b", "(expr (name f o o b a r) + (name b))"); + } + + @Test + public void expr3() { + expr("\n\n\r\n\t\"foo bar\" + b\r\r\t", "(expr (name f o o b a r) + (name b))"); + } + + private void expr(String input, String expected) { + Test1Parser parser = load(input); + + Test1Parser.ExprContext expr = parser.expr(); + String stringTree = expr.toStringTree(parser); + + assertEquals(expected, stringTree); + } + + @Nonnull + private Test1Parser load(String input) { + System.out.println("Input : |" + input + "|"); + CommonTokenStream tokens = new CommonTokenStream(new Test1Lexer(new ANTLRInputStream(input))); + Test1Parser parser = new Test1Parser(tokens); + parser.setBuildParseTree(true); + parser.setTrace(true); + + tokens.fill(); + showTokens(tokens, parser); + return parser; + } + + public static void showTokens(BufferedTokenStream tokens, Parser parser) { + for (Token tok : tokens.getTokens()) { + String s; + if (tok instanceof CommonToken) { + CommonToken t = (CommonToken) tok; + + String channelStr = ""; + if (t.getChannel() > 0) { + channelStr = ",channel=" + t.getChannel(); + } + + String txt = t.getText(); + if (txt != null) { + txt = txt.replace("\n", "\\n"); + txt = txt.replace("\r", "\\r"); + txt = txt.replace("\t", "\\t"); + } else { + txt = ""; + } + + String type = parser.getVocabulary().getDisplayName(t.getType()); + s = "[@" + t.getTokenIndex() + "," + t.getStartIndex() + ":" + t.getStopIndex() + "=\'" + txt + "\',<" + type + ">" + channelStr + "," + t.getLine() + ":" + t.getCharPositionInLine() + "]"; + } else { + s = tok.toString(); + } + System.out.println(s); + } + } +} -- cgit v1.2.3