本文整理匯總了Java中org.antlr.v4.runtime.CommonTokenStream類的典型用法代碼示例。如果您正苦於以下問題:Java CommonTokenStream類的具體用法?Java CommonTokenStream怎麽用?Java CommonTokenStream使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CommonTokenStream類屬於org.antlr.v4.runtime包,在下文中一共展示了CommonTokenStream類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parse
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
/**
* @throws InvalidQueryException if the given query contains invalid characters or is incomplete
*/
public SqlGrammarParser.SelectStatementContext parse(String query) {
log.trace("About to parse [{}]", query);
long now = currentTimeMillis();
CharStream input = CharStreams.fromString(query);
SqlGrammarLexer lexer = new SqlGrammarLexer(input);
TokenStream tokenStream = new CommonTokenStream(lexer);
SqlGrammarParser parser = new SqlGrammarParser(tokenStream);
parser.removeErrorListeners();
parser.addErrorListener(new RaiseExceptionErrorListener());
SqlGrammarParser.SelectStatementContext result = parser.selectStatement();
log.trace("Successfully parsed [{}] into [{}] in [{}ms]",
query, result.toStringTree(parser), currentTimeMillis() - now);
return result;
}
示例2: parseString
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
@Override
public SortedMap<Integer, String> parseString(String fieldString) {
CharStream is = CharStreams.fromString(fieldString);
DrawGrammarLexer lex = new DrawGrammarLexer(is);
CommonTokenStream tokens = new CommonTokenStream(lex);
DrawGrammarParser parser = new DrawGrammarParser(tokens);
palette = Palette.makeDefaultPalette("DarkSpectrum");
final SortedMap<Integer, String> resultMap;
try {
resultMap = parser.root().map;
} catch (RecognitionException | NullPointerException | StringIndexOutOfBoundsException | RasterFormatException re) {
//Something wrong with the parsing do not update.
return null;
}
return resultMap;
}
示例3: main
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
/**
* @param args the command line arguments
* @throws java.io.IOException
* @throws java.net.URISyntaxException
*/
public static void main(String[] args) throws IOException, URISyntaxException {
final String entryPoint;
final URL res;
switch (args.length) {
case 2:
res = Paths.get(args[0]).toUri().toURL();
entryPoint = args[1];
break;
default:
System.err.println("Supply two parameters in the following order:\n- file name of the main function\n- name of the main function\n\nFor example: hello.ct main:argc:argv");
return;
}
final CharStream inp = CharStreams.fromStream(res.openStream());
final GrammarLexer lex = new GrammarLexer(inp);
final TokenStream toks = new CommonTokenStream(lex);
final GrammarParser parser = new GrammarParser(toks);
System.out.println(new Translator(res.toURI()).generate(parser.program(), entryPoint));
}
示例4: generateAST
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
private BooleanExpListNode generateAST(String code) {
FormalPropertyDescriptionLexer l = new FormalPropertyDescriptionLexer(new ANTLRInputStream(code));
CommonTokenStream ts = new CommonTokenStream(l);
FormalPropertyDescriptionParser p = new FormalPropertyDescriptionParser(ts);
BooleanExpScope declaredVars = new BooleanExpScope();
preAndPostConditionsDescription.getSymbolicVariableList().forEach((v) -> {
declaredVars.addTypeForId(v.getId(), v.getInternalTypeContainer());
});
return translator.generateFromSyntaxTree(
p.booleanExpList(),
electionDescription.getInputType().getType(),
electionDescription.getOutputType().getType(),
declaredVars);
}
示例5: link
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
/**
* Returns an ExecutableCode that can then be run by the processor. If the given code contains
* data section, linker will only generate memory addresses. Without loading process, referenced
* data will be unpredictable.
*
* @param code The code that will be linked.
*
* @return The executable code.
*
* @throws LabelError If an undefined label used or duplicate labels exist.
* @see Loader#load(Linker.ExecutableCode)
*/
public ExecutableCode link(String code) throws LabelError {
definedBranches.clear();
definedData.clear();
addressBook.clear();
secondPass = false;
this.code = parseCode(code);
AssemblerLexer lexer = new AssemblerLexer(CharStreams.fromString(code));
CommonTokenStream tokens = new CommonTokenStream(lexer);
AssemblerParser parser = new AssemblerParser(tokens);
AssemblerParser.ProgContext program = parser.prog();
visit(program);
secondPass = true;
visit(program);
List<Data> temp = new ArrayList<>(definedData.size());
definedData.forEach((label, data) -> {
temp.add(data);
});
return new ExecutableCode(this.code, temp);
}
示例6: buildAntlrTree
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
private SourceContext buildAntlrTree(String source) {
ANTLRInputStream stream = new ANTLRInputStream(source);
PainlessLexer lexer = new EnhancedPainlessLexer(stream, sourceName);
PainlessParser parser = new PainlessParser(new CommonTokenStream(lexer));
ParserErrorStrategy strategy = new ParserErrorStrategy(sourceName);
lexer.removeErrorListeners();
parser.removeErrorListeners();
if (settings.isPicky()) {
setupPicky(parser);
}
parser.setErrorHandler(strategy);
return parser.source();
}
示例7: callSql
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
public PlanNode callSql(String sqlstring, ArrayList list) throws FileNotFoundException, IOException {
for (int i=0; i< list.size() ; i++) {
sqlstring = sqlstring.replaceFirst("\\?", (String) list.get(i));
}
ANTLRInputStream input = new ANTLRInputStream( new ByteArrayInputStream(sqlstring.getBytes()));
PLSQLLexer lexer = new PLSQLLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PLSQLParser parser = new PLSQLParser(tokens);
parser.setBuildParseTree(true);
ParseTree tree = parser.sql_statement();
ParseTreeWalker walker = new ParseTreeWalker();
SQLModel model = new SQLModel();
walker.walk(new MyListener(model), tree);
return model.current;
}
示例8: GraphParser
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
public GraphParser(InputStream is) throws GraphParserException {
DOTLexer lexer = null;
DOTParser parser = null;
try {
lexer = new DOTLexer(new ANTLRInputStream(is));
lexer.addErrorListener(new ErrorListener());
parser = new DOTParser(new CommonTokenStream(lexer));
parser.setErrorHandler(new ExceptionErrorStrategy());
parser.addErrorListener(new ErrorListener());
ParseTree tree = parser.graph();
ParseTreeWalker.DEFAULT.walk(new NodeListener(), tree);
ParseTreeWalker.DEFAULT.walk(new EdgeListener(), tree);
}
catch (Throwable t) {
if (mErrMsg != null) throw new GraphParserException(mErrMsg, t);
if (lexer != null) {
mErrMsg = "at line " + lexer.getLine() + ":" + lexer.getCharPositionInLine();
throw new GraphParserException(mErrMsg, t);
}
throw new GraphParserException(t);
}
if (mErrMsg != null) throw new GraphParserException(mErrMsg);
}
示例9: parseInputStream
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
private void parseInputStream(CharStream inputStream, OboParseResultListener listener) {
final OboLexer l = new OboLexer(inputStream);
final Antlr4OboParser p = new Antlr4OboParser(new CommonTokenStream(l));
p.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
int charPositionInLine, String msg, RecognitionException e) {
throw new IllegalStateException("Failed to parse at line " + line + " due to " + msg, e);
}
});
if (debug) {
p.addErrorListener(new DiagnosticErrorListener());
}
p.addParseListener(new OboParserListener(listener));
p.oboFile();
}
示例10: parse
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
public EObject parse(final String typeExpression, final Scope scope) {
final CharStream charStream = CharStreams.fromString(typeExpression);
final TypeExpressionLexer lexer = new TypeExpressionLexer(charStream);
final TokenStream tokenStream = new CommonTokenStream(lexer);
final TypeExpressionParser typeExpressionParser = new TypeExpressionParser(tokenStream);
lexer.removeErrorListeners();
typeExpressionParser.removeErrorListeners();
final ParserErrorCollector errorCollector = new ParserErrorCollector();
lexer.addErrorListener(errorCollector);
typeExpressionParser.addErrorListener(errorCollector);
final TypeExpressionParser.Type_exprContext typeExpr = typeExpressionParser.type_expr();
final EObject anyType = new TypeExpressionBuilder(scope, ARRAY_TYPE).visit(typeExpr);
scope.getResource().getErrors().addAll(errorCollector.getErrors());
return anyType;
}
示例11: doLoad
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
@Override
protected void doLoad(final InputStream inputStream, final Map<?, ?> options) throws IOException {
final BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
final Optional<AbstractConstructor> optionalRootConstructor = getRootConstructor(bufferedInputStream);
if (optionalRootConstructor.isPresent()) {
final AbstractConstructor rootConstructor = optionalRootConstructor.get();
final RAMLCustomLexer lexer = new RAMLCustomLexer(uri, getURIConverter());
final TokenStream tokenStream = new CommonTokenStream(lexer);
final RAMLParser parser = new RAMLParser(tokenStream);
parser.removeErrorListeners();
final ParserErrorCollector errorCollector = new ParserErrorCollector();
parser.addErrorListener(errorCollector);
try {
rootConstructor.construct(parser, resourceScope);
validate();
} catch (final Exception e) {
getErrors().addAll(errorCollector.getErrors());
throw e;
}
}
}
示例12: api
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
@Ignore
@Test
public void api() {
final File apiFile = new File("/Users/mkoester/Development/commercetools-api-reference/api.raml");
assumeTrue(apiFile.exists());
final URI apiUri = URI.createURI(apiFile.toURI().toString());
final URIConverter uriConverter = new RamlResourceSet().getURIConverter();
final RAMLCustomLexer lexer = new RAMLCustomLexer(apiUri, uriConverter);
final TokenStream tokenStream = new CommonTokenStream(lexer);
final RAMLParser parser = new RAMLParser(tokenStream);
final Resource resource = new RamlResourceSet().createResource(apiUri);
final Scope scope = Scope.of(resource);
final TypeDeclarationResolver resolver = new TypeDeclarationResolver();
resolver.resolve(parser.api(), scope);
assertThat(resource.getErrors()).isEmpty();
}
示例13: scriptValidation
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
public void scriptValidation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String content=req.getParameter("content");
if(StringUtils.isNotBlank(content)){
ScriptType type=ScriptType.valueOf(req.getParameter("type"));
ANTLRInputStream antlrInputStream=new ANTLRInputStream(content);
RuleParserLexer lexer=new RuleParserLexer(antlrInputStream);
CommonTokenStream steam=new CommonTokenStream(lexer);
RuleParserParser parser=new RuleParserParser(steam);
parser.removeErrorListeners();
ScriptErrorListener errorListener=new ScriptErrorListener();
parser.addErrorListener(errorListener);
switch(type){
case Script:
parser.ruleSet();
break;
case DecisionNode:
parser.condition();
break;
case ScriptNode:
parser.actions();
}
List<ErrorInfo> infos=errorListener.getInfos();
writeObjectToJson(resp, infos);
}
}
示例14: build
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
public RuleSet build(String script) throws IOException{
ANTLRInputStream antlrInputStream=new ANTLRInputStream(script);
RuleParserLexer lexer=new RuleParserLexer(antlrInputStream);
CommonTokenStream tokenStream=new CommonTokenStream(lexer);
RuleParserParser parser=new RuleParserParser(tokenStream);
ScriptDecisionTableErrorListener errorListener=new ScriptDecisionTableErrorListener();
parser.addErrorListener(errorListener);
BuildRulesVisitor visitor=new BuildRulesVisitor(contextBuilders,tokenStream);
RuleSet ruleSet=visitor.visitRuleSet(parser.ruleSet());
rebuildRuleSet(ruleSet);
String error=errorListener.getErrorMessage();
if(error!=null){
throw new RuleException("Script parse error:"+error);
}
return ruleSet;
}
示例15: evaluate
import org.antlr.v4.runtime.CommonTokenStream; //導入依賴的package包/類
public static String evaluate(String templateString, Map<String, Object> environment) {
final Scanner s = new Scanner(templateString);
// TODO: This is a hack that fails if a string literal contains "}"
// TODO: It's because i couldn't be bothered nesting grammars.
s.useDelimiter("((?!\\\\)\\$\\{)|(})");
StringBuilder ret = new StringBuilder();
boolean isInsideExpr = templateString.startsWith("${");
while(s.hasNext()) {
final String str = s.next();
if (isInsideExpr) {
final JsLikeExpressionLexer lexer = new JsLikeExpressionLexer(CharStreams.fromString(str));
final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
final JsLikeExpressionParser parser = new JsLikeExpressionParser(tokenStream);
final TemplateStringEvaluatorVisitor visitor = new TemplateStringEvaluatorVisitor(environment);
ret.append(parser.expression().accept(visitor).toString());
} else {
ret.append(str);
}
isInsideExpr = !isInsideExpr;
}
return ret.toString();
}