Пример #1
0
        private void Reduce(Token t, Text Text, Dictionary <Object, TextRange> TokenPositions, Dictionary <Object, TextRange> Positions)
        {
            var InnerNodes = Nodes.AsEnumerable().Reverse().TakeWhile(n => !(n.OnToken && (n.Token.Type.OnLeftParenthesis || n.Token.Type.OnComma))).Reverse().ToList();
            var RangeStart = LineRangeStart;

            if (Nodes.Count - InnerNodes.Count - 1 >= 0)
            {
                var LeftParenthesisOrComma = Nodes[Nodes.Count - InnerNodes.Count - 1].Token;
                if (LeftParenthesisOrComma.Type.OnComma && (InnerNodes.Count == 0))
                {
                    throw new InvalidSyntaxException("InvalidParenthesis", new FileTextRange {
                        Text = Text, Range = TokenPositions.ContainsKey(t) ? TokenPositions[t] : Firefly.Texting.TreeFormat.Optional <TextRange> .Empty
                    });
                }
                if (TokenPositions.ContainsKey(LeftParenthesisOrComma))
                {
                    RangeStart = TokenPositions[LeftParenthesisOrComma];
                }
            }

            foreach (var n in InnerNodes)
            {
                if (!n.OnNode)
                {
                    throw new InvalidSyntaxException("InvalidSyntaxRule", new FileTextRange {
                        Text = Text, Range = TokenPositions.ContainsKey(n.Token) ? TokenPositions[n.Token] : Firefly.Texting.TreeFormat.Optional <TextRange> .Empty
                    });
                }
            }
            if (InnerNodes.Count > 1)
            {
                var RangeEnd = Optional <TextRange> .Empty;
                if (TokenPositions.ContainsKey(t))
                {
                    RangeEnd = TokenPositions[t];
                }

                var Children     = InnerNodes.Select(Part => Part.Node).ToList();
                var Undetermined = new ExprNodeUndetermined {
                    Nodes = Children
                };
                var Node = ExprNode.CreateUndetermined(Undetermined);
                if (RangeStart.OnSome && RangeEnd.OnSome)
                {
                    Positions.Add(Undetermined, new TextRange {
                        Start = RangeStart.Value.Start, End = RangeEnd.Value.End
                    });
                    Positions.Add(Node, new TextRange {
                        Start = RangeStart.Value.Start, End = RangeEnd.Value.End
                    });
                }
                Nodes.RemoveRange(Nodes.Count - InnerNodes.Count, InnerNodes.Count);
                Nodes.Add(StackNode.CreateNode(Node));
            }
        }
Пример #2
0
 /// <summary>待定序列</summary>
 public static ExprNode CreateUndetermined(ExprNodeUndetermined Value)
 {
     return(new ExprNode {
         _Tag = ExprNodeTag.Undetermined, Undetermined = Value
     });
 }