changeset 368:2adf808343d6

- Renamed method start() to init() in Parser. - Renamed method parseModule() to start() in Parser. - Added method parseModuleDeclaration(). - Added class ImportParser which inherits from Parser. It's used as a lightweight parser for parsing module and import declarations. - Added member isLightweight to Module. - Removed calls to Parser.parseModule() and replaced them with Parser.start().
author aziz
date Sat, 01 Sep 2007 11:23:01 +0000
parents dda55fae37de
children 27767e203d64
files trunk/src/cmd/Generate.d trunk/src/cmd/ImportGraph.d trunk/src/dil/Module.d trunk/src/dil/Parser.d trunk/src/dil/Settings.d trunk/src/main.d
diffstat 6 files changed, 61 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/cmd/Generate.d	Fri Aug 31 23:07:05 2007 +0000
+++ b/trunk/src/cmd/Generate.d	Sat Sep 01 11:23:01 2007 +0000
@@ -247,8 +247,7 @@
   auto tags = options & DocOption.HTML ? html_tags : xml_tags;
   auto sourceText = loadFile(fileName);
   auto parser = new Parser(sourceText, fileName);
-  parser.start();
-  auto root = parser.parseModule();
+  auto root = parser.start();
   auto lx = parser.lx;
 
   auto token = lx.head;
--- a/trunk/src/cmd/ImportGraph.d	Fri Aug 31 23:07:05 2007 +0000
+++ b/trunk/src/cmd/ImportGraph.d	Sat Sep 01 11:23:01 2007 +0000
@@ -38,13 +38,13 @@
     auto mod_ = moduleFQN in loadedModules;
     if (mod_ !is null)
       return *mod_;
-
+// writefln(moduleFQN);
     auto modulePath = findModule(moduleFQN, importPaths);
     if (modulePath is null)
       writefln("Warning: Module %s.d couldn't be found.", moduleFQN);
     else
     {
-      auto mod = new Module(modulePath);
+      auto mod = new Module(modulePath, true);
       mod.parse();
 
       loadedModules[moduleFQN] = mod;
@@ -58,7 +58,7 @@
     return null;
   }
 
-  auto mod = new Module(fileName);
+  auto mod = new Module(fileName, true);
   mod.parse();
 
   auto moduleFQNs = mod.getImports();
--- a/trunk/src/dil/Module.d	Fri Aug 31 23:07:05 2007 +0000
+++ b/trunk/src/dil/Module.d	Sat Sep 01 11:23:01 2007 +0000
@@ -12,6 +12,7 @@
 
 class Module
 {
+  bool isLightweight; /// If true an ImportParser is used instead of a full Parser.
   string fileName; /// Path to the source file.
   string packageName;
   string moduleName;
@@ -22,18 +23,21 @@
 
   Module[] modules;
 
-  this(string fileName)
+  this(string fileName, bool isLight = false)
   {
     this.fileName = fileName;
+    this.isLightweight = isLightweight;
   }
 
   void parse()
   {
     auto sourceText = loadFile(fileName);
-    this.parser = new Parser(sourceText, fileName);
-    parser.start();
+    if (this.isLightweight)
+      this.parser = new ImportParser(sourceText, fileName);
+    else
+      this.parser = new Parser(sourceText, fileName);
 
-    this.root = parser.parseModule();
+    this.root = parser.start();
 
     if (root.children.length)
     {
--- a/trunk/src/dil/Parser.d	Fri Aug 31 23:07:05 2007 +0000
+++ b/trunk/src/dil/Parser.d	Sat Sep 01 11:23:01 2007 +0000
@@ -16,6 +16,35 @@
 
 private alias TOK T;
 
+class ImportParser : Parser
+{
+  this(char[] srcText, string fileName)
+  {
+    super(srcText, fileName);
+  }
+
+  Declarations start()
+  {
+    auto decls = new Declarations;
+    super.init();
+    if (token.type == T.Module)
+      decls ~= parseModuleDeclaration();
+    while (token.type != T.EOF)
+    {
+      if (token.type == T.Import)
+      {
+        auto decl = parseImportDeclaration();
+        assert(decl && decl.kind == NodeKind.ImportDeclaration);
+        super.imports ~= cast(ImportDeclaration)cast(void*)decl;
+        decls ~= decl;
+      }
+      else
+        nT();
+    }
+    return decls;
+  }
+}
+
 class Parser
 {
   Lexer lx;
@@ -33,7 +62,7 @@
 
   debug char* prev;
 
-  void start()
+  private void init()
   {
     debug prev = lx.text.ptr;
     nT();
@@ -114,26 +143,29 @@
   + Declaration parsing methods +
   ++++++++++++++++++++++++++++++/
 
-  Declarations parseModule()
+  Declarations start()
   {
+    init();
     auto decls = new Declarations;
-
     if (token.type == T.Module)
-    {
-      auto begin = token;
-      ModuleFQN moduleFQN;
-      do
-      {
-        nT();
-        moduleFQN ~= requireId();
-      } while (token.type == T.Dot)
-      require(T.Semicolon);
-      decls ~= set(new ModuleDeclaration(moduleFQN), begin);
-    }
+      decls ~= parseModuleDeclaration();
     decls ~= parseDeclarationDefinitions();
     return decls;
   }
 
+  Declaration parseModuleDeclaration()
+  {
+    auto begin = token;
+    ModuleFQN moduleFQN;
+    do
+    {
+      nT();
+      moduleFQN ~= requireId();
+    } while (token.type == T.Dot)
+    require(T.Semicolon);
+    return set(new ModuleDeclaration(moduleFQN), begin);
+  }
+
   Declarations parseDeclarationDefinitions()
   {
     auto decls = new Declarations;
--- a/trunk/src/dil/Settings.d	Fri Aug 31 23:07:05 2007 +0000
+++ b/trunk/src/dil/Settings.d	Sat Sep 01 11:23:01 2007 +0000
@@ -53,8 +53,7 @@
     auto fileName = "config.d"[];
     auto sourceText = loadFile(fileName);
     auto parser = new Parser(sourceText, fileName);
-    parser.start();
-    auto root = parser.parseModule();
+    auto root = parser.start();
 
     if (parser.errors.length || parser.lx.errors.length)
     {
@@ -96,8 +95,7 @@
     // Load messages
     sourceText = loadFile(fileName);
     parser = new Parser(sourceText, fileName);
-    parser.start();
-    root = parser.parseModule();
+    root = parser.start();
 
     if (parser.errors.length || parser.lx.errors.length)
     {
--- a/trunk/src/main.d	Fri Aug 31 23:07:05 2007 +0000
+++ b/trunk/src/main.d	Sat Sep 01 11:23:01 2007 +0000
@@ -107,8 +107,7 @@
 {
   auto sourceText = loadFile(fileName);
   auto parser = new Parser(sourceText, fileName);
-  parser.start();
-  auto root = parser.parseModule();
+  auto root = parser.start();
 
 void print(Node[] decls, char[] indent)
 {