Пример #1
0
        internal RecursiveCodeLog Analyse(SyntaxGroup code)
        {
            Assignee = code.Component.text;
            Operator = code.Operator.text;
            Line     = code.Component.line;
            //If we can detect at least one code block
            List <SyntaxGroup> list = code.Operand as List <SyntaxGroup>;

            if (list != null)
            {
                try
                {
                    CodeBlock        block = new CodeBlock(Level + 1);
                    RecursiveCodeLog log   = block.Analyse(list);
                    if (log != null)
                    {
                        log.AddToRecursiveChain("Error during analysis chain",
                                                Assignee, Line.ToString());
                        return(log);
                    }
                    Value = block;
                }
                catch (Exception)
                {
                    //TODO: Add language support
                    RecursiveCodeLog log = new RecursiveCodeLog();
                    log.AddToRecursiveChain("Impossible to analyze associated code",
                                            Assignee, Line.ToString());
                    return(log);
                }
            }
            //If we get pure text
            else if (code.Operand is Token)
            {
                Value = new CodeValue(((Token)code.Operand).text);
            }
            //If we got a list of tokens, a chain of pure text
            else if (code.Operand is List <Token> )
            {
                Value = new CodeBlock();
                ((CodeBlock)Value).Analyse((List <Token>)code.Operand);
            }
            return(null);
        }
Пример #2
0
        public Script GetContentAsScript(string[] except, Dictionary <int, string> Comments = null)
        {
            Script newScript = new Script();

            if (Value is CodeBlock)
            {
                foreach (ICodeStruct codeStruct in ((CodeBlock)Value).Code)
                {
                    Assignation item = (Assignation)codeStruct;
                    if (!except.Contains(item.Assignee))
                    {
                        newScript.Code.Add(item);
                    }
                }
            }
            else
            {
                //TODO: Add language support
                RecursiveCodeLog log = new RecursiveCodeLog();
                log.AddToRecursiveChain("Impossible to obtain content, assigned value is not code",
                                        Assignee, Line.ToString());
                newScript.Logger.Errors.Add(new SyntaxError(log.Message));
            }
            //If no comments are given
            if (Comments == null)
            {
                return(newScript);
            }
            Assignation firstLine = newScript.Code.Where(i => i is Assignation)
                                    .OrderBy(i => ((Assignation)i).Line).FirstOrDefault() as Assignation;
            Assignation lastLine = newScript.Code.Where(i => i is Assignation)
                                   .OrderBy(i => ((Assignation)i).Line).LastOrDefault() as Assignation;

            //If no lines were found
            if (firstLine == null || lastLine == null)
            {
                return(newScript);
            }
            //Try to get the comments
            newScript.Comments = Comments.SkipWhile(c => c.Key < firstLine.Line)
                                 .TakeWhile(c => c.Key <= lastLine.Line)
                                 .ToDictionary(c => c.Key, c => c.Value);
            return(newScript);
        }
Пример #3
0
        internal RecursiveCodeLog Analyse(List <SyntaxGroup> code)
        {
            if (!code.Any())
            {
                return(null);
            }
            foreach (SyntaxGroup group in code)
            {
                //Check if there is an assignation or code value at the end of the code
                Assignation last = Code.LastOrDefault() as Assignation;
                if (last != null)
                {
                    //set its end line as this line's end - 1
                    last.EndLine = group.Component.line - 1;
                }
                Assignation      tempo = new Assignation(Level + 1);
                RecursiveCodeLog log   = tempo.Analyse(group);
                if (log != null)
                {
                    return(log);
                }
                //If tempo has a value
                if (!string.IsNullOrEmpty(tempo.Assignee))
                {
                    Code.Add(tempo);
                }
            }
            //Check if there is an assignation or code value at the end of the code
            Assignation Verylast = Code.LastOrDefault() as Assignation;

            if (Verylast != null)
            {
                //set its end line as this line's end - 1
                Verylast.EndLine = code.Last().Component.line - 1;
            }
            return(null);
        }
        public void Analyse(string code, int line = -1)
        {
            Code.Clear();
            Tokenizer          tokenizer = new Tokenizer();
            List <SyntaxGroup> list      =
                tokenizer.GroupTokensByBlocks(tokenizer.Tokenize(code, this))
                as List <SyntaxGroup>;

            //Set this logger as the tokenizer logger
            Logger.Errors = tokenizer.Logger.Errors;
            //Check if there are errors.
            if (Logger.hasErrors())
            {
                return;
            }
            //Return if list is null
            if (list == null || !list.Any())
            {
                return;
            }
            foreach (SyntaxGroup group in list)
            {
                try
                {
                    //Check if there is an assignation or code value at the end of the code
                    Assignation last = Code.LastOrDefault() as Assignation;
                    if (last != null)
                    {
                        //set its end line as this line's end - 1
                        last.EndLine = group.Component.line - 1;
                    }
                    Assignation      tempo = new Assignation(1);
                    RecursiveCodeLog log   = tempo.Analyse(group);
                    if (log != null)
                    {
                        Logger.Errors.Add(new SyntaxError(log.Message));
                        //We don't want to add broken code if possible
                        continue;
                    }
                    Code.Add(tempo);
                }
                catch (Exception)
                {
                    //TODO: Add language support
                    Logger.Errors.Add(new SyntaxError("Unknown error in script"));
                }
            }
            //Check if there is an assignation or code value at the end of the code
            Assignation Verylast = Code.LastOrDefault() as Assignation;

            if (Verylast != null)
            {
                //Check if the last is Empty
                if (list.Last().Component.line == Verylast.Line)
                {
                    //Empty block, set the end line as starting line + 2
                    Verylast.EndLine = Verylast.Line + 2;
                }
                else
                {
                    //Set its end line as this line's end - 1
                    Verylast.EndLine = list.Last().Component.line - 1;
                }
            }
        }