Beispiel #1
0
        public static bool GetSyntaxTree(Stream data, SyntaxTree <PreprocessorToken> tree, PreprocessorToken root = null)
        {
            while (!data.Eof())
            {
                char c = data.Peek();
                switch ((TokenTypes)c)
                {
                case TokenTypes.IfDefined:
                case TokenTypes.IfNotDefined:
                case TokenTypes.If:
                case TokenTypes.ElseIf:
                {
                    ConditionToken token = new ConditionToken((TokenTypes)c);
                    token.Deserialize(data);

                    if (root == null)
                    {
                        tree.Add(token);
                    }
                    else
                    {
                        tree.AddAppend(root, token);
                    }

                    if (!GetSyntaxTree(data, tree, token))
                    {
                        return(false);
                    }
                }
                break;

                case TokenTypes.Else:
                {
                    PreprocessorToken token = new PreprocessorToken(TokenTypes.Else);
                    token.Deserialize(data);

                    if (root == null)
                    {
                        tree.Add(token);
                    }
                    else
                    {
                        tree.AddAppend(root, token);
                    }

                    if (!GetSyntaxTree(data, tree, token))
                    {
                        return(false);
                    }
                }
                break;

                case TokenTypes.Define:
                case TokenTypes.Undefine:
                case TokenTypes.Include:
                case TokenTypes.Error:
                case TokenTypes.Pragma:
                {
                    TextToken token = new TextToken((TokenTypes)c);
                    token.Deserialize(data);

                    if (root == null)
                    {
                        tree.Add(token);
                    }
                    else
                    {
                        tree.AddAppend(root, token);
                    }
                }
                break;

                case TokenTypes.Text:
                {
                    TextToken token = new TextToken();
                    token.Deserialize(data);

                    if (root == null)
                    {
                        tree.Add(token);
                    }
                    else
                    {
                        tree.AddAppend(root, token);
                    }
                }
                break;

                default:
                {
                    if (root != null && c == 0)
                    {
                        data.Get();
                        return(true);
                    }
                    else
                    {
                        return(ThrowError(new CodeProcessorContext(new CodeProcessorConfig()), PreprocessorCodes.UnexpectedCharacter));
                    }
                }
                }
            }
            return(true);
        }