Search in sources :

Example 96 with CharStream

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

the class TimeLexerSpeed method load_legacy_java_ascii.

public void load_legacy_java_ascii(String resourceName, int n) throws Exception {
    // keep refs around so we can average memory
    CharStream[] input = new CharStream[n];
    ClassLoader loader = TimeLexerSpeed.class.getClassLoader();
    InputStream[] streams = new InputStream[n];
    for (int i = 0; i < n; i++) {
        streams[i] = loader.getResourceAsStream(resourceName);
    }
    // track only time to suck data out of stream
    long start = System.nanoTime();
    for (int i = 0; i < n; i++) {
        try (InputStream is = streams[i]) {
            input[i] = new ANTLRInputStream(is);
        }
    }
    long stop = System.nanoTime();
    long tus = (stop - start) / 1000;
    int size = input[0].size();
    long streamSize = GraphLayout.parseInstance((Object) input[0]).totalSize();
    streamFootprints.add(basename(resourceName) + " (" + size + " char): " + GraphLayout.parseInstance((Object) input[0]).toFootprint());
    String currentMethodName = new Exception().getStackTrace()[0].getMethodName();
    if (output)
        System.out.printf("%27s average time %5dus size %6db over %4d loads of %5d symbols from %s\n", currentMethodName, tus / n, streamSize, n, size, basename(resourceName));
}
Also used : ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) CharStream(org.antlr.v4.runtime.CharStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) IOException(java.io.IOException)

Example 97 with CharStream

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

the class TimeLexerSpeed method lex_new_java_utf8.

public void lex_new_java_utf8(int n, boolean clearLexerDFACache) throws Exception {
    ClassLoader loader = TimeLexerSpeed.class.getClassLoader();
    try (InputStream is = loader.getResourceAsStream(Parser_java_file)) {
        long size = getResourceSize(loader, Parser_java_file);
        CharStream input = CharStreams.fromStream(is, StandardCharsets.UTF_8, size);
        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" : "");
    }
}
Also used : JavaLexer(org.antlr.v4.test.runtime.java.api.JavaLexer) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) CharStream(org.antlr.v4.runtime.CharStream) IOException(java.io.IOException)

Example 98 with CharStream

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

the class TimeLexerSpeed method load_new_utf8.

public void load_new_utf8(String resourceName, int n) throws Exception {
    // keep refs around so we can average memory
    CharStream[] input = new CharStream[n];
    ClassLoader loader = TimeLexerSpeed.class.getClassLoader();
    InputStream[] streams = new InputStream[n];
    for (int i = 0; i < n; i++) {
        streams[i] = loader.getResourceAsStream(resourceName);
    }
    URLConnection uc = null;
    long streamLength = getResourceSize(loader, resourceName);
    // track only time to suck data out of stream
    long start = System.nanoTime();
    for (int i = 0; i < n; i++) {
        try (InputStream is = streams[i]) {
            input[i] = CharStreams.fromStream(is, StandardCharsets.UTF_8, streamLength);
        }
    }
    long stop = System.nanoTime();
    long tus = (stop - start) / 1000;
    int size = input[0].size();
    long streamSize = GraphLayout.parseInstance((Object) input[0]).totalSize();
    streamFootprints.add(basename(resourceName) + " (" + size + " char): " + GraphLayout.parseInstance((Object) input[0]).toFootprint());
    String currentMethodName = new Exception().getStackTrace()[0].getMethodName();
    if (output)
        System.out.printf("%27s average time %5dus size %6db over %4d loads of %5d symbols from %s\n", currentMethodName, tus / n, streamSize, n, size, basename(resourceName));
}
Also used : ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) CharStream(org.antlr.v4.runtime.CharStream) URLConnection(java.net.URLConnection) IOException(java.io.IOException)

Example 99 with CharStream

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

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();
    try (InputStream is = loader.getResourceAsStream(resourceName)) {
        long size = getResourceSize(loader, resourceName);
        CharStream input = CharStreams.fromStream(is, StandardCharsets.UTF_8, 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" : "");
    }
}
Also used : ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) CharStream(org.antlr.v4.runtime.CharStream) IOException(java.io.IOException)

Example 100 with CharStream

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

the class TestCharStreams method fromReader.

@Test
public void fromReader() throws Exception {
    Path p = folder.newFile().toPath();
    Files.write(p, "hello \uD83C\uDF0E".getBytes(StandardCharsets.UTF_8));
    try (Reader r = Files.newBufferedReader(p, StandardCharsets.UTF_8)) {
        CharStream s = CharStreams.fromReader(r);
        assertEquals(7, s.size());
        assertEquals(0, s.index());
        assertEquals("hello \uD83C\uDF0E", s.toString());
    }
}
Also used : Path(java.nio.file.Path) Reader(java.io.Reader) CharStream(org.antlr.v4.runtime.CharStream) Test(org.junit.Test)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)187 Test (org.junit.Test)93 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)85 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)46 ParseTree (org.antlr.v4.runtime.tree.ParseTree)38 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)30 IOException (java.io.IOException)28 InputStream (java.io.InputStream)23 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)23 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)22 File (java.io.File)21 TokenStream (org.antlr.v4.runtime.TokenStream)21 StringReader (java.io.StringReader)20 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)18 Token (org.antlr.v4.runtime.Token)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 Utils.toCharStream (clawfc.Utils.toCharStream)15 Path (java.nio.file.Path)14