Esempio 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>());
        }
Esempio 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());
        }
Esempio n. 3
0
 public static ManyNodeParser MakeMany(NodeParser parser) => new ManyNodeParser(parser);
Esempio n. 4
0
 public ManyNodeParser(NodeParser parser, bool allowEmpty = true)
 {
     this.Parser     = parser;
     this.allowEmpty = allowEmpty;
 }
 public static ForeverNodeParser Forever(NodeParser parser) => new ForeverNodeParser(parser);
 public ForeverNodeParser(NodeParser parser)
 {
     this.Parser = parser;
 }
Esempio 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);
                }
            }
        }
Esempio n. 8
0
 public MaybeNodeParser(NodeParser parser)
 {
     this.Parser = parser;
 }
Esempio n. 9
0
 public static MaybeNodeParser Maybe(NodeParser parser) => new MaybeNodeParser(parser);
Esempio n. 10
0
 public static WhileNodeParser While(NodeParser parser) => new WhileNodeParser(parser);
Esempio n. 11
0
 public WhileNodeParser(NodeParser parser)
 {
     this.Parser = parser;
 }
Esempio n. 12
0
 public FailedParsingResult(SourceLocation location, NodeParser parser) : base(parser, success: true)
 {
     this.location = location;
 }
Esempio n. 13
0
 public SuccessfulParsingResult(SourceContext context, NodeParser parser) : base(parser, success: true)
 {
     this.Context = context;
 }
Esempio n. 14
0
 public ParsingResult(NodeParser parser, bool success)
 {
     this.Parser  = parser;
     this.Success = success;
 }