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:de.huberlin.wbi.cuneiform.core.semanticmodel.CfSemanticModelVisitor.java

License:Apache License

public static TopLevelContext process(String input) {

    ANTLRInputStream instream;/*from www .  j a  v  a2 s . c om*/
    CuneiformLexer lexer;
    CommonTokenStream tokenStream;
    CuneiformParser parser;
    ParseTree tree;
    CfNode node;
    CfSemanticModelVisitor calculusVisitor;

    // parse original content
    instream = new ANTLRInputStream(input);

    lexer = new CuneiformLexer(instream);
    lexer.removeErrorListeners();

    tokenStream = new CommonTokenStream(lexer);

    parser = new CuneiformParser(tokenStream);
    parser.removeErrorListeners();

    calculusVisitor = new CfSemanticModelVisitor();
    lexer.addErrorListener(calculusVisitor);
    parser.addErrorListener(calculusVisitor);

    tree = parser.script();

    node = calculusVisitor.visit(tree);

    if (node == null)
        throw new NullPointerException("Node must not be null.");

    if (!(node instanceof TopLevelContext))
        throw new RuntimeException("Top level context expected.");

    return (TopLevelContext) node;
}

From source file:de.interactive_instruments.ShapeChange.SBVR.Sbvr2FolParser.java

License:Open Source License

/**
 * Parsed a first order logic expression from the given constraint. If
 * errors occur while parsing they are logged and <code>null</code> is
 * returned.//from   ww w .j  av a  2  s . c  om
 * 
 * @param con
 * @return the first order logic expression represented by the constraint,
 *         or <code>null</code> if errors were detected while parsing
 */
public FolExpression parse(FolConstraint con) {

    Variable.reset();

    SbvrParsingResult parsingResult = new SbvrParsingResult();
    parsingResult.setConstraint(con);

    ANTLRInputStream input = new ANTLRInputStream(con.text());

    // create a lexer that feeds off of input CharStream
    SBVRLexer lexer = new SBVRLexer(input);

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

    // create a parser that feeds off the tokens buffer
    SBVRParser parser = new SBVRParser(tokens);
    parser.helper = helper;

    /*
     * remove ConsoleErrorListener and add our own
     */
    parser.removeErrorListeners();
    SbvrErrorListener parsingErrorListener = new SbvrErrorListener();
    parser.addErrorListener(parsingErrorListener);

    // execute parsing, starting with rule 'sentence'
    ParseTree tree = parser.sentence();

    // get rule invocation stack for debugging
    parsingResult.setRuleInvocationStack(tree.toStringTree(parser));

    // if there were parsing errors, log them
    if (parsingErrorListener.hasErrors()) {

        parsingResult.addErrors(parsingErrorListener.getErrors());

    } else {

        // walk parse tree to apply further validation
        SbvrValidationErrorListener validationErrorListener = new SbvrValidationErrorListener(nouns, verbs);

        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(validationErrorListener, tree);

        // if there were validation errors, log them
        if (validationErrorListener.hasErrors()) {

            parsingResult.addErrors(validationErrorListener.getErrors());

        } else {

            // no parsing or validation errors encountered

            // create FOL expression

            Sbvr2FolVisitor folVisitor = new Sbvr2FolVisitor(model, con);

            FolExpression folExpr = folVisitor.visit(tree);

            if (folExpr == null) {

                if (folVisitor.hasErrors()) {
                    parsingResult.addErrors(folVisitor.getErrors());
                } else {
                    result.addError(this, 1);
                }

            } else {
                parsingResult.setFirstOrderLogicExpression(folExpr);
            }
        }
    }

    logParsingResult(parsingResult);

    if (parsingResult.hasFirstOrderLogicExpression()) {
        return parsingResult.getFirstOrderLogicExpression();
    } else {
        return null;
    }
}

From source file:de.interactive_instruments.ShapeChange.SBVR.SbvrRuleLoader.java

License:Open Source License

private String parseClassNameFromRuleText(String text) {

    ANTLRInputStream input = new ANTLRInputStream(text);

    // create a lexer that feeds off of input CharStream
    SBVRLexer lexer = new SBVRLexer(input);

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

    // create a parser that feeds off the tokens buffer
    SBVRParser parser = new SBVRParser(tokens);
    parser.helper = helper;/*from www. j  a  v  a  2s  . c o  m*/

    /*
     * remove ConsoleErrorListener and add our own
     */
    parser.removeErrorListeners();
    SbvrErrorListener parsingErrorListener = new SbvrErrorListener();
    parser.addErrorListener(parsingErrorListener);

    // execute parsing, starting with rule 'sentence'
    ParseTree tree = parser.sentence();

    // if there were parsing errors, we cannot identify the main class name
    if (parsingErrorListener.hasErrors()) {

        SbvrUtil.printErrors(parsingErrorListener.getErrors(), text, result);

        return null;

    } else {

        SbvrClassNameDetectionListener nameDetectionListener = new SbvrClassNameDetectionListener();

        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(nameDetectionListener, tree);

        return nameDetectionListener.getMainClassName();
    }
}

From source file:de.se_rwth.langeditor.language.ParserConfig.java

License:Open Source License

private P setupParser(String document) {
    ANTLRInputStream inputStream = new ANTLRInputStream(document);
    Lexer lexer = lexerCreator.apply(inputStream);
    TokenStream tokens = new CommonTokenStream(lexer);
    return parserCreator.apply(tokens);
}

From source file:de.swm.nis.logicaldecoding.parser.AntlrBasedParser.java

License:Open Source License

public Event parseLogLine(String message) {
    PgLogicalDecodingLexer lexer = new PgLogicalDecodingLexer(new ANTLRInputStream(message));
    PgLogicalDecodingParser parser = new PgLogicalDecodingParser(new CommonTokenStream(lexer));
    LoglineContext tree = parser.logline();
    ParserListener parserListener = new ParserListener();
    ParseTreeWalker.DEFAULT.walk(parserListener, tree);
    return parserListener.getEvent();
}

From source file:de.undercouch.citeproc.bibtex.NameParser.java

License:Apache License

/**
 * Parses names to {@link CSLName} objects. Also handles strings
 * containing multiple names separated by <code>and</code>. The
 * method always returns at least one object, even if the given
 * names cannot be parsed. In this case the returned object just
 * contains a literal string./*from  w ww.ja  v  a2 s.co  m*/
 * @param names the names to parse
 * @return the {@link CSLName} objects (never null and never empty)
 */
public static CSLName[] parse(String names) {
    ANTLRInputStream is = new ANTLRInputStream(names);
    InternalNameLexer lexer = new InternalNameLexer(is);
    lexer.removeErrorListeners(); //do not output errors to console
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    InternalNameParser parser = new InternalNameParser(tokens);
    parser.removeErrorListeners(); //do not output errors to console
    NamesContext ctx = parser.names();
    if (ctx.result.isEmpty() || ctx.exception != null) {
        //unparsable fall back to literal string
        return new CSLName[] { new CSLNameBuilder().literal(names).build() };
    }
    return ctx.result.toArray(new CSLName[ctx.result.size()]);
}

From source file:de.undercouch.citeproc.bibtex.PageParser.java

License:Apache License

/**
 * Parses a given page or range of pages. If the given string cannot
 * be parsed, the method will return a page range with a literal string.
 * @param pages the page or range of pages
 * @return the parsed page or page range (never null)
 *///from   ww w.  j  a  v a 2 s. c  o  m
public static PageRange parse(String pages) {
    ANTLRInputStream is = new ANTLRInputStream(pages);
    InternalPageLexer lexer = new InternalPageLexer(is);
    lexer.removeErrorListeners(); //do not output errors to console
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    InternalPageParser parser = new InternalPageParser(tokens);
    parser.removeErrorListeners(); //do not output errors to console
    PagesContext ctx = parser.pages();
    return new PageRange(ctx.literal != null ? ctx.literal : pages, ctx.pageFrom, ctx.numberOfPages);
}

From source file:de.uni.bremen.monty.moco.ast.AntlrAdapter.java

License:Open Source License

public MontyParser createParser(final InputStream file) throws IOException {

    InputStream in = createInputStream(file);

    // the additional line-break is needed because of our indentation rule
    // and the fact that a statement should be terminated by a line break
    ANTLRInputStream input = new ANTLRInputStream(in);
    MontyLexer lexer = new MontyLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    return new MontyParser(tokens);
}

From source file:de.up.ling.irtg.codec.BottomUpTreeAutomatonInputCodec.java

@Override
public TreeAutomaton read(InputStream is) throws ParseException, IOException {
    BottomUpTreeAutomatonLexer l = new BottomUpTreeAutomatonLexer(new ANTLRInputStream(is));
    BottomUpTreeAutomatonParser p = new BottomUpTreeAutomatonParser(new CommonTokenStream(l));
    p.setErrorHandler(new ExceptionErrorStrategy());
    p.getInterpreter().setPredictionMode(PredictionMode.SLL);

    automaton = new ConcreteTreeAutomaton<String>();

    try {/*from ww w  .j a  v a 2  s. c o  m*/
        BottomUpTreeAutomatonParser.FtaContext result = p.fta();

        for (BottomUpTreeAutomatonParser.StateContext stateContext : result.state()) {
            automaton.addFinalState(automaton.addState(state(stateContext)));
        }

        for (BottomUpTreeAutomatonParser.Auto_ruleContext ruleContext : result.auto_rule()) {
            autoRule(ruleContext);
        }

        return automaton;
    } catch (RecognitionException e) {
        throw new ParseException(e.getMessage());
    }
}

From source file:de.up.ling.irtg.codec.IrtgInputCodec.java

@Override
public InterpretedTreeAutomaton read(InputStream is) throws IOException, ParseException {
    IrtgLexer l = new IrtgLexer(new ANTLRInputStream(is));
    IrtgParser p = new IrtgParser(new CommonTokenStream(l));
    p.setErrorHandler(new ExceptionErrorStrategy());
    p.getInterpreter().setPredictionMode(PredictionMode.SLL);

    automaton = new ConcreteTreeAutomaton<String>();
    homomorphisms = new HashMap<String, Homomorphism>();
    interpretations = new HashMap<String, Interpretation>();
    features = new HashMap<String, FeatureFunction>();

    try {//from   w ww. j a  v a  2  s  .  c o  m
        IrtgParser.IrtgContext result = p.irtg();
        InterpretedTreeAutomaton irtg = build(result);
        return irtg;
    } catch (ParseException e) {
        throw e;
    } catch (RecognitionException e) {
        throw new ParseException(e.getMessage());
    }
}