本文整理匯總了Java中org.antlr.v4.runtime.Lexer.setInputStream方法的典型用法代碼示例。如果您正苦於以下問題:Java Lexer.setInputStream方法的具體用法?Java Lexer.setInputStream怎麽用?Java Lexer.setInputStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.antlr.v4.runtime.Lexer
的用法示例。
在下文中一共展示了Lexer.setInputStream方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parseQuery
import org.antlr.v4.runtime.Lexer; //導入方法依賴的package包/類
private ParserRuleContext parseQuery (String q) {
// TODO replace AqlLexer with lexer for your Antlr4 grammar!
Lexer qlLexer = new AqlLexer((CharStream) null);
ParserRuleContext tree = null;
// Like p. 111
try {
// Tokenize input data
ANTLRInputStream input = new ANTLRInputStream(q);
qlLexer.setInputStream(input);
CommonTokenStream tokens = new CommonTokenStream(qlLexer);
// TODO replace AqlParser with parser for your Antlr4
// grammar!
parser = new AqlParser(tokens);
// Don't throw out erroneous stuff
parser.setErrorHandler(new BailErrorStrategy());
parser.removeErrorListeners();
// Get starting rule from parser
// TODO replace AqlParser with parser for your Antlr4
// grammar!
Method startRule = AqlParser.class.getMethod("start");
tree = (ParserRuleContext) startRule
.invoke(parser, (Object[]) null);
}
// Some things went wrong ...
catch (Exception e) {
System.err.println(e.getMessage());
}
// Return the generated tree
return tree;
}
示例2: parseAnnisQuery
import org.antlr.v4.runtime.Lexer; //導入方法依賴的package包/類
private ParserRuleContext parseAnnisQuery (String query) {
Lexer lexer = new AqlLexer((CharStream) null);
ParserRuleContext tree = null;
Antlr4DescriptiveErrorListener errorListener = new Antlr4DescriptiveErrorListener(
query);
// Like p. 111
try {
// Tokenize input data
ANTLRInputStream input = new ANTLRInputStream(query);
lexer.setInputStream(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
parser = new AqlParser(tokens);
// Don't throw out erroneous stuff
parser.setErrorHandler(new BailErrorStrategy());
lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
// Get starting rule from parser
Method startRule = AqlParser.class.getMethod("start");
tree = (ParserRuleContext) startRule.invoke(parser,
(Object[]) null);
}
// Some things went wrong ...
catch (Exception e) {
log.error("Could not parse query. "
+ "Please make sure it is well-formed.");
log.error(errorListener.generateFullErrorMsg().toString());
addError(errorListener.generateFullErrorMsg());
}
return tree;
}
示例3: process
import org.antlr.v4.runtime.Lexer; //導入方法依賴的package包/類
protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, InvocationTargetException, PrintException {
try {
ANTLRInputStream input = new ANTLRInputStream(r);
lexer.setInputStream(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
if ( showTokens ) {
for (Object tok : tokens.getTokens()) {
System.out.println(tok);
}
}
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.setTokenStream(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 ) {
tree.inspect(parser);
}
if ( psFile!=null ) {
tree.save(parser, psFile); // Generate postscript
}
}
catch (NoSuchMethodException nsme) {
System.err.println("No method for rule "+startRuleName+" or it has arguments");
}
}
finally {
if ( r!=null ) r.close();
if ( is!=null ) is.close();
}
}
示例4: process
import org.antlr.v4.runtime.Lexer; //導入方法依賴的package包/類
protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, InvocationTargetException, PrintException {
try {
ANTLRInputStream input = new ANTLRInputStream(r);
lexer.setInputStream(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
if ( showTokens ) {
for (Object tok : tokens.getTokens()) {
System.out.println(tok);
}
}
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.setTokenStream(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 ) {
Trees.save(tree, parser, psFile); // Generate postscript
}
}
catch (NoSuchMethodException nsme) {
System.err.println("No method for rule "+startRuleName+" or it has arguments");
}
}
finally {
if ( r!=null ) r.close();
if ( is!=null ) is.close();
}
}
示例5: process
import org.antlr.v4.runtime.Lexer; //導入方法依賴的package包/類
protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser,
InputStream is, Reader r, String filename) throws IOException, IllegalAccessException,
InvocationTargetException, PrintException, ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException {
try {
ANTLRInputStream input = new ANTLRInputStream(r);
lexer.setInputStream(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
if (this.startRuleName.equals(App.LEXER_START_RULE_NAME)) {
return;
}
parser.setBuildParseTree(true);
parser.setTokenStream(tokens);
if (this.startRuleName.equals("")){
this.startRuleName = parser.getRuleNames()[0];
}
try {
Method startRule = parserClass.getMethod(this.startRuleName);
ParserRuleContext tree = (ParserRuleContext) startRule.invoke(parser,
(Object[]) null);
ParseTreeWalker walker = new ParseTreeWalker();
XMLListener proxy = new XMLListener(filename, lexer.getTokenTypeMap());
walker.walk(proxy, tree);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(proxy.doc),
new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
} catch (NoSuchMethodException nsme) {
System.err.println("No method for rule " + this.startRuleName
+ " or it has arguments");
}
} finally {
if (r != null) {
r.close();
}
if (is != null) {
is.close();
}
}
}
示例6: parse
import org.antlr.v4.runtime.Lexer; //導入方法依賴的package包/類
protected SpecificationContext parse(Lexer lexer, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, PrintException {
ANTLRInputStream input = new ANTLRInputStream(r);
lexer.setInputStream(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
if (options.showTokens) {
for (Object tok : tokens.getTokens()) {
System.out.println(tok);
}
}
if (options.diagnostics) {
parser.addErrorListener(new DiagnosticErrorListener());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
}
parser.setBuildParseTree(true);
// SLL overrides diagnostics
// %%% Not sure what it really is though.
if (options.sll) {
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
}
parser.setTokenStream(tokens);
parser.setTrace(options.trace);
SpecificationContext tree = ((WebkitIDLParser)parser).specification();
if (options.printTree) {
System.out.println(tree.toStringTree(parser));
}
if (options.gui) {
tree.inspect(parser);
}
if (options.psFile != null) {
tree.save(parser, options.psFile); // Generate postscript
}
return tree;
}
示例7: parse
import org.antlr.v4.runtime.Lexer; //導入方法依賴的package包/類
protected SpecificationContext parse(Lexer lexer, Parser parser, InputStream is, Reader r) throws IOException, IllegalAccessException, PrintException {
ANTLRInputStream input = new ANTLRInputStream(r);
lexer.setInputStream(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
if (options.showTokens) {
for (Object tok : tokens.getTokens()) {
System.out.println(tok);
}
}
if (options.diagnostics) {
parser.addErrorListener(new DiagnosticErrorListener());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
}
parser.setBuildParseTree(true);
// SLL overrides diagnostics
// %%% Not sure what it really is though.
if (options.sll) {
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
}
parser.setTokenStream(tokens);
parser.setTrace(options.trace);
SpecificationContext tree = ((GeckoIDLParser)parser).specification();
if (options.printTree) {
System.out.println(tree.toStringTree(parser));
}
if (options.gui) {
tree.inspect(parser);
}
if (options.psFile != null) {
tree.save(parser, options.psFile); // Generate postscript
}
return tree;
}