Java Code Examples for org.antlr.v4.runtime.Lexer#removeErrorListeners()

The following examples show how to use org.antlr.v4.runtime.Lexer#removeErrorListeners() . 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: ECParser.java    From editorconfig-netbeans with MIT License 6 votes vote down vote up
@Override
public void parse(Snapshot snapshot, Task task, SourceModificationEvent event) throws ParseException {
  this.snapshot = snapshot;
  String text = snapshot.getText().toString();
  ANTLRInputStream input = new ANTLRInputStream(text);
  Lexer lexer = new EditorConfigLexer(input);
  lexer.removeErrorListeners();

  CommonTokenStream tokens = new CommonTokenStream(lexer);
  parser = new EditorConfigParser(tokens);
  parser.removeErrorListeners();
  syntaxErrors = new ArrayList<>();
  EditorConfigErrorListener errorListener = new EditorConfigErrorListener(syntaxErrors);
  parser.addErrorListener(errorListener);
  EditorConfigParser.FileContext root = parser.file();
  result = new ECParserResult(snapshot, parser, root);
}
 
Example 2
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 3
Source File: SourceExceptionParser.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Use a Lexer/Parser pair extracting the parser's root item.
 *
 * @param lexer lexer to use
 * @param parser parser to use
 * @param parseMethod Root item extractor method
 * @param ref Source reference
 * @return Parsed item
 * @throws NullPointerException if any argument is null
 * @throws SourceException if a parser error occurs
 */
public static <T> T parse(final Lexer lexer, final Parser parser, final Supplier<T> parseMethod,
        final StatementSourceReference ref) {
    final Listener listener = new Listener(ref);
    lexer.removeErrorListeners();
    lexer.addErrorListener(listener);
    parser.removeErrorListeners();
    parser.addErrorListener(listener);

    final T ret = parseMethod.get();
    listener.validate();
    return ret;
}
 
Example 4
Source File: GroovyAutocomplete.java    From beakerx with Apache License 2.0 4 votes vote down vote up
private AutocompleteResult tryFindAutocomplete(String txt, int cur, ClassLoader l, Imports imports) {
   registry = AutocompleteRegistryFactory.createRegistry(cps);
   GroovyClassUtils cu = createClassUtils(l);
   setup(cu, registry);
   AutocompleteRegistryFactory.addDefaultImports(cu, registry, imports.toListOfStrings(), cps);
   AutocompleteRegistryFactory.moreSetup(cu);

   Lexer lexer = new GroovyLexer(new ANTLRInputStream(txt));
   lexer.removeErrorListeners();
   CommonTokenStream tokens = new CommonTokenStream(lexer);

   // Create a parser that reads from the scanner
   GroovyParser parser = new GroovyParser(tokens);
   parser.removeErrorListeners();

   // start parsing at the compilationUnit rule
   ParserRuleContext t = parser.compilationUnit();
   ParseTreeWalker walker = new ParseTreeWalker();
   List<AutocompleteCandidate> q = new ArrayList<>();

   GroovyImportDeclarationCompletion extractor = new GroovyImportDeclarationCompletion(txt, cur, registry, cps, cu);
   GroovyNameBuilder extractor2 = new GroovyNameBuilder(registry, cu);
   GroovyNodeCompletion extractor3 = new GroovyNodeCompletion(txt, cur, registry, cu);
   GroovyReflectionCompletion extractor4 = new GroovyReflectionCompletion(scriptBinding, groovyClassLoader, imports);

   walker.walk(extractor, t);
   if (extractor.getQuery() != null)
     q.addAll(extractor.getQuery());
   walker.walk(extractor2, t);
   walker.walk(extractor3, t);
   if (extractor3.getQuery() != null)
     q.addAll(extractor3.getQuery());
   List<String> ret = registry.searchCandidates(q);
   
List<String> reflectionCompletions = extractor4.autocomplete(txt, cur);
ret.addAll(reflectionCompletions);

   if (!ret.isEmpty()) {
     return new AutocompleteResult(ret, getStartIndex(extractor, extractor2, extractor3));
   }
   return findAutocompleteResult(txt, cur, cu);
 }