use of org.antlr.v4.runtime.CharStream in project antlr4 by tunnelvisionlabs.
the class TestParseTreeMatcher method getPattern.
public ParseTreePattern getPattern(String grammarName, String pattern, String ruleName) throws Exception {
Class<? extends Lexer> lexerClass = loadLexerClassFromTempDir(grammarName + "Lexer");
Constructor<? extends Lexer> ctor = lexerClass.getConstructor(CharStream.class);
Lexer lexer = ctor.newInstance((CharStream) null);
Class<? extends Parser> parserClass = loadParserClassFromTempDir(grammarName + "Parser");
Constructor<? extends Parser> pctor = parserClass.getConstructor(TokenStream.class);
Parser parser = pctor.newInstance(new CommonTokenStream(lexer));
return parser.compileParseTreePattern(pattern, parser.getRuleIndex(ruleName));
}
use of org.antlr.v4.runtime.CharStream in project antlr4 by tunnelvisionlabs.
the class GrammarAST method dupTree.
public GrammarAST dupTree() {
GrammarAST t = this;
CharStream input = this.token.getInputStream();
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input);
return (GrammarAST) adaptor.dupTree(t);
}
use of org.antlr.v4.runtime.CharStream in project antlr4 by tunnelvisionlabs.
the class Tool method parse.
public GrammarRootAST parse(String fileName, CharStream in) {
try {
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in);
ToolANTLRLexer lexer = new ToolANTLRLexer(in, this);
CommonTokenStream tokens = new CommonTokenStream(lexer);
lexer.tokens = tokens;
ToolANTLRParser p = new ToolANTLRParser(tokens, this);
p.setTreeAdaptor(adaptor);
try {
ParserRuleReturnScope r = p.grammarSpec();
GrammarAST root = (GrammarAST) r.getTree();
if (root instanceof GrammarRootAST) {
((GrammarRootAST) root).hasErrors = lexer.getNumberOfSyntaxErrors() > 0 || p.getNumberOfSyntaxErrors() > 0;
assert ((GrammarRootAST) root).tokenStream == tokens;
if (grammarOptions != null) {
((GrammarRootAST) root).cmdLineOptions = grammarOptions;
}
return ((GrammarRootAST) root);
}
} catch (v3TreeGrammarException e) {
errMgr.grammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location);
}
return null;
} catch (RecognitionException re) {
// TODO: do we gen errors now?
ErrorManager.internalError("can't generate this message at moment; antlr recovers");
}
return null;
}
use of org.antlr.v4.runtime.CharStream in project SpinalTap by airbnb.
the class MysqlSchemaDatabase method addSourcePrefix.
@VisibleForTesting
String addSourcePrefix(@NotNull final String ddl) {
CharStream charStream = CharStreams.fromString(ddl);
MySQLLexer lexer = new MySQLLexer(charStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MySQLParser parser = new MySQLParser(tokens);
ParseTree tree = parser.root();
ParseTreeWalker walker = new ParseTreeWalker();
MySQLDBNamePrefixAdder prefixAdder = new MySQLDBNamePrefixAdder(tokens);
walker.walk(prefixAdder, tree);
return prefixAdder.rewriter.getText();
}
use of org.antlr.v4.runtime.CharStream in project antlr4 by tunnelvisionlabs.
the class TestRig method process.
protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, CharStream input) throws IOException, IllegalAccessException, InvocationTargetException, PrintException {
lexer.setInputStream(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
if (showTokens) {
for (Token tok : tokens.getTokens()) {
if (tok instanceof CommonToken) {
System.out.println(((CommonToken) tok).toString(lexer));
} else {
System.out.println(tok.toString());
}
}
}
if (startRuleName.equals(LEXER_START_RULE_NAME))
return;
if (diagnostics) {
parser.addErrorListener(new DiagnosticErrorListener());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
}
if (printTree || gui || psFile != null) {
parser.setBuildParseTree(true);
}
if (SLL) {
// overrides diagnostics
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
}
parser.setInputStream(tokens);
parser.setTrace(trace);
try {
Method startRule = parserClass.getMethod(startRuleName);
ParserRuleContext tree = (ParserRuleContext) startRule.invoke(parser, (Object[]) null);
if (printTree) {
System.out.println(tree.toStringTree(parser));
}
if (gui) {
Trees.inspect(tree, parser);
}
if (psFile != null) {
// Generate postscript
Trees.save(tree, parser, psFile);
}
} catch (NoSuchMethodException nsme) {
System.err.println("No method for rule " + startRuleName + " or it has arguments");
}
}
Aggregations