Example #1
0
        public bool TryParse(
            TokenReader reader, 
            out ProgramUnit result)
        {
            result = null;
            Token tok = reader.Peek();
            ProgramUnit programUnit = new ProgramUnit(tok);
            if (tok.Is(Keyword.Namespace))
            {
                reader.Read();
                string namespaceDecl = null;
                if (!this.ParseFullNameDeclaration(reader, out namespaceDecl))
                {
                    return false;
                }

                if (!this.Expect(reader, Keyword.SemiColon))
                {
                    return false;
                }

                programUnit.Namespace = namespaceDecl;
            }

            tok = reader.Peek();
            if (tok.Is(Keyword.Uses))
            {
                reader.Read();
                string usesRef = null;
                if (!this.ParseFullNameDeclaration(reader, out usesRef))
                {
                    return false;
                }

                programUnit.AddUses(usesRef);
                tok = reader.Peek();
                while (tok.Is(Keyword.Comma))
                {
                    reader.Read();
                    if (!this.ParseFullNameDeclaration(reader, out usesRef))
                    {
                        return false;
                    }

                    programUnit.AddUses(usesRef);
                    tok = reader.Peek();
                }

                if (!this.Expect(reader, Keyword.SemiColon))
                {
                    return false;
                }
            }

            tok = reader.Peek();
            if (tok.Is(Keyword.Type))
            {
                if (!this.ParseTypeBlock(reader, programUnit))
                {
                    return false;
                }
            }

            tok = reader.Peek();
            if (tok.Is(Keyword.Var))
            {
                VarBlock globalVars = null;
                if (!this.ParseVarBlock(reader, true, out globalVars))
                {
                    return false;
                }

                programUnit.Variables = globalVars;
            }

            tok = reader.Peek();
            while (
                tok.Is(Keyword.Function) ||
                tok.Is(Keyword.Procedure) ||
                tok.Is(Keyword.Constructor) ||
                tok.Is(Keyword.Destructor))
            {
                MethodDefinition methodDef = null;
                if (!this.ParseMethodDefinition(reader, out methodDef))
                {
                    return false;
                }

                programUnit.AddMethod(methodDef);
                if (!this.Expect(reader, Keyword.SemiColon))
                {
                    return false;
                }

                tok = reader.Peek();
            }

            bool passed = ExpectEndOfFile(reader);
            if (passed)
            {
                result = programUnit;
            }

            return passed;
        }