public static Maybe <T[]> List <T>(this TextParser p, ReadOnlySpan <char> separator, Func <TextParser, Maybe <T> > itemParser) { List <T> items = new List <T>(); if (p.Match("none")) { return(new Maybe <T[]>(new T[0])); } while (!p.EOF) { Maybe <TextParser> span = p.Before(separator); if (span) { p.Seek(separator.Length); } var item = itemParser(span ? span : p); if (!item) { return(item.Convert <T[]>()); } items.Add(item.Value); if (!span) { break; } } return(new Maybe <T[]>(items.ToArray())); }
private bool IsStructure(TextParser p) { var isRegionContent = p.Match("+"); p.Reset(); return(isRegionContent); }
protected override Maybe <IReportNode> Execute(TextParser p) { var prefix = p.After("Skills:").SkipWhitespaces(); if (!prefix) { return(Error(prefix)); } List <IReportNode> skills = new List <IReportNode>(); if (!p.Match("none")) { while (!p.EOF) { if (skills.Count > 0) { if (!Mem(p.After(",").SkipWhitespaces())) { return(Error(LastResult)); } } var skill = skillParser.Parse(p); if (!skill) { return(Error(skill)); } skills.Add(skill.Value); } } return(Ok(ReportNode.Bag( ReportNode.Key("skills", ReportNode.Array(skills)) ))); }
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)); }