org.antlr.v4.runtime.ANTLRErrorListener Java Examples

The following examples show how to use org.antlr.v4.runtime.ANTLRErrorListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ANTLRParseTreeToPSIConverter.java    From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License 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 #2
Source File: ParserModule.java    From protostuff-compiler with Apache License 2.0 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 #3
Source File: LexerWrapper.java    From antlr4-autosuggest with Apache License 2.0 5 votes vote down vote up
private TokenizationResult tokenize(String input) {
    Lexer lexer = this.createLexer(input);
    lexer.removeErrorListeners();
    final TokenizationResult result = new TokenizationResult();
    ANTLRErrorListener newErrorListener = new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException {
            result.untokenizedText = input.substring(charPositionInLine); // intended side effect
        }
    };
    lexer.addErrorListener(newErrorListener);
    result.tokens = lexer.getAllTokens();
    return result;
}
 
Example #4
Source File: AntlrHelper.java    From jmespath-java with BSD 3-Clause "New" or "Revised" License 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 #5
Source File: Antlr4TestUtils.java    From openCypher with Apache License 2.0 5 votes vote down vote up
static void parseLegacyWithListeners( String query, ANTLRErrorListener lexerListener, ANTLRErrorListener parserListener )
{
    System.setProperty( INCLUDE_LEGACY.name(), "true" );

    initLegacyGrammar( "/cypher.xml" );
    parseWithListeners( legacyGrammar, query, lexerListener, parserListener );
}
 
Example #6
Source File: Antlr4TestUtils.java    From openCypher with Apache License 2.0 5 votes vote down vote up
private static void parseWithListeners( Grammar grammar, String query, ANTLRErrorListener lexerListener, ANTLRErrorListener parserListener )
{
    LexerInterpreter lexer = grammar.createLexerInterpreter( new ANTLRInputStream( query ) );
    ParserInterpreter parser = grammar.createParserInterpreter( new CommonTokenStream( lexer ) );
    lexer.removeErrorListeners();
    parser.removeErrorListeners();
    lexer.addErrorListener( lexerListener );
    parser.addErrorListener( parserListener );
    parser.parse( grammar.getRule( Antlr4.PREFIX + "Cypher" ).index );
}
 
Example #7
Source File: ECParserResult.java    From editorconfig-netbeans with MIT License 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 #8
Source File: AntlrHelper.java    From jmespath-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static JmesPathLexer createLexer(String input, ANTLRErrorListener errorListener) {
  JmesPathLexer lexer = new JmesPathLexer(CharStreams.fromString(input));
  lexer.removeErrorListener(ConsoleErrorListener.INSTANCE);
  lexer.addErrorListener(errorListener);
  return lexer;
}
 
Example #9
Source File: Program.java    From eo with MIT License 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 org.eolang.compiler.ProgramLexer lexer =
        new org.eolang.compiler.ProgramLexer(
            CharStreams.fromStream(this.input.stream())
        );
    lexer.removeErrorListeners();
    lexer.addErrorListener(errors);
    final org.eolang.compiler.ProgramParser parser =
        new org.eolang.compiler.ProgramParser(
            new CommonTokenStream(lexer)
        );
    parser.removeErrorListeners();
    parser.addErrorListener(errors);
    final Tree tree = parser.program().ret;
    new IoCheckedScalar<>(
        new And(
            path -> {
                new LengthOf(
                    new TeeInput(
                        path.getValue(),
                        this.target.apply(path.getKey())
                    )
                ).value();
            },
            tree.java().entrySet()
        )
    ).value();
}
 
Example #10
Source File: FileDescriptorLoaderImpl.java    From protostuff-compiler with Apache License 2.0 4 votes vote down vote up
@Inject
public FileDescriptorLoaderImpl(ANTLRErrorListener errorListener, Set<ProtoContextPostProcessor> postProcessors) {
    this.errorListener = errorListener;
    this.postProcessors = postProcessors;
}