@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;
}
java类org.antlr.v4.runtime.CommonTokenStream的实例源码
PainterLogic.java 文件源码
项目:bitbox
阅读 19
收藏 0
点赞 0
评论 0
App.java 文件源码
项目:CTalk
阅读 27
收藏 0
点赞 0
评论 0
/**
* @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));
}
SqlParser.java 文件源码
项目:coherence-sql
阅读 26
收藏 0
点赞 0
评论 0
/**
* @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;
}
CBMCCodeGenerator.java 文件源码
项目:BEAST
阅读 24
收藏 0
点赞 0
评论 0
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);
}
Linker.java 文件源码
项目:ETUmulator
阅读 33
收藏 0
点赞 0
评论 0
/**
* 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);
}
Walker.java 文件源码
项目:elasticsearch_my
阅读 27
收藏 0
点赞 0
评论 0
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();
}
SQLParser.java 文件源码
项目:SQLParser
阅读 29
收藏 0
点赞 0
评论 0
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;
}
GraphParser.java 文件源码
项目:digraph-parser
阅读 43
收藏 0
点赞 0
评论 0
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);
}
OboParser.java 文件源码
项目:ontolib
阅读 22
收藏 0
点赞 0
评论 0
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();
}
TypeExpressionConstructor.java 文件源码
项目:rest-modeling-framework
阅读 22
收藏 0
点赞 0
评论 0
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;
}
RamlResource.java 文件源码
项目:rest-modeling-framework
阅读 14
收藏 0
点赞 0
评论 0
@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;
}
}
}
TypeDeclarationResolverTest.java 文件源码
项目:rest-modeling-framework
阅读 20
收藏 0
点赞 0
评论 0
@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();
}
CommonServletHandler.java 文件源码
项目:urule
阅读 46
收藏 0
点赞 0
评论 0
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);
}
}
DSLRuleSetBuilder.java 文件源码
项目:urule
阅读 20
收藏 0
点赞 0
评论 0
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;
}
TemplateStringEvaluator.java 文件源码
项目:jobson
阅读 17
收藏 0
点赞 0
评论 0
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();
}
AntlrParser.java 文件源码
项目:pgcodekeeper
阅读 27
收藏 0
点赞 0
评论 0
private static <T extends Parser> T makeBasicParser(Class<T> parserClass,
ANTLRInputStream stream, String parsedObjectName, List<AntlrError> errors) {
Lexer lexer;
Parser parser;
if (parserClass.isAssignableFrom(SQLParser.class)) {
lexer = new SQLLexer(stream);
parser = new SQLParser(new CommonTokenStream(lexer));
} else if (parserClass.isAssignableFrom(IgnoreListParser.class)) {
lexer = new IgnoreListLexer(stream);
parser = new IgnoreListParser(new CommonTokenStream(lexer));
} else {
throw new IllegalArgumentException("Unknown parser class: " + parserClass);
}
CustomAntlrErrorListener err = new CustomAntlrErrorListener(parsedObjectName, errors);
lexer.removeErrorListeners();
lexer.addErrorListener(err);
parser.removeErrorListeners();
parser.addErrorListener(err);
return parserClass.cast(parser);
}
LingualQueryCreatorTest.java 文件源码
项目:Hydrograph
阅读 20
收藏 0
点赞 0
评论 0
@Test
public void itShouldGenerateLingualQuery() throws ParseException {
String dateInString = "2012-01-10 00:00:00";
remoteFilterJson.setCondition("(f1 not in ('1') and f2 = '"+dateInString+"') or (f3=23.23 and f4=1234.123)");
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
formatter.setTimeZone(TimeZone.getDefault());
Date date = formatter.parse(dateInString);
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
dateInString = formatter.format(date);
ANTLRInputStream stream = new ANTLRInputStream(remoteFilterJson.getCondition());
QueryParserLexer lexer = new QueryParserLexer(stream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
QueryParserParser parser = new QueryParserParser(tokenStream);
parser.removeErrorListeners();
LingualQueryCreator customVisitor = new LingualQueryCreator(remoteFilterJson.getSchema());
String lingualExpression = customVisitor.visit(parser.eval());
Assert.assertEquals(
"((\"f1\" is not null and \"f1\" not in('1')) and \"f2\" = timestamp '"+dateInString+"') or (\"f3\" = cast(23.23 as float) and \"f4\" = cast(1234.123 as double))",
lingualExpression);
}
LingualQueryCreatorTest.java 文件源码
项目:Hydrograph
阅读 19
收藏 0
点赞 0
评论 0
@Test
public void itShouldGenerateLingualQueryWithNotLikeClause() throws ParseException {
String dateInString = "2012-01-10 00:00:00";
remoteFilterJson.setCondition("(f1 not like 'condition' and f2 = '"+dateInString+"') or (f3=23.23 and f4=1234.123)");
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
formatter.setTimeZone(TimeZone.getDefault());
Date date = formatter.parse(dateInString);
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
dateInString = formatter.format(date);
ANTLRInputStream stream = new ANTLRInputStream(remoteFilterJson.getCondition());
QueryParserLexer lexer = new QueryParserLexer(stream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
QueryParserParser parser = new QueryParserParser(tokenStream);
parser.removeErrorListeners();
LingualQueryCreator customVisitor = new LingualQueryCreator(remoteFilterJson.getSchema());
String lingualExpression = customVisitor.visit(parser.eval());
Assert.assertEquals(
"((\"f1\" is not null and \"f1\" not like 'condition') and \"f2\" = timestamp '"+dateInString+"') or (\"f3\" = cast(23.23 as float) and \"f4\" = cast(1234.123 as double))",
lingualExpression);
}
LingualQueryCreatorTest.java 文件源码
项目:Hydrograph
阅读 24
收藏 0
点赞 0
评论 0
@Test
public void itShouldGenerateLingualQueryWithBetweenClause() {
remoteFilterJson.setCondition("f3 between 10 and 20 and f3 BETWEEN 10 AND 20");
ANTLRInputStream stream = new ANTLRInputStream(remoteFilterJson.getCondition());
QueryParserLexer lexer = new QueryParserLexer(stream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
QueryParserParser parser = new QueryParserParser(tokenStream);
parser.removeErrorListeners();
LingualQueryCreator customVisitor = new LingualQueryCreator(remoteFilterJson.getSchema());
String lingualExpression = customVisitor.visit(parser.eval());
Assert.assertEquals(
"\"f3\" between cast(10 as float) and cast(20 as float) and \"f3\" BETWEEN cast(10 as float) AND cast(20 as float)",
lingualExpression);
}
LingualQueryCreatorTest.java 文件源码
项目:Hydrograph
阅读 21
收藏 0
点赞 0
评论 0
@Test
public void itShouldGenerateLingualQueryWithMultipleCondition() {
remoteFilterJson.setCondition("f1 = 'or maybe' AND f1 <> 'or mat' and f3 between 10 and 20");
ANTLRInputStream stream = new ANTLRInputStream(remoteFilterJson.getCondition());
QueryParserLexer lexer = new QueryParserLexer(stream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
QueryParserParser parser = new QueryParserParser(tokenStream);
parser.removeErrorListeners();
LingualQueryCreator customVisitor = new LingualQueryCreator(remoteFilterJson.getSchema());
String lingualExpression = customVisitor.visit(parser.eval());
Assert.assertEquals(
"\"f1\" = 'or maybe' AND \"f1\" <> 'or mat' and \"f3\" between cast(10 as float) and cast(20 as float)",
lingualExpression);
}
LingualQueryCreatorTest.java 文件源码
项目:Hydrograph
阅读 17
收藏 0
点赞 0
评论 0
@Test
public void itShouldGenerateLingualQueryForBetweenClauseAndTimestampDataType() throws ParseException {
String dateInString1 = "1999-12-31 18:30:00";
String dateInString2 = "2000-01-11 18:30:00";
remoteFilterJson.setCondition("f2 between '"+dateInString1+"' AND '"+dateInString2+"'");
String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
formatter.setTimeZone(TimeZone.getDefault());
Date date1 = formatter.parse(dateInString1);
Date date2 = formatter.parse(dateInString2);
formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
dateInString1 = formatter.format(date1);
dateInString2 = formatter.format(date2);
ANTLRInputStream stream = new ANTLRInputStream(remoteFilterJson.getCondition());
QueryParserLexer lexer = new QueryParserLexer(stream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
QueryParserParser parser = new QueryParserParser(tokenStream);
parser.removeErrorListeners();
LingualQueryCreator customVisitor = new LingualQueryCreator(remoteFilterJson.getSchema());
String lingualExpression = customVisitor.visit(parser.eval());
Assert.assertEquals("\"f2\" between timestamp '"+dateInString1+"' AND timestamp '"+dateInString2+"'", lingualExpression);
}
OboParser.java 文件源码
项目:boqa
阅读 22
收藏 0
点赞 0
评论 0
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();
}
IntervalParser.java 文件源码
项目:stvs
阅读 24
收藏 0
点赞 0
评论 0
/**
* Parse an interval, for example <tt>[1,-]</tt> or <tt>-</tt> (a wildcard) or <tt>[1,4]</tt>.
* Only fixed values are allowed, no variables.
*
* @param intervalAsString the string to be parsed.
* @return a LowerBoundedInterval as the runtime representation of interval strings.
* @throws ParseException in case the string doesn't fit the given fixed-interval grammar.
*/
public static LowerBoundedInterval parse(String intervalAsString) throws ParseException {
CharStream charStream = new ANTLRInputStream(intervalAsString);
CellExpressionLexer lexer = new CellExpressionLexer(charStream);
TokenStream tokens = new CommonTokenStream(lexer);
CellExpressionParser parser = new CellExpressionParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new ThrowingErrorListener());
try {
CellExpressionParser.Fixed_intervalContext ctx = parser.fixed_interval();
if (ctx == null) {
throw new ParseException(0, 0, "Expected fixed interval");
}
return INSTANCE.visit(ctx);
} catch (ParseRuntimeException runtimeException) {
throw runtimeException.getParseException();
}
}
ParseTreeErrorListenerTest.java 文件源码
项目:athena
阅读 20
收藏 0
点赞 0
评论 0
/**
* Checks that no exception is generated for YANG file with valid syntax.
*/
@Test
public void checkValidYangFileForNoSyntaxError() throws IOException {
ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithoutSyntaxError.yang");
// Create a lexer that feeds off of input char stream.
GeneratedYangLexer lexer = new GeneratedYangLexer(input);
// Create a buffer of tokens pulled from the lexer.
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Create a parser that feeds off the tokens buffer.
GeneratedYangParser parser = new GeneratedYangParser(tokens);
// Remove console error listener.
parser.removeErrorListeners();
// Create instance of customized error listener.
ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
// Add customized error listener to catch errors during parsing.
parser.addErrorListener(parseTreeErrorListener);
// Begin parsing YANG file and generate parse tree.
ParseTree tree = parser.yangfile();
}
ParseTreeErrorListenerTest.java 文件源码
项目:athena
阅读 16
收藏 0
点赞 0
评论 0
/**
* Checks that exception is generated for YANG file with invalid syntax.
*/
@Test
public void checkInvalidYangFileForSyntaxError() throws IOException {
// Get the exception occurred during parsing.
thrown.expect(ParserException.class);
thrown.expect(CustomExceptionMatcher.errorLocation(3, 0));
thrown.expectMessage("no viable alternative at input 'yang-version 1\\nnamespace'");
ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithSyntaxError.yang");
// Create a lexer that feeds off of input char stream.
GeneratedYangLexer lexer = new GeneratedYangLexer(input);
// Create a buffer of tokens pulled from the lexer.
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Create a parser that feeds off the tokens buffer.
GeneratedYangParser parser = new GeneratedYangParser(tokens);
// Remove console error listener.
parser.removeErrorListeners();
// Create instance of customized error listener.
ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
// Add customized error listener to catch errors during parsing.
parser.addErrorListener(parseTreeErrorListener);
// Begin parsing YANG file and generate parse tree.
ParseTree tree = parser.yangfile();
}
ParserUtil.java 文件源码
项目:antlr-examples
阅读 19
收藏 0
点赞 0
评论 0
public static <L extends Lexer, P extends Parser> P newParser(
Function<CharStream, L> lexerFactory,
Function<TokenStream, P> parserFactory,
String input,
boolean useBailErrorStrategy,
boolean removeErrorListeners) {
CharStream charStream = new ANTLRInputStream(input);
L lexer = lexerFactory.apply(charStream);
if (removeErrorListeners) {
lexer.removeErrorListeners();
}
TokenStream tokenStream = new CommonTokenStream(lexer);
P parser = parserFactory.apply(tokenStream);
if (useBailErrorStrategy) {
parser.setErrorHandler(new BailErrorStrategy());
}
if (removeErrorListeners) {
parser.removeErrorListeners();
}
return parser;
}
SGFReader.java 文件源码
项目:Suji
阅读 20
收藏 0
点赞 0
评论 0
public SGFReader(String sgfString) {
ANTLRInputStream inputStream = new ANTLRInputStream(sgfString);
SGFLexer lexer = new SGFLexer(inputStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
parser = new SGFParser(tokens);
visitor = new SGFVisitor();
}
SGFVisitorTest.java 文件源码
项目:Suji
阅读 17
收藏 0
点赞 0
评论 0
@Before
public void commonSetup() {
ANTLRInputStream inputStream = new ANTLRInputStream("");
sgfLexer = new SGFLexer(inputStream);
CommonTokenStream tokenStream = new CommonTokenStream(sgfLexer);
parser = new SGFParser(tokenStream);
StringWriter errorWriter = new StringWriter();
errorListener = new SGFErrorListener(errorWriter);
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
visitor = new SGFVisitor();
}
TreeViewGenerator.java 文件源码
项目:Blindfold
阅读 21
收藏 0
点赞 0
评论 0
public static Forest generate(String code) throws SyntaxError {
Java8Lexer lexer = new Java8Lexer(CharStreams.fromString(code));
CommonTokenStream tokens = new CommonTokenStream(lexer);
Java8Parser parser = new Java8Parser(tokens);
Java8Parser.CompilationUnitContext ast = parser.compilationUnit();
if(parser.getNumberOfSyntaxErrors() > 0) {
throw new SyntaxError();
}
Forest forest = new Forest();
TreeViewListener treeViewListener = new TreeViewListener(forest);
ParseTreeWalker.DEFAULT.walk(treeViewListener, ast);
return forest;
}
ParsedCode.java 文件源码
项目:stvs
阅读 27
收藏 0
点赞 0
评论 0
/**
* Parses a code. The handlers and listeners provided as parameters are called with the results
* of the parsing; i.e. the parsedCodeListener is called with the resulting {@link ParsedCode},
* the parsedTokenHandler is called with the list of parsed tokens etc.
*
* @param input the source code to parse
* @param parsedTokenHandler a handler for lexed tokens
* @param syntaxErrorsListener listener for a list of {@link SyntaxError}s
* @param parsedCodeListener listener for parsed code.
*/
public static void parseCode(String input, ParsedTokenHandler parsedTokenHandler,
ParsedSyntaxErrorHandler syntaxErrorsListener, ParsedCodeHandler parsedCodeListener) {
SyntaxErrorListener syntaxErrorListener = new SyntaxErrorListener();
IEC61131Lexer lexer = lex(input, parsedTokenHandler, syntaxErrorListener);
TopLevelElements ast = parse(new CommonTokenStream(lexer), syntaxErrorListener);
syntaxErrorsListener.accept(syntaxErrorListener.getSyntaxErrors());
// Find types in parsed code
TypeDeclarationVisitor typeVisitor = new TypeDeclarationVisitor();
ast.accept(typeVisitor);
Map<String, Type> definedTypesByName = new HashMap<>();
typeVisitor.getDefinedTypes()
.forEach(type -> definedTypesByName.put(type.getTypeName(), type));
// Find IoVariables in parsed code
VariableVisitor variableVisitor = new VariableVisitor();
ast.accept(variableVisitor);
// Find code blocks in parsed code
BlockVisitor blockVisitor = new BlockVisitor();
ast.accept(blockVisitor);
List<FoldableCodeBlock> foldableCodeBlocks = blockVisitor.getFoldableCodeBlocks();
parsedCodeListener.accept(new ParsedCode(foldableCodeBlocks,
variableVisitor.getDefinedVariables(), typeVisitor.getDefinedTypes()));
}