port working cql grammer into cql module

This commit is contained in:
Jonathan Shook 2020-09-23 23:28:36 -05:00
parent afc807285f
commit 1403e8ff0f
6 changed files with 2526 additions and 0 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,29 @@
package io.nosqlbench.generators.cql.lang;
import io.nosqlbench.generators.cql.generated.CqlParser;
import io.nosqlbench.generators.cql.generated.CqlParserBaseListener;
import org.antlr.v4.runtime.Token;
import java.util.List;
public class CQLAstBuilder extends CqlParserBaseListener {
@Override
public void exitCreateTable(CqlParser.CreateTableContext ctx) {
List<CqlParser.ColumnDefinitionContext> columnDefinitionContexts =
ctx.columnDefinitionList().columnDefinition();
for (CqlParser.ColumnDefinitionContext coldef : columnDefinitionContexts) {
CqlParser.ColumnContext column = coldef.column();
Token symbol = column.OBJECT_NAME().getSymbol();
CqlParser.DataTypeContext datatype = coldef.dataType();
CqlParser.DataTypeNameContext dtn = datatype.dataTypeName();
CqlParser.DataTypeDefinitionContext dtd = datatype.dataTypeDefinition();
if (dtd != null) {
List<CqlParser.DataTypeNameContext> dataTypeNameContexts = dtd.dataTypeName();
for (CqlParser.DataTypeNameContext dtnc : dataTypeNameContexts) {
System.out.println("here");
}
}
}
}
}

View File

@ -0,0 +1,28 @@
package io.nosqlbench.generators.cql.lang;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.ParseTreeListener;
import org.antlr.v4.runtime.tree.TerminalNode;
public class CQLTreeBuilder implements ParseTreeListener {
@Override
public void visitTerminal(TerminalNode node) {
}
@Override
public void visitErrorNode(ErrorNode node) {
}
@Override
public void enterEveryRule(ParserRuleContext ctx) {
}
@Override
public void exitEveryRule(ParserRuleContext ctx) {
}
}

View File

@ -0,0 +1,27 @@
package io.nosqlbench.generators.cql.lang;
import io.nosqlbench.generators.cql.generated.CqlLexer;
import io.nosqlbench.generators.cql.generated.CqlParser;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
public class ParserForCql {
public static void parse(String input) {
CharStream instream = CharStreams.fromString(input);
Lexer lexer = new CqlLexer(instream);
CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
CqlParser cqlParser = new CqlParser(commonTokenStream);
cqlParser.addParseListener(new CQLAstBuilder());
CqlParser.RootContext root = cqlParser.root();
}
// public static String fingerprint(String input) {
//
// }
}

View File

@ -0,0 +1,51 @@
package io.nosqlbench.generators.cql.lang;
import org.junit.Test;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class ParserForCqlTest {
@Test
public void parseAll() {
List<Path> cql3_examples = getSubPaths("cql3_examples");
for (Path examplePath : cql3_examples) {
try {
String example = Files.readString(examplePath, StandardCharsets.UTF_8);
ParserForCql.parse(example);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
private static List<Path> getSubPaths(String resourcePath) {
List<Path> subpaths = new ArrayList<>();
try {
Enumeration<URL> resources = ParserForCqlTest.class.getClassLoader().getResources(resourcePath);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
System.out.println("url=" + url.toExternalForm());
Path path = Paths.get(url.toURI());
Files.walk(path, FileVisitOption.FOLLOW_LINKS)
.filter(p -> !Files.isDirectory(p, LinkOption.NOFOLLOW_LINKS))
.forEach(subpaths::add);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return subpaths;
}
}