org.antlr.v4.runtime.LexerNoViableAltException Java Examples

The following examples show how to use org.antlr.v4.runtime.LexerNoViableAltException. 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: JavascriptErrorHandlingLexer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures the ANTLR lexer will throw an exception after the first error
 * @param lnvae the lexer exception
 */
@Override
public void recover(LexerNoViableAltException lnvae) {
  CharStream charStream = lnvae.getInputStream();
  int startIndex = lnvae.getStartIndex();
  String text = charStream.getText(Interval.of(startIndex, charStream.index()));

  ParseException parseException = new ParseException("unexpected character '" + getErrorDisplay(text) + "'" +
      " on line (" + _tokenStartLine + ") position (" + _tokenStartCharPositionInLine + ")", _tokenStartCharIndex);
  parseException.initCause(lnvae);
  throw new RuntimeException(parseException);
}
 
Example #2
Source File: BatfishLexer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
public void recover(LexerNoViableAltException e) {
  if (_recoveryStrategy != null) {
    _recoveryStrategy.recover();
  } else {
    super.recover(e);
  }
}
 
Example #3
Source File: DelimiterLexer.java    From presto with Apache License 2.0 4 votes vote down vote up
@Override
public Token nextToken()
{
    if (_input == null) {
        throw new IllegalStateException("nextToken requires a non-null input stream.");
    }

    // Mark start location in char stream so unbuffered streams are
    // guaranteed at least have text of current token
    int tokenStartMarker = _input.mark();
    try {
        outer:
        while (true) {
            if (_hitEOF) {
                emitEOF();
                return _token;
            }

            _token = null;
            _channel = Token.DEFAULT_CHANNEL;
            _tokenStartCharIndex = _input.index();
            _tokenStartCharPositionInLine = getInterpreter().getCharPositionInLine();
            _tokenStartLine = getInterpreter().getLine();
            _text = null;
            do {
                _type = Token.INVALID_TYPE;
                int ttype = -1;

                // This entire method is copied from org.antlr.v4.runtime.Lexer, with the following bit
                // added to match the delimiters before we attempt to match the token
                boolean found = false;
                for (String terminator : delimiters) {
                    if (match(terminator)) {
                        ttype = SqlBaseParser.DELIMITER;
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    try {
                        ttype = getInterpreter().match(_input, _mode);
                    }
                    catch (LexerNoViableAltException e) {
                        notifyListeners(e);        // report error
                        recover(e);
                        ttype = SKIP;
                    }
                }

                if (_input.LA(1) == IntStream.EOF) {
                    _hitEOF = true;
                }
                if (_type == Token.INVALID_TYPE) {
                    _type = ttype;
                }
                if (_type == SKIP) {
                    continue outer;
                }
            }
            while (_type == MORE);
            if (_token == null) {
                emit();
            }
            return _token;
        }
    }
    finally {
        // make sure we release marker after match or
        // unbuffered char stream will keep buffering
        _input.release(tokenStartMarker);
    }
}
 
Example #4
Source File: DelimiterLexer.java    From rainbow with Apache License 2.0 4 votes vote down vote up
@Override
public Token nextToken()
{
    if (_input == null) {
        throw new IllegalStateException("nextToken requires a non-null input stream.");
    }

    // Mark start location in char stream so unbuffered streams are
    // guaranteed at least have text of current token
    int tokenStartMarker = _input.mark();
    try {
        outer:
        while (true) {
            if (_hitEOF) {
                emitEOF();
                return _token;
            }

            _token = null;
            _channel = Token.DEFAULT_CHANNEL;
            _tokenStartCharIndex = _input.index();
            _tokenStartCharPositionInLine = getInterpreter().getCharPositionInLine();
            _tokenStartLine = getInterpreter().getLine();
            _text = null;
            do {
                _type = Token.INVALID_TYPE;
                int ttype = -1;

                // This entire method is copied from org.antlr.v4.runtime.Lexer, with the following bit
                // added to match the delimiters before we attempt to match the token
                boolean found = false;
                for (String terminator : delimiters) {
                    if (match(terminator)) {
                        ttype = SqlBaseParser.DELIMITER;
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    try {
                        ttype = getInterpreter().match(_input, _mode);
                    }
                    catch (LexerNoViableAltException e) {
                        notifyListeners(e);        // report error
                        recover(e);
                        ttype = SKIP;
                    }
                }

                if (_input.LA(1) == IntStream.EOF) {
                    _hitEOF = true;
                }
                if (_type == Token.INVALID_TYPE) {
                    _type = ttype;
                }
                if (_type == SKIP) {
                    continue outer;
                }
            }
            while (_type == MORE);
            if (_token == null) {
                emit();
            }
            return _token;
        }
    }
    finally {
        // make sure we release marker after match or
        // unbuffered char stream will keep buffering
        _input.release(tokenStartMarker);
    }
}
 
Example #5
Source File: CapitulatingFunctionExpressionLexer.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void recover(LexerNoViableAltException e) {
	throw new CapitulatingRuntimeException();
}
 
Example #6
Source File: PropertyHandler.java    From development with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the firewall configuration as set of policies.
 * 
 * @return the set of policies - may be empty but not <code>null</code>
 */
public Set<FWPolicy> getFirewallPolicies() throws Exception {

    String config = getFirewallConfiguration();
    if (notNullNorEmpty(config)) {
        logger.debug("Given firewall configuration: " + config);

        ANTLRInputStream input = new ANTLRInputStream(config);
        FWPolicyLexer lexer = null;
        try {
            lexer = new FWPolicyLexer(input) {
                @Override
                public void recover(LexerNoViableAltException e) {
                    Interval interval = new Interval(e.getStartIndex(), e
                            .getInputStream().size() - 1);

                    String policy = e.getInputStream().getText(interval);
                    String message = Messages.get(
                            Messages.DEFAULT_LOCALE,
                            "error_invalid_firewallconfig_character",
                            new Object[] {
                                    Integer.valueOf(e.getStartIndex()),
                                    policy });

                    throw new RuntimeException(message);
                }
            };
        } catch (RuntimeException rex) {
            throw new Exception(rex);
        }
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        FWPolicyParser parser = new FWPolicyParser(tokens);
        parser.setBuildParseTree(true);
        parser.setErrorHandler(new FWPolicyErrorStrategy());
        ParseTree tree = parser.policies();
        PoliciesContext context = (PoliciesContext) tree.getPayload();
        return Collections.unmodifiableSet(new HashSet<>(context.pList));
    }
    // return unmodifiable to signal the the result is not backed by the
    // handler
    return Collections.unmodifiableSet(new HashSet<FWPolicy>());
}
 
Example #7
Source File: GroovyLangLexer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void recover(LexerNoViableAltException e) {
    throw e; // if some lexical error occurred, stop parsing!
}
 
Example #8
Source File: PseudoBooleanLexer.java    From LogicNG with Apache License 2.0 4 votes vote down vote up
@Override
public void recover(final LexerNoViableAltException exception) {
  throw new LexerException(exception.getMessage());
}
 
Example #9
Source File: PropositionalLexer.java    From LogicNG with Apache License 2.0 4 votes vote down vote up
@Override
public void recover(final LexerNoViableAltException exception) {
  throw new LexerException(exception.getMessage());
}
 
Example #10
Source File: DelimiterLexer.java    From macrobase with Apache License 2.0 4 votes vote down vote up
@Override
public Token nextToken() {
    if (_input == null) {
        throw new IllegalStateException("nextToken requires a non-null input stream.");
    }

    // Mark start location in char stream so unbuffered streams are
    // guaranteed at least have text of current token
    int tokenStartMarker = _input.mark();
    try {
        outer:
        while (true) {
            if (_hitEOF) {
                emitEOF();
                return _token;
            }

            _token = null;
            _channel = Token.DEFAULT_CHANNEL;
            _tokenStartCharIndex = _input.index();
            _tokenStartCharPositionInLine = getInterpreter().getCharPositionInLine();
            _tokenStartLine = getInterpreter().getLine();
            _text = null;
            do {
                _type = Token.INVALID_TYPE;
                int ttype = -1;

                // This entire method is copied from org.antlr.v4.runtime.Lexer, with the following bit
                // added to match the delimiters before we attempt to match the token
                boolean found = false;
                for (String terminator : delimiters) {
                    if (match(terminator)) {
                        ttype = SqlBaseParser.DELIMITER;
                        found = true;
                        break;
                    }
                }

                if (!found) {
                    try {
                        ttype = getInterpreter().match(_input, _mode);
                    } catch (LexerNoViableAltException e) {
                        notifyListeners(e);        // report error
                        recover(e);
                        ttype = SKIP;
                    }
                }

                if (_input.LA(1) == IntStream.EOF) {
                    _hitEOF = true;
                }
                if (_type == Token.INVALID_TYPE) {
                    _type = ttype;
                }
                if (_type == SKIP) {
                    continue outer;
                }
            }
            while (_type == MORE);
            if (_token == null) {
                emit();
            }
            return _token;
        }
    } finally {
        // make sure we release marker after match or
        // unbuffered char stream will keep buffering
        _input.release(tokenStartMarker);
    }
}
 
Example #11
Source File: BdsCompiler.java    From BigDataScript with Apache License 2.0 4 votes vote down vote up
/**
 * Create an AST from a program (using ANTLR lexer & parser)
 * Returns null if error
 * Use 'alreadyIncluded' to keep track of from 'include' statements
 */
ParseTree createAst(CharStream input, boolean debug, Set<String> alreadyIncluded) {
	BigDataScriptLexer lexer = null;
	BigDataScriptParser parser = null;

	try {
		//---
		// Lexer: Create a lexer that feeds off of input CharStream
		//---
		lexer = new BigDataScriptLexer(input) {
			@Override
			public void recover(LexerNoViableAltException e) {
				throw new RuntimeException(e); // Bail out
			}
		};

		//---
		// Parser
		//---
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		parser = new BigDataScriptParser(tokens);

		// Parser error handling
		parser.setErrorHandler(new CompileErrorStrategy()); // Bail out with pendingException if errors in parser
		parser.addErrorListener(new CompilerErrorListener()); // Catch some other error messages that 'CompileErrorStrategy' fails to catch

		// Begin parsing at main rule
		ParseTree tree = parserNode(parser);

		// Error loading file?
		if (tree == null) {
			System.err.println("Can't parse file '" + programFileName + "'");
			return null;
		}

		// Show main nodes
		if (debug) {
			Timer.showStdErr("AST:");
			for (int childNum = 0; childNum < tree.getChildCount(); childNum++) {
				Tree child = tree.getChild(childNum);
				System.err.println("\t\tChild " + childNum + ":\t" + child + "\tTree:'" + child.toStringTree() + "'");
			}
		}

		// Included files
		boolean resolveIncludePending = true;
		while (resolveIncludePending)
			resolveIncludePending = resolveIncludes(tree, debug, alreadyIncluded);

		return tree;
	} catch (Exception e) {
		String msg = e.getMessage();
		CompilerMessages.get().addError("Could not compile " + programFileName //
				+ (msg != null ? " :" + e.getMessage() : "") //
		);
		return null;
	}
}