예제 #1
0
        public override SyntaxError Check(string context)
        {
            SyntaxError check = base.Check(context);

            if (check == SyntaxError.NoError)
            {
                Elements.Elements.RemoveAt(3);
            }
            this.Context = Elements.Context;
            return(check);
        }
예제 #2
0
파일: Compiler.cs 프로젝트: HTCGS/Compiler
        public SyntaxError CheckSyntax()
        {
            int line = 0;

            foreach (var item in Syntax)
            {
                line++;
                SyntaxError syntaxError = item.Check();
                if (syntaxError != SyntaxError.NoError)
                {
                    Console.WriteLine(syntaxError + " at line " + line);
                    return(syntaxError);
                }
            }
            return(SyntaxError.NoError);
        }
예제 #3
0
        public ISyntaxTree GetSyntaxTree()
        {
            WriteTree        tree             = new WriteTree();
            ExpressionParser expressionParser = new ExpressionParser(Line[0].Element);
            SyntaxError      syntaxError      = expressionParser.Check();

            if (syntaxError == SyntaxError.NoError)
            {
                expressionParser.Normalize();
                tree.Childs.Add(expressionParser.GetSyntaxTree());
            }
            else
            {
                Console.WriteLine(syntaxError);
                return(null);
            }
            return(tree);
        }
예제 #4
0
        private bool CheckNext(string line, int pos, int index, string element)
        {
            SyntaxError nextCheck   = SyntaxError.SyntaxError;
            string      tmpElement  = string.Empty;
            string      nextElement = string.Empty;

            foreach (string item in Syntax[index].Elements)
            {
                if (line != string.Empty)
                {
                    if (item.Length <= line.Length && pos + item.Length <= line.Length)
                    {
                        nextElement = line.Substring(pos, item.Length);
                    }
                }
                nextCheck = Syntax[index].Check(nextElement);
                if (nextCheck == SyntaxError.NoError)
                {
                    int from = element.Length + ElementMinPosition(index - 1, index) - 1;
                    int to   = line.Length - ElementMaxPosition(index);
                    if (to != line.Length - 1)
                    {
                        from--;
                    }

                    if (pos > from && pos <= to)
                    {
                        tmpElement = nextElement;
                    }
                    if (from == to && pos == from)
                    {
                        tmpElement = nextElement;
                    }
                }
            }
            if (tmpElement == string.Empty)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
예제 #5
0
파일: Compiler.cs 프로젝트: HTCGS/Compiler
 public void MakeSyntaxTree()
 {
     Parser.Syntax = this.Syntax;
     Parser.Check();
     foreach (var item in Parser.ParserElements)
     {
         SyntaxError syntaxError = item.Check();
         if (syntaxError == SyntaxError.NoError)
         {
             item.Normalize();
             Programs.Add(item.GetSyntaxTree());
         }
         else
         {
             Console.WriteLine(syntaxError);
             return;
         }
     }
 }
예제 #6
0
        public override SyntaxError Check()
        {
            string line = this.Context;

            line = line.Replace(" ", string.Empty);
            int startIndex = 0;

            if (Elements.ElementCount != 0)
            {
                line       = line.Remove(0, Elements[0].Length);
                startIndex = 1;
            }
            else
            {
                startIndex = 0;
            }

            int         i     = 0;
            SyntaxError check = SyntaxError.SyntaxError;

            for (i = startIndex; i < Syntax.Count; i++)
            {
                string element = "";
                if (Syntax[i] is SyntaxObject)
                {
                    check = Syntax[i].Check(line);
                    if (check != SyntaxError.NoError)
                    {
                        break;
                    }
                }
                else
                {
                    if (Syntax[i].Elements.SignCount != 0)
                    {
                        string tmpElement = string.Empty;
                        foreach (string item in Syntax[i].Elements)
                        {
                            if (line != string.Empty)
                            {
                                if (item.Length <= line.Length)
                                {
                                    tmpElement = line.Substring(0, item.Length);
                                }
                            }
                            check = Syntax[i].Check(tmpElement);
                            if (check == SyntaxError.NoError)
                            {
                                element = tmpElement;
                            }
                        }
                        if (element == string.Empty)
                        {
                            break;
                        }
                    }
                    else
                    {
                        //if (i == Syntax.Count - 1)
                        //{
                        //    element = line;
                        //    check = Syntax[i].Check(element);
                        //    if (check != SyntaxError.NoError) break;
                        //}
                        //else
                        {
                            bool isFound = false;
                            for (int j = 1; j <= line.Length; j++)
                            {
                                element = line.Substring(0, j);
                                check   = Syntax[i].Check(element);
                                if (check != SyntaxError.NoError)
                                {
                                    element = element.Remove(element.Length - 1, 1);
                                    break;
                                }

                                if ((i + 1) < Syntax.Count)
                                {
                                    if (Syntax[i + 1] is SyntaxObject)
                                    {
                                        string nextLine = line.Substring(element.Length, line.Length - element.Length);
                                        if (Syntax[i + 1].Check(nextLine) != SyntaxError.NoError)
                                        {
                                            isFound = false;
                                        }
                                        else
                                        {
                                            isFound = true;
                                        }
                                        (Syntax[i + 1] as SyntaxObject).ClearElements();
                                    }
                                    else
                                    {
                                        if (Syntax[i + 1].Elements.SignCount != 0)
                                        {
                                            isFound = CheckNext(line, j, i + 1, element);
                                        }
                                    }
                                }
                                if (isFound)
                                {
                                    break;
                                }
                                if (j >= line.Length - (Syntax.Count - i - 1))
                                {
                                    break;
                                }
                            }
                            if (i != Syntax.Count - 1)
                            {
                                if (!isFound)
                                {
                                    check = SyntaxError.SyntaxError;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (Syntax[i] is SyntaxObject)
                {
                    Elements.Add(Syntax[i] as SyntaxObject);
                }
                else
                {
                    if (Syntax[i] is AbstractSyntaxObject)
                    {
                        Elements.Add(element);
                    }
                }
                line = line.Remove(0, Elements[Elements.ElementCount - 1].Length);
            }

            if (check != SyntaxError.NoError)
            {
                for (int n = 0; n < Syntax.Count; n++)
                {
                    SyntaxObject syntaxObject = Activator.CreateInstance(this.GetType()) as SyntaxObject;
                    syntaxObject.Context = this.Context;
                    syntaxObject.Syntax.Clear();
                    syntaxObject.Syntax.AddRange(this.Syntax);
                    int nullablePos = GetNextNullableElement();
                    if (nullablePos != -1)
                    {
                        syntaxObject.Syntax.RemoveAt(nullablePos);
                        check = syntaxObject.Check();
                        if (check == SyntaxError.NoError)
                        {
                            AddNewSyntax(syntaxObject);
                            line = string.Empty;
                            break;
                        }
                        //else return check;
                    }
                    else
                    {
                        return(check);
                    }
                }
            }

            this.Context = Elements.Context;

            //if (line != string.Empty || !IsFulSyntax()) return SyntaxError.SyntaxError;
            if (!IsFulSyntax())
            {
                return(SyntaxError.SyntaxError);
            }
            return(SyntaxError.NoError);
        }