Exemplo n.º 1
0
        public override void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory)
        {
            TokenWalker LocalWalker = new TokenWalker(walker);

            TokenWalker.StateWalker localWalk = LocalWalker.State;
            Debug.WriteLine(Parsers.Node.Program.ToStringRecursively(highlighted: this, enableHighlighting: false));

            while (!LocalWalker.IsLast())
            {
                try
                {
                    this.Parser.ParseNode(ref node, out localWalk, LocalWalker, resultHistory);
                }
                catch (Failure)
                {
                    resultHistory.AddResult(new FailedParsingResult(walker.Location, this));
                    throw;
                }
                catch (Success)
                {
                    localWalk(LocalWalker);
                    LocalWalker.SkipWhitespace();
                    continue;
                }
            }
            walk = LocalWalker.State;
            resultHistory.AddResult(new SuccessfulParsingResult(walker.To(localWalk), this));
            throw Succeded;
        }
Exemplo n.º 2
0
        public override void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory)
        {
            TokenWalker LocalWalker = new TokenWalker(walker);

            TokenWalker.StateWalker localWalk = LocalWalker.State;

            foreach (NodeParser parser in this.Parsers)
            {
                try
                {
                    parser.ParseNode(ref node, out localWalk, LocalWalker, resultHistory);
                }
                catch (Failure)
                {
                    continue;
                }
                catch (Success)
                {
                    localWalk(LocalWalker);
                    walk = localWalk;
                    resultHistory.AddResult(new SuccessfulParsingResult(walker.To(localWalk), this));
                    throw;
                }
            }
            resultHistory.AddResult(new FailedParsingResult(walker.Location, this));
            throw Failed;
        }
Exemplo n.º 3
0
        public SourceContext To(StateWalker otherWalker)
        {
            TokenWalker other = new TokenWalker(this);

            otherWalker(other);
            return(this.To(other));
        }
Exemplo n.º 4
0
        public override void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory)
        {
            TokenWalker LocalWalker = new TokenWalker(walker);

            TokenWalker.StateWalker localWalk = LocalWalker.State;

            while (true)
            {
                try
                {
                    this.Parser.ParseNode(ref node, out localWalk, LocalWalker, resultHistory);
                }
                catch (Failure)
                {
                    resultHistory.AddResult(new SuccessfulParsingResult(walker.To(localWalk), this));
                    throw Succeded;
                }
                catch (Success)
                {
                    localWalk(LocalWalker);
                    walk = localWalk;
                    continue;
                }
                throw new ParserException("Parser didnt specifiy if it succeded or not");
            }
        }
Exemplo n.º 5
0
        public override void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory)
        {
            TokenWalker LocalWalker = new TokenWalker(walker);

            walk = LocalWalker.State;

            this.parserReference().ParseNode(ref node, out walk, LocalWalker, resultHistory);
        }
Exemplo n.º 6
0
        public override void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory)
        {
            TokenWalker LocalWalker = new TokenWalker(walker);

            TokenWalker.StateWalker localWalk = LocalWalker.State;

            List <Node> parsedNodes = new List <Node>();

            while (true)
            {
                Node parsedNode = null;
                try
                {
                    this.Parser.ParseNode(ref parsedNode, out localWalk, LocalWalker, resultHistory);
                }
                catch (Failure)
                {
                    break;
                }
                catch (Success)
                {
                    localWalk(LocalWalker);

                    if (parsedNode != null)
                    {
                        parsedNodes.Add(parsedNode);
                    }
                    else
                    {
                        resultHistory.AddResult(new FailedParsingResult(walker.Location, this));
                        throw Failed;
                    }
                }
            }

            if (!this.allowEmpty && parsedNodes.Count == 0)
            {
                resultHistory.AddResult(new FailedParsingResult(walker.Location, this));
                throw Failed;
            }

            node = new ManyNode(parsedNodes, walker.To(LocalWalker));
            walk = localWalk;
            resultHistory.AddResult(new SuccessfulParsingResult(walker.To(localWalk), this));
            throw Succeded;
        }
Exemplo n.º 7
0
        public override void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory)
        {
            TokenWalker LocalWalker = new TokenWalker(walker);

            TokenWalker.StateWalker localWalk = LocalWalker.State;

            List <Node> parsedNodes = new List <Node>();

            foreach (NodeParser parser in this.Parsers)
            {
                Node parsedNode = null;
                try
                {
                    parser.ParseNode(ref parsedNode, out localWalk, LocalWalker, resultHistory);
                }
                catch (Failure)
                {
                    continue;
                }
                catch (Success)
                {
                    localWalk(LocalWalker);
                    if (parsedNode != null)
                    {
                        parsedNodes.Add(parsedNode);
                    }
                    else
                    {
                        throw new Exception("Parser succeeded but no node was parsed");
                    }
                }
            }

            if (parsedNodes.Count == 1)
            {
                var parsedNode = parsedNodes[0];
                node = new UnaryNode(this.type, parsedNode, node, node.Context.To(parsedNode.Context));
                walk = localWalk;
                resultHistory.AddResult(new SuccessfulParsingResult(walker.To(localWalk), this));
                throw Succeded;
            }

            resultHistory.AddResult(new FailedParsingResult(walker.Location, this));
            throw Failed;
        }
Exemplo n.º 8
0
        public override void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory)
        {
            TokenWalker LocalWalker = new TokenWalker(walker);

            TokenWalker.StateWalker localWalk = LocalWalker.State;

            List <Node> parsedNodes = new List <Node>();

            foreach (NodeParser parser in this.Parsers)
            {
                Node parsedNode = null;
                try
                {
                    parser.ParseNode(ref parsedNode, out localWalk, LocalWalker, resultHistory);
                }
                catch (Failure)
                {
                    continue;
                }
                catch (Success)
                {
                    localWalk(LocalWalker);
                    walk = localWalk;
                    if (parsedNode != null)
                    {
                        parsedNodes.Add(parsedNode);
                    }
                }
            }

            if (parsedNodes.Count == 2)
            {
                Node          left              = node;
                Node          binaryModifier    = parsedNodes[0];
                Node          right             = parsedNodes[1];
                SourceContext binaryNodeContext = left.Context.To(right);

                node = new BinaryNode(this.type, left, binaryModifier, right, binaryNodeContext);
                resultHistory.AddResult(new SuccessfulParsingResult(binaryNodeContext, this));
                throw Succeded;
            }
            resultHistory.AddResult(new FailedParsingResult(walker.Location, this));
            throw Failed;
        }
Exemplo n.º 9
0
        public override void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory)
        {
            TokenWalker LocalWalker = new TokenWalker(walker);

            TokenWalker.StateWalker localWalk = LocalWalker.State;

            if (LocalWalker.TryGetNext(out Token token, this.Keywords))
            {
                var walked = walker.To(LocalWalker);
                localWalk(LocalWalker);
                node = new TokenNode(token, walked);
                walk = localWalk;
                resultHistory.AddResult(new SuccessfulParsingResult(walker.To(localWalk), this));
                throw Succeded;
            }

            resultHistory.AddResult(new FailedParsingResult(walker.Location, this));
            throw Failed;
        }
Exemplo n.º 10
0
        public void Parse(ref Node node, TokenWalker walker, out ParseResultHistory resultHistory)
        {
            StateWalker walk = null;

            resultHistory = new ParseResultHistory();
            try
            {
                this.ParseNode(ref node, out walk, walker, resultHistory);
            }
            catch (Success)
            {
                walk(walker);
                if (node is null)
                {
                    throw new FailedParsingNodeException("Success, but result node is empty");
                }
            }
            catch (Failure)
            {
                throw new FailedParsingNodeException($"Failed parsing node");
            }
        }
Exemplo n.º 11
0
        public override void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory)
        {
            TokenWalker LocalWalker = new TokenWalker(walker);

            TokenWalker.StateWalker localWalk = LocalWalker.State;

            List <Node> parsedNodes = new List <Node>();

            foreach (NodeParser parser in this.Parsers)
            {
                Node parsedNode = null;

                try
                {
                    parser.ParseNode(ref parsedNode, out localWalk, LocalWalker, resultHistory);
                }
                catch (Failure)
                {
                    resultHistory.AddResult(new FailedParsingResult(walker.Location, this));
                    throw;
                }
                catch (Success)
                {
                    if (parsedNode != null)
                    {
                        parsedNodes.Add(parsedNode);
                    }
                    localWalk(LocalWalker);
                    continue;
                }
            }

            walk = localWalk;
            var context = walker.To(walk);

            node = new ManyNode(parsedNodes, context);
            resultHistory.AddResult(new SuccessfulParsingResult(context, this));
            throw Succeded;
        }
Exemplo n.º 12
0
        public override void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory)
        {
            TokenWalker LocalWalker = new TokenWalker(walker);

            TokenWalker.StateWalker localWalk = LocalWalker.State;

            try
            {
                this.Parser.ParseNode(ref node, out localWalk, LocalWalker, resultHistory);
            }
            catch (Failure)
            {
            }
            catch (Success)
            {
                localWalk(LocalWalker);
                walk = localWalk;
                resultHistory.AddResult(new SuccessfulParsingResult(walker.To(localWalk), this));
            }

            throw Succeded;
        }
Exemplo n.º 13
0
 public SourceContext To(TokenWalker other) => this.Location.To(other.Location);
Exemplo n.º 14
0
 public TokenWalker(TokenWalker walker)
 {
     this.streamReference = walker.streamReference;
     this.CurrentElement  = walker.CurrentElement;
 }
Exemplo n.º 15
0
 public abstract void ParseNode(ref Node node, out TokenWalker.StateWalker walk, TokenWalker walker, ParseResultHistory resultHistory);
Exemplo n.º 16
0
 public static void Parse(ref Parsing.Node node, TokenWalker walker, out ParseResultHistory resultHistory) => Program.Parse(ref node, walker, out resultHistory);