Exemplo n.º 1
0
        public List <NodeParser> PathTo(NodeParser lookingFor)
        {
            if (this == lookingFor)
            {
                return new List <NodeParser>()
                       {
                           this
                       }
            }
            ;

            if (this is IContainsChildren containsChildren)
            {
                foreach (NodeParser child in containsChildren.Children)
                {
                    List <NodeParser> path = child.PathTo(lookingFor);
                    if (path.Count() > 0)
                    {
                        path.Insert(0, this);
                        return(path);
                    }
                }
            }
            return(new List <NodeParser>());
        }
Exemplo n.º 2
0
        public string ToStringRecursively(NodeParser highlighted = null, bool enableHighlighting = true)
        {
            List <NodeParser> referenced = new List <NodeParser>();
            StringBuilder     builder    = new StringBuilder();

            this.ToStringRecursively(builder, indent: "", isLast: true, referenced, highlighted, enableHighlighting);
            return(builder.ToString());
        }
Exemplo n.º 3
0
 public static ManyNodeParser MakeMany(NodeParser parser) => new ManyNodeParser(parser);
Exemplo n.º 4
0
 public ManyNodeParser(NodeParser parser, bool allowEmpty = true)
 {
     this.Parser     = parser;
     this.allowEmpty = allowEmpty;
 }
Exemplo n.º 5
0
 public static ForeverNodeParser Forever(NodeParser parser) => new ForeverNodeParser(parser);
Exemplo n.º 6
0
 public ForeverNodeParser(NodeParser parser)
 {
     this.Parser = parser;
 }
Exemplo n.º 7
0
        private void ToStringRecursively(StringBuilder builder, string indent, bool isLast, List <NodeParser> referenced, NodeParser highlighted, bool enableHighlighting)
        {
            if (this is ReferenceNodeParser referenceNodeParser)
            {
                NodeParser referencedParser = referenceNodeParser.Referenced;
                referencedParser.ToStringRecursively(builder, indent, isLast, referenced, highlighted, enableHighlighting);
                return;
            }

            builder.Append(indent);
            if (isLast)
            {
                builder.Append(Corner);
                indent += Space;
            }
            else
            {
                builder.Append(Cross);
                indent += Vertical;
            }

            if (referenced.Contains(this))
            {
                string referencedLine = enableHighlighting ? $"{Output.BrightMagenta("referenced")}({this.formattedName(enableHighlighting)})" : $"referenced({this.formattedName(enableHighlighting)})";
                if (highlighted == this)
                {
                    builder.AppendLine(enableHighlighting ? referencedLine.Reversed() : referencedLine);
                }
                else
                {
                    builder.AppendLine(referencedLine);
                }

                return;
            }
            referenced.Add(this);

            string prefix = this.formattedName(enableHighlighting);

            if (this.name != null)
            {
                prefix += enableHighlighting ? Output.BrightGreen(":") : ":";
            }

            string fullLine = $"{prefix}{this.ToString()}";

            if (highlighted == this)
            {
                builder.AppendLine(enableHighlighting ? $"{fullLine.Reversed()}{Output.Red(" <------ current parser")}" : $"{fullLine} <------ current parser");
            }
            else
            {
                builder.AppendLine(fullLine);
            }

            if (this is IContainsChildren recursiveNode)
            {
                IEnumerable <NodeParser> children = recursiveNode.Children.Except(referenced);
                int childrenCount = children.Count();

                IEnumerable <(NodeParser Child, bool)> joinedWithLast = children.Select((Child, Index) => (Child, Index == childrenCount - 1));
                foreach ((NodeParser child, bool childIsLast) in joinedWithLast)
                {
                    child.ToStringRecursively(builder, indent, childIsLast, referenced, highlighted, enableHighlighting);
                }
            }
        }
Exemplo n.º 8
0
 public MaybeNodeParser(NodeParser parser)
 {
     this.Parser = parser;
 }
Exemplo n.º 9
0
 public static MaybeNodeParser Maybe(NodeParser parser) => new MaybeNodeParser(parser);
Exemplo n.º 10
0
 public static WhileNodeParser While(NodeParser parser) => new WhileNodeParser(parser);
Exemplo n.º 11
0
 public WhileNodeParser(NodeParser parser)
 {
     this.Parser = parser;
 }
Exemplo n.º 12
0
 public FailedParsingResult(SourceLocation location, NodeParser parser) : base(parser, success: true)
 {
     this.location = location;
 }
Exemplo n.º 13
0
 public SuccessfulParsingResult(SourceContext context, NodeParser parser) : base(parser, success: true)
 {
     this.Context = context;
 }
Exemplo n.º 14
0
 public ParsingResult(NodeParser parser, bool success)
 {
     this.Parser  = parser;
     this.Success = success;
 }