-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add conditionnal grammar file for #2
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
java-vtl-parser/src/test/java/imports/UnionParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |