Пример #1
0
        internal override Syntax_Node ParseLex(Lex.Data data)
        {
            switch (data.token)
            {
            case Token.Keyword:
            {
                var keyword = (Keyword)data.number.i_32;
                switch (keyword)
                {
                case Keyword.Interface:
                    return(AddChild(Syntax_Node.Create <Syntax_Interface>(data, this)));

                case Keyword.Class:
                    return(AddChild(Syntax_Node.Create <Syntax_Class>(data, this)));

                default:
                {
                    var exception = new SyntaxException(Syntax.Error.InvalidKeyword);
                    exception.content = keyword.ToString();
                    throw exception;
                }
                }
            }

            case Token.Identify:
                return(AddChild(Syntax_Node.Create <Syntax_Function>(data, this)));

            default:
            {
                var exception = new SyntaxException(Syntax.Error.InvalidToken);
                exception.content = data.token.ToString();
                throw exception;
            }
            }
        }
Пример #2
0
        internal override Syntax_Node ParseLex(Lex.Data data)
        {
            switch (phase)
            {
            case Phase.ConditionBegin:
                if (Token.Operator == data.token &&
                    Operator.S_Brackets_Beg == (Operator)data.number.i_32)
                {
                    phase = Phase.Condition;
                    return(this);
                }
                else
                {
                    var exception = new SyntaxException(Syntax.Error.InvalidFunction);
                    exception.content = "Need\"(\"";
                    throw exception;
                }

            case Phase.Condition:
                if (Token.Operator == data.token &&
                    Operator.S_Brackets_End == (Operator)data.number.i_32)
                {
                    phase = Phase.Code;
                    return(this);
                }
                // TODO
                break;

            case Phase.Code:
                if (Token.Keyword == data.token)
                {
                    var keyword = (Keyword)data.number.i_32;
                    switch (keyword)
                    {
                    case Keyword.Elseif:
                        phase = Phase.ConditionBegin;
                        return(this);

                    case Keyword.Else:
                        phase = Phase.LastCode;
                        return(this);

                    case Keyword.End:
                        return(parent_);
                    }
                }
                break;

            case Phase.LastCode:
                if (Token.Keyword == data.token &&
                    Keyword.End == (Keyword)data.number.i_32)
                {
                    return(parent_);
                }
                break;
            }
            return(base.ParseLex(data));
        }
Пример #3
0
 internal static void Log_SyntaxException(SyntaxException e)
 {
     Console.ForegroundColor = ConsoleColor.Red;
     Console.Write(String.Format("SyntaxException: {0}, {1}\n at [{2},{3}]: ", e.errorCode.ToString(), e.content, e.row, e.col));
     if (null != e.line && 0 < e.col && e.line.Length >= e.col)
     {
         Console.ForegroundColor = ConsoleColor.Black;
         Console.Write(e.line.Substring(0, e.col - 1));
         Console.ForegroundColor = ConsoleColor.Red;
         Console.Write(e.line[e.col - 1]);
         Console.ForegroundColor = ConsoleColor.Black;
         Console.WriteLine(e.line.Substring(e.col));
     }
 }
Пример #4
0
        public static void ParseStream(StreamReader reader, string path = null)
        {
            var root = Syntax_Node.Create <Syntax_File>();

            root.path = path;
            var context = new Context(root);

            Lex.ParseStream(reader, LexDataReceiver, context);
            if (!(context.node is Syntax_File))
            {
                var exception = new SyntaxException(Error.NotComplete);
                exception.content = context.node.ToString();
                throw exception;
            }
        }
Пример #5
0
        internal override Syntax_Node ParseLex(Lex.Data data)
        {
            switch (phase)
            {
            case Phase.ParamsBegin:
                if (Token.Operator == data.token &&
                    Operator.S_Brackets_Beg == (Operator)data.number.i_32)
                {
                    phase = Phase.ParamType;
                    return(this);
                }
                else
                {
                    var exception = new SyntaxException(Syntax.Error.InvalidFunction);
                    exception.content = "Need\"(\"";
                    throw exception;
                }

            case Phase.ParamType:
                switch (data.token)
                {
                case Token.Operator:
                    if (Operator.S_Brackets_End == (Operator)data.number.i_32)
                    {
                        phase = Phase.Code;
                        return(this);
                    }
                    else
                    {
                        var exception = new SyntaxException(Syntax.Error.InvalidOperator);
                        exception.content = ((Operator)data.number.i_32).ToString();
                        throw exception;
                    }

                case Token.Keyword:
                    if (Syntax.typeKeywordList.Contains((Keyword)data.number.i_32))
                    {
                        paramType_ = data;
                        phase      = Phase.ParamName;
                        return(this);
                    }
                    else
                    {
                        var exception = new SyntaxException(Syntax.Error.InvalidKeyword);
                        exception.content = ((Keyword)data.number.i_32).ToString();
                        throw exception;
                    }

                case Token.Identify:
                    paramType_ = data;
                    phase      = Phase.ParamName;
                    return(this);

                default:
                {
                    var exception = new SyntaxException(Syntax.Error.InvalidToken);
                    exception.content = data.token.ToString();
                    throw exception;
                }
                }

            case Phase.ParamName:
                if (Token.Identify == data.token)
                {
                    if (null == paramList)
                    {
                        paramList = new List <KeyValuePair <Lex.Data, Lex.Data> >();
                    }
                    paramList.Add(new KeyValuePair <Lex.Data, Lex.Data>(paramType_, data));
                    paramType_ = null;
                    phase      = Phase.ParamSeparator;
                    return(this);
                }
                else
                {
                    var exception = new SyntaxException(Syntax.Error.InvalidToken);
                    exception.content = data.token.ToString();
                    throw exception;
                }

            case Phase.ParamSeparator:
                if (Token.Operator == data.token)
                {
                    var op = (Operator)data.number.i_32;
                    switch (op)
                    {
                    case Operator.L_Brackets_End:
                        phase = Phase.Code;
                        return(this);

                    case Operator.Comma:
                        phase = Phase.ParamType;
                        return(this);

                    default:
                    {
                        var exception = new SyntaxException(Syntax.Error.InvalidOperator);
                        exception.content = op.ToString();
                        throw exception;
                    }
                    }
                }
                else
                {
                    var exception = new SyntaxException(Syntax.Error.InvalidToken);
                    exception.content = data.token.ToString();
                    throw exception;
                }

            case Phase.Code:
                if (Token.Keyword == data.token &&
                    Keyword.End == (Keyword)data.number.i_32)
                {
                    return(parent_);
                }
                break;
            }
            return(base.ParseLex(data));
        }
Пример #6
0
        internal override Syntax_Node ParseLex(Lex.Data data)
        {
            switch (codePhase)
            {
            case CodePhase.Code:
                // TODO parse code
                switch (data.token)
                {
                case Token.Keyword:
                {
                    var keyword = (Keyword)data.number.i_32;
                    switch (keyword)
                    {
                    case Keyword.If:
                        return(AddChild(Syntax_Node.Create <Syntax_If>(data, this)));

                    case Keyword.Loop:
                        return(AddChild(Syntax_Node.Create <Syntax_Loop>(data, this)));

                    case Keyword.Block:
                        return(AddChild(Syntax_Node.Create <Syntax_Block>(data, this)));

                    case Keyword.Return:
                        codePhase = CodePhase.Return;
                        return(this);

                    default:
//							{
//								var exception = new SyntaxException(Syntax.Error.InvalidKeyword);
//								exception.content = keyword.ToString();
//								throw exception;
//							}
                        return(this);
                    }
                }

                default:
//					{
//						var exception = new SyntaxException(Syntax.Error.InvalidToken);
//						exception.content = data.token.ToString();
//						throw exception;
//					}
                    return(this);
                }

            case CodePhase.Return:
                switch (data.token)
                {
                case Token.Keyword:
                    // TODO save
                    if (Syntax.constantKeywordList.Contains((Keyword)data.number.i_32))
                    {
                        // TODO save
                        codePhase = CodePhase.ReturnSeparator;
                        return(this);
                    }
                    else
                    {
                        var exception = new SyntaxException(Syntax.Error.InvalidToken);
                        exception.content = data.token.ToString();
                        throw exception;
                    }

                case Token.Identify:
                    // TODO save
                    codePhase = CodePhase.ReturnSeparator;
                    return(this);

                default:
                    if (Syntax.constantTokenList.Contains(data.token))
                    {
                        // TODO save
                        codePhase = CodePhase.ReturnSeparator;
                        return(this);
                    }
                    else
                    {
                        var exception = new SyntaxException(Syntax.Error.InvalidToken);
                        exception.content = data.token.ToString();
                        throw exception;
                    }
                }

            case CodePhase.ReturnSeparator:
                if (Token.Operator == data.token)
                {
                    var op = (Operator)data.number.i_32;
                    if (Operator.Comma == op)
                    {
                        codePhase = CodePhase.Return;
                        return(this);
                    }
                    else
                    {
                        var exception = new SyntaxException(Syntax.Error.InvalidOperator);
                        exception.content = op.ToString();
                        throw exception;
                    }
                }
                else
                {
                    var exception = new SyntaxException(Syntax.Error.InvalidToken);
                    exception.content = data.token.ToString();
                    throw exception;
                }
            }
            return(this);
        }