Skip to content

Commit

Permalink
Add conditionnal grammar file for #2
Browse files Browse the repository at this point in the history
  • Loading branch information
hadrienk committed Oct 24, 2016
1 parent 8919ba6 commit fa1eb6f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions java-vtl-parser/src/main/antlr4/imports/Relational.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
grammar Relational;

relationalExpression : unionExpression ;

unionExpression : 'union' '(' datasetExpression (',' datasetExpression)+ ')' ;

datasetExpression : 'datasetExpr' NUM+;


NUM : '0'..'9' ;
57 changes: 57 additions & 0 deletions java-vtl-parser/src/test/java/imports/UnionParserTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit fa1eb6f

Please sign in to comment.