Search in sources :

Example 1 with ExpectFun

use of suite.ebnf.topdown.Expect.ExpectFun in project suite by stupidsing.

the class TopDownParse method build.

private Parser build(Grammar eg) {
    Parser parser, g;
    List<Parser> parsers;
    switch(eg.type) {
        case AND___:
            parsers = buildChildren(eg);
            parser = (parse, st) -> {
                Outlet<State> o = Outlet.of(st);
                for (Parser g_ : parsers) o = o.concatMap(st_ -> st_.pr(parse, g_));
                return o;
            };
            break;
        case ENTITY:
            parser = buildEntity(eg.content);
            break;
        case EXCEPT:
            Parser parser0 = build(eg.children.get(0));
            Parser parser1 = build(eg.children.get(1));
            parser = (parse, st) -> st.p(parse, parser0).filter(st1 -> {
                String in1 = parse.in.substring(st.pos, st1.pos);
                return new State(null, 0, null, 0).p(new Parse(in1), parser1).count() == 0;
            });
            break;
        case NAMED_:
            parser = deepen(build(eg.children.get(0)), eg.content);
            break;
        case ONCE__:
            g = build(eg.children.get(0));
            parser = (parse, st) -> Outlet.of(st.pr(parse, g).take(1));
            break;
        case OPTION:
            g = build(eg.children.get(0));
            parser = (parse, st) -> st.pr(parse, g).cons(st);
            break;
        case OR____:
            parsers = buildChildren(eg);
            parser = (parse, st) -> Outlet.of(parsers).concatMap(g_ -> st.pr(parse, g_));
            break;
        case REPT0_:
            parser = buildRepeat(eg, true);
            break;
        case REPT0H:
            parser = buildRepeatHeadRecursion(eg);
            break;
        case REPT1_:
            parser = buildRepeat(eg, false);
            break;
        case STRING:
            ExpectFun e = expect.string(eg.content);
            parser = skipWhitespaces((parse, st) -> parse.expect(st, e, st.pos));
            break;
        default:
            parser = null;
    }
    return parser;
}
Also used : Outlet(suite.streamlet.Outlet) Read(suite.streamlet.Read) Ast(suite.ebnf.Ebnf.Ast) ExpectFun(suite.ebnf.topdown.Expect.ExpectFun) LogUtil(suite.os.LogUtil) IntIntPair(suite.primitive.adt.pair.IntIntPair) Source(suite.util.FunUtil.Source) Grammar(suite.ebnf.Grammar) Deque(java.util.Deque) List(java.util.List) GrammarType(suite.ebnf.Grammar.GrammarType) String_(suite.util.String_) Map(java.util.Map) ArrayDeque(java.util.ArrayDeque) Fail(suite.util.Fail) ExpectFun(suite.ebnf.topdown.Expect.ExpectFun)

Aggregations

ArrayDeque (java.util.ArrayDeque)1 Deque (java.util.Deque)1 List (java.util.List)1 Map (java.util.Map)1 Ast (suite.ebnf.Ebnf.Ast)1 Grammar (suite.ebnf.Grammar)1 GrammarType (suite.ebnf.Grammar.GrammarType)1 ExpectFun (suite.ebnf.topdown.Expect.ExpectFun)1 LogUtil (suite.os.LogUtil)1 IntIntPair (suite.primitive.adt.pair.IntIntPair)1 Outlet (suite.streamlet.Outlet)1 Read (suite.streamlet.Read)1 Fail (suite.util.Fail)1 Source (suite.util.FunUtil.Source)1 String_ (suite.util.String_)1