1 #include "SourceFile.hpp"
  
    2 #include <soulng/util/Unicode.hpp>
  
    3 #include <sngcpp/lexer/CppLexer.hpp>
  
    4 #include <sngcpp/lexer/CppTokens.hpp>
  
    5 
  
    6 
  
    7 
  
    8 using namespace soulng::unicode;
  
    9 using namespace CppTokens;
  
   10 using namespace soulng::lexer;
  
   11 
  
   12 void SourceFileParser::Parse(CppLexer& lexer, sngcpp::ast::SourceFileNode* sourceFile)
  
   13 {
  
   14     #ifdef SOULNG_PARSER_DEBUG_SUPPORT
  
   15 
  
   16 
  
   17 
  
   18 
  
   19 
  
   20     #endif // SOULNG_PARSER_DEBUG_SUPPORT
  
   21     ++lexer;
  
   22     soulng::lexer::Span span = lexer.GetSpan();
  
   23     soulng::parser::Match match = SourceFileParser::SourceFile(lexer, sourceFile);
  
   24     #ifdef SOULNG_PARSER_DEBUG_SUPPORT
  
   25 
  
   26 
  
   27 
  
   28 
  
   29 
  
   30     #endif // SOULNG_PARSER_DEBUG_SUPPORT
  
   31     if (match.hit)
  
   32     {
  
   33         if (*lexer == soulng::lexer::END_TOKEN)
  
   34         {
  
   35             return;
  
   36         }
  
   37         else
  
   38         {
  
   39             lexer.ThrowExpectationFailure(lexer.GetSpan(), ToUtf32(soulng::lexer::GetEndTokenInfo()));
  
   40         }
  
   41     }
  
   42     else
  
   43     {
  
   44         lexer.ThrowExpectationFailure(span, U"SourceFile");
  
   45     }
  
   46     return;
  
   47 }
  
   48 
  
   49 soulng::parser::Match SourceFileParser::SourceFile(CppLexer& lexer, sngcpp::ast::SourceFileNode* sourceFile)
  
   50 {
  
   51     #ifdef SOULNG_PARSER_DEBUG_SUPPORT
  
   52 
  
   53 
  
   54 
  
   55 
  
   56 
  
   57 
  
   58 
  
   59     #endif // SOULNG_PARSER_DEBUG_SUPPORT
  
   60     sngcpp::cppparser::ParsingContext ctx = sngcpp::cppparser::ParsingContext();
  
   61     soulng::parser::Match match(false);
  
   62     soulng::parser::Match* parentMatch0 = &match;
  
   63     {
  
   64         soulng::parser::Match match(false);
  
   65         soulng::parser::Match* parentMatch1 = &match;
  
   66         {
  
   67             int64_t pos = lexer.GetPos();
  
   68             soulng::parser::Match match(true);
  
   69             if (match.hit)
  
   70             {
  
   71                 ctx.SetSourceFileNode(sourceFile);
  
   72             }
  
   73             *parentMatch1 = match;
  
   74         }
  
   75         *parentMatch0 = match;
  
   76     }
  
   77     if (match.hit)
  
   78     {
  
   79         soulng::parser::Match match(false);
  
   80         soulng::parser::Match* parentMatch2 = &match;
  
   81         {
  
   82             soulng::parser::Match match = SourceFileParser::Declarations(lexer, &ctx, sourceFile->GlobalNs());
  
   83             *parentMatch2 = match;
  
   84         }
  
   85         *parentMatch0 = match;
  
   86     }
  
   87     #ifdef SOULNG_PARSER_DEBUG_SUPPORT
  
   88 
  
   89 
  
   90 
  
   91 
  
   92 
  
   93     #endif // SOULNG_PARSER_DEBUG_SUPPORT
  
   94     if (!match.hit)
  
   95     {
  
   96         match.value = nullptr;
  
   97     }
  
   98     return match;
  
   99 }
  
  100 
  
  101 soulng::parser::Match SourceFileParser::Declarations(CppLexer& lexer, sngcpp::cppparser::ParsingContext* ctx, sngcpp::ast::NamespaceNode* ns)
  
  102 {
  
  103     #ifdef SOULNG_PARSER_DEBUG_SUPPORT
  
  104 
  
  105 
  
  106 
  
  107 
  
  108 
  
  109 
  
  110 
  
  111     #endif // SOULNG_PARSER_DEBUG_SUPPORT
  
  112     std::unique_ptr<sngcpp::ast::Node> declaration;
  
  113     soulng::parser::Match match(true);
  
  114     soulng::parser::Match* parentMatch0 = &match;
  
  115     {
  
  116         while (true)
  
  117         {
  
  118             int64_t save = lexer.GetPos();
  
  119             {
  
  120                 soulng::parser::Match match(false);
  
  121                 soulng::parser::Match* parentMatch1 = &match;
  
  122                 {
  
  123                     soulng::parser::Match match(false);
  
  124                     soulng::parser::Match* parentMatch2 = &match;
  
  125                     {
  
  126                         int64_t pos = lexer.GetPos();
  
  127                         soulng::lexer::Span span = lexer.GetSpan();
  
  128                         soulng::parser::Match match = DeclarationParser::Declaration(lexer, ctx);
  
  129                         declaration.reset(static_cast<sngcpp::ast::Node*>(match.value));
  
  130                         if (match.hit)
  
  131                         {
  
  132                             ns->AddMember(span, declaration.release());
  
  133                         }
  
  134                         *parentMatch2 = match;
  
  135                     }
  
  136                     *parentMatch1 = match;
  
  137                 }
  
  138                 if (match.hit)
  
  139                 {
  
  140                     *parentMatch0 = match;
  
  141                 }
  
  142                 else
  
  143                 {
  
  144                     lexer.SetPos(save);
  
  145                     break;
  
  146                 }
  
  147             }
  
  148         }
  
  149     }
  
  150     #ifdef SOULNG_PARSER_DEBUG_SUPPORT
  
  151 
  
  152 
  
  153 
  
  154 
  
  155 
  
  156     #endif // SOULNG_PARSER_DEBUG_SUPPORT
  
  157     if (!match.hit)
  
  158     {
  
  159         match.value = nullptr;
  
  160     }
  
  161     return match;
  
  162 }