Search in sources :

Example 11 with CharStream

use of org.antlr.v4.runtime.CharStream in project jaybird by FirebirdSQL.

the class TestGrammar method createParser.

// TODO Add more testcases for grammar
protected JaybirdSqlParser createParser(String testString) {
    CharStream stream = CharStreams.fromString(testString);
    JaybirdSqlLexer lexer = new JaybirdSqlLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    return new JaybirdSqlParser(tokenStream);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CharStream(org.antlr.v4.runtime.CharStream)

Example 12 with CharStream

use of org.antlr.v4.runtime.CharStream in project ballerina by ballerina-lang.

the class ManifestProcessor method getManifest.

/**
 * Get the manifest object by passing the ballerina toml file.
 *
 * @param charStream toml file content as a char stream
 * @return manifest object
 */
private static Manifest getManifest(CharStream charStream) {
    Manifest manifest = new Manifest();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(new ManifestBuildListener(manifest), TomlProcessor.parseTomlContent(charStream));
    return manifest;
}
Also used : Manifest(org.ballerinalang.toml.model.Manifest) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 13 with CharStream

use of org.antlr.v4.runtime.CharStream in project ballerina by ballerina-lang.

the class TomlProcessor method parseTomlContent.

/**
 * Generate the proxy object by passing in the toml file.
 *
 * @param stream charstream object containing the content
 * @return proxy object
 */
public static ParseTree parseTomlContent(CharStream stream) {
    TomlLexer lexer = new TomlLexer(stream);
    // Get a list of matched tokens
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Pass the tokens to the parser
    TomlParser parser = new TomlParser(tokens);
    return parser.toml();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TomlLexer(org.ballerinalang.toml.antlr4.TomlLexer) TomlParser(org.ballerinalang.toml.antlr4.TomlParser)

Example 14 with CharStream

use of org.antlr.v4.runtime.CharStream in project titan.EclipsePlug-ins by eclipse.

the class ASN1ReferenceParser method parseReference.

private Reference parseReference(final IFile file, final String input, final int line, final int offset) {
    Reference reference = null;
    StringReader reader = new StringReader(input);
    CharStream charStream = new UnbufferedCharStream(reader);
    Asn1Lexer lexer = new Asn1Lexer(charStream);
    lexer.setTokenFactory(new TokenWithIndexAndSubTokensFactory(true));
    ASN1Listener lexerListener = new ASN1Listener();
    // remove ConsoleErrorListener
    lexer.removeErrorListeners();
    lexer.addErrorListener(lexerListener);
    ModuleLevelTokenStreamTracker tracker = new ModuleLevelTokenStreamTracker(lexer);
    tracker.discard(Asn1Lexer.WS);
    tracker.discard(Asn1Lexer.MULTILINECOMMENT);
    tracker.discard(Asn1Lexer.SINGLELINECOMMENT);
    Asn1Parser parser = new Asn1Parser(tracker);
    parser.setProject(file.getProject());
    parser.setActualFile(file);
    parser.setLine(line);
    parser.setOffset(offset);
    parser.setBuildParseTree(false);
    ASN1Listener parserListener = new ASN1Listener();
    // remove ConsoleErrorListener
    parser.removeErrorListeners();
    parser.addErrorListener(parserListener);
    reference = parser.pr_parseReference().reference;
    return reference;
}
Also used : Asn1Parser(org.eclipse.titan.designer.parsers.asn1parser.Asn1Parser) Reference(org.eclipse.titan.designer.AST.Reference) Asn1Lexer(org.eclipse.titan.designer.parsers.asn1parser.Asn1Lexer) StringReader(java.io.StringReader) ASN1Listener(org.eclipse.titan.designer.parsers.asn1parser.ASN1Listener) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) ModuleLevelTokenStreamTracker(org.eclipse.titan.designer.parsers.asn1parser.ModuleLevelTokenStreamTracker) TokenWithIndexAndSubTokensFactory(org.eclipse.titan.designer.parsers.asn1parser.TokenWithIndexAndSubTokensFactory) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream)

Example 15 with CharStream

use of org.antlr.v4.runtime.CharStream in project titan.EclipsePlug-ins by eclipse.

the class ASN1Analyzer method parse.

@Override
public void parse(final IFile file, final String code) throws FileNotFoundException {
    Reader reader = null;
    if (code != null) {
        reader = new StringReader(code);
    } else if (file != null) {
        try {
            reader = new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset()));
        } catch (Exception e) {
            ErrorReporter.logExceptionStackTrace("Could not get the contents of `" + file.getName() + "'", e);
            return;
        }
    } else {
        return;
    }
    final CharStream charStream = new UnbufferedCharStream(reader);
    final Asn1Lexer lexer = new Asn1Lexer(charStream);
    lexer.setTokenFactory(new TokenWithIndexAndSubTokensFactory(true));
    lexer.setActualFile(file);
    lexerListener = new ASN1Listener();
    // remove ConsoleErrorListener
    lexer.removeErrorListeners();
    lexer.addErrorListener(lexerListener);
    final ModuleLevelTokenStreamTracker tracker = new ModuleLevelTokenStreamTracker(lexer);
    tracker.discard(Asn1Lexer.WS);
    tracker.discard(Asn1Lexer.MULTILINECOMMENT);
    tracker.discard(Asn1Lexer.SINGLELINECOMMENT);
    tracker.setActualFile(file);
    final Asn1Parser parser = new Asn1Parser(tracker);
    parser.setProject(file.getProject());
    parser.setActualFile(file);
    parser.setBuildParseTree(false);
    parserListener = new ASN1Listener(parser);
    // remove ConsoleErrorListener
    parser.removeErrorListeners();
    parser.addErrorListener(parserListener);
    parser.pr_ASN1ModuleDefinition();
    actualAsn1Module = parser.getModule();
    warnings = parser.getWarnings();
    unsupportedConstructs = parser.getUnsupportedConstructs();
    try {
        reader.close();
    } catch (IOException e) {
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CharStream(org.antlr.v4.runtime.CharStream) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) UnbufferedCharStream(org.antlr.v4.runtime.UnbufferedCharStream)

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