Example usage for org.antlr.v4.runtime CommonTokenStream CommonTokenStream

List of usage examples for org.antlr.v4.runtime CommonTokenStream CommonTokenStream

Introduction

In this page you can find the example usage for org.antlr.v4.runtime CommonTokenStream CommonTokenStream.

Prototype

public CommonTokenStream(TokenSource tokenSource) 

Source Link

Document

Constructs a new CommonTokenStream using the specified token source and the default token channel ( Token#DEFAULT_CHANNEL ).

Usage

From source file:com.chiralbehaviors.CoRE.universal.Spa.java

License:Open Source License

public static Spa manifest(String resource) throws IOException {
    SpaLexer l = new SpaLexer(CharStreams.fromStream(Utils.resolveResource(Spa.class, resource)));
    SpaParser p = new SpaParser(new CommonTokenStream(l));
    p.addErrorListener(new BaseErrorListener() {
        @Override/*from   w w  w .  ja  va  2 s . c  o m*/
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) {
            throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e);
        }
    });
    SpaContext spa = p.spa();
    SpaImporter importer = new SpaImporter();
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(importer, spa);
    return importer.getSpa();
}

From source file:com.cisco.yangide.core.parser.YangParserUtil.java

License:Open Source License

public static YangContext parseYangSource(char[] content, final IYangValidationListener validationListener) {
    final ANTLRInputStream input = new ANTLRInputStream(content, content.length);
    final YangLexer lexer = new YangLexer(input);
    final CommonTokenStream tokens = new CommonTokenStream(lexer);
    final YangParser parser = new YangParser(tokens);
    parser.removeErrorListeners();/*from  w w  w.  j a va  2s . co m*/
    if (validationListener != null) {
        parser.addErrorListener(new BaseErrorListener() {
            @Override
            public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                    int charPositionInLine, String msg, RecognitionException e) {

                int charStart = 0;
                int charEnd = 0;
                if (offendingSymbol != null && offendingSymbol instanceof Token) {
                    charStart = ((Token) offendingSymbol).getStartIndex();
                    charEnd = ((Token) offendingSymbol).getStopIndex() + 1;
                }
                validationListener.syntaxError(msg, line, charStart, charEnd);
            }
        });
    }
    return parser.yang();
}

From source file:com.cloudbees.plugins.credentials.CredentialsMatchers.java

License:Open Source License

/**
 * Attempts to parse a Credentials Query Language expression and construct the corresponding matcher.
 *
 * @param cql the Credentials Query Language expression to parse.
 * @return a {@link CredentialsMatcher} for this expression.
 * @throws CQLSyntaxException if the expression could not be parsed.
 * @since 2.1.0/* w  ww  .j  a  v a2 s.c  o m*/
 */
@NonNull
public static CredentialsMatcher parse(final String cql) {

    if (StringUtils.isEmpty(cql)) {
        return always();
    }

    CQLLexer lexer = new CQLLexer(new ANTLRInputStream(cql));

    CommonTokenStream tokens = new CommonTokenStream(lexer);

    CQLParser parser = new CQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) {
            StringBuilder expression = new StringBuilder(
                    cql.length() + msg.length() + charPositionInLine + 256);
            String[] lines = StringUtils.split(cql, '\n');
            for (int i = 0; i < line; i++) {
                expression.append("    ").append(lines[i]).append('\n');
            }
            expression.append("    ").append(StringUtils.repeat(" ", charPositionInLine)).append("^ ")
                    .append(msg);
            for (int i = line; i < lines.length; i++) {
                expression.append("\n    ").append(lines[i]);
            }
            throw new CQLSyntaxException(
                    String.format("CQL syntax error: line %d:%d%n%s", line, charPositionInLine, expression),
                    charPositionInLine);
        }
    });

    CQLParser.ExpressionContext expressionContext = parser.expression();

    ParseTreeWalker walker = new ParseTreeWalker();

    MatcherBuildingListener listener = new MatcherBuildingListener();

    try {
        walker.walk(listener, expressionContext);

        return listener.getMatcher();
    } catch (EmptyStackException e) {
        throw new IllegalStateException("There should not be an empty stack when starting from an expression",
                e);
    } catch (CQLSyntaxError e) {
        throw new CQLSyntaxException(String.format("CQL syntax error:%n    %s%n    %s%s unexpected symbol %s",
                cql, StringUtils.repeat(" ", e.interval.a), StringUtils.repeat("^", e.interval.length()),
                e.text), e.interval.a);
    }
}

From source file:com.codenvy.modeling.adapter.editor.EditorConfigurationAdapterImpl.java

License:Apache License

@Nonnull
@Override/*from w w  w  .j  a v  a 2  s  .c  om*/
public EditorConfiguration getConfiguration() throws IOException {
    ANTLRInputStream antlrInputStream = new ANTLRInputStream(inputStream);
    EditorLexer lexer = new EditorLexer(antlrInputStream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    EditorParser parser = new EditorParser(tokenStream);
    EditorConfigurationAdapterListener editorConfigurationAdapterListener = editorConfigurationAdapterListenerProvider
            .get();
    (new ParseTreeWalker()).walk(editorConfigurationAdapterListener, parser.editor());

    return editorConfigurationAdapterListener.getEditorConfiguration();
}

From source file:com.codenvy.modeling.adapter.metamodel.diagram.DiagramConfigurationAdapterImpl.java

License:Apache License

@Nonnull
@Override/*from  w  w w  . j  ava2s. co m*/
public DiagramConfiguration getConfiguration() throws IOException, ParseConfigurationException {
    ANTLRInputStream antlrInputStream = new ANTLRInputStream(inputStream);
    DiagramLexer lexer = new DiagramLexer(antlrInputStream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    DiagramParser parser = new DiagramParser(tokenStream);
    DiagramConfigurationAdapterListener diagramConfigurationAdapterListener = diagramConfigurationAdapterListenerProvider
            .get();
    (new ParseTreeWalker()).walk(diagramConfigurationAdapterListener, parser.diagram());

    return diagramConfigurationAdapterListener.getDiagramConfiguration();
}

From source file:com.codenvy.modeling.adapter.metamodel.serialization.SerializationConfigurationAdapterImpl.java

License:Apache License

@Nonnull
@Override//from  w ww . j  av  a 2 s . co  m
public SerializationConfiguration getConfiguration() throws IOException {

    ANTLRInputStream antlrInputStream = new ANTLRInputStream(inputStream);
    SerializationLexer lexer = new SerializationLexer(antlrInputStream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    SerializationParser parser = new SerializationParser(tokenStream);
    SerializationConfigurationAdapterListener serializationConfigurationAdapterListener = serializationConfigurationAdapterListenerProvider
            .get();
    (new ParseTreeWalker()).walk(serializationConfigurationAdapterListener, parser.serialization());

    return serializationConfigurationAdapterListener.getSerializationConfiguration();
}

From source file:com.comcast.oscar.configurationfile.ConfigurationFileImport.java

License:Apache License

/**
 * /*from   ww  w .jav a  2 s  . c  o  m*/
 * @param sbConfigurationFile
 */
private void processConfigurationFile(StringBuilder sbConfigurationFile) {

    // create a CharStream that reads from standard input
    ANTLRInputStream input = new ANTLRInputStream(sbConfigurationFile.toString());

    // create a Lexer that feeds off of input CharStream
    tlvLexer tlConfigLexer = new tlvLexer(input);

    // create a buffer of tokens pulled from the lexer
    CommonTokenStream ctsConfigTokens = new CommonTokenStream(tlConfigLexer);

    tlvParser tpConfigParser = new tlvParser(ctsConfigTokens);

    ParseTree ptConfigParseTree = tpConfigParser.begin();

    ParseTreeWalker walker = new ParseTreeWalker();

    tcfpConfig = new TlvConfigurationFileParser();

    walker.walk(tcfpConfig, ptConfigParseTree);

    //Update local JSONArray
    this.jaTlvDefinition = tcfpConfig.getTLVDefinitions();

    this.iConfigurationFileType = tcfpConfig.getConfigurationFileType();

}

From source file:com.compilerlab.compiler.Main.java

License:Open Source License

/**
 * This function compiles the source code to java assembly code. 
 * @param input The source code./*from w w w  .  ja v  a2 s.  c o  m*/
 * @return The java assembly code.
 */
public static String createJavaAssemblyCode(ANTLRInputStream input) {
    ProgramLexer lexer = new ProgramLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    ProgramParser parser = new ProgramParser(tokens);

    ParseTree tree = parser.program();

    //Find global Variables and definitions
    HashMap<String, Value> globalVariables = Finder.findGlobalVariables(tree);
    HashMap<String, Class<? extends Value>> functionDefinitions = Finder.findFunctionDefinitions(tree);
    Program program = new Program(globalVariables, functionDefinitions, programName);
    Collection<Function> functionList = (Collection<Function>) new FunctionVisitor(globalVariables,
            functionDefinitions.keySet()).visit(tree);

    program.setFunctions(functionList);

    if (debug) {
        System.err.println(hr);
        System.err.println("The parsed program:");
        System.err.println(hr);
        System.out.println(program);
    }

    return program.compile();
}

From source file:com.cybernostics.jsp.parser.JSPParserRunner.java

private static void printJSP(String inputText) {
    // Get our lexer
    JSPLexer lexer = new JSPLexer(new ANTLRInputStream(inputText));

    // Get a list of matched tokens
    CommonTokenStream tokens = new CommonTokenStream(lexer);

    // Pass the tokens to the parser
    JSPParser parser = new JSPParser(tokens);

    // Specify our entry point
    JSPParser.JspDocumentContext documentContext = parser.jspDocument();

    // Walk it and attach our listener
    ParseTreeWalker walker = new ParseTreeWalker();
    AntlrJSPListener listener = new AntlrJSPListener(parser);
    walker.walk(listener, documentContext);
}

From source file:com.cybernostics.jsp2thymeleaf.api.common.TokenisedFile.java

private void gatherIncludes(JSPLexer lexer) {
    lexer.reset();/*  ww  w .  j  a  va 2 s. co  m*/
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Pass the tokens to the parser
    JSPParser parser = new JSPParser(tokens);
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(new IncludeGatherer(includedPaths, filePath), parser.jspDocument());

}