Esempio n. 1
0
        public void Test_Reading_A_Tree_With_A_Null_BinaryReader()
        {
            Action action = () => treeReader.Read(null, Substitute.For <IDataNode>());

            action
            .Should()
            .ThrowArgumentNullException("binaryReader");
        }
Esempio n. 2
0
        public List <TreeElement> GetTree()
        {
            _tokens = new List <TreeElement>();

            while (!_reader.IsEmpty())
            {
                var token = GetTreeElement(_reader.Read());
                if (token == null)
                {
                    break;
                }
                _tokens.Add(token);
            }

            return(_tokens);
        }
Esempio n. 3
0
        public string GetCode()
        {
            var ns =
                _file.Replace('\\', '_').Replace('/', '_').Replace(".tengri", "");

            _code += "namespace FILE_TENGRI_" + ns + " {";

            if (_inClass)
            {
                _code = "";
            }

            while (!_reader.IsEmpty())
            {
                var part = GetPart(_reader.Read());
                if (part == null)
                {
                    break;
                }

                _code += part;
            }

            if (!_inClass)
            {
                _code += "}";
                var code = _code;
                _code = "\t/* GENERATED BY TENGRI TRANSLATOR */\n// =============================================== //\n\n\n";

                foreach (var needNamespace in NeedNamespaces)
                {
                    _code += $"using {needNamespace};\n";
                }

                _code += $"\n{code}";
            }

            return(_code);
        }
Esempio n. 4
0
        public TreeElement ParseElement(TreeBuilder builder, TreeReader reader)
        {
            if (reader.Read(1) is SpecialLexeme specialLexeme && specialLexeme.Value == "{" && IsFuncBrackets)
            {
                reader.Next(2);
                var args = new List <List <TreeElement> >()
                {
                    new List <TreeElement>()
                };

                foreach (var treeElement in Block)
                {
                    if (treeElement is SpecialLexeme special && special.Value == ",")
                    {
                        args.Add(new List <TreeElement>());
                    }
Esempio n. 5
0
        public TreeElement ParseElement(TreeBuilder builder, TreeReader reader)
        {
            switch (Value)
            {
            case "@":
                if (reader.Read() is VariableLexeme variableLexeme)
                {
                    if (!builder.AvailableAttributes.Split(' ').Contains(variableLexeme.Value))
                    {
                        variableLexeme.Exception($"Unknown attribute \"{variableLexeme.Value}\"");
                    }

                    reader.Next();
                    return(new AttributeElement(variableLexeme));
                }

                return(null);

            case "{":
                var blockClass = new BlockElement(this, builder.ParseInBrackets('{', '}')[0]);
                return(blockClass.ParseElement(builder, reader));

            case "[":
                return(ParseArray(builder, reader));

            case ",": return(this);

            case "(":
                var block = new BlockElement(this, builder.ParseInBrackets('(', ')')[0])
                {
                    IsFuncBrackets = true
                };
                return(block.ParseElement(builder, reader));

            default: return(null);
            }
        }
Esempio n. 6
0
        public TreeElement ParseElement(TreeBuilder builder, TreeReader reader)
        {
            if (reader.Read() is SpecialLexeme specialLexeme)
            {
                if (specialLexeme.Value == "." || specialLexeme.Value == "[")
                {
                    Args = ParseArrayBrackets(builder, reader);
                }
            }

            // Этот иф нужен, так как в прошлом могли оказаться скобки от массива
            if (reader.Read() is SpecialLexeme newSpecialLexeme)
            {
                if (newSpecialLexeme.Value == "{")
                {
                    reader.Next();
                    var block = builder.ParseInBrackets('{', '}');
                    reader.Next();

                    return(new InvokeElement("it", this, block[0]));
                }

                if (newSpecialLexeme.Value == ":")
                {
                    if (reader.Read(1) is VariableLexeme variableLexeme && reader.Read(2) is SpecialLexeme special &&
                        special.Value == "{")
                    {
                        if (variableLexeme.Args.Count > 0)
                        {
                            Exception("Wrong var name");
                        }
                        reader.Next(3);
                        var block = builder.ParseInBrackets('{', '}');
                        reader.Read();

                        return(new InvokeElement(variableLexeme.Value, this, block[0]));
                    }
                    else
                    {
                        FuncType type = builder.GetType(Value, builder.ParseAttributes());
                        reader.Next();

                        var body = builder.ParseToNewLine();

                        var declareField = new DeclareFieldElement(this, body, type);
                        var response     = declareField.ParseElement(builder, reader);
                        return(response ?? declareField);
                    }
                }

                if (newSpecialLexeme.Value == "(")
                {
                    reader.Next();
                    var args = builder.ParseInBrackets('(', ')', ',');
                    reader.Next();

                    var callFunction = new CallFunctionElement(this, args);
                    var response     = callFunction.ParseElement(builder, reader);
                    return(response ?? callFunction);
                }
            }
Esempio n. 7
0
        public TreeElement ParseElement(TreeBuilder builder, TreeReader reader)
        {
            switch (Value)
            {
            case "for":
            case "while":
            case "do":
                return(ParseLoop(builder, reader));

            case "if":
                return(ParseIf(builder, reader));

            case "return":
                return(new ReturnElement(this, builder.ParseToNewLine()));

            case "try":
                reader.Next();
                var tryBlock = builder.ParseInBrackets('{', '}')[0];
                reader.Next();

                List <TreeElement> catchBlock   = new List <TreeElement>();
                List <TreeElement> finallyBlock = new List <TreeElement>();

                VariableLexeme variableLexeme = null;

                if (reader.Read() is KeywordLexeme keywordLexeme && keywordLexeme.Value == "catch")
                {
                    reader.Next();

                    if (reader.Read() is SpecialLexeme specialLexeme && specialLexeme.Value == ":" &&
                        reader.Read(1) is VariableLexeme variable)
                    {
                        if (variable.Args.Count > 0)
                        {
                            Exception("Wrong name var in catch block");
                        }
                        variableLexeme = variable;
                    }

                    reader.Next(2);
                    catchBlock = builder.ParseInBrackets('{', '}')[0];
                    reader.Next();
                }

                if (reader.Read() is KeywordLexeme finallyKeyword && finallyKeyword.Value == "finally")
                {
                    reader.Next();
                    finallyBlock = builder.ParseInBrackets('{', '}')[0];
                    reader.Next();
                }

                return(new TryElement(this, variableLexeme, tryBlock, catchBlock, finallyBlock));

            case "import":
            {
                var block = builder.ParseToNewLine();
                if (block.Count != 1)
                {
                    Exception("Wrong import!");
                }
                if (block[0] is StringLexeme lexeme)
                {
                    return(new ImportElement(this, lexeme.Value));
                }

                Exception("Wrong import!");
                break;
            }

            case "export":
                return(new ExportElement(this, builder.ParseToNewLine()));

            case "fun" when reader.Read() is VariableLexeme name:
                return(ParseFun(name, builder, reader));
            }

            return(this);
        }