/** * {@inheritDoc} */ @Override public void syntaxError(BaseRecognizer recognizer, String[] tokenNames, RecognitionException e) { String hdr = recognizer.getErrorHeader(e); String msg = recognizer.getErrorMessage(e, tokenNames); StringBuilder builder = new StringBuilder().append(hdr) .append(' ') .append(msg); if (recognizer instanceof Parser) appendQuerySnippet((Parser) recognizer, builder); errorMsgs.add(builder.toString()); }
/** Creates a parser for a given term, generating trees of this kind. */ public <P extends Parser> P createParser(Class<P> parserType, I info, String term) { try { // find the lexer type String parserName = parserType.getName(); String lexerName = parserName.substring(0, parserName.indexOf("Parser")) .concat("Lexer"); @SuppressWarnings("unchecked") Class<? extends Lexer> lexerType = (Class<? extends Lexer>) Class.forName(lexerName); Lexer lexer = createLexer(lexerType, info, term); // instantiate the parser CommonTokenStream tokenStream = new CommonTokenStream(lexer); Constructor<P> parserConstructor = parserType.getConstructor(TokenStream.class); P result = parserConstructor.newInstance(tokenStream); Method adaptorSetter = parserType.getMethod("setTreeAdaptor", TreeAdaptor.class); adaptorSetter.invoke(result, new ParseTreeAdaptor<>(this, info, tokenStream)); callInitialise(result, info); return result; } catch (Exception e) { throw toRuntime(e); } }
/** * Appends a query snippet to the message to help the user to understand the problem. * * @param parser the parser used to parse the query * @param builder the <code>StringBuilder</code> used to build the error message */ private void appendQuerySnippet(Parser parser, StringBuilder builder) { TokenStream tokenStream = parser.getTokenStream(); int index = tokenStream.index(); int size = tokenStream.size(); Token from = tokenStream.get(getSnippetFirstTokenIndex(index)); Token to = tokenStream.get(getSnippetLastTokenIndex(index, size)); Token offending = tokenStream.get(getOffendingTokenIndex(index, size)); appendSnippet(builder, from, to, offending); }
public String getParserErrorMessage(Parser parser, RecognitionException e) { String msg; if ( e instanceof NoViableAltException) { String name = parser.getTokenErrorDisplay(e.token); msg = name+" came as a complete surprise to me"; } else if ( e instanceof v4ParserException) { msg = ((v4ParserException)e).msg; } else { msg = parser.getErrorMessage(e, parser.getTokenNames()); } return msg; }
public Object buildKeywordEscapeIdentifier(Parser parser, TreeAdaptor adaptor, Object tree) { CommonTree ct = (CommonTree)tree; String txt = ct.getText(); if (txt == null) { CommonToken leftToken = (CommonToken) parser.getTokenStream().get(ct.getTokenStartIndex()); CommonToken rightToken = (CommonToken) parser.getTokenStream().get(ct.getTokenStopIndex()); txt = leftToken.getInputStream().substring(leftToken.getStartIndex(), rightToken.getStopIndex()); } return adaptor.create(TokenTypes.Regular_Identifier, ct.getToken(), /*ct.getText()*/txt); }
public Object execParser( String ruleName, String input, int scriptLine) throws Exception { ANTLRStringStream is = new ANTLRStringStream(input); Class<? extends TokenSource> lexerClass = Class.forName(lexerClassName).asSubclass(TokenSource.class); Constructor<? extends TokenSource> lexConstructor = lexerClass.getConstructor(CharStream.class); TokenSource lexer = lexConstructor.newInstance(is); is.setLine(scriptLine); CommonTokenStream tokens = new CommonTokenStream(lexer); Class<? extends Parser> parserClass = Class.forName(parserClassName).asSubclass(Parser.class); Constructor<? extends Parser> parConstructor = parserClass.getConstructor(TokenStream.class); Parser parser = parConstructor.newInstance(tokens); // set up customized tree adaptor if necessary if ( adaptorClassName!=null ) { Method m = parserClass.getMethod("setTreeAdaptor", TreeAdaptor.class); Class<? extends TreeAdaptor> adaptorClass = Class.forName(adaptorClassName).asSubclass(TreeAdaptor.class); m.invoke(parser, adaptorClass.newInstance()); } Method ruleMethod = parserClass.getMethod(ruleName); // INVOKE RULE return ruleMethod.invoke(parser); }
public Parser[] getDelegates() { return new Parser[] {}; }
public Parser[] getDelegates() { return new Parser[] {gParser}; }
public Object buildIdentifier(Parser parser, TreeAdaptor adaptor, String text) { return adaptor.create(TokenTypes.Regular_Identifier, text); }
public String extractLine(Parser parser, RecognitionException e) { return extractLine(parser.getTokenStream(), e); }
public CLParserHelper(Parser parser) { this.parser = parser; clObject = new CLObject(); }