示例#1
0
        protected override Maybe <IReportNode> Execute(TextParser p)
        {
            p.PushBookmark();
            var word = p.Word();

            if (!word)
            {
                return(Error(word));
            }

            int amount = 1;

            if (word.Match("unlimited"))
            {
                amount = -1;
                p.RemoveBookmark();
            }
            else
            {
                var intAmount = word.Integer();
                if (intAmount)
                {
                    amount = intAmount.Value;
                    p.RemoveBookmark();
                }
                else
                {
                    p.PopBookmark();
                }
            }

            Maybe <string> name = p.SkipWhitespaces().Before("[").SkipWhitespacesBackwards().AsString();

            if (!name)
            {
                return(Error(name));
            }

            Maybe <string> code = p.Between("[", "]").AsString();

            if (!code)
            {
                return(Error(code));
            }

            Maybe <int> price = p.Try(parser => parser.SkipWhitespaces().Word().Match("at")
                ? p.SkipWhitespaces().Word().Seek(1).Integer()
                : Maybe <int> .NA
                                      );

            return(Ok(ReportNode.Object(
                          ReportNode.Int("amount", amount),
                          ReportNode.Str("name", name),
                          ReportNode.Str("code", code),
                          price ? ReportNode.Int("price", price) : null
                          )));
        }
示例#2
0
        protected override Maybe <IReportNode> Execute(TextParser p)
        {
            if (!Mem(p.Between("(", ")")))
            {
                return(Error(LastResult));
            }
            var content = LastResult.Value;

            Maybe <int> x = content.SkipWhitespaces().Integer();

            if (!x)
            {
                return(Error(x));
            }

            Maybe <int> y = content.SkipWhitespaces().After(",").SkipWhitespaces().Integer();

            if (!y)
            {
                return(Error(y));
            }

            Maybe <int>    z     = Maybe <int> .NA;
            Maybe <string> label = Maybe <string> .NA;

            if (content.SkipWhitespaces().After(","))
            {
                z = content.SkipWhitespaces().Integer();
                content.SkipWhitespaces();
                label = content.Between("<", ">").AsString();
            }

            content.Try(parser => {
                if (parser.SkipWhitespaces().After(","))
                {
                    z = parser.SkipWhitespaces().Integer();
                    parser.SkipWhitespaces();
                    label = parser.Between("<", ">").AsString();

                    return(new Maybe <object>(null));
                }

                return(Maybe <object> .NA);
            });

            return(Ok(ReportNode.Object(
                          ReportNode.Int("x", x),
                          ReportNode.Int("y", y),
                          z ? ReportNode.Int("z", z) : null,
                          label ? ReportNode.Str("label", label) : null
                          )));
        }
示例#3
0
        protected override Maybe <IReportNode> Execute(TextParser p)
        {
            var name = p.Before("(").SkipWhitespacesBackwards().AsString();

            if (!name)
            {
                return(Error(name));
            }

            var num = p.Between("(", ")").Integer();

            if (!num)
            {
                return(Error(num));
            }

            return(Ok(ReportNode.Bag(
                          ReportNode.Str("name", name),
                          ReportNode.Int("number", num)
                          )));
        }
示例#4
0
        protected override Maybe <IReportNode> Execute(TextParser p)
        {
            var name = p.Before("[").SkipWhitespacesBackwards().AsString();

            if (!name)
            {
                return(Error(name));
            }

            var code = p.Between("[", "]").AsString();

            if (!code)
            {
                return(Error(code));
            }

            p.PushBookmark();
            var level = p.SkipWhitespaces().Integer();
            var days  = Maybe <int> .NA;

            if (level)
            {
                p.RemoveBookmark();
                days = p.Try(parser => parser.SkipWhitespaces().Between("(", ")").Integer());
            }
            else
            {
                p.PopBookmark();
            }

            return(Ok(ReportNode.Object(
                          ReportNode.Str("name", name),
                          ReportNode.Str("code", code),
                          level ? ReportNode.Int("level", level) : null,
                          days ? ReportNode.Int("days", days) : null
                          )));
        }
示例#5
0
        protected override Maybe <IReportNode> Execute(TextParser p)
        {
            var terrain = p.Before("(").SkipWhitespacesBackwards().AsString();

            if (!terrain)
            {
                return(Error(terrain));
            }

            var coords = coordsParser.Parse(p);

            if (!coords)
            {
                return(Error(coords));
            }

            var province = p.After("in").SkipWhitespaces().OneOf(
                x => x.Before(","),
                x => x.Before(".")
                ).AsString();

            if (!province)
            {
                return(Error(province));
            }

            var settlement = p.Try(x => {
                var name = p.After(",").After("contains").SkipWhitespaces().Before("[").SkipWhitespacesBackwards().AsString();
                if (!name)
                {
                    return(name.Convert <(string, string)>());
                }

                var size = p.Between("[", "]").AsString();
                if (!size)
                {
                    return(size.Convert <(string, string)>());
                }

                return(new Maybe <(string, string)>((name.Value, size.Value)));
            });

            var population = p.Try(x => {
                var amount = p.After(",").SkipWhitespaces().Integer();
                if (!amount)
                {
                    return(amount.Convert <(int, string)>());
                }

                var race = p.Between("(", ")").AsString();
                if (!race)
                {
                    return(race.Convert <(int, string)>());
                }

                return(new Maybe <(int, string)>((amount.Value, race.Value)));
            });

            var tax = p.Try(x => x.After("$").Integer());

            return(Ok(ReportNode.Bag(
                          ReportNode.Str("terrain", terrain),
                          ReportNode.Key("coords", coords.Value),
                          ReportNode.Str("province", province),
                          settlement ? ReportNode.Key("settlement", ReportNode.Object(
                                                          ReportNode.Str("name", settlement.Value.Item1),
                                                          ReportNode.Str("size", settlement.Value.Item2)
                                                          )) : null,
                          population ? ReportNode.Key("population", ReportNode.Object(
                                                          ReportNode.Int("amount", population.Value.Item1),
                                                          ReportNode.Str("race", population.Value.Item2)
                                                          )) : null,
                          tax ? ReportNode.Int("tax", tax) : null
                          )));
        }