Exemple #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;
            }
            }
        }
Exemple #2
0
        public static T Create <T>(Lex.Data data, Syntax_Node parent) where T : Syntax_Node, new()
        {
            // TODO reuse
            var node = new T();

            node.Construct(data, parent);
            return(node);
        }
Exemple #3
0
 internal Syntax_Node AddChild(Syntax_Node child)
 {
     if (null == children_)
     {
         children_ = new List <Syntax_Node>();
     }
     children_.Add(child);
     return(child);
 }
Exemple #4
0
 internal void Deconstruct()
 {
     DoDeconstruct();
     parent_ = null;
     if (null != children_)
     {
         children_.Clear();
     }
 }
Exemple #5
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;
            }
        }
Exemple #6
0
 internal void Construct(Lex.Data data, Syntax_Node p)
 {
     parent_ = p;
     DoConstruct(data);
 }
Exemple #7
0
 public Context(Syntax_Node root)
 {
     node = root;
 }
        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);
        }