Example #1
0
        public static Grammar Create()
        {
            RuleExpr Json        = "Json";
            RuleExpr Object      = "Object";
            RuleExpr Pair        = "Pair";
            RuleExpr PairRepeat  = "PairRepeat";
            RuleExpr Array       = "Array";
            RuleExpr Value       = "Value";
            RuleExpr ValueRepeat = "ValueRepeat";

            var number  = NumberTerminal();
            var @string = StringTerminal();

            Json.Body = Value;

            Value.Body = @string | number | Object | Array | "false" | "true" | "null";

            Object.Body = '{' + PairRepeat + '}';

            PairRepeat.Body = Pair | (Pair + ',' + PairRepeat) | ChainExpr.Epsilon;

            Pair.Body = @string + ':' + Value;

            Array.Body = '[' + ValueRepeat + ']';

            ValueRepeat.Body = Value | (Value + ',' + ValueRepeat) | ChainExpr.Epsilon;

            return(new GrammarBuilder().From(Json));
        }
Example #2
0
        public static Grammar Create()
        {
            RuleExpr Expr    = "Expr";
            RuleExpr Sum     = "Sum";
            RuleExpr Product = "Product";
            RuleExpr Factor  = "Factor";
            RuleExpr Primary = "Primary";
            RuleExpr Number  = "Number";

            Expr.Body = Sum;

            Sum.Body =
                (Sum + '+' + Product) |
                (Sum + '-' + Product) |
                Product;

            Product.Body =
                (Product + '*' + Factor) |
                (Product + '/' + Factor) |
                Factor;

            Factor.Body = Primary;

            Primary.Body =
                ('(' + Sum + ')') |
                Number;

            Number.Body = NumberTerminal();

            return(new GrammarBuilder().From(Expr));
        }
Example #3
0
        private EarleyEngine MakeEngine()
        {
            RuleExpr argument     = "argument";
            RuleExpr argumentList = "argument-list";
            RuleExpr atom         = "atom";

            argument.Body =
                atom;
            argumentList.Body =
                argument |
                (argumentList + ',' + argument);
            atom.Body =
                'a';

            var grammar = new GrammarBuilder().From(argumentList);

            var engine = new EarleyEngine(grammar);

            return(engine);
        }
Example #4
0
        private void Check1()
        {
            RuleExpr argument     = "argument";
            RuleExpr argumentList = "argument-list";
            RuleExpr atom         = "atom";

            var name   = TerminalExpr.From(DfaProvision.From("name", 'n'));
            var number = TerminalExpr.From(DfaProvision.From("number", '1'));

            argument.Body     = atom;
            argumentList.Body = argument | (argumentList + ',' + argument);
            atom.Body         = name | number;

            var grammar = new GrammarBuilder().From(argumentList);

            var engine = new EarleyEngine(grammar);

            engine.Pulse(XToken.Name);
            engine.Pulse(XToken.Comma);
            engine.Pulse(XToken.Number);
        }
        public static IEnumerable <string> GetUsedKeys(RuleExpr <int> expr)
        {
            var loader = new Loader();

            IEnumerable <ILoader> Loaders()
            {
                while (true)
                {
                    yield return(loader);
                }
            }

            var(_, context) = expr(new RuleExprContext
            {
                Amount      = 1200,
                Loaders     = Loaders(),
                KeyValueMap = ImmutableDictionary <string, int> .Empty
            });

            return(context.KeyValueMap.Keys);
        }
Example #6
0
        public static RuleExpr <T, RuleExprContext <Unit> > LiftImpl <T>(this RuleExpr <Option <T>, RuleExprContext <string> > sRule, VoteMethod <T> vote, IEnumerable <string> sKeys)
        {
            return(mcontext =>
            {
                var scontext = mcontext.WithSelector <string>(null);
                var result = new List <Option <T> >();
                foreach (var applicant in mcontext.Applicants)
                {
                    scontext = scontext.WithSelector(applicant.Key);
                    var(maybea, newSContext) = sRule(scontext);
                    scontext = newSContext;
                    if (!maybea.IsSome(out var a))
                    {
                        throw new Exception("Internal error. Lifting failed. Exception scope did not catch error as expected.");
                    }
                    result.Add(a);
                }

                return (vote(result), scontext.WithSelector(Unit.Value));
            });
        }
Example #7
0
        public static Grammar Create()
        {
            TerminalExpr dot = ".";

            RuleExpr Start      = "start";
            RuleExpr Dot        = "dot";
            RuleExpr DotDot     = "dotdot";
            RuleExpr Dot_DotDot = "dot-dotdot";
            RuleExpr DotDot_Dot = "dotdot-dot";

            Start.Body =
                (Dot_DotDot + DotDot_Dot) |
                (DotDot_Dot + Dot_DotDot);

            Dot.Body = dot;

            DotDot.Body = Dot + Dot;

            Dot_DotDot.Body = Dot + DotDot;

            DotDot_Dot.Body = DotDot + Dot;

            return(new GrammarBuilder().From(Start));
        }