Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[r302]: cmajor++ / Cm / Cm.Parser / CompilationUnit.soul Maximize Restore History

Download this file

CompilationUnit.soul    151 lines (128 with data), 7.7 kB

/*========================================================================
    Copyright (c) 2012-2013 Seppo Laakko
    http://sourceforge.net/projects/cmajor/
 
    Distributed under the GNU General Public License, version 3 (GPLv3).
    (See accompanying LICENSE.txt or http://www.gnu.org/licenses/gpl.html)

 ========================================================================*/

[hpp]#include <Cm.Core/CompilationUnit.hpp>
[cpp]#include <Cm.Core/Name.hpp>
[cpp]#include <Cm.Parser/Identifier.hpp>
[cpp]#include <Cm.Parser/Function.hpp>
[cpp]#include <Cm.Parser/Constant.hpp>
[cpp]#include <Cm.Parser/Enumeration.hpp>
[cpp]#include <Cm.Parser/Typedef.hpp>
[cpp]#include <Cm.Parser/Class.hpp>
[cpp]#include <Cm.Parser/Delegate.hpp>
[cpp]#include <Cm.Parser/Concept.hpp>

namespace Cm.Parser
{
    [hpp]using Cm::Core::CompilationUnitPtr;
    [hpp]using boost::filesystem::path;
    [cpp]using Cm::Core::FunctionPtr;
    [cpp]using Cm::Core::NamespacePtr;
    [cpp]using Cm::Core::Namespace;
    [cpp]using Cm::Core::NamespaceImport;
    [cpp]using Cm::Core::NamespaceImportPtr;
    [cpp]using Cm::Core::AliasPtr;
    [cpp]using Cm::Core::Alias;
    [cpp]using Cm::Core::GlobalScope;
    [cpp]using Cm::Core::TypeCheckContext;
    [cpp]using Cm::Core::TypeCheckExtent;
    
    grammar CompilationUnitGrammar
    {
        using FunctionGrammar.Function;
        using ConstantGrammar.Constant;
        using EnumerationGrammar.Enumeration;
        using TypedefGrammar.Typedef;
        using IdentifierGrammar.Identifier;
        using IdentifierGrammar.QualifiedId;
        using ClassGrammar.Class;
        using DelegateGrammar.Delegate;
        using ConceptGrammar.Concept;
        using stdlib.spaces_and_comments;
        skip spaces_and_comments;
        
        CompilationUnit(path outputBase, var ParsingContextPtr ctx): CompilationUnitPtr
                        ::= empty{ ctx = ParsingContextPtr(new ParsingContext(fileName, outputBase)); value = ctx->GetCompilationUnit(); }
                            NamespaceContent(GlobalScope(), ctx)
                        ;
                    
        NamespaceContent(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= UsingDirectives(enclosingScope, ctx)
                            Definitions(enclosingScope, ctx)
                        ;
                        
        UsingDirectives(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= UsingDirective(enclosingScope, ctx)*
                        ;
                        
        UsingDirective(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= UsingAliasDirective(enclosingScope, ctx){ UsingAliasDirective->AddToScope(); }
                        |   UsingNamespaceDirective(enclosingScope, ctx){ UsingNamespaceDirective->AddToScope(); }
                        ;
                        
        UsingAliasDirective(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx): AliasPtr
                        ::= (keyword("using") Identifier '=' QualifiedId ';')
                        {
                            value = AliasPtr(new Alias(Identifier, position, enclosingScope, ctx->FileScope(), Cm::Core::ObjectPtr(new Cm::Core::Name(QualifiedId, position, enclosingScope, ctx->FileScope(), true))));
                        }
                        ;
                        
        UsingNamespaceDirective(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx): NamespaceImportPtr
                        ::= (keyword("using") QualifiedId ';')
                        {
                            value = NamespaceImportPtr(new NamespaceImport(QualifiedId, position, enclosingScope, ctx->FileScope())); 
                        }
                        ;
                        
        Definitions(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= Definition(enclosingScope, ctx)*
                        ;
                        
        Definition(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= NamespaceDefinition(enclosingScope, ctx)
                        |   FunctionDefinition(enclosingScope, ctx)
                        |   ConstantDefinition(enclosingScope, ctx)
                        |   EnumerationDefinition(enclosingScope, ctx)
                        |   TypedefDefinition(enclosingScope, ctx)
                        |   ClassDefinition(enclosingScope, ctx)
                        |   DelegateDefinition(enclosingScope, ctx)
                        |   ConceptDefinition(enclosingScope, ctx)
                        ;
                        
        NamespaceDefinition(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= Namespace(enclosingScope, ctx)
                        ;
                    
        Namespace(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx): NamespacePtr
                        ::= keyword("namespace") QualifiedId! 
                        {   
                            value = enclosingScope->GetNamespace(QualifiedId, position);
                            if (!value)
                            {
                                value = NamespacePtr(new Namespace(QualifiedId, position, enclosingScope, ctx->FileScope())); 
                            }
                            value->AddToScope();
                        }
                        '{'! NamespaceContent(value->GetScope(), ctx)! '}'!
                        ;
                        
        FunctionDefinition(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= Function(enclosingScope, ctx)
                        { 
                            TypeCheckContext tcctx(TypeCheckExtent::name);
                            tcctx.SetNoThrow();
                            Function->TypeCheck(tcctx);
                            Function->AddToScope(); 
                            ctx->GetCompilationUnit()->AddObject(Function); 
                        }
                        ;

        ConstantDefinition(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= Constant(enclosingScope, ctx){ Constant->AddToScope(); ctx->GetCompilationUnit()->AddObject(Constant); }
                        ;

        EnumerationDefinition(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= Enumeration(enclosingScope, ctx){ Enumeration->AddToScope(); ctx->GetCompilationUnit()->AddObject(Enumeration); }
                        ;

        TypedefDefinition(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= Typedef(enclosingScope, ctx){ Typedef->AddToScope(); ctx->GetCompilationUnit()->AddObject(Typedef); }
                        ;

        ClassDefinition(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= Class(enclosingScope, ctx){ Class->AddToScope(); ctx->GetCompilationUnit()->AddObject(Class); }
                        ;

        DelegateDefinition(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= Delegate(enclosingScope, ctx){ Delegate->AddToScope(); ctx->GetCompilationUnit()->AddObject(Delegate); }
                        ;

        ConceptDefinition(Cm::Core::ScopePtr enclosingScope, ParsingContextPtr ctx)
                        ::= Concept(enclosingScope, ctx){ Concept->AddToScope(); ctx->GetCompilationUnit()->AddObject(Concept); }
                        ;
    }
}