Example #1
0
        public static IEnumerable<Segment> Parse(string s)
        {
            var segments = new List<Segment>();

            var stack = new Stack<Tuple<int, Segment>>();

            var lines = s.Split('\n');

            foreach (var line in lines)
            {
                var match = Regex.Match(line, @"^(\s*)(.*)$");
                var whiteSpace = match.Result("$1").Replace("\t", "    ");
                var text = match.Result("$2");

                if (String.IsNullOrWhiteSpace(text))
                {
                    stack.Clear();
                    continue;
                }

                var segment = new Segment(text.Trim());

                var indent = whiteSpace.Length;

                if (stack.Any())
                {
                    while (stack.Any() && stack.Peek().Item1 >= indent)
                        stack.Pop();
                    if (stack.Any())
                        stack.Peek().Item2.Add(segment);
                }

                if (stack.Any() == false)
                    segments.Add(segment);
                stack.Push(Tuple.Create(indent, segment));
            }

            return segments;
        }
Example #2
0
 public void Add(Segment s)
 {
     _segments.Add(s);
 }
Example #3
0
        private static IEnumerable<PlanPart> Parse(Segment segment, PlanPart parent)
        {
            var parts = new List<PlanPart>();

            if (parent == default(PlanPart))
            {
                parts.AddRange(RootPlanPart.Parse(segment.Text));
            }
            else
            {
                var text = segment.Text.Substring(1);
                var prefix = segment.Text[0];

                switch (prefix)
                {
                    case '@':
                        parts.AddRange(LookupPart.Parse(text));
                        break;
                    case '<':
                        parts.AddRange(HasManyPart.Parse(text));
                        break;
                    case '>':
                        parts.AddRange(HasOnePart.Parse(text));
                        break;
                    default:
                        Console.Error.WriteLine("Invalid: " + segment.Text);
                        break;
                }
            }

            foreach (var part in parts)
            {
                part.Children = segment.SubSegments.SelectMany(x => Parse(x, part)).ToArray();
            }

            return parts;
        }