Search in sources :

Example 16 with CodePointCharStream

use of org.antlr.v4.runtime.CodePointCharStream in project bmoth by hhu-stups.

the class Parser method parseLTLFormula.

private LtlStartContext parseLTLFormula(String inputString) throws ParseErrorException {
    CodePointCharStream fromString = CharStreams.fromString(inputString);
    final BMoThLexer lexer = new BMoThLexer(fromString);
    lexer.pushMode(BMoThLexer.LTL_MODE);
    // create a buffer of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    BMoThParser parser = new BMoThParser(tokens);
    parser.removeErrorListeners();
    ErrorListener errorListener = new ErrorListener();
    parser.addErrorListener(errorListener);
    try {
        return parser.ltlStart();
    } catch (VisitorException e) {
        final Logger logger = Logger.getLogger(getClass().getName());
        logger.log(Level.SEVERE, PARSE_ERROR, e);
        throw e.getParseErrorException();
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BMoThParser(de.bmoth.antlr.BMoThParser) VisitorException(de.bmoth.parser.ErrorListener.VisitorException) Logger(java.util.logging.Logger) BMoThLexer(de.bmoth.antlr.BMoThLexer) CodePointCharStream(org.antlr.v4.runtime.CodePointCharStream)

Example 17 with CodePointCharStream

use of org.antlr.v4.runtime.CodePointCharStream in project inmemantlr by julianthome.

the class GenericParser method parse.

/**
 * parse string and create a context
 *
 * @param toParse    string to parseFile
 * @param production production name to parseFile
 * @param cs         case sensitivity
 * @return context
 * @throws IllegalWorkflowException if compilation did not take place
 * @throws ParsingException         if an error occurs while parsing
 */
public ParserRuleContext parse(String toParse, String production, CaseSensitiveType cs) throws IllegalWorkflowException, ParsingException {
    if (!antrlObjectsAvailable()) {
        throw new IllegalWorkflowException("No antlr objects have been compiled or loaded");
    }
    switch(cs) {
        case NONE:
            break;
        case UPPER:
            toParse = toParse.toUpperCase();
            break;
        case LOWER:
            toParse = toParse.toLowerCase();
            break;
    }
    InmemantlrErrorListener el = new InmemantlrErrorListener();
    listener.reset();
    // CodePointCharStream input = CharStreams.fromString(toParse);
    CharStream input = provider.getCharStream(toParse);
    Objects.requireNonNull(input, "char stream must not be null");
    LOGGER.debug("load lexer {}", lexerName);
    Lexer lex = sc.instanciateLexer(input, lexerName, useCached);
    lex.addErrorListener(el);
    Objects.requireNonNull(lex, "lex must not be null");
    CommonTokenStream tokens = new CommonTokenStream(lex);
    tokens.fill();
    LOGGER.debug("load parser {}", parserName);
    Parser parser = sc.instanciateParser(tokens, parserName);
    Objects.requireNonNull(parser, "Parser must not be null");
    // make parser information available to listener
    listener.setParser(parser);
    parser.removeErrorListeners();
    parser.addErrorListener(el);
    parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
    parser.setBuildParseTree(true);
    parser.setTokenStream(tokens);
    String[] rules = parser.getRuleNames();
    String entryPoint;
    if (production == null) {
        entryPoint = rules[0];
    } else {
        if (!Arrays.asList(rules).contains(production)) {
            throw new IllegalArgumentException("Rule " + production + " not found");
        }
        entryPoint = production;
    }
    ParserRuleContext data = null;
    try {
        Class<?> pc = parser.getClass();
        Method m = pc.getDeclaredMethod(entryPoint, (Class<?>[]) null);
        Objects.requireNonNull(m, "method should not be null");
        data = (ParserRuleContext) m.invoke(parser, (Object[]) null);
    } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        // e.printStackTrace();
        return null;
    }
    Set<String> msgs = el.getLog().entrySet().stream().filter(e -> e.getKey() == InmemantlrErrorListener.Type.SYNTAX_ERROR).map(e -> e.getValue()).collect(Collectors.toSet());
    if (msgs.size() > 0) {
        String result = msgs.stream().collect(Collectors.joining());
        throw new ParsingException(result);
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(listener, data);
    return data;
}
Also used : java.util(java.util) org.snt.inmemantlr.comp(org.snt.inmemantlr.comp) FileUtils(org.snt.inmemantlr.utils.FileUtils) LoggerFactory(org.slf4j.LoggerFactory) MemoryTuple(org.snt.inmemantlr.memobjects.MemoryTuple) DefaultStreamProvider(org.snt.inmemantlr.stream.DefaultStreamProvider) org.snt.inmemantlr.exceptions(org.snt.inmemantlr.exceptions) GenericParserSerialize(org.snt.inmemantlr.memobjects.GenericParserSerialize) InmemantlrTool(org.snt.inmemantlr.tool.InmemantlrTool) ToolCustomizer(org.snt.inmemantlr.tool.ToolCustomizer) GrammarRootAST(org.antlr.v4.tool.ast.GrammarRootAST) Method(java.lang.reflect.Method) Tuple(org.snt.inmemantlr.utils.Tuple) org.antlr.v4.runtime(org.antlr.v4.runtime) Logger(org.slf4j.Logger) StreamProvider(org.snt.inmemantlr.stream.StreamProvider) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) DefaultListener(org.snt.inmemantlr.listener.DefaultListener) IOUtils.closeQuietly(org.apache.commons.io.IOUtils.closeQuietly) PredictionMode(org.antlr.v4.runtime.atn.PredictionMode) Collectors(java.util.stream.Collectors) InmemantlrErrorListener(org.snt.inmemantlr.tool.InmemantlrErrorListener) InvocationTargetException(java.lang.reflect.InvocationTargetException) MemorySource(org.snt.inmemantlr.memobjects.MemorySource) MemoryTupleSet(org.snt.inmemantlr.memobjects.MemoryTupleSet) java.io(java.io) FileExistsException(org.apache.commons.io.FileExistsException) Paths(java.nio.file.Paths) ParseTreeListener(org.antlr.v4.runtime.tree.ParseTreeListener) FilenameUtils(org.apache.commons.io.FilenameUtils) InmemantlrErrorListener(org.snt.inmemantlr.tool.InmemantlrErrorListener) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 18 with CodePointCharStream

use of org.antlr.v4.runtime.CodePointCharStream in project checkstyle by checkstyle.

the class JavaParser method parse.

/**
 * Static helper method to parses a Java source file.
 *
 * @param contents contains the contents of the file
 * @return the root of the AST
 * @throws CheckstyleException if the contents is not a valid Java source
 */
public static DetailAST parse(FileContents contents) throws CheckstyleException {
    final String fullText = contents.getText().getFullText().toString();
    final CharStream codePointCharStream = CharStreams.fromString(fullText);
    final JavaLanguageLexer lexer = new JavaLanguageLexer(codePointCharStream, true);
    lexer.setCommentListener(contents);
    lexer.removeErrorListeners();
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    final JavaLanguageParser parser = new JavaLanguageParser(tokenStream, JavaLanguageParser.CLEAR_DFA_LIMIT);
    parser.setErrorHandler(new CheckstyleParserErrorStrategy());
    parser.removeErrorListeners();
    parser.addErrorListener(new CheckstyleErrorListener());
    final JavaLanguageParser.CompilationUnitContext compilationUnit;
    try {
        compilationUnit = parser.compilationUnit();
    } catch (IllegalStateException ex) {
        final String exceptionMsg = String.format(Locale.ROOT, "%s occurred while parsing file %s.", ex.getClass().getSimpleName(), contents.getFileName());
        throw new CheckstyleException(exceptionMsg, ex);
    }
    return new JavaAstVisitor(tokenStream).visit(compilationUnit);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) JavaLanguageParser(com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser) JavaLanguageLexer(com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageLexer) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) CharStream(org.antlr.v4.runtime.CharStream)

Example 19 with CodePointCharStream

use of org.antlr.v4.runtime.CodePointCharStream in project checkstyle by checkstyle.

the class JavaAstVisitorTest method testNoStackOverflowOnDeepStringConcat.

/**
 * This test exists to kill surviving mutation from pitest removing expression AST building
 * optimization in {@link JavaAstVisitor#visitBinOp(JavaLanguageParser.BinOpContext)}.
 * We do not use {@link JavaParser#parse(FileContents)} here due to DFA clearing hack.
 *
 * <p>
 * Reason: we have iterative expression AST building to avoid stackoverflow
 * in {@link JavaAstVisitor#visitBinOp(JavaLanguageParser.BinOpContext)}. In actual
 * generated parser, we avoid stackoverflow thanks to the left recursive expression
 * rule (eliminating unnecessary recursive calls to hierarchical expression production rules).
 * However, ANTLR's ParserATNSimulator has no such optimization. So, the number of recursive
 * calls to ParserATNSimulator#closure when calling ParserATNSimulator#clearDFA causes a
 * StackOverflow error. We avoid this by using the single argument constructor (thus not
 * forcing DFA clearing) in this test.
 * </p>
 *
 * @throws Exception if input file does not exist
 */
@Test
public void testNoStackOverflowOnDeepStringConcat() throws Exception {
    final File file = new File(getPath("InputJavaAstVisitorNoStackOverflowOnDeepStringConcat.java"));
    final FileText fileText = new FileText(file, StandardCharsets.UTF_8.name());
    final FileContents contents = new FileContents(fileText);
    final String fullText = contents.getText().getFullText().toString();
    final CharStream codePointCharStream = CharStreams.fromString(fullText);
    final JavaLanguageLexer lexer = new JavaLanguageLexer(codePointCharStream, true);
    lexer.setCommentListener(contents);
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    final JavaLanguageParser parser = new JavaLanguageParser(tokenStream);
    final JavaLanguageParser.CompilationUnitContext compilationUnit = parser.compilationUnit();
    // We restrict execution to use limited resources here, so that we can
    // kill surviving pitest mutation from removal of nested binary operation
    // optimization in JavaAstVisitor#visitBinOp. Limited resources (small stack size)
    // ensure that we throw a StackOverflowError if optimization is removed.
    final DetailAST root = TestUtil.getResultWithLimitedResources(() -> new JavaAstVisitor(tokenStream).visit(compilationUnit));
    assertWithMessage("File parsing and AST building should complete successfully.").that(root).isNotNull();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) JavaLanguageParser(com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser) FileContents(com.puppycrawl.tools.checkstyle.api.FileContents) JavaLanguageLexer(com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageLexer) DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST) FileText(com.puppycrawl.tools.checkstyle.api.FileText) File(java.io.File) CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.jupiter.api.Test)

Example 20 with CodePointCharStream

use of org.antlr.v4.runtime.CodePointCharStream in project antlr4 by antlr.

the class TestCodePointCharStream method singleLatinCodePointHasSize1.

@Test
public void singleLatinCodePointHasSize1() {
    CodePointCharStream s = CharStreams.fromString("X");
    assertEquals(1, s.size());
}
Also used : CodePointCharStream(org.antlr.v4.runtime.CodePointCharStream) Test(org.junit.Test)

Aggregations

CodePointCharStream (org.antlr.v4.runtime.CodePointCharStream)37 Test (org.junit.Test)34 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)5 JavaLanguageLexer (com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageLexer)2 JavaLanguageParser (com.puppycrawl.tools.checkstyle.grammar.java.JavaLanguageParser)2 BMoThLexer (de.bmoth.antlr.BMoThLexer)2 BMoThParser (de.bmoth.antlr.BMoThParser)2 CharStream (org.antlr.v4.runtime.CharStream)2 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)1 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)1 FileText (com.puppycrawl.tools.checkstyle.api.FileText)1 VisitorException (de.bmoth.parser.ErrorListener.VisitorException)1 java.io (java.io)1 File (java.io.File)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Paths (java.nio.file.Paths)1 java.util (java.util)1 Logger (java.util.logging.Logger)1