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.up.ling.irtg.codec.PcfgIrtgInputCodec.java

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

    try {//from  w  ww  .  j av a2 s.c om
        PcfgAsIrtgParser.PcfgContext result = p.pcfg();

        ConcreteTreeAutomaton<String> auto = new ConcreteTreeAutomaton<>();
        StringAlgebra stringAlgebra = new StringAlgebra();
        Homomorphism stringHom = new Homomorphism(auto.getSignature(), stringAlgebra.getSignature());
        TreeWithAritiesAlgebra treeAlgebra = new TreeWithAritiesAlgebra();
        Homomorphism treeHom = new Homomorphism(auto.getSignature(), treeAlgebra.getSignature());

        pcfg(result, auto, stringHom, treeHom);

        InterpretedTreeAutomaton irtg = new InterpretedTreeAutomaton(auto);
        irtg.addInterpretation("string", new Interpretation(stringAlgebra, stringHom));
        irtg.addInterpretation("tree", new Interpretation(treeAlgebra, treeHom));
        return irtg;
    } catch (RecognitionException e) {
        throw new ParseException(e.getMessage());
    }
}

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

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

    tirtg = new TemplateInterpretedTreeAutomaton();

    try {/*from   w  w  w .ja  va 2  s . co m*/
        TemplateIrtgParser.Template_irtgContext result = p.template_irtg();
        build(result);
        return tirtg;
    } catch (ParseException e) {
        throw e;
    } catch (RecognitionException e) {
        throw new ParseException(e.getMessage());
    }
}

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

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

    automaton = new ConcreteTreeAutomaton<String>();

    try {//  w w  w .  j av  a  2s . c o m
        TiburonTreeAutomatonParser.FtaContext result = p.fta();

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

        Set<String> states = new HashSet<>();
        List<RawRule> rawRules = new ArrayList<>();

        for (TiburonTreeAutomatonParser.Auto_ruleContext ruleContext : result.auto_rule()) {
            autoRule(ruleContext, rawRules, states);
        }

        for (RawRule rule : rawRules) {
            List<String> children = util.introduceAnonymousStates(automaton, rule.rhs, states);
            automaton.addRule(automaton.createRule(rule.lhs, rule.label, children));
        }

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

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

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

    automaton = new ConcreteTreeAutomaton<>();

    try {//from  w  w w  .  jav a  2s .c om
        TreeAutomatonParser.FtaContext result = p.fta();

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

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

From source file:de.weltraumschaf.caythe.parser.Parsers.java

License:BEER-WARE LICENSE

/**
 * Parser for the Caythe language./*  ww  w.j a v  a2  s.  com*/
 *
 * @param source must not be {@code null}
 * @return never {@code nul}
 * @throws IOException if source can't be opened
 */
public static CaytheParser caythe(final SourceFile source) throws IOException {
    return new CaytheParser(new CommonTokenStream(new CaytheLexer(source.newStream())));
}

From source file:de.weltraumschaf.caythe.parser.Parsers.java

License:BEER-WARE LICENSE

/**
 * Parser for the Java language (example from ANTLR).
 *
 * @param source must not be {@code null}
 * @return never {@code nul}/*from   w  w w  . j  av a2  s.  c o  m*/
 * @throws IOException if source can't be opened
 */
public static JavaParser java(final SourceFile source) throws IOException {
    return new JavaParser(new CommonTokenStream(new JavaLexer(source.newStream())));
}

From source file:de.weltraumschaf.shackhack.ShackHackCommand.java

License:BEER-WARE LICENSE

private List<Instruction> parseSource(final String filename) throws IOException {
    final CharStream input = new ANTLRFileStream(filename, ENCODING);
    final ShackHackLexer lexer = new ShackHackLexer(input);
    final TokenStream tokens = new CommonTokenStream(lexer);
    final ShackHackParser parser = new ShackHackParser(tokens);

    if (echoDebug) {
        parser.setErrorHandler(new BailErrorStrategy());
    }/*from   www .j a  va2 s.  c  o m*/

    final ByteCodeVisitor visitor = new ByteCodeVisitor();
    return visitor.visit(parser.program());
}

From source file:depgraphs.scraper.JavaDirectiveScraper.java

@Override
public void scrape(File f, EventAdapter adapter) {
    try {//w  ww.j  a v  a 2s .c om
        CharStream input = new ANTLRFileStream(f.getCanonicalPath());
        JavaDirectiveLexer lex = new JavaDirectiveLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lex);
        JavaDirectiveParser parser = new JavaDirectiveParser(tokens);
        ParseTree pt = parser.lang_source();

        JavaDirectiveVisitor visitor = new JavaDirectiveVisitor();
        visitor.useAdapter(adapter);
        visitor.visit(pt);
    } catch (IOException | RecognitionException ex) {
        env.log(f.getName() + " FAIL");
    }
}

From source file:depgraphs.scraper.JavaScraper.java

@Override
public void scrape(File f, EventAdapter adapter) {
    try {//from   w  ww  .  j a  v a2  s.  c om
        CharStream input = new ANTLRFileStream(f.getCanonicalPath());
        JavaLexer lex = new JavaLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lex);
        JavaParser parser = new JavaParser(tokens);
        ParseTree pt = parser.compilationUnit();

        JavaVisitor visitor = new JavaVisitor();

        visitor.visit(pt);

        String name = f.getName().replace(".java", "");

        HashSet<String> set = visitor.col;
        List<String> fqn = visitor.fqn;

        fqn.add(name);

        Local.storage.add(fqn).get().setLocal(set).isTerminal = true;

    } catch (IOException | RecognitionException ex) {
        Logger.getLogger(JavaDirectiveScraper.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:dijkstra.utility.DijkstraFactory.java

License:Open Source License

/**
 * @param inputText// w ww. j  a  v a 2  s.c  o  m
 * @return
 */
static public DijkstraParser makeParser(ANTLRInputStream inputText) {
    final DijkstraLexer lexer = makeLexer(inputText);
    final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    final DijkstraParser parser = new DijkstraParser(tokenStream);
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line,
                int charPositionInLine, String msg, RecognitionException e) {
            throw new DijkstraParserException(msg, e);
        }
    });
    return parser;
}