Beispiel #1
0
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            c.IP   = TextBox_IP.Text;
            c.Port = NumericStrings.InvariantParseInt32(TextBox_Port.Text);
            c.Mode = GetMode();

            var x = (new XmlSerializer()).Write(c);

            try
            {
                TreeFile.WriteFile(ConfigurationFilePath, x);
            }
            catch
            {
            }
        }
Beispiel #2
0
        public TokenParserResult ReadToken(TextRange RangeInLine)
        {
            var s     = Text.GetTextInLine(RangeInLine);
            int Index = 0;

            Func <Boolean>     EndOfLine       = () => Index >= s.Length;
            Func <int, String> Peek            = n => s.Substring(Index, Math.Min(n, s.Length - Index));
            Action             Proceed         = () => Index += 1;
            Action <int>       ProceedMultiple = n => Index += n;

            Func <Optional <TextRange> > MakeRemainingChars = () => new TextRange {
                Start = Text.Calc(RangeInLine.Start, Index), End = RangeInLine.End
            };
            var NullRemainingChars = Optional <TextRange> .Empty;
            var NullToken          = Optional <SyntaxRule> .Empty;
            Func <int, int, TextRange> MakeTokenRange = (TokenStart, TokenEnd) => new TextRange {
                Start = Text.Calc(RangeInLine.Start, TokenStart), End = Text.Calc(RangeInLine.Start, TokenEnd)
            };
            Func <int, FileTextRange> MakeNextErrorTokenRange = n => new FileTextRange {
                Text = Text, Range = MakeTokenRange(Index, n)
            };

            var State = 0;

            var StartIndex = 0;
            Func <String, InvalidTokenException> MakeCurrentErrorTokenException = Message => new InvalidTokenException(Message, new FileTextRange {
                Text = Text, Range = MakeTokenRange(StartIndex, Index)
            }, Text.GetTextInLine(MakeTokenRange(StartIndex, Index)));
            Func <String, InvalidTokenException> MakeNextCharErrorTokenException = Message => new InvalidTokenException(Message, MakeNextErrorTokenRange(1), Peek(1));
            Action          MarkStart = () => StartIndex = Index;
            var             Output    = new List <Char>();
            Action <String> Write     = cs => Output.AddRange(cs);

            Func <Optional <SyntaxRule> > MakeSymbol = () =>
            {
                var Range  = MakeTokenRange(StartIndex, Index);
                var Symbol = new String(Output.ToArray());
                if (rBooleanLiteral.Match(Symbol).Success)
                {
                    var t  = TokenLiteral.CreateBooleanValue(!Symbol.Equals("false", StringComparison.Ordinal));
                    var st = SyntaxRule.CreateLiteral(t);
                    Positions.Add(t, Range);
                    Positions.Add(st, Range);
                    return(st);
                }
                else if (rIntLiteral.Match(Symbol).Success)
                {
                    var t  = TokenLiteral.CreateIntValue(NumericStrings.InvariantParseInt32(Symbol));
                    var st = SyntaxRule.CreateLiteral(t);
                    Positions.Add(t, Range);
                    Positions.Add(st, Range);
                    return(st);
                }
                else if (rRealLiteral.Match(Symbol).Success)
                {
                    var t  = TokenLiteral.CreateRealValue(NumericStrings.InvariantParseFloat64(Symbol));
                    var st = SyntaxRule.CreateLiteral(t);
                    Positions.Add(t, Range);
                    Positions.Add(st, Range);
                    return(st);
                }
                else if (rIdentifier.Match(Symbol).Success)
                {
                    var t = new TokenIdentifier {
                        Name = Symbol
                    };
                    var st = SyntaxRule.CreateIdentifier(t);
                    Positions.Add(t, Range);
                    Positions.Add(st, Range);
                    return(st);
                }
                else
                {
                    throw MakeCurrentErrorTokenException("InvalidToken");
                }
            };

            Func <Optional <SyntaxRule> > MakeBinaryOperator = () =>
            {
                var Range = MakeTokenRange(StartIndex, Index);
                var t     = new TokenBinaryOperator {
                    Name = new String(Output.ToArray())
                };
                var st = SyntaxRule.CreateBinaryOperator(t);
                Positions.Add(t, Range);
                Positions.Add(st, Range);
                return(st);
            };

            Func <Optional <SyntaxRule> > MakeUnaryOperator = () =>
            {
                var Range = MakeTokenRange(StartIndex, Index);
                var t     = new TokenUnaryOperator {
                    Name = new String(Output.ToArray())
                };
                var st = SyntaxRule.CreateUnaryOperator(t);
                Positions.Add(t, Range);
                Positions.Add(st, Range);
                return(st);
            };

            Func <Optional <SyntaxRule> > MakeLeftParen = () =>
            {
                var Range = MakeTokenRange(StartIndex, Index);
                var t     = new TokenLeftParen {
                };
                var st    = SyntaxRule.CreateLeftParen(t);
                Positions.Add(t, Range);
                Positions.Add(st, Range);
                return(st);
            };

            Func <Optional <SyntaxRule> > MakeRightParen = () =>
            {
                var Range = MakeTokenRange(StartIndex, Index);
                var t     = new TokenRightParen {
                };
                var st    = SyntaxRule.CreateRightParen(t);
                Positions.Add(t, Range);
                Positions.Add(st, Range);
                return(st);
            };

            Func <Optional <SyntaxRule> > MakeComma = () =>
            {
                var Range = MakeTokenRange(StartIndex, Index);
                var t     = new TokenComma {
                };
                var st    = SyntaxRule.CreateComma(t);
                Positions.Add(t, Range);
                Positions.Add(st, Range);
                return(st);
            };

            while (true)
            {
                if (State == 0)
                {
                    if (EndOfLine())
                    {
                        return(new TokenParserResult {
                            Token = NullToken, RemainingChars = NullRemainingChars
                        });
                    }
                    var c2 = Peek(2);
                    if (c2 == "&&" || c2 == "||" || c2 == "<=" || c2 == ">=" || c2 == "==" || c2 == "!=")
                    {
                        MarkStart();
                        Write(c2);
                        ProceedMultiple(2);
                        return(new TokenParserResult {
                            Token = MakeBinaryOperator(), RemainingChars = MakeRemainingChars()
                        });
                    }
                    var c = Peek(1);
                    if (c == " ")
                    {
                        Proceed();
                        continue;
                    }
                    if (c == "+" || c == "-" || c == "*" || c == "/" || c == "<" || c == ">")
                    {
                        MarkStart();
                        Write(c);
                        Proceed();
                        return(new TokenParserResult {
                            Token = MakeBinaryOperator(), RemainingChars = MakeRemainingChars()
                        });
                    }
                    if (c == "!")
                    {
                        MarkStart();
                        Write(c);
                        Proceed();
                        return(new TokenParserResult {
                            Token = MakeUnaryOperator(), RemainingChars = MakeRemainingChars()
                        });
                    }
                    if (c == "(")
                    {
                        MarkStart();
                        Proceed();
                        return(new TokenParserResult {
                            Token = MakeLeftParen(), RemainingChars = MakeRemainingChars()
                        });
                    }
                    if (c == ")")
                    {
                        MarkStart();
                        Proceed();
                        return(new TokenParserResult {
                            Token = MakeRightParen(), RemainingChars = MakeRemainingChars()
                        });
                    }
                    if (c == ",")
                    {
                        MarkStart();
                        Proceed();
                        return(new TokenParserResult {
                            Token = MakeComma(), RemainingChars = MakeRemainingChars()
                        });
                    }
                    if (rSymbol.Match(c).Success)
                    {
                        MarkStart();
                        Write(c);
                        Proceed();
                        State = 1;
                        continue;
                    }
                    throw MakeNextCharErrorTokenException("InvalidChar");
                }
                else if (State == 1)
                {
                    var c = Peek(1);
                    if (rSymbol.Match(c).Success)
                    {
                        Write(c);
                        Proceed();
                        continue;
                    }
                    return(new TokenParserResult {
                        Token = MakeSymbol(), RemainingChars = MakeRemainingChars()
                    });
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
        }
Beispiel #3
0
        private Func <Node, ColumnVal> GetColumnReader(EntityDef e, VariableDef c)
        {
            String  TypeName;
            Boolean IsOptional;

            if (c.Type.OnTypeRef)
            {
                TypeName   = c.Type.TypeRef.Value;
                IsOptional = false;
            }
            else if (c.Type.OnOptional)
            {
                TypeName   = c.Type.Optional.Value;
                IsOptional = true;
            }
            else if (c.Type.OnList)
            {
                var ElementTypeName = c.Type.List.Value;
                if (!ElementTypeName.Equals("Byte", StringComparison.OrdinalIgnoreCase))
                {
                    throw new InvalidOperationException("InvalidColumnListType: List<{0}>".Formats(ElementTypeName));
                }

                TypeName   = "Binary";
                IsOptional = false;
            }
            else
            {
                throw new InvalidOperationException();
            }
            Dictionary <String, Int64> EnumParser = null;

            if (EnumUnderlyingTypes.ContainsKey(TypeName))
            {
                EnumParser = EnumParsers[TypeName];
                TypeName   = EnumUnderlyingTypes[TypeName];
            }

            Func <Node, ColumnVal> Reader;

            if (!IsOptional)
            {
                if (TypeName.Equals("Boolean", StringComparison.OrdinalIgnoreCase))
                {
                    Reader = TreeRow =>
                    {
                        var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                        if (cvs.Count != 1)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                        }
                        var cv = cvs.Single().Stem.Children.Single().Leaf;
                        try
                        {
                            var v = Boolean.Parse(cv);
                            return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateBooleanValue(v)));
                        }
                        catch (FormatException)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                        }
                    };
                }
                else if (TypeName.Equals("String", StringComparison.OrdinalIgnoreCase))
                {
                    Reader = TreeRow =>
                    {
                        var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                        if (cvs.Count != 1)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                        }
                        var cv = cvs.Single().Stem.Children.Single().Leaf;
                        var v  = cv;
                        return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateStringValue(v)));
                    };
                }
                else if (TypeName.Equals("Int", StringComparison.OrdinalIgnoreCase))
                {
                    if (EnumParser != null)
                    {
                        Reader = TreeRow =>
                        {
                            var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                            if (cvs.Count != 1)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                            }
                            var cv = cvs.Single().Stem.Children.Single().Leaf;
                            if (EnumParser.ContainsKey(cv))
                            {
                                var v = (int)(EnumParser[cv]);
                                return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateIntValue(v)));
                            }
                            else
                            {
                                try
                                {
                                    var v = NumericStrings.InvariantParseInt32(cv);
                                    return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateIntValue(v)));
                                }
                                catch (FormatException)
                                {
                                    throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                                }
                            }
                        };
                    }
                    else
                    {
                        Reader = TreeRow =>
                        {
                            var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                            if (cvs.Count != 1)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                            }
                            var cv = cvs.Single().Stem.Children.Single().Leaf;
                            try
                            {
                                var v = NumericStrings.InvariantParseInt32(cv);
                                return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateIntValue(v)));
                            }
                            catch (FormatException)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                            }
                        };
                    }
                }
                else if (TypeName.Equals("Int64", StringComparison.OrdinalIgnoreCase))
                {
                    if (EnumParser != null)
                    {
                        Reader = TreeRow =>
                        {
                            var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                            if (cvs.Count != 1)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                            }
                            var cv = cvs.Single().Stem.Children.Single().Leaf;
                            if (EnumParser.ContainsKey(cv))
                            {
                                var v = (Int64)(EnumParser[cv]);
                                return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateInt64Value(v)));
                            }
                            else
                            {
                                try
                                {
                                    var v = NumericStrings.InvariantParseInt64(cv);
                                    return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateInt64Value(v)));
                                }
                                catch (FormatException)
                                {
                                    throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                                }
                            }
                        };
                    }
                    else
                    {
                        Reader = TreeRow =>
                        {
                            var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                            if (cvs.Count != 1)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                            }
                            var cv = cvs.Single().Stem.Children.Single().Leaf;
                            try
                            {
                                var v = NumericStrings.InvariantParseInt64(cv);
                                return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateInt64Value(v)));
                            }
                            catch (FormatException)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                            }
                        };
                    }
                }
                else if (TypeName.Equals("Real", StringComparison.OrdinalIgnoreCase))
                {
                    Reader = TreeRow =>
                    {
                        var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                        if (cvs.Count != 1)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                        }
                        var cv = cvs.Single().Stem.Children.Single().Leaf;
                        try
                        {
                            var v = NumericStrings.InvariantParseFloat64(cv);
                            return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateRealValue(v)));
                        }
                        catch (FormatException)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                        }
                    };
                }
                else if (TypeName.Equals("Binary", StringComparison.OrdinalIgnoreCase))
                {
                    Reader = TreeRow =>
                    {
                        var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                        if (cvs.Count != 1)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                        }
                        var cv = cvs.Single().Stem.Children.Single().Leaf;
                        try
                        {
                            var v = Regex.Split(cv.Trim(" \t\r\n".ToCharArray()), "( |\t|\r|\n)+", RegexOptions.ExplicitCapture).Where(s => s != "").Select(s => Byte.Parse(s, System.Globalization.NumberStyles.HexNumber)).ToList();
                            return(ColumnVal.CreatePrimitive(PrimitiveVal.CreateBinaryValue(v)));
                        }
                        catch (FormatException)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                        }
                    };
                }
                else
                {
                    throw new InvalidOperationException("InvalidType: {0}".Formats(TypeName));
                }
            }
            else
            {
                if (TypeName.Equals("Boolean", StringComparison.OrdinalIgnoreCase))
                {
                    Reader = TreeRow =>
                    {
                        var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                        if (cvs.Count != 1)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                        }
                        var cv = cvs.Single().Stem.Children.Single().Leaf;
                        if ((cv == null) || (cv == "-"))
                        {
                            return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty));
                        }
                        try
                        {
                            var v = Boolean.Parse(cv);
                            return(ColumnVal.CreateOptional(PrimitiveVal.CreateBooleanValue(v)));
                        }
                        catch (FormatException)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                        }
                    };
                }
                else if (TypeName.Equals("String", StringComparison.OrdinalIgnoreCase))
                {
                    Reader = TreeRow =>
                    {
                        var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                        if (cvs.Count != 1)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                        }
                        var cv = cvs.Single().Stem.Children.Single().Leaf;
                        if ((cv == null) || (cv == "-"))
                        {
                            return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty));
                        }
                        var v = cv;
                        return(ColumnVal.CreateOptional(PrimitiveVal.CreateStringValue(v)));
                    };
                }
                else if (TypeName.Equals("Int", StringComparison.OrdinalIgnoreCase))
                {
                    if (EnumParser != null)
                    {
                        Reader = TreeRow =>
                        {
                            var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                            if (cvs.Count != 1)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                            }
                            var cv = cvs.Single().Stem.Children.Single().Leaf;
                            if ((cv == null) || (cv == "-"))
                            {
                                return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty));
                            }
                            if (EnumParser.ContainsKey(cv))
                            {
                                var v = (int)(EnumParser[cv]);
                                return(ColumnVal.CreateOptional(PrimitiveVal.CreateIntValue(v)));
                            }
                            else
                            {
                                try
                                {
                                    var v = NumericStrings.InvariantParseInt32(cv);
                                    return(ColumnVal.CreateOptional(PrimitiveVal.CreateIntValue(v)));
                                }
                                catch (FormatException)
                                {
                                    throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                                }
                            }
                        };
                    }
                    else
                    {
                        Reader = TreeRow =>
                        {
                            var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                            if (cvs.Count != 1)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                            }
                            var cv = cvs.Single().Stem.Children.Single().Leaf;
                            if ((cv == null) || (cv == "-"))
                            {
                                return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty));
                            }
                            try
                            {
                                var v = NumericStrings.InvariantParseInt32(cv);
                                return(ColumnVal.CreateOptional(PrimitiveVal.CreateIntValue(v)));
                            }
                            catch (FormatException)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                            }
                        };
                    }
                }
                else if (TypeName.Equals("Int64", StringComparison.OrdinalIgnoreCase))
                {
                    if (EnumParser != null)
                    {
                        Reader = TreeRow =>
                        {
                            var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                            if (cvs.Count != 1)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                            }
                            var cv = cvs.Single().Stem.Children.Single().Leaf;
                            if ((cv == null) || (cv == "-"))
                            {
                                return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty));
                            }
                            if (EnumParser.ContainsKey(cv))
                            {
                                var v = (Int64)(EnumParser[cv]);
                                return(ColumnVal.CreateOptional(PrimitiveVal.CreateInt64Value(v)));
                            }
                            else
                            {
                                try
                                {
                                    var v = NumericStrings.InvariantParseInt64(cv);
                                    return(ColumnVal.CreateOptional(PrimitiveVal.CreateInt64Value(v)));
                                }
                                catch (FormatException)
                                {
                                    throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                                }
                            }
                        };
                    }
                    else
                    {
                        Reader = TreeRow =>
                        {
                            var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                            if (cvs.Count != 1)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                            }
                            var cv = cvs.Single().Stem.Children.Single().Leaf;
                            if ((cv == null) || (cv == "-"))
                            {
                                return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty));
                            }
                            try
                            {
                                var v = NumericStrings.InvariantParseInt64(cv);
                                return(ColumnVal.CreateOptional(PrimitiveVal.CreateInt64Value(v)));
                            }
                            catch (FormatException)
                            {
                                throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                            }
                        };
                    }
                }
                else if (TypeName.Equals("Real", StringComparison.OrdinalIgnoreCase))
                {
                    Reader = TreeRow =>
                    {
                        var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                        if (cvs.Count != 1)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                        }
                        var cv = cvs.Single().Stem.Children.Single().Leaf;
                        if ((cv == null) || (cv == "-"))
                        {
                            return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty));
                        }
                        try
                        {
                            var v = NumericStrings.InvariantParseFloat64(cv);
                            return(ColumnVal.CreateOptional(PrimitiveVal.CreateRealValue(v)));
                        }
                        catch (FormatException)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                        }
                    };
                }
                else if (TypeName.Equals("Binary", StringComparison.OrdinalIgnoreCase))
                {
                    Reader = TreeRow =>
                    {
                        var cvs = TreeRow.Stem.Children.Where(col => col.OnStem && col.Stem.Name.Equals(c.Name, StringComparison.OrdinalIgnoreCase)).ToList();
                        if (cvs.Count != 1)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1}", e.Name, c.Name));
                        }
                        var cv = cvs.Single().Stem.Children.Single().Leaf;
                        if ((cv == null) || (cv == "-"))
                        {
                            return(ColumnVal.CreateOptional(Optional <PrimitiveVal> .Empty));
                        }
                        try
                        {
                            var v = Regex.Split(cv.Trim(" \t\r\n".ToCharArray()), "( |\t|\r|\n)+", RegexOptions.ExplicitCapture).Select(s => Byte.Parse(s, System.Globalization.NumberStyles.HexNumber)).ToList();
                            return(ColumnVal.CreateOptional(PrimitiveVal.CreateBinaryValue(v)));
                        }
                        catch (FormatException)
                        {
                            throw new InvalidOperationException(String.Format("InvalidData: {0}.{1} '{2}'", e.Name, c.Name, cv));
                        }
                    };
                }
                else
                {
                    throw new InvalidOperationException("InvalidType: {0}".Formats(TypeName));
                }
            }
            return(Reader);
        }
Beispiel #4
0
        private static Func <PrimitiveVal, PrimitiveVal> GetPrimitiveTranslator(TypeRef OldType, TypeRef NewType)
        {
            if (OldType.Value == NewType.Value)
            {
                return(v => v);
            }
            if (OldType.Value == "Boolean")
            {
                if (NewType.Value == "String")
                {
                    return(v =>
                    {
                        if (!v.OnBooleanValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateStringValue(v.BooleanValue ? "True" : "False");
                    });
                }
                else if (NewType.Value == "Int")
                {
                    return(v =>
                    {
                        if (!v.OnBooleanValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateIntValue(v.BooleanValue ? -1 : 0);
                    });
                }
                else if (NewType.Value == "Real")
                {
                    return(v =>
                    {
                        if (!v.OnBooleanValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateRealValue(v.BooleanValue ? -1 : 0);
                    });
                }
                else if (NewType.Value == "Binary")
                {
                    return(v => { throw new InvalidOperationException(); });
                }
                else if (NewType.Value == "Int64")
                {
                    return(v =>
                    {
                        if (!v.OnBooleanValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateInt64Value(v.BooleanValue ? -1 : 0);
                    });
                }
            }
            else if (OldType.Value == "String")
            {
                if (NewType.Value == "Boolean")
                {
                    return(v =>
                    {
                        if (!v.OnStringValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        var b = false;
                        if (v.StringValue == "True")
                        {
                            b = true;
                        }
                        else if (v.StringValue == "False")
                        {
                            b = false;
                        }
                        return PrimitiveVal.CreateBooleanValue(b);
                    });
                }
                else if (NewType.Value == "Int")
                {
                    return(v =>
                    {
                        if (!v.OnStringValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateIntValue(NumericStrings.InvariantParseInt32(v.StringValue));
                    });
                }
                else if (NewType.Value == "Real")
                {
                    return(v =>
                    {
                        if (!v.OnStringValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateRealValue(NumericStrings.InvariantParseFloat64(v.StringValue));
                    });
                }
                else if (NewType.Value == "Binary")
                {
                    return(v => { throw new InvalidOperationException(); });
                }
                else if (NewType.Value == "Int64")
                {
                    return(v =>
                    {
                        if (!v.OnStringValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateInt64Value(NumericStrings.InvariantParseInt64(v.StringValue));
                    });
                }
            }
            else if (OldType.Value == "Int")
            {
                if (NewType.Value == "Boolean")
                {
                    return(v =>
                    {
                        if (!v.OnIntValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateBooleanValue(v.IntValue != 0);
                    });
                }
                else if (NewType.Value == "String")
                {
                    return(v =>
                    {
                        if (!v.OnIntValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateStringValue(v.IntValue.ToInvariantString());
                    });
                }
                else if (NewType.Value == "Real")
                {
                    return(v =>
                    {
                        if (!v.OnIntValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateRealValue(v.IntValue);
                    });
                }
                else if (NewType.Value == "Binary")
                {
                    return(v => { throw new InvalidOperationException(); });
                }
                else if (NewType.Value == "Int64")
                {
                    return(v =>
                    {
                        if (!v.OnIntValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateInt64Value(v.IntValue);
                    });
                }
            }
            else if (OldType.Value == "Real")
            {
                if (NewType.Value == "Boolean")
                {
                    return(v =>
                    {
                        if (!v.OnRealValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateBooleanValue(v.RealValue != 0.0);
                    });
                }
                else if (NewType.Value == "String")
                {
                    return(v =>
                    {
                        if (!v.OnRealValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateStringValue(v.RealValue.ToInvariantString());
                    });
                }
                else if (NewType.Value == "Int")
                {
                    return(v =>
                    {
                        if (!v.OnRealValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateIntValue(Convert.ToInt32(v.RealValue));
                    });
                }
                else if (NewType.Value == "Binary")
                {
                    return(v => { throw new InvalidOperationException(); });
                }
                else if (NewType.Value == "Int64")
                {
                    return(v =>
                    {
                        if (!v.OnRealValue)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateInt64Value(Convert.ToInt64(v.RealValue));
                    });
                }
            }
            else if (OldType.Value == "Binary")
            {
                return(v => { throw new InvalidOperationException(); });
            }
            else if (OldType.Value == "Int64")
            {
                if (NewType.Value == "Boolean")
                {
                    return(v =>
                    {
                        if (!v.OnInt64Value)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateBooleanValue(v.Int64Value != 0);
                    });
                }
                else if (NewType.Value == "String")
                {
                    return(v =>
                    {
                        if (!v.OnInt64Value)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateStringValue(v.Int64Value.ToInvariantString());
                    });
                }
                else if (NewType.Value == "Int")
                {
                    return(v =>
                    {
                        if (!v.OnInt64Value)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateIntValue((int)(v.Int64Value));
                    });
                }
                else if (NewType.Value == "Real")
                {
                    return(v =>
                    {
                        if (!v.OnInt64Value)
                        {
                            throw new InvalidOperationException();
                        }
                        ;
                        return PrimitiveVal.CreateRealValue(v.Int64Value);
                    });
                }
                else if (NewType.Value == "Binary")
                {
                    return(v => { throw new InvalidOperationException(); });
                }
            }

            throw new InvalidOperationException();
        }
Beispiel #5
0
        public static FileParserResult ParseFile(Text Text)
        {
            var TypeFunctions = new HashSet <String>()
            {
                "Primitive", "Alias", "Record", "TaggedUnion", "Enum", "ClientCommand", "ServerCommand"
            };
            var Functions = new HashSet <String>(TypeFunctions.Concat(new List <String>()
            {
                "Namespace", "Import"
            }));

            var ps = new TreeFormatParseSetting()
            {
                IsTableParameterFunction = Name => Functions.Contains(Name),
                IsTableContentFunction   = Name => Functions.Contains(Name),
                IsTreeParameterFunction  = Name => false,
                IsTreeContentFunction    = Name => false
            };

            var sp           = new TreeFormatSyntaxParser(ps, Text);
            var ParserResult = sp.Parse();
            var ts           = new TreeSerializer();

            var Types                   = new List <TypeDef>();
            var TypeRefs                = new List <TypeDef>();
            var Imports                 = new List <String>();
            var TypeToNamespace         = new Dictionary <TypeDef, List <String> >();
            var TypeToNamespaceImports  = new Dictionary <TypeDef, List <List <String> > >();
            var CurrentNamespace        = new List <String>();
            var CurrentNamespaceImports = new List <List <String> >();

            var Positions = new Dictionary <Object, TextRange>();

            foreach (var TopNode in ParserResult.Value.MultiNodesList)
            {
                if (TopNode.OnFunctionNodes)
                {
                    var pr = new TreeFormatParseResult
                    {
                        Value = new Forest {
                            MultiNodesList = new List <MultiNodes> {
                                TopNode
                            }
                        },
                        Text             = Text,
                        Positions        = ParserResult.Positions,
                        RawFunctionCalls = ParserResult.RawFunctionCalls
                    };
                    var es = new TreeFormatEvaluateSetting
                    {
                        FunctionCallEvaluator = (f, nm) =>
                        {
                            Action <Object, Object> Mark = (SemanticsObj, SyntaxObj) =>
                            {
                                var Range = nm.GetRange(SyntaxObj);
                                if (Range.OnSome)
                                {
                                    Positions.Add(SemanticsObj, Range.Value);
                                }
                            };

                            Func <TFSemantics.Node, List <String> > ExtractNamespaceParts = Node =>
                            {
                                var Namespace = GetLeafNodeValue(Node, nm, "InvalidName");

                                var NamespaceParts = new List <String>();
                                int InvalidCharIndex;
                                var osml = TokenParser.TrySplitSymbolMemberChain(Namespace, out InvalidCharIndex);
                                if (osml.OnNone)
                                {
                                    var Range       = nm.GetRange(Node);
                                    var InvalidChar = Namespace.Substring(InvalidCharIndex, 1);
                                    if (Range.OnSome)
                                    {
                                        Range = new TextRange {
                                            Start = nm.Text.Calc(Range.Value.Start, InvalidCharIndex), End = nm.Text.Calc(Range.Value.Start, InvalidCharIndex + 1)
                                        };
                                    }
                                    throw new InvalidTokenException("InvalidChar", new FileTextRange {
                                        Text = nm.Text, Range = Range
                                    }, InvalidChar);
                                }
                                foreach (var p in osml.Value)
                                {
                                    if (p.Parameters.Count > 0)
                                    {
                                        var Range = nm.GetRange(Node);
                                        var Part  = Namespace.Substring(p.SymbolStartIndex, p.SymbolEndIndex);
                                        if (Range.OnSome)
                                        {
                                            Range = new TextRange {
                                                Start = nm.Text.Calc(Range.Value.Start, p.SymbolStartIndex), End = nm.Text.Calc(Range.Value.Start, p.SymbolEndIndex)
                                            };
                                        }
                                        throw new InvalidTokenException("InvalidNamespacePart", new FileTextRange {
                                            Text = nm.Text, Range = Range
                                        }, Part);
                                    }
                                    int LocalInvalidCharIndex;
                                    var oName = TokenParser.TryUnescapeSymbolName(p.Name, out LocalInvalidCharIndex);
                                    if (oName.OnNone)
                                    {
                                        InvalidCharIndex = p.NameStartIndex + LocalInvalidCharIndex;
                                        var Range       = nm.GetRange(Node);
                                        var InvalidChar = Namespace.Substring(InvalidCharIndex, 1);
                                        if (Range.OnSome)
                                        {
                                            Range = new TextRange {
                                                Start = nm.Text.Calc(Range.Value.Start, InvalidCharIndex), End = nm.Text.Calc(Range.Value.Start, InvalidCharIndex + 1)
                                            };
                                        }
                                        throw new InvalidTokenException("InvalidChar", new FileTextRange {
                                            Text = nm.Text, Range = Range
                                        }, InvalidChar);
                                    }
                                    NamespaceParts.Add(p.Name);
                                }

                                return(NamespaceParts);
                            };

                            if (TypeFunctions.Contains(f.Name.Text))
                            {
                                if (f.Parameters.Count < 1 || f.Parameters.Count > 2)
                                {
                                    throw new InvalidEvaluationException("InvalidParameterCount", nm.GetFileRange(f), f);
                                }

                                var TypeRef = ParseTypeRef(f.Parameters[0], nm, Positions);
                                var Name    = TypeRef.Name;
                                var Version = TypeRef.Version;

                                var Attributes  = new List <KeyValuePair <String, List <String> > >();
                                var Description = "";
                                if (f.Parameters.Count >= 2)
                                {
                                    var DescriptionParameter = f.Parameters[1];
                                    if (!DescriptionParameter.OnLeaf)
                                    {
                                        throw new InvalidEvaluationException("InvalidDescription", nm.GetFileRange(DescriptionParameter), DescriptionParameter);
                                    }
                                    var c = TokenParser.DecomposeDescription(DescriptionParameter.Leaf);
                                    Attributes = c.Attributes;
                                    Mark(Attributes, f.Parameters[1]);
                                    Description = c.Description;
                                }

                                var ContentLines = new List <FunctionCallTableLine> {
                                };
                                if (f.Content.OnSome)
                                {
                                    var ContentValue = f.Content.Value;
                                    if (!ContentValue.OnTableContent)
                                    {
                                        throw new InvalidEvaluationException("InvalidContent", nm.GetFileRange(ContentValue), ContentValue);
                                    }
                                    ContentLines = ContentValue.TableContent;
                                }

                                switch (f.Name.Text)
                                {
                                case "Primitive":
                                {
                                    if (Version != "")
                                    {
                                        throw new InvalidEvaluationException("InvalidName", nm.GetFileRange(f.Parameters[0]), f.Parameters[0]);
                                    }

                                    var GenericParameters = new List <VariableDef>();

                                    foreach (var Line in ContentLines)
                                    {
                                        String   cName        = null;
                                        TypeSpec cType        = null;
                                        var      cAttributes  = new List <KeyValuePair <String, List <String> > >();
                                        var      cDescription = "";

                                        if (Line.Nodes.Count == 2)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                        }
                                        else if (Line.Nodes.Count == 3)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                            var c = TokenParser.DecomposeDescription(GetLeafNodeValue(Line.Nodes[2], nm, "InvalidDescription"));
                                            cAttributes = c.Attributes;
                                            Mark(cAttributes, Line.Nodes[2]);
                                            cDescription = c.Description;
                                        }
                                        else if (Line.Nodes.Count == 0)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                        }

                                        if (cName.StartsWith("'"))
                                        {
                                            cName = new String(cName.Skip(1).ToArray());
                                            var gp = new VariableDef {
                                                Name = cName, Type = cType, Attributes = cAttributes, Description = cDescription
                                            };
                                            Mark(gp, Line);
                                            GenericParameters.Add(gp);
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLine", nm.GetFileRange(Line), Line);
                                        }
                                    }

                                    var p = new PrimitiveDef {
                                        Name = Name, GenericParameters = GenericParameters, Attributes = Attributes, Description = Description
                                    };
                                    Mark(p, f);
                                    var t = TypeDef.CreatePrimitive(p);
                                    Mark(t, f);
                                    Types.Add(t);
                                    TypeToNamespace.Add(t, CurrentNamespace);
                                    TypeToNamespaceImports.Add(t, CurrentNamespaceImports);
                                    return(new List <TFSemantics.Node> {
                                        });
                                }

                                case "Alias":
                                {
                                    var      GenericParameters = new List <VariableDef>();
                                    TypeSpec Type = null;

                                    foreach (var Line in ContentLines)
                                    {
                                        String   cName        = null;
                                        TypeSpec cType        = null;
                                        var      cAttributes  = new List <KeyValuePair <String, List <String> > >();
                                        var      cDescription = "";

                                        if (Line.Nodes.Count == 1)
                                        {
                                            if (Type != null)
                                            {
                                                throw new InvalidEvaluationException("InvalidLine", nm.GetFileRange(Line), Line);
                                            }
                                            Type = ParseTypeSpec(Line.Nodes[0], nm, Positions);
                                            continue;
                                        }
                                        else if (Line.Nodes.Count == 2)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                        }
                                        else if (Line.Nodes.Count == 3)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                            var c = TokenParser.DecomposeDescription(GetLeafNodeValue(Line.Nodes[2], nm, "InvalidDescription"));
                                            cAttributes = c.Attributes;
                                            Mark(cAttributes, Line.Nodes[2]);
                                            cDescription = c.Description;
                                        }
                                        else if (Line.Nodes.Count == 0)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                        }

                                        if (cName.StartsWith("'"))
                                        {
                                            cName = new String(cName.Skip(1).ToArray());
                                            var gp = new VariableDef {
                                                Name = cName, Type = cType, Attributes = cAttributes, Description = cDescription
                                            };
                                            Mark(gp, Line);
                                            GenericParameters.Add(gp);
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLine", nm.GetFileRange(Line), Line);
                                        }
                                    }

                                    if (Type == null)
                                    {
                                        throw new InvalidEvaluationException("InvalidContent", nm.GetFileRange(ContentLines), ContentLines);
                                    }

                                    var a = new AliasDef {
                                        Name = Name, Version = Version, GenericParameters = GenericParameters, Type = Type, Attributes = Attributes, Description = Description
                                    };
                                    Mark(a, f);
                                    var t = TypeDef.CreateAlias(a);
                                    Mark(t, f);
                                    Types.Add(t);
                                    TypeToNamespace.Add(t, CurrentNamespace);
                                    TypeToNamespaceImports.Add(t, CurrentNamespaceImports);
                                    return(new List <TFSemantics.Node> {
                                        });
                                }

                                case "Record":
                                {
                                    var GenericParameters = new List <VariableDef>();
                                    var Fields            = new List <VariableDef>();

                                    foreach (var Line in ContentLines)
                                    {
                                        String   cName        = null;
                                        TypeSpec cType        = null;
                                        var      cAttributes  = new List <KeyValuePair <String, List <String> > >();
                                        var      cDescription = "";

                                        if (Line.Nodes.Count == 2)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                        }
                                        else if (Line.Nodes.Count == 3)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                            var c = TokenParser.DecomposeDescription(GetLeafNodeValue(Line.Nodes[2], nm, "InvalidDescription"));
                                            cAttributes = c.Attributes;
                                            Mark(cAttributes, Line.Nodes[2]);
                                            cDescription = c.Description;
                                        }
                                        else if (Line.Nodes.Count == 0)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                        }

                                        if (cName.StartsWith("'"))
                                        {
                                            cName = new String(cName.Skip(1).ToArray());
                                            var gp = new VariableDef {
                                                Name = cName, Type = cType, Attributes = cAttributes, Description = cDescription
                                            };
                                            Mark(gp, Line);
                                            GenericParameters.Add(gp);
                                        }
                                        else
                                        {
                                            var p = new VariableDef {
                                                Name = cName, Type = cType, Attributes = cAttributes, Description = cDescription
                                            };
                                            Mark(p, Line);
                                            Fields.Add(p);
                                        }
                                    }

                                    var r = new RecordDef {
                                        Name = Name, Version = Version, GenericParameters = GenericParameters, Fields = Fields, Attributes = Attributes, Description = Description
                                    };
                                    Mark(r, f);
                                    var t = TypeDef.CreateRecord(r);
                                    Mark(t, f);
                                    Types.Add(t);
                                    TypeToNamespace.Add(t, CurrentNamespace);
                                    TypeToNamespaceImports.Add(t, CurrentNamespaceImports);
                                    return(new List <TFSemantics.Node> {
                                        });
                                }

                                case "TaggedUnion":
                                {
                                    var GenericParameters = new List <VariableDef>();
                                    var Alternatives      = new List <VariableDef>();

                                    foreach (var Line in ContentLines)
                                    {
                                        String   cName        = null;
                                        TypeSpec cType        = null;
                                        var      cAttributes  = new List <KeyValuePair <String, List <String> > >();
                                        var      cDescription = "";

                                        if (Line.Nodes.Count == 2)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidAlternativeName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                        }
                                        else if (Line.Nodes.Count == 3)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidAlternativeName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                            var c = TokenParser.DecomposeDescription(GetLeafNodeValue(Line.Nodes[2], nm, "InvalidDescription"));
                                            cAttributes = c.Attributes;
                                            Mark(cAttributes, Line.Nodes[2]);
                                            cDescription = c.Description;
                                        }
                                        else if (Line.Nodes.Count == 0)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                        }

                                        if (cName.StartsWith("'"))
                                        {
                                            cName = new String(cName.Skip(1).ToArray());
                                            var gp = new VariableDef {
                                                Name = cName, Type = cType, Attributes = cAttributes, Description = cDescription
                                            };
                                            Mark(gp, Line);
                                            GenericParameters.Add(gp);
                                        }
                                        else
                                        {
                                            var p = new VariableDef {
                                                Name = cName, Type = cType, Attributes = cAttributes, Description = cDescription
                                            };
                                            Mark(p, Line);
                                            Alternatives.Add(p);
                                        }
                                    }

                                    var tu = new TaggedUnionDef {
                                        Name = Name, Version = Version, GenericParameters = GenericParameters, Alternatives = Alternatives, Attributes = Attributes, Description = Description
                                    };
                                    Mark(tu, f);
                                    var t = TypeDef.CreateTaggedUnion(tu);
                                    Mark(t, f);
                                    Types.Add(t);
                                    TypeToNamespace.Add(t, CurrentNamespace);
                                    TypeToNamespaceImports.Add(t, CurrentNamespaceImports);
                                    return(new List <TFSemantics.Node> {
                                        });
                                }

                                case "Enum":
                                {
                                    var Literals = new List <LiteralDef>();

                                    Int64 NextValue = 0;
                                    foreach (var Line in ContentLines)
                                    {
                                        String cName        = null;
                                        Int64  cValue       = NextValue;
                                        var    cAttributes  = new List <KeyValuePair <String, List <String> > >();
                                        var    cDescription = "";

                                        if (Line.Nodes.Count == 1)
                                        {
                                            cName  = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidLiteralName");
                                            cValue = NextValue;
                                        }
                                        else if (Line.Nodes.Count == 2)
                                        {
                                            cName  = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidLiteralName");
                                            cValue = NumericStrings.InvariantParseInt64(GetLeafNodeValue(Line.Nodes[1], nm, "InvalidLiteralValue"));
                                        }
                                        else if (Line.Nodes.Count == 3)
                                        {
                                            cName  = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidLiteralName");
                                            cValue = NumericStrings.InvariantParseInt64(GetLeafNodeValue(Line.Nodes[1], nm, "InvalidLiteralValue"));
                                            var c = TokenParser.DecomposeDescription(GetLeafNodeValue(Line.Nodes[2], nm, "InvalidDescription"));
                                            cAttributes = c.Attributes;
                                            Mark(cAttributes, Line.Nodes[2]);
                                            cDescription = c.Description;
                                        }
                                        else if (Line.Nodes.Count == 0)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                        }
                                        NextValue = cValue + 1;

                                        var ltl = new LiteralDef {
                                            Name = cName, Value = cValue, Attributes = cAttributes, Description = cDescription
                                        };
                                        Mark(ltl, Line);
                                        Literals.Add(ltl);
                                    }

                                    var IntTypeName = new List <String> {
                                        "Int"
                                    };
                                    Mark(IntTypeName, f);
                                    var r = new TypeRef {
                                        Name = IntTypeName, Version = ""
                                    };
                                    Mark(r, f);
                                    var UnderlyingType = TypeSpec.CreateTypeRef(r);
                                    Mark(UnderlyingType, f);
                                    var ed = new EnumDef {
                                        Name = Name, Version = Version, UnderlyingType = UnderlyingType, Literals = Literals, Attributes = Attributes, Description = Description
                                    };
                                    Mark(ed, f);
                                    var t = TypeDef.CreateEnum(ed);
                                    Mark(t, f);
                                    Types.Add(t);
                                    TypeToNamespace.Add(t, CurrentNamespace);
                                    TypeToNamespaceImports.Add(t, CurrentNamespaceImports);
                                    return(new List <TFSemantics.Node> {
                                        });
                                }

                                case "ClientCommand":
                                {
                                    var OutParameters = new List <VariableDef>();
                                    var InParameters  = new List <VariableDef>();

                                    Boolean IsInParameter = false;
                                    foreach (var Line in ContentLines)
                                    {
                                        String   cName        = null;
                                        TypeSpec cType        = null;
                                        var      cAttributes  = new List <KeyValuePair <String, List <String> > >();
                                        var      cDescription = "";

                                        if (Line.Nodes.Count == 1)
                                        {
                                            if (GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName") == ">")
                                            {
                                                IsInParameter = true;
                                                continue;
                                            }
                                            else
                                            {
                                                throw new InvalidEvaluationException("InvalidLine", nm.GetFileRange(Line), Line);
                                            }
                                        }
                                        else if (Line.Nodes.Count == 2)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                        }
                                        else if (Line.Nodes.Count == 3)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                            var c = TokenParser.DecomposeDescription(GetLeafNodeValue(Line.Nodes[2], nm, "InvalidDescription"));
                                            cAttributes = c.Attributes;
                                            Mark(cAttributes, Line.Nodes[2]);
                                            cDescription = c.Description;
                                        }
                                        else if (Line.Nodes.Count == 0)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                        }

                                        var p = new VariableDef {
                                            Name = cName, Type = cType, Attributes = cAttributes, Description = cDescription
                                        };
                                        Mark(p, Line);
                                        if (IsInParameter)
                                        {
                                            InParameters.Add(p);
                                        }
                                        else
                                        {
                                            OutParameters.Add(p);
                                        }
                                    }

                                    var cc = new ClientCommandDef {
                                        Name = Name, Version = Version, OutParameters = OutParameters, InParameters = InParameters, Attributes = Attributes, Description = Description
                                    };
                                    Mark(cc, f);
                                    var t = TypeDef.CreateClientCommand(cc);
                                    Mark(t, f);
                                    Types.Add(t);
                                    TypeToNamespace.Add(t, CurrentNamespace);
                                    TypeToNamespaceImports.Add(t, CurrentNamespaceImports);
                                    return(new List <TFSemantics.Node> {
                                        });
                                }

                                case "ServerCommand":
                                {
                                    var OutParameters = new List <VariableDef>();

                                    foreach (var Line in ContentLines)
                                    {
                                        String   cName        = null;
                                        TypeSpec cType        = null;
                                        var      cAttributes  = new List <KeyValuePair <String, List <String> > >();
                                        var      cDescription = "";

                                        if (Line.Nodes.Count == 2)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                        }
                                        else if (Line.Nodes.Count == 3)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                            var c = TokenParser.DecomposeDescription(GetLeafNodeValue(Line.Nodes[2], nm, "InvalidDescription"));
                                            cAttributes = c.Attributes;
                                            Mark(cAttributes, Line.Nodes[2]);
                                            cDescription = c.Description;
                                        }
                                        else if (Line.Nodes.Count == 0)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                        }

                                        var p = new VariableDef {
                                            Name = cName, Type = cType, Attributes = cAttributes, Description = cDescription
                                        };
                                        Mark(p, Line);
                                        OutParameters.Add(p);
                                    }

                                    var sc = new ServerCommandDef {
                                        Name = Name, Version = Version, OutParameters = OutParameters, Attributes = Attributes, Description = Description
                                    };
                                    Mark(sc, f);
                                    var t = TypeDef.CreateServerCommand(sc);
                                    Mark(t, f);
                                    Types.Add(t);
                                    TypeToNamespace.Add(t, CurrentNamespace);
                                    TypeToNamespaceImports.Add(t, CurrentNamespaceImports);
                                    return(new List <TFSemantics.Node> {
                                        });
                                }

                                default:
                                {
                                    throw new InvalidEvaluationException("UnknownFunction", nm.GetFileRange(f), f);
                                }
                                }
                            }
                            else if (f.Name.Text == "Namespace")
                            {
                                if (f.Parameters.Count != 1)
                                {
                                    throw new InvalidEvaluationException("InvalidParameterCount", nm.GetFileRange(f), f);
                                }
                                var NamespaceParts = ExtractNamespaceParts(f.Parameters[0]);

                                Mark(NamespaceParts, f);
                                CurrentNamespace = NamespaceParts;
                                return(new List <TFSemantics.Node> {
                                });
                            }
                            else if (f.Name.Text == "Import")
                            {
                                if (f.Parameters.Count != 0)
                                {
                                    throw new InvalidEvaluationException("InvalidParameterCount", nm.GetFileRange(f), f);
                                }

                                var ContentLines = new List <FunctionCallTableLine> {
                                };
                                if (f.Content.OnSome)
                                {
                                    var ContentValue = f.Content.Value;
                                    if (!ContentValue.OnTableContent)
                                    {
                                        throw new InvalidEvaluationException("InvalidContent", nm.GetFileRange(ContentValue), ContentValue);
                                    }
                                    ContentLines = ContentValue.TableContent;
                                }

                                var NamespaceImports = new List <List <String> >();

                                foreach (var Line in ContentLines)
                                {
                                    if (Line.Nodes.Count == 1)
                                    {
                                        var NamespaceParts = ExtractNamespaceParts(Line.Nodes[0]);
                                        Mark(NamespaceParts, Line.Nodes[0]);
                                        NamespaceImports.Add(NamespaceParts);
                                    }
                                    else if (Line.Nodes.Count == 0)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                    }
                                }

                                CurrentNamespaceImports = CurrentNamespaceImports.Concat(NamespaceImports).ToList();

                                return(new List <TFSemantics.Node> {
                                });
                            }
                            else
                            {
                                throw new InvalidEvaluationException("UnknownFunction", nm.GetFileRange(f), f);
                            }
                        }
                    };
                    var e = new TreeFormatEvaluator(es, pr);
                    e.Evaluate();
                }
                else
                {
                    var pr = new TreeFormatParseResult
                    {
                        Value = new Forest {
                            MultiNodesList = new List <MultiNodes> {
                                TopNode
                            }
                        },
                        Text             = Text,
                        Positions        = ParserResult.Positions,
                        RawFunctionCalls = ParserResult.RawFunctionCalls
                    };
                    var es = new TreeFormatEvaluateSetting {
                    };
                    var e  = new TreeFormatEvaluator(es, pr);
                    var er = e.Evaluate();
                    if (er.Value.Nodes.Count > 0)
                    {
                        var ReadResult = ts.Read <Schema>(CollectionOperations.CreatePair(er.Value, er.Positions));
                        Types.AddRange(ReadResult.Key.Types);
                        TypeRefs.AddRange(ReadResult.Key.TypeRefs);
                        Imports.AddRange(ReadResult.Key.Imports);
                        foreach (var p in ReadResult.Value)
                        {
                            if (p.Value.Range.OnSome)
                            {
                                Positions.Add(p.Key, p.Value.Range.Value);
                            }
                        }
                    }
                }
            }

            return(new FileParserResult {
                Text = Text, Positions = Positions, Types = Types, TypeRefs = TypeRefs, Imports = Imports, TypeToNamespace = TypeToNamespace, TypeToNamespaceImports = TypeToNamespaceImports
            });
        }
Beispiel #6
0
        public static FileParserResult ParseFile(Text Text)
        {
            var TypeFunctions = new HashSet <String>()
            {
                "Primitive", "Entity", "Enum"
            };
            var TableParameterFunctions = new HashSet <String>(TypeFunctions.Concat(new List <String> {
                "Query"
            }));
            var TableContentFunctions = TypeFunctions;

            var ps = new TreeFormatParseSetting()
            {
                IsTableParameterFunction = Name => TableParameterFunctions.Contains(Name),
                IsTableContentFunction   = Name => TableContentFunctions.Contains(Name)
            };

            var sp           = new TreeFormatSyntaxParser(ps, Text);
            var ParserResult = sp.Parse();
            var ts           = new TreeSerializer();

            var Types    = new List <TypeDef>();
            var TypeRefs = new List <TypeDef>();
            var Imports  = new List <String>();

            var Positions = new Dictionary <Object, TextRange>();

            foreach (var TopNode in ParserResult.Value.MultiNodesList)
            {
                if (TopNode.OnFunctionNodes)
                {
                    var pr = new TreeFormatParseResult
                    {
                        Value = new Forest {
                            MultiNodesList = new List <MultiNodes> {
                                TopNode
                            }
                        },
                        Text             = Text,
                        Positions        = ParserResult.Positions,
                        RawFunctionCalls = ParserResult.RawFunctionCalls
                    };
                    var es = new TreeFormatEvaluateSetting
                    {
                        FunctionCallEvaluator = (f, nm) =>
                        {
                            Action <Object, Object> Mark = (SemanticsObj, SyntaxObj) =>
                            {
                                var Range = nm.GetRange(SyntaxObj);
                                if (Range.OnSome)
                                {
                                    Positions.Add(SemanticsObj, Range.Value);
                                }
                            };

                            if (TypeFunctions.Contains(f.Name.Text))
                            {
                                if (f.Parameters.Count < 1 || f.Parameters.Count > 2)
                                {
                                    throw new InvalidEvaluationException("InvalidParameterCount", nm.GetFileRange(f), f);
                                }

                                var VersionedName = GetLeafNodeValue(f.Parameters[0], nm, "InvalidName");
                                var TypeRef       = ParseTypeRef(VersionedName);
                                Mark(TypeRef, f.Parameters[0]);
                                var Name = (String)TypeRef;

                                var Attributes  = new List <KeyValuePair <String, List <String> > >();
                                var Description = "";
                                if (f.Parameters.Count >= 2)
                                {
                                    var DescriptionParameter = f.Parameters[1];
                                    if (!DescriptionParameter.OnLeaf)
                                    {
                                        throw new InvalidEvaluationException("InvalidDescription", nm.GetFileRange(DescriptionParameter), DescriptionParameter);
                                    }
                                    var c = OS.TokenParser.DecomposeDescription(DescriptionParameter.Leaf);
                                    Attributes = c.Attributes;
                                    Mark(Attributes, f.Parameters[1]);
                                    Description = c.Description;
                                }

                                var ContentLines = new List <FunctionCallTableLine> {
                                };
                                if (f.Content.OnSome)
                                {
                                    var ContentValue = f.Content.Value;
                                    if (!ContentValue.OnTableContent)
                                    {
                                        throw new InvalidEvaluationException("InvalidContent", nm.GetFileRange(ContentValue), ContentValue);
                                    }
                                    ContentLines = ContentValue.TableContent;
                                }

                                switch (f.Name.Text)
                                {
                                case "Primitive":
                                {
                                    var GenericParameters = new List <VariableDef>();

                                    foreach (var Line in ContentLines)
                                    {
                                        String   cName        = null;
                                        TypeSpec cType        = null;
                                        var      cAttributes  = new List <KeyValuePair <String, List <String> > >();
                                        var      cDescription = "";

                                        if (Line.Nodes.Count == 2)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                        }
                                        else if (Line.Nodes.Count == 3)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                            var c = OS.TokenParser.DecomposeDescription(GetLeafNodeValue(Line.Nodes[2], nm, "InvalidDescription"));
                                            cAttributes = c.Attributes;
                                            Mark(cAttributes, Line.Nodes[2]);
                                            cDescription = c.Description;
                                        }
                                        else if (Line.Nodes.Count == 0)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                        }

                                        if (cName.StartsWith("'"))
                                        {
                                            cName = new String(cName.Skip(1).ToArray());
                                            var gp = new VariableDef {
                                                Name = cName, Type = cType, Attributes = cAttributes, Description = cDescription, Attribute = null
                                            };
                                            Mark(gp, Line);
                                            GenericParameters.Add(gp);
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLine", nm.GetFileRange(Line), Line);
                                        }
                                    }

                                    var p = new PrimitiveDef {
                                        Name = Name, Attributes = Attributes, Description = Description
                                    };
                                    Mark(p, f);
                                    var t = TypeDef.CreatePrimitive(p);
                                    Mark(t, f);
                                    Types.Add(t);
                                    return(new List <TFSemantics.Node> {
                                        });
                                }

                                case "Entity":
                                {
                                    var Fields = new List <VariableDef>();

                                    foreach (var Line in ContentLines)
                                    {
                                        String   cName        = null;
                                        TypeSpec cType        = null;
                                        var      cAttributes  = new List <KeyValuePair <String, List <String> > >();
                                        var      cDescription = "";

                                        if (Line.Nodes.Count == 2)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                        }
                                        else if (Line.Nodes.Count == 3)
                                        {
                                            cName = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidFieldName");
                                            cType = ParseTypeSpec(Line.Nodes[1], nm, Positions);
                                            var c = OS.TokenParser.DecomposeDescription(GetLeafNodeValue(Line.Nodes[2], nm, "InvalidDescription"));
                                            cAttributes = c.Attributes;
                                            Mark(cAttributes, Line.Nodes[2]);
                                            cDescription = c.Description;
                                        }
                                        else if (Line.Nodes.Count == 0)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                        }

                                        var p = new VariableDef {
                                            Name = cName, Type = cType, Attributes = cAttributes, Description = cDescription
                                        };
                                        Mark(p, Line);
                                        Fields.Add(p);
                                    }

                                    var ed = new EntityDef {
                                        Name = Name, Fields = Fields, Attributes = Attributes, Description = Description
                                    };
                                    Mark(ed, f);
                                    var t = TypeDef.CreateEntity(ed);
                                    Mark(t, f);
                                    Types.Add(t);
                                    return(new List <TFSemantics.Node> {
                                        });
                                }

                                case "Enum":
                                {
                                    var Literals = new List <LiteralDef>();

                                    Int64 NextValue = 0;
                                    foreach (var Line in ContentLines)
                                    {
                                        String cName        = null;
                                        Int64  cValue       = NextValue;
                                        var    cAttributes  = new List <KeyValuePair <String, List <String> > >();
                                        var    cDescription = "";

                                        if (Line.Nodes.Count == 1)
                                        {
                                            cName  = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidLiteralName");
                                            cValue = NextValue;
                                        }
                                        else if (Line.Nodes.Count == 2)
                                        {
                                            cName  = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidLiteralName");
                                            cValue = NumericStrings.InvariantParseInt64(GetLeafNodeValue(Line.Nodes[1], nm, "InvalidLiteralValue"));
                                        }
                                        else if (Line.Nodes.Count == 3)
                                        {
                                            cName  = GetLeafNodeValue(Line.Nodes[0], nm, "InvalidLiteralName");
                                            cValue = NumericStrings.InvariantParseInt64(GetLeafNodeValue(Line.Nodes[1], nm, "InvalidLiteralValue"));
                                            var c = OS.TokenParser.DecomposeDescription(GetLeafNodeValue(Line.Nodes[2], nm, "InvalidDescription"));
                                            cAttributes = c.Attributes;
                                            Mark(cAttributes, Line.Nodes[2]);
                                            cDescription = c.Description;
                                        }
                                        else if (Line.Nodes.Count == 0)
                                        {
                                            continue;
                                        }
                                        else
                                        {
                                            throw new InvalidEvaluationException("InvalidLineNodeCount", nm.GetFileRange(Line), Line);
                                        }
                                        NextValue = cValue + 1;

                                        var ltl = new LiteralDef {
                                            Name = cName, Value = cValue, Attributes = cAttributes, Description = cDescription
                                        };
                                        Mark(ltl, Line);
                                        Literals.Add(ltl);
                                    }

                                    var r = (TypeRef)("Int");
                                    Mark(r, f);
                                    var UnderlyingType = TypeSpec.CreateTypeRef(r);
                                    Mark(UnderlyingType, f);
                                    var ed = new EnumDef {
                                        Name = Name, UnderlyingType = UnderlyingType, Literals = Literals, Attributes = Attributes, Description = Description
                                    };
                                    Mark(ed, f);
                                    var t = TypeDef.CreateEnum(ed);
                                    Mark(t, f);
                                    Types.Add(t);
                                    return(new List <TFSemantics.Node> {
                                        });
                                }

                                default:
                                {
                                    throw new InvalidEvaluationException("UnknownFunction", nm.GetFileRange(f), f);
                                }
                                }
                            }
                            else if (f.Name.Text == "Query")
                            {
                                if (f.Parameters.Count != 0)
                                {
                                    throw new InvalidEvaluationException("InvalidParameterCount", nm.GetFileRange(f), f);
                                }

                                var Verbs = new Dictionary <String, Func <Verb> >
                                {
                                    { "Select", () => Verb.CreateSelect() },
                                    { "Lock", () => Verb.CreateLock() },
                                    { "Insert", () => Verb.CreateInsert() },
                                    { "Update", () => Verb.CreateUpdate() },
                                    { "Upsert", () => Verb.CreateUpsert() },
                                    { "Delete", () => Verb.CreateDelete() }
                                };
                                var Numerals = new Dictionary <String, Func <Numeral> >
                                {
                                    { "Optional", () => Numeral.CreateOptional() },
                                    { "One", () => Numeral.CreateOne() },
                                    { "Many", () => Numeral.CreateMany() },
                                    { "All", () => Numeral.CreateAll() },
                                    { "Range", () => Numeral.CreateRange() },
                                    { "Count", () => Numeral.CreateCount() }
                                };
                                Func <int, TextLine, List <QueryDef> > ParseQueryDef = (IndentLevel, Line) =>
                                {
                                    var l = new List <TFSemantics.Node>();
                                    List <TFSemantics.Node> cl      = null;
                                    TextPosition            clStart = default(TextPosition);
                                    TextPosition            clEnd   = default(TextPosition);
                                    if (Line.Text.Length < IndentLevel * 4)
                                    {
                                        return(new List <QueryDef> {
                                        });
                                    }
                                    var LineRange = new TextRange {
                                        Start = Text.Calc(Line.Range.Start, IndentLevel * 4), End = Line.Range.End
                                    };
                                    var Range = LineRange;
                                    while (true)
                                    {
                                        var tpr = TreeFormatTokenParser.ReadToken(Text, pr.Positions, Range);
                                        if (!tpr.OnSome)
                                        {
                                            break;
                                        }

                                        var v = tpr.Value.Token;
                                        if (v.OnSingleLineComment)
                                        {
                                            break;
                                        }
                                        if (v.OnLeftParenthesis)
                                        {
                                            if (cl != null)
                                            {
                                                throw new InvalidTokenException("DoubleLeftParenthesis", new FileTextRange {
                                                    Text = Text, Range = Range
                                                }, "(");
                                            }
                                            cl      = new List <TFSemantics.Node>();
                                            clStart = Range.Start;
                                            clEnd   = Range.End;
                                        }
                                        else if (v.OnRightParenthesis)
                                        {
                                            if (cl == null)
                                            {
                                                throw new InvalidTokenException("DismatchedRightParenthesis", new FileTextRange {
                                                    Text = Text, Range = Range
                                                }, ")");
                                            }
                                            if (cl.Count == 0)
                                            {
                                                throw new InvalidTokenException("EmptyIndex", new FileTextRange {
                                                    Text = Text, Range = Range
                                                }, ")");
                                            }
                                            if (tpr.Value.RemainingChars.OnSome)
                                            {
                                                clEnd = tpr.Value.RemainingChars.Value.End;
                                            }
                                            l.Add(nm.MakeStemNode("", cl, new TextRange {
                                                Start = clStart, End = clEnd
                                            }));
                                            cl      = null;
                                            clStart = default(TextPosition);
                                            clEnd   = default(TextPosition);
                                        }
                                        else if (v.OnSingleLineLiteral)
                                        {
                                            if (cl != null)
                                            {
                                                cl.Add(nm.MakeLeafNode(v.SingleLineLiteral, pr.Positions[v]));
                                            }
                                            else
                                            {
                                                l.Add(nm.MakeLeafNode(v.SingleLineLiteral, pr.Positions[v]));
                                            }
                                        }
                                        else
                                        {
                                            throw new InvalidTokenException("UnknownToken", new FileTextRange {
                                                Text = Text, Range = Range
                                            }, Text.GetTextInLine(Range));
                                        }

                                        if (!tpr.Value.RemainingChars.OnSome)
                                        {
                                            break;
                                        }

                                        Range = tpr.Value.RemainingChars.Value;
                                    }
                                    if (cl != null)
                                    {
                                        throw new InvalidTokenException("DismatchedRightParentheses", new FileTextRange {
                                            Text = Text, Range = Range
                                        }, "");
                                    }

                                    if (l.Count == 0)
                                    {
                                        return(new List <QueryDef> {
                                        });
                                    }

                                    if (l.Count != 4 && l.Count != 6 && l.Count != 8)
                                    {
                                        throw new InvalidSyntaxException("InvalidQuery", new FileTextRange {
                                            Text = Text, Range = LineRange
                                        });
                                    }
                                    var From = GetLeafNodeValue(l[0], nm, "InvalidFrom");
                                    if (From != "From")
                                    {
                                        throw new InvalidTokenException("InvalidFrom", nm.GetFileRange(l[0]), From);
                                    }
                                    var EntityName = GetLeafNodeValue(l[1], nm, "InvalidEntityName");
                                    var VerbName   = GetLeafNodeValue(l[2], nm, "InvalidVerb");
                                    if (!Verbs.ContainsKey(VerbName))
                                    {
                                        throw new InvalidTokenException("InvalidVerb", nm.GetFileRange(l[2]), VerbName);
                                    }
                                    var Verb = Verbs[VerbName]();
                                    Mark(Verb, l[2]);
                                    var NumeralName = GetLeafNodeValue(l[3], nm, "InvalidNumeral");
                                    if (!Numerals.ContainsKey(NumeralName))
                                    {
                                        throw new InvalidTokenException("InvalidNumeral", nm.GetFileRange(l[3]), NumeralName);
                                    }
                                    var Numeral = Numerals[NumeralName]();
                                    Mark(Numeral, l[3]);

                                    var By      = new List <String> {
                                    };
                                    var OrderBy = new List <KeyColumn> {
                                    };

                                    if (l.Count >= 6)
                                    {
                                        var ByOrOrderByName = GetLeafNodeValue(l[4], nm, "InvalidByOrOrderBy");
                                        if (ByOrOrderByName == "By")
                                        {
                                            if (l[5].OnLeaf)
                                            {
                                                By = new List <String> {
                                                    l[5].Leaf
                                                };
                                            }
                                            else if (l[5].OnStem)
                                            {
                                                By = l[5].Stem.Children.Select(c => GetLeafNodeValue(c, nm, "InvalidKeyColumn")).ToList();
                                            }
                                            else
                                            {
                                                throw new InvalidSyntaxException("InvalidBy", nm.GetFileRange(l[5]));
                                            }
                                            Mark(By, l[5]);
                                        }
                                        else if (ByOrOrderByName == "OrderBy")
                                        {
                                            if (l[5].OnLeaf)
                                            {
                                                OrderBy = (new List <String> {
                                                    l[5].Leaf
                                                }).Select(c => c.EndsWith("-") ? new KeyColumn {
                                                    Name = c.Substring(0, c.Length - 1), IsDescending = true
                                                } : new KeyColumn {
                                                    Name = c, IsDescending = false
                                                }).ToList();
                                            }
                                            else if (l[5].OnStem)
                                            {
                                                OrderBy = l[5].Stem.Children.Select(c => GetLeafNodeValue(c, nm, "InvalidKeyColumn")).Select(c => c.EndsWith("-") ? new KeyColumn {
                                                    Name = c.Substring(0, c.Length - 1), IsDescending = true
                                                } : new KeyColumn {
                                                    Name = c, IsDescending = false
                                                }).ToList();
                                            }
                                            else
                                            {
                                                throw new InvalidSyntaxException("InvalidOrderBy", nm.GetFileRange(l[5]));
                                            }
                                            Mark(OrderBy, l[5]);
                                        }
                                        else
                                        {
                                            throw new InvalidSyntaxException("InvalidByOrOrderBy", nm.GetFileRange(l[5]));
                                        }
                                    }
                                    if (l.Count >= 8)
                                    {
                                        if (OrderBy.Count != 0)
                                        {
                                            throw new InvalidSyntaxException("InvalidOrderBy", nm.GetFileRange(l[6]));
                                        }
                                        var OrderByName = GetLeafNodeValue(l[6], nm, "InvalidOrderBy");
                                        if (OrderByName == "OrderBy")
                                        {
                                            if (l[7].OnLeaf)
                                            {
                                                OrderBy = (new List <String> {
                                                    l[7].Leaf
                                                }).Select(c => c.EndsWith("-") ? new KeyColumn {
                                                    Name = c.Substring(0, c.Length - 1), IsDescending = true
                                                } : new KeyColumn {
                                                    Name = c, IsDescending = false
                                                }).ToList();
                                            }
                                            else if (l[7].OnStem)
                                            {
                                                OrderBy = l[7].Stem.Children.Select(c => GetLeafNodeValue(c, nm, "InvalidKeyColumn")).Select(c => c.EndsWith("-") ? new KeyColumn {
                                                    Name = c.Substring(0, c.Length - 1), IsDescending = true
                                                } : new KeyColumn {
                                                    Name = c, IsDescending = false
                                                }).ToList();
                                            }
                                            else
                                            {
                                                throw new InvalidSyntaxException("InvalidOrderBy", nm.GetFileRange(l[7]));
                                            }
                                            Mark(OrderBy, l[7]);
                                        }
                                        else
                                        {
                                            throw new InvalidSyntaxException("InvalidOrderBy", nm.GetFileRange(l[7]));
                                        }
                                    }

                                    var q = new QueryDef {
                                        EntityName = EntityName, Verb = Verb, Numeral = Numeral, By = By, OrderBy = OrderBy
                                    };
                                    Mark(q, Line);
                                    return(new List <QueryDef> {
                                        q
                                    });
                                };

                                var Queries = f.Content.Value.LineContent.Lines.SelectMany(Line => ParseQueryDef(f.Content.Value.LineContent.IndentLevel, Line)).ToList();
                                Mark(Queries, f);
                                var ql = new QueryListDef {
                                    Queries = Queries
                                };
                                Mark(ql, f);
                                var t = TypeDef.CreateQueryList(ql);
                                Mark(t, f);
                                Types.Add(t);
                                return(new List <TFSemantics.Node> {
                                });
                            }
                            else
                            {
                                throw new InvalidEvaluationException("UnknownFunction", nm.GetFileRange(f), f);
                            }
                        }
                    };
                    var e = new TreeFormatEvaluator(es, pr);
                    e.Evaluate();
                }
                else
                {
                    var pr = new TreeFormatParseResult
                    {
                        Value = new Forest {
                            MultiNodesList = new List <MultiNodes> {
                                TopNode
                            }
                        },
                        Text             = Text,
                        Positions        = ParserResult.Positions,
                        RawFunctionCalls = ParserResult.RawFunctionCalls
                    };
                    var es = new TreeFormatEvaluateSetting {
                    };
                    var e  = new TreeFormatEvaluator(es, pr);
                    var er = e.Evaluate();
                    if (er.Value.Nodes.Count > 0)
                    {
                        var ReadResult = ts.Read <Schema>(CollectionOperations.CreatePair(er.Value, er.Positions));
                        Types.AddRange(ReadResult.Key.Types);
                        TypeRefs.AddRange(ReadResult.Key.TypeRefs);
                        Imports.AddRange(ReadResult.Key.Imports);
                        foreach (var p in ReadResult.Value)
                        {
                            if (p.Value.Range.OnSome)
                            {
                                Positions.Add(p.Key, p.Value.Range.Value);
                            }
                        }
                    }
                }
            }

            var Schema = new Schema {
                Types = Types, TypeRefs = TypeRefs, Imports = Imports
            };

            return(new FileParserResult {
                Schema = Schema, Text = Text, Positions = Positions
            });
        }