Exemplo n.º 1
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));
        }