From fa1eb6fcf2176ba47b181524da6514092fbe6cdf Mon Sep 17 00:00:00 2001 From: Hadrien Kohl Date: Mon, 24 Oct 2016 09:17:39 +0200 Subject: [PATCH] Add conditionnal grammar file for #2 --- .../src/main/antlr4/imports/Relational.g4 | 10 ++++ .../test/java/imports/UnionParserTest.java | 57 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 java-vtl-parser/src/main/antlr4/imports/Relational.g4 create mode 100644 java-vtl-parser/src/test/java/imports/UnionParserTest.java diff --git a/java-vtl-parser/src/main/antlr4/imports/Relational.g4 b/java-vtl-parser/src/main/antlr4/imports/Relational.g4 new file mode 100644 index 00000000..b3c43aef --- /dev/null +++ b/java-vtl-parser/src/main/antlr4/imports/Relational.g4 @@ -0,0 +1,10 @@ +grammar Relational; + +relationalExpression : unionExpression ; + +unionExpression : 'union' '(' datasetExpression (',' datasetExpression)+ ')' ; + +datasetExpression : 'datasetExpr' NUM+; + + +NUM : '0'..'9' ; diff --git a/java-vtl-parser/src/test/java/imports/UnionParserTest.java b/java-vtl-parser/src/test/java/imports/UnionParserTest.java new file mode 100644 index 00000000..d64f2811 --- /dev/null +++ b/java-vtl-parser/src/test/java/imports/UnionParserTest.java @@ -0,0 +1,57 @@ +package imports; + + +import com.google.common.io.Resources; +import org.antlr.v4.runtime.ANTLRInputStream; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.LexerInterpreter; +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.tool.Grammar; +import org.antlr.v4.tool.GrammarParserInterpreter; +import org.antlr.v4.tool.Rule; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.ExternalResource; + +import java.net.URL; +import java.nio.charset.Charset; + +import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.io.Resources.getResource; + +public class UnionParserTest { + + private static Grammar grammar; + @ClassRule + public static ExternalResource grammarResource = new ExternalResource() { + @Override + protected void before() throws Throwable { + URL grammarURL = getResource(this.getClass(), "Relational.g4"); + String grammarString = Resources.toString(grammarURL, Charset.defaultCharset()); + grammar = new Grammar(checkNotNull(grammarString)); + } + }; + + + @Test + public void testUnion() throws Exception { + String expression = "union ( dataset1, dataset2, dataset3)"; + + + } + + // TODO: Build a more robust way to test. + private String parse(String expression) { + LexerInterpreter lexerInterpreter = grammar.createLexerInterpreter( + new ANTLRInputStream(expression) + ); + GrammarParserInterpreter parserInterpreter = grammar.createGrammarParserInterpreter( + new CommonTokenStream(lexerInterpreter) + ); + + Rule clause = grammar.getRule("clauseExpression"); + parserInterpreter.setErrorHandler(new GrammarParserInterpreter.BailButConsumeErrorStrategy()); + ParserRuleContext parse = parserInterpreter.parse(clause.index); + return parse.toStringTree(parserInterpreter); + } +}