Java Code Examples for org.antlr.v4.runtime.ANTLRErrorListener

The following are top voted examples for showing how to use org.antlr.v4.runtime.ANTLRErrorListener. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: protostuff-compiler   File: ParserModule.java   View source code 6 votes vote down vote up
@Override
protected void configure() {
    bind(Importer.class).to(ImporterImpl.class);
    bind(FileDescriptorLoader.class).to(FileDescriptorLoaderImpl.class);
    bind(ANTLRErrorListener.class).to(ParseErrorLogger.class);
    bind(ANTLRErrorStrategy.class).to(BailErrorStrategy.class);
    bind(ProtoContext.class)
            .annotatedWith(Names.named(DESCRIPTOR_PROTO))
            .toProvider(DefaultDescriptorProtoProvider.class);

    Multibinder<ProtoContextPostProcessor> postProcessors = Multibinder
            .newSetBinder(binder(), ProtoContextPostProcessor.class);
    postProcessors.addBinding().to(ImportsPostProcessor.class);
    postProcessors.addBinding().to(TypeRegistratorPostProcessor.class);
    postProcessors.addBinding().to(TypeResolverPostProcessor.class);
    postProcessors.addBinding().to(ExtensionRegistratorPostProcessor.class);
    postProcessors.addBinding().to(OptionsPostProcessor.class);
    postProcessors.addBinding().to(UserTypeValidationPostProcessor.class);

    install(new FactoryModuleBuilder()
            .implement(FileReader.class, MultiPathFileReader.class)
            .build(FileReaderFactory.class));
}
 
Example 2
Project: jetbrains   File: ANTLRParseTreeToPSIConverter.java   View source code 6 votes vote down vote up
public ANTLRParseTreeToPSIConverter(Language language, Parser parser, PsiBuilder builder) {
	this.language = language;
	this.builder = builder;

	this.tokenElementTypes = PSIElementTypeFactory.getTokenIElementTypes(language);
	this.ruleElementTypes = PSIElementTypeFactory.getRuleIElementTypes(language);

	for (ANTLRErrorListener listener : parser.getErrorListeners()) {
		if (listener instanceof SyntaxErrorListener) {
			syntaxErrors = ((SyntaxErrorListener)listener).getSyntaxErrors();
			for (SyntaxError error : syntaxErrors) {
				// record first error per token
				int StartIndex = error.getOffendingSymbol().getStartIndex();
				if ( !tokenToErrorMap.containsKey(StartIndex) ) {
					tokenToErrorMap.put(StartIndex, error);
				}
			}
		}
	}
}
 
Example 3
Project: org.pshdl   File: PSHDLParser.java   View source code 6 votes vote down vote up
private static HDLPackage parseStream(CharStream input, String libURI, final Set<Problem> syntaxProblems, String src) {
	final PSHDLLangLexer lexer = new PSHDLLangLexer(input);
	final CommonTokenStream tokens = new CommonTokenStream(lexer);
	final PSHDLLang parser = new PSHDLLang(tokens);
	final ANTLRErrorListener listener = new SyntaxErrorCollector(tokens, syntaxProblems);
	lexer.removeErrorListeners();
	lexer.addErrorListener(listener);
	parser.removeErrorListeners();
	parser.addErrorListener(listener);
	final PsModelContext psModel = parser.psModel();
	if (syntaxProblems.size() == 0) {
		final HDLPackage hdl = ParserToModelExtension.toHDL(tokens, psModel, libURI, src);
		return hdl;
	}
	return null;
}
 
Example 4
Project: org.pshdl   File: PSHDLParser.java   View source code 6 votes vote down vote up
private static HDLExpression parseExpressionStream(CharStream input, final Set<Problem> syntaxProblems) {
	final PSHDLLangLexer lexer = new PSHDLLangLexer(input);
	final CommonTokenStream tokens = new CommonTokenStream(lexer);
	final ANTLRErrorListener parserListener = new SyntaxErrorCollector(tokens, syntaxProblems);
	final PSHDLLang parser = new PSHDLLang(tokens);
	parser.removeErrorListeners();
	parser.addErrorListener(parserListener);
	lexer.removeErrorListeners();
	lexer.addErrorListener(parserListener);
	final PsExpressionContext psExpression = parser.psExpression();
	if (syntaxProblems.size() == 0) {
		final HDLExpression hdl = ParserToModelExtension.toHDLExpression(tokens, psExpression);
		return hdl;
	}
	return null;
}
 
Example 5
Project: queryj   File: AbstractTemplatesTest.java   View source code 6 votes vote down vote up
/**
 * Sets up the Java parser.
 * @param javaFile the Java contents to parse.
 * @return the {@link JavaParser} instance.
 * @throws RecognitionException if the format is invalid.
 * @throws IOException if the source cannot be read.
 */
@SuppressWarnings("unchecked")
@NotNull
protected JavaParser setUpParser(@NotNull final File javaFile)
    throws RecognitionException,
           IOException
{
    @NotNull final JavaParser result;

    @NotNull final JavaLexer t_Lexer =
        new JavaLexer(new ANTLRFileStream(javaFile.getAbsolutePath()));

    @NotNull final ANTLRErrorListener errorListener = new PropagatingErrorListener(javaFile);

    t_Lexer.addErrorListener(errorListener);

    @NotNull final CommonTokenStream t_Tokens = new CommonTokenStream(t_Lexer);

    result = new JavaParser(t_Tokens);

    result.addErrorListener(errorListener);

    return result;
}
 
Example 6
Project: intellij-plugin-v4   File: AstBuilderListener.java   View source code 6 votes vote down vote up
public AstBuilderListener(Language language, Parser parser, PsiBuilder builder) {
	this.language = language;
	this.builder = builder;

	this.tokenElementTypes = PSIElementTypeFactory.getTokenIElementTypes(language, Arrays.asList(parser.getTokenNames()));
	this.ruleElementTypes = PSIElementTypeFactory.getRuleIElementTypes(language, Arrays.asList(parser.getRuleNames()));

	for (ANTLRErrorListener listener : parser.getErrorListeners()) {
		if (listener instanceof SyntaxErrorListener) {
			syntaxErrors.addAll(((SyntaxErrorListener)listener).getSyntaxErrors());
		}
	}

	Collections.sort(syntaxErrors, new Comparator<SyntaxError>() {

		@Override
		public int compare(SyntaxError o1, SyntaxError o2) {
			return Integer.valueOf(getStart(o1)).compareTo(getStart(o2));
		}
	});
}
 
Example 7
Project: libraries   File: JSSDParserFactory.java   View source code 5 votes vote down vote up
public JSSDParser create(final JSSDLexer lexer, final ANTLRErrorListener errorListener) {
  if (errorListener != null) {
    lexer.removeErrorListeners();
  }
  lexer.addErrorListener(errorListener);
  final JSSDParser parser = new JSSDParser(new CommonTokenStream(lexer));
  if (errorListener != null) {
    parser.removeErrorListeners();
  }
  parser.addErrorListener(errorListener);
  return parser;
}
 
Example 8
Project: jmespath-java   File: AntlrHelper.java   View source code 5 votes vote down vote up
public static JmesPathParser createParser(String input, ANTLRErrorListener errorListener) {
  JmesPathLexer lexer = createLexer(input, errorListener);
  CommonTokenStream tokens = new CommonTokenStream(lexer);
  JmesPathParser parser = new JmesPathParser(tokens);
  parser.removeErrorListener(ConsoleErrorListener.INSTANCE);
  parser.addErrorListener(errorListener);
  parser.setBuildParseTree(true);
  return parser;
}
 
Example 9
Project: netbeans-editorconfig-editor   File: ECParserResult.java   View source code 5 votes vote down vote up
public List<SyntaxError> getErrors() {
    List<? extends ANTLRErrorListener> errorListeners = parser.getErrorListeners();
    for (ANTLRErrorListener errorListener : errorListeners) {
        if (errorListener instanceof EditorConfigErrorListener) {
            EditorConfigErrorListener ecErrorListener = (EditorConfigErrorListener) errorListener;
            return ecErrorListener.getErrorMessages();
        }
    }
    return Collections.emptyList();
}
 
Example 10
Project: arithmetic   File: TestArithmeticParser.java   View source code 5 votes vote down vote up
public static ProgramContext parseProgram(String program, ANTLRErrorListener errorListener) throws IOException
{
	CharStream inputCharStream = new ANTLRInputStream(new StringReader(program));
	TokenSource tokenSource = new ArithmeticLexer(inputCharStream);
	TokenStream inputTokenStream = new CommonTokenStream(tokenSource);
	ArithmeticParser parser = new ArithmeticParser(inputTokenStream);
	parser.addErrorListener(errorListener);
	ProgramContext context = parser.program();
	return context;
}
 
Example 11
Project: groovy   File: AstBuilder.java   View source code 5 votes vote down vote up
private ANTLRErrorListener createANTLRErrorListener() {
    return new ANTLRErrorListener() {
        @Override
        public void syntaxError(
                Recognizer recognizer,
                Object offendingSymbol, int line, int charPositionInLine,
                String msg, RecognitionException e) {

            collectSyntaxError(new SyntaxException(msg, line, charPositionInLine + 1));
        }
    };
}
 
Example 12
Project: jetbrains-plugin-st4   File: STLexer.java   View source code 5 votes vote down vote up
public void lexerError(int start, int stop) {
	System.err.println("ST lexerError " + start + ".." + stop);
	ANTLRErrorListener listener = getErrorListenerDispatch();
	final String text = input.getText(Interval.of(start, stop));
	final Token t = newToken(Token.INVALID_TYPE, text, start, stop, getLine(), getCharPositionInLine());
	listener.syntaxError(this, t, getLine(), getCharPositionInLine(),
						 "Bad char or token error in template: "+ text, null);
}
 
Example 13
Project: editorconfig-netbeans   File: ECParserResult.java   View source code 5 votes vote down vote up
public List<SyntaxError> getErrors() {
  List<? extends ANTLRErrorListener> errorListeners = parser.getErrorListeners();
  for (ANTLRErrorListener errorListener : errorListeners) {
    if (errorListener instanceof EditorConfigErrorListener) {
      EditorConfigErrorListener ecErrorListener = (EditorConfigErrorListener) errorListener;
      return ecErrorListener.getErrorMessages();
    }
  }
  return Collections.emptyList();
}
 
Example 14
Project: sfera   File: Parser.java   View source code 5 votes vote down vote up
/**
 * Builds a {@link SferaScriptGrammarParser} for the specified input and
 * with the specified error listener.
 * 
 * @param input
 *            the input
 * @param errorListener
 *            the error listener
 * @return the built parser
 */
private static SferaScriptGrammarParser getParser(CharStream input,
		ANTLRErrorListener errorListener) {
	SferaScriptGrammarLexer lexer = new SferaScriptGrammarLexer(input);
	CommonTokenStream tokens = new CommonTokenStream(lexer);
	SferaScriptGrammarParser parser = new SferaScriptGrammarParser(tokens);

	lexer.removeErrorListeners();
	lexer.addErrorListener(errorListener);
	parser.removeErrorListeners();
	parser.addErrorListener(errorListener);

	return parser;
}
 
Example 15
Project: jooby   File: DocParser.java   View source code 5 votes vote down vote up
public List<DocItem> collect(Path file) throws IOException {
  this.file = file;
  doc = new ArrayList<>();
  CharStream input = CharStreams.fromPath(file, StandardCharsets.UTF_8);
  FuzzyDocLexer lexer = new FuzzyDocLexer(input);
  ANTLRErrorListener error = errorListener(LoggerFactory.getLogger(DocCollector.class), file);
  lexer.removeErrorListeners();
  lexer.addErrorListener(error);
  TokenStream tokens = new CommonTokenStream(lexer);
  FuzzyDocParser parser = new FuzzyDocParser(tokens);
  parser.removeErrorListeners();
  parser.addErrorListener(error);
  ParseTreeWalker.DEFAULT.walk(this, parser.source());
  return doc;
}
 
Example 16
Project: jooby   File: DocParser.java   View source code 5 votes vote down vote up
private static ANTLRErrorListener errorListener(final Logger log, Path file) {
  return new BaseErrorListener() {
    @Override
    public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol,
        final int line,
        final int charPositionInLine, final String msg, final RecognitionException e) {
      log.debug("{}:{}:{} {}", file, line, charPositionInLine, msg);
    }
  };
}
 
Example 17
Project: jooby   File: DocParser.java   View source code 5 votes vote down vote up
private static JavadocParser.DocumentationContext parseJavadoc(Path file, String doc) {
  ANTLRErrorListener error = errorListener(LoggerFactory.getLogger(DocCollector.class), file);
  CharStream stream = CharStreams.fromString(doc == null ? "" : doc);
  JavadocLexer lexer = new JavadocLexer(stream);
  lexer.removeErrorListeners();
  lexer.addErrorListener(error);
  CommonTokenStream tokens = new CommonTokenStream(lexer);
  JavadocParser parser = new JavadocParser(tokens);
  parser.removeErrorListeners();
  parser.addErrorListener(error);
  return parser.documentation();
}
 
Example 18
Project: libraries   File: JSSDParserFactory.java   View source code 4 votes vote down vote up
public JSSDParser create(final InputStream inputStream, final String encoding, final ANTLRErrorListener errorListener)
    throws IOException {
  return create(new InputStreamReader(inputStream, encoding), errorListener);
}
 
Example 19
Project: libraries   File: JSSDParserFactory.java   View source code 4 votes vote down vote up
public JSSDParser create(final Reader reader, final ANTLRErrorListener errorListener) throws IOException {
  return create(CharStreams.fromReader(reader), errorListener);
}
 
Example 20
Project: libraries   File: JSSDParserFactory.java   View source code 4 votes vote down vote up
public JSSDParser create(final CharStream stream, final ANTLRErrorListener errorListener) {
  return create(new JSSDLexer(stream), errorListener);
}
 
Example 21
Project: jmespath-java   File: AntlrHelper.java   View source code 4 votes vote down vote up
private static JmesPathLexer createLexer(String input, ANTLRErrorListener errorListener) {
  JmesPathLexer lexer = new JmesPathLexer(new ANTLRInputStream(input));
  lexer.removeErrorListener(ConsoleErrorListener.INSTANCE);
  lexer.addErrorListener(errorListener);
  return lexer;
}
 
Example 22
Project: eo   File: Program.java   View source code 4 votes vote down vote up
/**
 * Compile it to Java and save.
 * @throws IOException If fails
 */
public void compile() throws IOException {
    final String[] lines = new TextOf(this.input).asString().split("\n");
    final ANTLRErrorListener errors = new BaseErrorListener() {
        // @checkstyle ParameterNumberCheck (10 lines)
        @Override
        public void syntaxError(final Recognizer<?, ?> recognizer,
            final Object symbol, final int line,
            final int position, final String msg,
            final RecognitionException error) {
            throw new CompileException(
                String.format(
                    "[%d:%d] %s: \"%s\"",
                    line, position, msg, lines[line - 1]
                ),
                error
            );
        }
    };
    final ProgramLexer lexer = new ProgramLexer(
        CharStreams.fromStream(this.input.stream())
    );
    lexer.removeErrorListeners();
    lexer.addErrorListener(errors);
    final ProgramParser parser = new ProgramParser(
        new CommonTokenStream(lexer)
    );
    parser.removeErrorListeners();
    parser.addErrorListener(errors);
    final Tree tree = parser.program().ret;
    new IoCheckedScalar<>(
        new And(
            tree.java().entrySet(),
            path -> {
                new LengthOf(
                    new TeeInput(
                        path.getValue(),
                        this.target.apply(path.getKey())
                    )
                ).value();
            }
        )
    ).value();
}
 
Example 23
Project: protostuff-compiler   File: FileDescriptorLoaderImpl.java   View source code 4 votes vote down vote up
@Inject
public FileDescriptorLoaderImpl(ANTLRErrorListener errorListener, Set<ProtoContextPostProcessor> postProcessors) {
    this.errorListener = errorListener;
    this.postProcessors = postProcessors;
}
 
Example 24
Project: ecmascript-parser   File: Builder.java   View source code 4 votes vote down vote up
public Lexer withErrorListener(ANTLRErrorListener listener) {
    this.lexer.removeErrorListeners();
    this.lexer.addErrorListener(listener);
    return this;
}
 
Example 25
Project: ecmascript-parser   File: Builder.java   View source code 4 votes vote down vote up
public Parser withErrorListener(ANTLRErrorListener listener) {
    this.parser.removeErrorListeners();
    this.parser.addErrorListener(listener);
    return this;
}
 
Example 26
Project: rocana-configuration   File: ConfigurationParser.java   View source code 3 votes vote down vote up
public <T> T parse(Reader reader, Class<T> targetType) throws IOException {
  logger.debug("Parsing configuration for type:{}", targetType);

  ANTLRErrorListener listener = new BaseErrorListener() {

    @Override
    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
      throw new ConfigurationException(String.format("Unable to parse configuration: %s (at line:%s pos:%s)", msg, line, charPositionInLine), e);
    }

  };

  TypeDescriptor typeDescriptor = TypeMapping.ofType(targetType);

  ANTLRInputStream inputStream = new ANTLRInputStream(reader);
  ConfigurationLexer lexer = new ConfigurationLexer(inputStream);
  lexer.removeErrorListeners();
  lexer.addErrorListener(listener);

  CommonTokenStream tokenStream = new CommonTokenStream(lexer);
  com.rocana.configuration.antlr.ConfigurationParser parser = new com.rocana.configuration.antlr.ConfigurationParser(tokenStream);

  parser.removeErrorListeners();
  parser.addErrorListener(listener);

  ParseTree parseTree = parser.config();

  logger.debug("Parsed configuration:{}", parseTree.toStringTree(parser));

  Visitor<T> visitor = new Visitor<>(typeDescriptor);

  visitor.visit(parseTree);

  logger.debug("Configured object:{}", visitor.getResult());

  return visitor.getResult();
}
 
Example 27
Project: sfera   File: Parser.java   View source code 2 votes vote down vote up
/**
 * Builds a {@link SferaScriptGrammarParser} for the specified reader and
 * with the specified error listener.
 * 
 * @param r
 *            the reader
 * @param errorListener
 *            the error listener
 * @return the built parser
 * @throws IOException
 *             if an I/O error occurs
 */
public static SferaScriptGrammarParser getParser(Reader r, ANTLRErrorListener errorListener)
		throws IOException {
	return getParser(new ANTLRInputStream(r), errorListener);
}