示例#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
        public static Maybe <IReportNode> OneOf(this TextParser p, params IReportParser[] parsers)
        {
            for (var i = 0; i < parsers.Length; i++)
            {
                var result = p.Try(parsers[i]);
                if (result)
                {
                    return(result);
                }
            }

            return(new Maybe <IReportNode>("all options does not fit", p.Ln, p.Pos + 1));
        }
示例#3
0
        public static Maybe <T> OneOf <T>(this TextParser p, params Func <TextParser, Maybe <T> >[] parsers)
        {
            for (var i = 0; i < parsers.Length; i++)
            {
                var result = p.Try(parsers[i]);
                if (result)
                {
                    return(result);
                }
            }

            return(new Maybe <T>("all options does not fit", p.Ln, p.Pos + 1));
        }
示例#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
                          )));
        }
示例#6
0
        protected override Maybe <IReportNode> Execute(TextParser p)
        {
            // don't need dot at the end of line
            p = p.BeforeBackwards(".");

            bool own = false;

            if (p.Match("*"))
            {
                own = true;
            }
            else if (!Mem(p.Match("-")))
            {
                return(Error(LastResult));
            }

            p = p.SkipWhitespaces();

            // unit name
            // Legatus Legionis Marcus (640)
            var name = nameParser.Parse(p);

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

            // optional on guard flag
            // , on guard
            var onGuard = p.Try(parser => parser.After(",").SkipWhitespaces().Match("on guard"));

            // optional faction name unless this is own unit
            // , Avalon Empire (15)
            var faction = p.Try(parser => factionParser.Parse(parser.After(",").SkipWhitespaces()));

            if (!faction && own)
            {
                return(Error(faction));
            }

            // now check for description, it must start with `;`
            Maybe <string> description = Maybe <string> .NA;

            p.PushBookmark();
            if (p.After(";"))
            {
                var descPos = p.Pos - 2;
                description = new Maybe <string>(p.SkipWhitespaces().SkipWhitespacesBackwards().AsString());

                // w/o description
                p.PopBookmark();
                p = p.Slice(descPos - p.Pos + 1);
            }
            else
            {
                p.RemoveBookmark();
            }

            List <string>      flags = new List <string>();
            List <IReportNode> items = new List <IReportNode>();

            // all element after faction name till first item are flags
            while (items.Count == 0 && !p.EOF)
            {
                // , flag
                if (!Mem(p.After(",").SkipWhitespaces()))
                {
                    return(Error(LastResult));
                }

                // there could be no flags
                var flagOrItem = p.Before(",", ".").RecoverWith(() => p);

                var item = itemParser.Parse(flagOrItem);
                if (item)
                {
                    items.Add(item.Value);
                }
                else
                {
                    flags.Add(flagOrItem.Reset().AsString());
                }
            }

            var remainingItems = p.Before(".").RecoverWith(() => p).Value;

            while (!remainingItems.EOF)
            {
                var nextItem = remainingItems.After(",").SkipWhitespaces();
                if (!nextItem)
                {
                    break;
                }

                var item = itemParser.ParseMaybe(nextItem.Before(",").RecoverWith(() => nextItem));
                if (item)
                {
                    items.Add(item.Value);
                }
            }

            List <IReportNode> props = new List <IReportNode>();

            while (!p.EOF)
            {
                var nextProp = p.After(".").SkipWhitespaces();
                if (!nextProp)
                {
                    break;
                }

                var notSuccess = !nextProp.Success;

                nextProp = nextProp.Before(".").RecoverWith(() => nextProp);
                var prop = nextProp.OneOf(
                    weightParser,
                    capacityParser,
                    canStudyParser,
                    skillsParser
                    );
                if (prop)
                {
                    props.Add(prop.Value);
                }
            }

            var result = ReportNode.Object();

            result.Add(ReportNode.Bool("own", own));
            result.Add(name.Value);
            result.Add(ReportNode.Key("faction", faction ? ReportNode.Object(faction.Value) : ReportNode.Null()));
            result.Add(ReportNode.Key("description", description ? ReportNode.Str(description) : ReportNode.Null()));
            result.Add(ReportNode.Bool("onGuard", onGuard));
            result.Add(ReportNode.Key("flags", ReportNode.Array(flags.Select(x => ReportNode.Str(x)))));
            result.Add(ReportNode.Key("items", ReportNode.Array(items)));
            result.AddRange(props);

            return(new Maybe <IReportNode>(result));
        }