[opengtl-commits] [754] use the parser, and generator in template

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 754
Author:   cyrille
Date:     2009-05-30 11:46:05 +0200 (Sat, 30 May 2009)

Log Message:
-----------
use the parser, and generator in template

Modified Paths:
--------------
    trunk/OpenGTL/OpenCTL/OpenCTL/Template.cpp
    trunk/OpenGTL/OpenCTL/OpenCTL/Template.h


Modified: trunk/OpenGTL/OpenCTL/OpenCTL/Template.cpp
===================================================================
--- trunk/OpenGTL/OpenCTL/OpenCTL/Template.cpp	2009-05-30 09:45:18 UTC (rev 753)
+++ trunk/OpenGTL/OpenCTL/OpenCTL/Template.cpp	2009-05-30 09:46:05 UTC (rev 754)
@@ -22,14 +22,24 @@
 #include <sstream>
 #include <fstream>
 
+#include <GTLCore/ErrorMessage.h>
+
 #include "Debug.h"
+#include "templatecompiler/GenerationContext_p.h"
+#include "templatecompiler/TemplateAST_p.h"
+#include "templatecompiler/TemplateLexer_p.h"
+#include "templatecompiler/TemplateParser_p.h"
+#include "Module.h"
 
 using namespace OpenCTL;
 
 struct Template::Private {
-  Private() : isCompiled(false) {}
+  Private() : isCompiled(false), node(0) {}
   GTLCore::String source;
+  GTLCore::String filename;
   bool isCompiled;
+  TemplateAST::Node* node;
+  std::list<GTLCore::ErrorMessage> errorMessages;
 };
 
 Template::Template() : d(new Private)
@@ -66,10 +76,40 @@
 
 void Template::compile()
 {
+  delete d->node;
+  d->node = 0;
+  d->errorMessages.clear();
   
+  std::istringstream iss(d->source);
+  TemplateLexer lexer(&iss);
+  TemplateParser parser(&lexer, d->filename);
+  d->node = parser.parse();
+  if(d->node)
+  {
+    d->isCompiled = true;
+  } else {
+    d->isCompiled = false;
+    d->errorMessages = parser.errorMessages();
+  }
 }
 
 bool Template::isCompiled() const
 {
   return d->isCompiled;
 }
+
+GTLCore::String Template::generateSource( const std::map< GTLCore::String, const GTLCore::Type* >& _namedTypes,
+                  const GTLCore::PixelDescription& _pixelDescription )
+{
+  TemplateGenerationContext context(_namedTypes, _pixelDescription);
+  d->node->generate(&context);
+  return context.code();
+}
+
+Module* Template::generateModule( const std::map< GTLCore::String, const GTLCore::Type* >& _namedTypes,
+                  const GTLCore::PixelDescription& _pixelDescription )
+{
+  Module* module = new Module();
+  module->setSource(generateSource(_namedTypes, _pixelDescription));
+  return module;
+}

Modified: trunk/OpenGTL/OpenCTL/OpenCTL/Template.h
===================================================================
--- trunk/OpenGTL/OpenCTL/OpenCTL/Template.h	2009-05-30 09:45:18 UTC (rev 753)
+++ trunk/OpenGTL/OpenCTL/OpenCTL/Template.h	2009-05-30 09:46:05 UTC (rev 754)
@@ -20,14 +20,16 @@
 #ifndef _OPENCTL_TEMPLATE_H_
 #define _OPENCTL_TEMPLATE_H_
 
+#include <GTLCore/PixelDescription.h>
 #include <GTLCore/String.h>
+#include <GTLCore/Type.h>
 
+#include <map>
+
 namespace OpenCTL {
+  class Module;
   class Template {
     public:
-/*      class GenerationContext {
-      };*/
-    public:
       Template();
       ~Template();
       /**
@@ -43,6 +45,16 @@
        * @return true if the module was successfully compiled.
        */
       bool isCompiled() const;
+      /**
+       * Generate a CTL code source from this template
+       */
+      GTLCore::String generateSource( const std::map< GTLCore::String, const GTLCore::Type* >& _namedTypes,
+                        const GTLCore::PixelDescription& _pixelDescription );
+      /**
+       * Convenient function that generate a Module (not compiled)
+       */
+      Module* generateModule( const std::map< GTLCore::String, const GTLCore::Type* >& _namedTypes,
+                        const GTLCore::PixelDescription& _pixelDescription );
     private:
       struct Private;
       Private* const d;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/