Exemplo n.º 1
0
 protected override ParseResult ImplementParse(StringIterator InputStream)
 {
     CallOnParse(InputStream);
     return(new ParseResult {
         ResultType = ResultType.Success, After = InputStream
     });
 }
Exemplo n.º 2
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var location = InputStream;

            if (InputStream.AtEnd || !IsLegalStartCharacter(InputStream.Next))
            {
                return(Fail("Unexpected end of stream"));
            }

            var text = "";

            while (!InputStream.AtEnd && IsLegalCharacter(InputStream.Next))
            {
                text       += InputStream.Next;
                InputStream = InputStream.Advance();
            }

            return(new ParseResult
            {
                ResultType = Ancora.ResultType.Success,
                Node = new AstNode {
                    NodeType = AstNodeType, Value = text, Location = location
                },
                After = InputStream
            });
        }
Exemplo n.º 3
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var r = new AstNode {
                NodeType = AstNodeType, Location = InputStream
            };

            foreach (var sub in SubParsers)
            {
                var subResult = sub.Parse(InputStream);

                if (subResult.ResultType == ResultType.HardError)
                {
                    return(Error("Child produced hard error", subResult.FailReason));
                }
                else if (subResult.ResultType == ResultType.Failure)
                {
                    return(Fail("Child failed", subResult.FailReason));
                }

                InputStream = subResult.After;

                if (subResult.Node != null)
                {
                    r.Children.Add(subResult.Node);
                }
            }

            return(new ParseResult
            {
                ResultType = ResultType.Success,
                After = InputStream,
                Node = r
            });
        }
Exemplo n.º 4
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var r = new AstNode { NodeType = String.IsNullOrEmpty(AstNodeType) ? "UNNAMED" : AstNodeType };
            AstNode passThroughChild = null;
            foreach (var sub in SubParsers)
            {
                var subResult = sub.Parse(InputStream);
                if (subResult.ResultType != ResultType.Success) return subResult;
                InputStream = subResult.StreamState;

                if (subResult.Node != null)
                {
                    if (subResult.CheckFlag(ParserFlags.PASSTHROUGH) && passThroughChild == null)
                        passThroughChild = subResult.Node;
                    else if (subResult.CheckFlag(ParserFlags.FLATTEN))
                        r.Children.AddRange(subResult.Node.Children);
                    else if (subResult.CheckFlag(ParserFlags.VALUE))
                        r.Value = subResult.Node.Value;
                    else
                        r.Children.Add(subResult.Node);
                }
            }

            if (passThroughChild != null && (Flags & ParserFlags.CREATE_AST) == ParserFlags.CREATE_AST)
                passThroughChild.NodeType = AstNodeType;

            return new ParseResult
            {
                ResultType = ResultType.Success,
                StreamState = InputStream,
                Node = passThroughChild == null ? r : passThroughChild,
                Flags = Flags
            };
        }
Exemplo n.º 5
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var subResult = SubParser.Parse(InputStream);

            if (subResult.ResultType == ResultType.Success)
            {
                return(WrapChild(subResult));
            }
            else if (subResult.ResultType == ResultType.HardError)
            {
                return(Error("Maybe child produced hard error", subResult.FailReason));
            }
            else
            {
                return new ParseResult
                       {
                           ResultType = ResultType.Success,
                           Node       = new AstNode {
                               NodeType = AstNodeType, Location = InputStream
                           },
                           After = InputStream
                       }
            };
        }
    }
Exemplo n.º 6
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            if (InputStream.AtEnd || !IsLegalStartCharacter(InputStream.Next))
            {
                return(Fail("Unexpected end of stream"));
            }

            var text = "";

            while (!InputStream.AtEnd && IsLegalCharacter(InputStream.Next))
            {
                text       += InputStream.Next;
                InputStream = InputStream.Advance();
            }

            return(new ParseResult
            {
                ResultType = Ancora.ResultType.Success,
                Node = new AstNode
                {
                    NodeType = String.IsNullOrEmpty(AstNodeType) ? "UNNAMED" : AstNodeType,
                    Value = text
                },
                StreamState = InputStream,
                Flags = Flags
            });
        }
Exemplo n.º 7
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var r = new AstNode {
                NodeType = AstNodeType, Location = InputStream
            };

            while (true)
            {
                var subResult = SubParser.Parse(InputStream);
                if (subResult.ResultType == ResultType.HardError)
                {
                    return(Error("Child produced hard error", subResult.FailReason));
                }
                else if (subResult.ResultType == ResultType.Success)
                {
                    InputStream = subResult.After;
                    if (subResult.Node != null)
                    {
                        r.Children.Add(subResult.Node);
                    }
                }
                else
                {
                    return new ParseResult
                           {
                               ResultType = ResultType.Success, //Acceptable to match none.
                               Node       = r,
                               After      = InputStream
                           }
                };
            }
        }
    }
Exemplo n.º 8
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            CompoundFailure failureReason = null;

            foreach (var sub in SubParsers)
            {
                var subResult = sub.Parse(InputStream);
                if (subResult.ResultType == ResultType.HardError)
                {
                    return(Error("Hard failure in child parser", subResult.FailReason));
                }
                else if (subResult.ResultType == ResultType.Success)
                {
                    return(WrapChild(subResult));
                }
                else if (subResult.FailReason != null)
                {
                    if (failureReason == null)
                    {
                        failureReason = new CompoundFailure();
                    }
                    failureReason.CompoundedFailures.Add(subResult.FailReason);
                }
            }

            return(Fail("No alternatives matched", failureReason));
        }
Exemplo n.º 9
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            if (SubParser == null)
            {
                return(Fail("LateBound was never bound"));
            }
            var result = SubParser.Parse(InputStream);

            return(result);
        }
Exemplo n.º 10
0
        public void Test3()
        {
            StringIterator stringIterator = new StringIterator("L2e2t2C2o2d2e23");

            Assert.AreEqual('L', stringIterator.Next());
            Assert.AreEqual('L', stringIterator.Next());
            Assert.AreEqual('e', stringIterator.Next());
            Assert.AreEqual('e', stringIterator.Next());
            Assert.AreEqual('t', stringIterator.Next());
            Assert.AreEqual('t', stringIterator.Next());
            Assert.IsTrue(stringIterator.HasNext());
            Assert.AreEqual('C', stringIterator.Next());
            Assert.IsTrue(stringIterator.HasNext());
        }
Exemplo n.º 11
0
        public void Test4()
        {
            StringIterator stringIterator = new StringIterator("x6");

            Assert.AreEqual('x', stringIterator.Next());
            Assert.AreEqual('x', stringIterator.Next());
            Assert.AreEqual('x', stringIterator.Next());
            Assert.AreEqual('x', stringIterator.Next());
            Assert.AreEqual('x', stringIterator.Next());
            Assert.AreEqual('x', stringIterator.Next());
            Assert.IsFalse(stringIterator.HasNext());
            Assert.AreEqual(' ', stringIterator.Next());
            Assert.IsFalse(stringIterator.HasNext());
        }
Exemplo n.º 12
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var r = new AstNode {
                NodeType = String.IsNullOrEmpty(AstNodeType) ? "UNNAMED" : AstNodeType
            };
            AstNode passThroughChild = null;

            while (true)
            {
                var subResult = SubParser.Parse(InputStream);
                if (subResult.ResultType == ResultType.Success)
                {
                    InputStream = subResult.StreamState;
                    if (subResult.Node != null)
                    {
                        if (subResult.CheckFlag(ParserFlags.PASSTHROUGH) && passThroughChild == null)
                        {
                            passThroughChild = subResult.Node;
                        }
                        else if (subResult.CheckFlag(ParserFlags.FLATTEN))
                        {
                            r.Children.AddRange(subResult.Node.Children);
                        }
                        else if (subResult.CheckFlag(ParserFlags.VALUE))
                        {
                            r.Value = subResult.Node.Value;
                        }
                        else
                        {
                            r.Children.Add(subResult.Node);
                        }
                    }
                }
                else if (subResult.ResultType == ResultType.HardError)
                {
                    return(subResult);
                }
                else
                {
                    return new ParseResult
                           {
                               ResultType  = r.Children.Count > 0 ? ResultType.Success : ResultType.Failure, //Must match at least 1
                               Node        = passThroughChild == null ? r : passThroughChild,
                               StreamState = InputStream,
                               Flags       = Flags
                           }
                };
            }
        }
    }
Exemplo n.º 13
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var subResult = SubParser.Parse(InputStream);

            if (subResult.ResultType == ResultType.Success)
            {
                return(subResult.ApplyFlags(Flags));
            }
            else
            {
                subResult.ResultType = ResultType.HardError;
                return(subResult);
            }
        }
Exemplo n.º 14
0
        public void Test1()
        {
            StringIterator stringIterator = new StringIterator("L1e2t1C1o1d1e1");

            Assert.AreEqual('L', stringIterator.Next());
            Assert.AreEqual('e', stringIterator.Next());
            Assert.AreEqual('e', stringIterator.Next());
            Assert.AreEqual('t', stringIterator.Next());
            Assert.AreEqual('C', stringIterator.Next());
            Assert.AreEqual('o', stringIterator.Next());
            Assert.IsTrue(stringIterator.HasNext());
            Assert.AreEqual('d', stringIterator.Next());
            Assert.IsTrue(stringIterator.HasNext());
        }
Exemplo n.º 15
0
 protected override ParseResult ImplementParse(StringIterator InputStream)
 {
     if (InputStream.AtEnd || InputStream.Next != Char)
     {
         return(Fail("Expected " + Char));
     }
     return(new ParseResult
     {
         ResultType = ResultType.Success,
         Node = new AstNode {
             NodeType = AstNodeType, Value = Char, Location = InputStream
         },
         After = InputStream.Advance()
     });
 }
Exemplo n.º 16
0
        public bool BackspaceCompare(string S, string T)
        {
            var sIterator = new StringIterator(S);
            var tIterator = new StringIterator(T);

            while (!sIterator.IsStringFinished() || !tIterator.IsStringFinished())
            {
                if (sIterator.Next() != tIterator.Next())
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 17
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var firstLhs = TermParser.Parse(InputStream);

            if (firstLhs.ResultType == ResultType.HardError)
            {
                return(firstLhs);
            }
            if (firstLhs.ResultType != ResultType.Success || firstLhs.Node == null)
            {
                return(Fail("LHS parse failed, or did not produce an AST node."));
            }

            return(ParseExpression(firstLhs.Node, firstLhs.StreamState, 0));
        }
Exemplo n.º 18
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            if (!InputStream.AtEnd)
            {
                Fail("Did not consume all input");
            }

            return(new ParseResult
            {
                ResultType = ResultType.Success,
                After = InputStream,
                Node = new AstNode {
                    NodeType = AstNodeType, Location = InputStream
                }
            });
        }
Exemplo n.º 19
0
 protected override ParseResult ImplementParse(StringIterator InputStream)
 {
     if (InputStream.AtEnd || InputStream.Next != Char)
     {
         return(Fail("Expected " + Char));
     }
     return(new ParseResult
     {
         ResultType = ResultType.Success,
         Node = ((Flags & ParserFlags.CREATE_AST) == ParserFlags.CREATE_AST) ? new AstNode {
             NodeType = AstNodeType, Value = Char
         } : null,
         StreamState = InputStream.Advance(),
         Flags = Flags
     });
 }
Exemplo n.º 20
0
        private static AssemblyName FindAssemblyIfNamePresent(string name)
        {
            AssemblyName result    = null;
            var          endOfType = name.Begin() + ReadTypeArgument(name.Begin(), name.End(), false);

            if (endOfType < name.End() && endOfType.Current == ',')
            {
                // There is an assembly name here
                int foundCommas       = 0;
                var endOfAssemblyName = endOfType;
                for (var ch = endOfType + 1; ch < name.End(); ch++)
                {
                    if (foundCommas == 3)
                    {
                        // We're now eating the public key token, looking for the end of the name,
                        // or a right bracket
                        if (ch.Current == ']' || ch.Current == ',')
                        {
                            endOfAssemblyName = ch - 1;
                            break;
                        }
                    }

                    if (ch.Current == ',')
                    {
                        foundCommas++;
                    }
                }
                if (endOfAssemblyName == endOfType)
                {
                    endOfAssemblyName = name.End();
                }

                // eat the comma
                endOfType++;
                for (; endOfType < endOfAssemblyName; ++endOfType)
                {
                    // trim off spaces
                    if (endOfType.Current != ' ')
                    {
                        break;
                    }
                }
                result = new AssemblyName(StringIterator.Substring(endOfType, endOfAssemblyName));
            }
            return(result);
        }
Exemplo n.º 21
0
        private static int ReadTypeArgument(StringIterator strBegin, StringIterator strEnd, bool ignoreComma)
        {
            int level  = 0;
            int length = 0;

            for (var c = strBegin; c < strEnd; c++)
            {
                if (c.Current == '\\')
                {
                    length++;
                    if ((c + 1) < strEnd)
                    {
                        c++;
                        length++;
                    }
                    continue;
                }
                if (c.Current == '[')
                {
                    level++;
                }
                else if (c.Current == ']')
                {
                    if (level == 0)
                    {
                        break;
                    }
                    level--;
                }
                else if (!ignoreComma && (c.Current == ','))
                {
                    if (level == 0)
                    {
                        break;
                    }
                }

                length++;
            }

            return(length);
        }
Exemplo n.º 22
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var text     = "";
            var location = InputStream;

            while (!InputStream.AtEnd && IsLegalCharacter(InputStream.Next))
            {
                text       += InputStream.Next;
                InputStream = InputStream.Advance();
            }

            return(new ParseResult
            {
                ResultType = String.IsNullOrEmpty(text) ? ResultType.Failure : ResultType.Success,
                Node = new AstNode {
                    NodeType = AstNodeType, Value = text, Location = location
                },
                After = InputStream
            });
        }
Exemplo n.º 23
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var text     = "";
            var location = InputStream;

            if (InputStream.AtEnd || InputStream.Next != Quote)
            {
                return(Fail("Not a string"));
            }

            InputStream = InputStream.Advance();

            while (!InputStream.AtEnd && InputStream.Next != Quote)
            {
                if (InputStream.Next == '\\')
                {
                    text       += InputStream.Peek(2);
                    InputStream = InputStream.Advance(2);
                    continue;
                }

                text       += InputStream.Next;
                InputStream = InputStream.Advance();
            }

            if (InputStream.AtEnd || InputStream.Next != Quote)
            {
                return(Fail("Unexpected end of string literal"));
            }

            InputStream = InputStream.Advance();

            return(new ParseResult
            {
                ResultType = ResultType.Success,
                Node = new AstNode {
                    NodeType = AstNodeType, Value = text, Location = location
                },
                After = InputStream
            });
        }
Exemplo n.º 24
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            if (InputStream.AtEnd)
            {
                return(Fail("Unexpected end of stream"));
            }

            var opSoFar = "";

            while (true)
            {
                opSoFar    += InputStream.Next;
                InputStream = InputStream.Advance();
                var possibleMatches = OperatorTable.PossibleMatches(opSoFar);
                if (possibleMatches == 0)
                {
                    return(Fail("Unable to match operator"));
                }
                else if (possibleMatches == 1 && OperatorTable.ExactMatches(opSoFar) == 1)
                {
                    return new ParseResult
                           {
                               ResultType = ResultType.Success,
                               Node       = (Flags & ParserFlags.CREATE_AST) == ParserFlags.CREATE_AST ? new AstNode
                               {
                                   NodeType = AstNodeType,
                                   Value    = opSoFar
                               } : null,
                               StreamState = InputStream,
                               Flags       = Flags
                           }
                }
                ;

                if (InputStream.AtEnd)
                {
                    return(Fail("Unexpected end of stream"));
                }
            }
        }
    }
Exemplo n.º 25
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var text = InputStream.Peek(Word.Length);

            if (text == Word)
            {
                return new ParseResult
                       {
                           ResultType = Ancora.ResultType.Success,
                           Node       = new AstNode {
                               NodeType = AstNodeType, Value = Word, Location = InputStream
                           },
                           After = InputStream.Advance(Word.Length)
                       }
            }
            ;
            else
            {
                return(Fail("KeyWord not found"));
            }
        }
Exemplo n.º 26
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            if (InputStream.AtEnd)
            {
                return(Fail("Unexpected end of stream"));
            }
            var location = InputStream;

            var opSoFar = "";

            while (true)
            {
                opSoFar    += InputStream.Next;
                InputStream = InputStream.Advance();
                var possibleMatches = OperatorTable.PossibleMatches(opSoFar);
                if (possibleMatches == 0)
                {
                    return(Fail("Unable to match operator"));
                }
                else if (possibleMatches == 1 && OperatorTable.ExactMatches(opSoFar) == 1)
                {
                    return new ParseResult
                           {
                               ResultType = ResultType.Success,
                               Node       = new AstNode {
                                   NodeType = AstNodeType, Value = opSoFar, Location = InputStream
                               },
                               After = InputStream
                           }
                }
                ;

                if (InputStream.AtEnd)
                {
                    return(Fail("Unexpected end of stream"));
                }
            }
        }
    }
Exemplo n.º 27
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var text = "";

            while (!InputStream.AtEnd && IsLegalCharacter(InputStream.Next))
            {
                text       += InputStream.Next;
                InputStream = InputStream.Advance();
            }

            return(new ParseResult
            {
                ResultType = String.IsNullOrEmpty(text) ? ResultType.Failure : ResultType.Success,
                Node = new AstNode
                {
                    NodeType = String.IsNullOrEmpty(AstNodeType) ? "UNNAMED" : AstNodeType,
                    Value = text
                },
                StreamState = InputStream,
                Flags = Flags
            });
        }
Exemplo n.º 28
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var subResult = SubParser.Parse(InputStream);

            if (subResult.ResultType == ResultType.Success)
            {
                return(subResult.ApplyFlags(Flags));
            }
            else if (subResult.ResultType == ResultType.HardError)
            {
                return(subResult);
            }
            else
            {
                return new ParseResult
                       {
                           ResultType  = ResultType.Success,
                           Node        = null,
                           StreamState = InputStream,
                           Flags       = Flags
                       }
            };
        }
    }
Exemplo n.º 29
0
        protected override ParseResult ImplementParse(StringIterator InputStream)
        {
            var text = InputStream.Peek(Word.Length);

            if (text == Word)
            {
                return new ParseResult
                       {
                           ResultType = Ancora.ResultType.Success,
                           Node       = ((Flags & ParserFlags.CREATE_AST) == ParserFlags.CREATE_AST) ? new AstNode
                           {
                               NodeType = AstNodeType,
                               Value    = Word
                           } : null,
                           StreamState = InputStream.Advance(Word.Length),
                           Flags       = Flags
                       }
            }
            ;
            else
            {
                return(Fail("KeyWord not found"));
            }
        }
Exemplo n.º 30
0
 public static string Substring(StringIterator it1, StringIterator it2)
 {
     Debug.Assert(Object.ReferenceEquals(it1._string, it2._string));
     return(it1._string.Substring(it1._index, it2._index - it1._index));
 }
        private static int ReadTypeArgument(StringIterator strBegin, StringIterator strEnd, bool ignoreComma)
        {
            int level = 0;
            int length = 0;
            for (var c = strBegin; c < strEnd; c++)
            {
                if (c.Current == '\\')
                {
                    length++;
                    if ((c + 1) < strEnd)
                    {
                        c++;
                        length++;
                    }
                    continue;
                }
                if (c.Current == '[')
                {
                    level++;
                }
                else if (c.Current == ']')
                {
                    if (level == 0)
                        break;
                    level--;
                }
                else if (!ignoreComma && (c.Current == ','))
                {
                    if (level == 0)
                        break;
                }

                length++;
            }

            return length;
        }
 public static string Substring(StringIterator it1, StringIterator it2)
 {
     Debug.Assert(Object.ReferenceEquals(it1._string, it2._string));
     return it1._string.Substring(it1._index, it2._index - it1._index);
 }