Search in sources :

Example 11 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project sts4 by spring-projects.

the class AntlrParser method parse.

@Override
public ParseResults parse(String text) {
    ArrayList<Problem> syntaxErrors = new ArrayList<>();
    ArrayList<Problem> problems = new ArrayList<>();
    ArrayList<PropertiesAst.Node> astNodes = new ArrayList<>();
    JavaPropertiesLexer lexer = new JavaPropertiesLexer(new ANTLRInputStream(text.toCharArray(), text.length()));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    JavaPropertiesParser parser = new JavaPropertiesParser(tokens);
    // To avoid printing parse errors in the console
    parser.removeErrorListener(ConsoleErrorListener.INSTANCE);
    // Add listener to collect various parser errors
    parser.addErrorListener(new ANTLRErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            syntaxErrors.add(createProblem(msg, ProblemCodes.PROPERTIES_SYNTAX_ERROR, (Token) offendingSymbol));
        }

        @Override
        public void reportAmbiguity(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
            problems.add(createProblem("Ambiguity detected!", ProblemCodes.PROPERTIES_AMBIGUITY_ERROR, recognizer.getCurrentToken()));
        }

        @Override
        public void reportAttemptingFullContext(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {
            problems.add(createProblem("Full-Context attempt detected!", ProblemCodes.PROPERTIES_FULL_CONTEXT_ERROR, recognizer.getCurrentToken()));
        }

        @Override
        public void reportContextSensitivity(org.antlr.v4.runtime.Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {
            problems.add(createProblem("Context sensitivity detected!", ProblemCodes.PROPERTIES_CONTEXT_SENSITIVITY_ERROR, recognizer.getCurrentToken()));
        }
    });
    // Add listener to the parse tree to collect AST nodes
    parser.addParseListener(new JavaPropertiesBaseListener() {

        private Key key = null;

        private Value value = null;

        @Override
        public void exitPropertyLine(PropertyLineContext ctx) {
            KeyValuePair pair = new KeyValuePair(ctx, key, value);
            key.parent = value.parent = pair;
            astNodes.add(pair);
            key = null;
            value = null;
        }

        @Override
        public void exitCommentLine(CommentLineContext ctx) {
            astNodes.add(new Comment(ctx));
        }

        @Override
        public void exitKey(KeyContext ctx) {
            key = new Key(ctx);
        }

        @Override
        public void exitSeparatorAndValue(SeparatorAndValueContext ctx) {
            value = new Value(ctx);
        }

        @Override
        public void exitEmptyLine(EmptyLineContext ctx) {
            astNodes.add(new EmptyLine(ctx));
        }
    });
    parser.parse();
    // Collect and return parse results
    return new ParseResults(new PropertiesAst(ImmutableList.copyOf(astNodes)), ImmutableList.copyOf(syntaxErrors), ImmutableList.copyOf(problems));
}
Also used : ATNConfigSet(org.antlr.v4.runtime.atn.ATNConfigSet) CommentLineContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.CommentLineContext) ArrayList(java.util.ArrayList) ANTLRErrorListener(org.antlr.v4.runtime.ANTLRErrorListener) PropertiesAst(org.springframework.ide.vscode.java.properties.parser.PropertiesAst) KeyContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.KeyContext) SeparatorAndValueContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.SeparatorAndValueContext) EmptyLineContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.EmptyLineContext) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BitSet(java.util.BitSet) PropertyLineContext(org.springframework.ide.vscode.java.properties.antlr.parser.JavaPropertiesParser.PropertyLineContext) Problem(org.springframework.ide.vscode.java.properties.parser.Problem) ParseResults(org.springframework.ide.vscode.java.properties.parser.ParseResults) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) RecognitionException(org.antlr.v4.runtime.RecognitionException) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 12 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class TimeLexerSpeed method lex_new_grapheme_utf8.

public void lex_new_grapheme_utf8(String fileName, int n, boolean clearLexerDFACache) throws Exception {
    String resourceName = PerfDir + "/" + fileName;
    ClassLoader loader = TimeLexerSpeed.class.getClassLoader();
    InputStream is = loader.getResourceAsStream(resourceName);
    try {
        long size = getResourceSize(loader, resourceName);
        CharStream input = CharStreams.fromStream(is, Charset.forName("UTF-8"), resourceName, size);
        graphemesLexer lexer = new graphemesLexer(input);
        double avg = tokenize(lexer, n, clearLexerDFACache);
        String currentMethodName = new Exception().getStackTrace()[0].getMethodName();
        if (output)
            System.out.printf("%27s average time %5dus over %4d runs of %5d symbols from %s%s\n", currentMethodName, (int) avg, n, input.size(), fileName, clearLexerDFACache ? " DFA cleared" : "");
    } finally {
        is.close();
    }
}
Also used : InputStream(java.io.InputStream) CharStream(org.antlr.v4.runtime.CharStream) IOException(java.io.IOException)

Example 13 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class TimeLexerSpeed method tokenize.

public double tokenize(Lexer lexer, int n, boolean clearLexerDFACache) {
    // always wipe the DFA before we begin tests so previous tests
    // don't affect this run!
    lexer.getInterpreter().clearDFA();
    long[] times = new long[n];
    for (int i = 0; i < n; i++) {
        lexer.reset();
        if (clearLexerDFACache) {
            lexer.getInterpreter().clearDFA();
        }
        long start = System.nanoTime();
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        // lex whole file.
        tokens.fill();
        // int size = lexer.getInputStream().size();
        long stop = System.nanoTime();
        times[i] = (stop - start) / 1000;
    // if ( output ) System.out.printf("Tokenized %d char in %dus\n", size, times[i]);
    }
    Arrays.sort(times);
    // drop highest 20% of times
    times = Arrays.copyOfRange(times, 0, times.length - (int) (n * .2));
    return avg(times);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream)

Example 14 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class TimeLexerSpeed method lex_legacy_java_utf8.

public void lex_legacy_java_utf8(int n, boolean clearLexerDFACache) throws Exception {
    InputStream is = TimeLexerSpeed.class.getClassLoader().getResourceAsStream(Parser_java_file);
    try {
        InputStreamReader isr = new InputStreamReader(is, Charset.forName("UTF-8"));
        try {
            BufferedReader br = new BufferedReader(isr);
            try {
                @SuppressWarnings("deprecation") CharStream input = new org.antlr.v4.runtime.ANTLRInputStream(br);
                JavaLexer lexer = new JavaLexer(input);
                double avg = tokenize(lexer, n, clearLexerDFACache);
                String currentMethodName = new Exception().getStackTrace()[0].getMethodName();
                if (output)
                    System.out.printf("%27s average time %5dus over %4d runs of %5d symbols%s\n", currentMethodName, (int) avg, n, input.size(), clearLexerDFACache ? " DFA cleared" : "");
            } finally {
                br.close();
            }
        } finally {
            isr.close();
        }
    } finally {
        is.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) CharStream(org.antlr.v4.runtime.CharStream) IOException(java.io.IOException) JavaLexer(org.antlr.v4.test.runtime.java.api.JavaLexer) BufferedReader(java.io.BufferedReader)

Example 15 with DFA

use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.

the class TimeLexerSpeed method lex_legacy_grapheme_utf8.

public void lex_legacy_grapheme_utf8(String fileName, int n, boolean clearLexerDFACache) throws Exception {
    InputStream is = TimeLexerSpeed.class.getClassLoader().getResourceAsStream(PerfDir + "/" + fileName);
    try {
        InputStreamReader isr = new InputStreamReader(is, Charset.forName("UTF-8"));
        try {
            BufferedReader br = new BufferedReader(isr);
            try {
                @SuppressWarnings("deprecation") CharStream input = new org.antlr.v4.runtime.ANTLRInputStream(br);
                graphemesLexer lexer = new graphemesLexer(input);
                double avg = tokenize(lexer, n, clearLexerDFACache);
                String currentMethodName = new Exception().getStackTrace()[0].getMethodName();
                if (output)
                    System.out.printf("%27s average time %5dus over %4d runs of %5d symbols from %s%s\n", currentMethodName, (int) avg, n, input.size(), fileName, clearLexerDFACache ? " DFA cleared" : "");
            } finally {
                br.close();
            }
        } finally {
            isr.close();
        }
    } finally {
        is.close();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) CharStream(org.antlr.v4.runtime.CharStream) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader)

Aggregations

DFA (org.antlr.v4.runtime.dfa.DFA)37 DFAState (org.antlr.v4.runtime.dfa.DFAState)28 Test (org.junit.Test)19 Grammar (org.antlr.v4.tool.Grammar)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)16 ArrayList (java.util.ArrayList)14 STGroupString (org.stringtemplate.v4.STGroupString)14 CharStream (org.antlr.v4.runtime.CharStream)13 IOException (java.io.IOException)10 IntegerList (org.antlr.v4.runtime.misc.IntegerList)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)9 InputStream (java.io.InputStream)8 BitSet (java.util.BitSet)8 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)7 ATN (org.antlr.v4.runtime.atn.ATN)7 Interval (org.antlr.v4.runtime.misc.Interval)7 NotNull (org.antlr.v4.runtime.misc.NotNull)7 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)6 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)5