[llvm-commits] [hlvm] r38050 - in /hlvm/trunk: hlvm/Reader/Reader.h hlvm/Reader/XML/XMLReader.cpp hlvm/Writer/Writer.h hlvm/Writer/XML/XMLWriter.cpp test/xml2xml/bundle.hlx tools/hlvm-xml2xml/hlvm-xml2xml.cpp

Reid Spencer reid at x10sys.com
Sat Jul 7 16:59:19 PDT 2007


Author: reid
Date: Sat Jul  7 18:59:19 2007
New Revision: 38050

URL: http://llvm.org/viewvc/llvm-project?rev=38050&view=rev
Log:
Use the AST class as the root of any AST node. This class can be used to hold
information that is logically "outside" the tree but still needed. For example,
we might use this to stored an APR pool someday. Also, add a little more
XMLReader functionality.

Modified:
    hlvm/trunk/hlvm/Reader/Reader.h
    hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp
    hlvm/trunk/hlvm/Writer/Writer.h
    hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp
    hlvm/trunk/test/xml2xml/bundle.hlx
    hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp

Modified: hlvm/trunk/hlvm/Reader/Reader.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Reader/Reader.h?rev=38050&r1=38049&r2=38050&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Reader/Reader.h (original)
+++ hlvm/trunk/hlvm/Reader/Reader.h Sat Jul  7 18:59:19 2007
@@ -31,7 +31,7 @@
 #define XPS_READER_READER_H
 
 namespace hlvm {
-namespace AST { class Node; }
+namespace AST { class AST; }
 
   class Reader
   {
@@ -41,7 +41,7 @@
 
     /// This method retrieves the construct AST that resulted from reading.
     /// @returns 0 if nothing has been read yet
-    virtual AST::Node* get() = 0;
+    virtual AST::AST* get() = 0;
   };
 }
 #endif

Modified: hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp?rev=38050&r1=38049&r2=38050&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp (original)
+++ hlvm/trunk/hlvm/Reader/XML/XMLReader.cpp Sat Jul  7 18:59:19 2007
@@ -31,6 +31,7 @@
 #include <hlvm/Reader/XML/HLVMTokenizer.h>
 #include <hlvm/Base/Locator.h>
 #include <hlvm/Base/Source.h>
+#include <hlvm/AST/AST.h>
 #include <hlvm/AST/Bundle.h>
 #include <libxml/parser.h>
 #include <libxml/relaxng.h>
@@ -48,29 +49,31 @@
 ;
 
 class XMLReaderImpl : public XMLReader {
-  std::string path_;
-  AST::Node* node_;
-  xmlDocPtr doc_;
+  std::string path;
+  AST::AST* node;
+  xmlDocPtr doc;
 public:
-  XMLReaderImpl(const std::string& path)
-    : path_(path), node_(0)
+  XMLReaderImpl(const std::string& p)
+    : path(p), node(0)
   {
+    node = new AST::AST();
+    node->setSystemID(p);
   }
 
   virtual ~XMLReaderImpl() 
   { 
-    if (node_) delete node_; 
-    if (doc_) xmlFreeDoc(doc_);
+    if (node) delete node; 
+    if (doc) xmlFreeDoc(doc);
   }
 
   virtual void read();
-  virtual AST::Node* get();
+  virtual AST::AST* get();
 
   void error(const std::string& msg) {
     std::cerr << msg << "\n";
   }
 
-  std::string getToken(int32_t token) const
+  std::string lookupToken(int32_t token) const
   {
     return HLVMTokenizer::lookup(token);
   }
@@ -79,7 +82,7 @@
   inline void handleValidationError(xmlErrorPtr error);
 
   void parseTree();
-  void parseBundle(xmlNodePtr cur);
+  AST::Bundle* parseBundle(xmlNodePtr& cur);
 private:
 };
 
@@ -115,42 +118,57 @@
   reader->handleValidationError(error);
 }
 
-bool skipBlanks(xmlNodePtr cur)
+bool skipBlanks(xmlNodePtr &cur)
 {
-  while ( cur && xmlIsBlankNode ( cur ) ) 
+  while (cur && 
+      (cur->type == XML_TEXT_NODE ||
+       cur->type == XML_COMMENT_NODE ||
+       cur->type == XML_PI_NODE))
   {
     cur = cur -> next;
   }
   return cur == 0;
 }
 
-void
-XMLReaderImpl::parseBundle(xmlNodePtr cur) 
+inline const char* getAttribute(xmlNodePtr cur,const char*name)
+{
+  return reinterpret_cast<const char*>(
+    xmlGetNoNsProp(cur,reinterpret_cast<const xmlChar*>(name)));
+}
+
+inline int getToken(const xmlChar* name)
+{
+  return HLVMTokenizer::recognize(reinterpret_cast<const char*>(name));
+}
+
+AST::Bundle*
+XMLReaderImpl::parseBundle(xmlNodePtr &cur) 
 {
-  int tkn = 
-    HLVMTokenizer::recognize(reinterpret_cast<const char*>(cur->name));
+  int tkn = getToken(cur->name);
   assert(tkn == TKN_bundle && "Expecting bundle element");
-  xmlChar* pubid = 
-    xmlGetNoNsProp(cur,reinterpret_cast<const xmlChar*>("pubid"));
+  std::string pubid(getAttribute(cur, "pubid"));
+  AST::Locator loc(cur->line,0,&node->getSystemID());
+  AST::Bundle* bundle = AST::AST::new_Bundle(loc,pubid);
+  return bundle;
 }
 
 void
 XMLReaderImpl::parseTree() 
 {
-  if (node_)
-    delete node_;
-  node_ = 0;
-  xmlNodePtr cur = xmlDocGetRootElement(doc_);
+  if (node)
+    delete node;
+  node = new AST::AST();
+  xmlNodePtr cur = xmlDocGetRootElement(doc);
   if (!cur) {
     error("No root node");
     return;
   }
-  int tkn = 
-    HLVMTokenizer::recognize(reinterpret_cast<const char*>(cur->name));
+  int tkn = getToken(cur->name);
   assert(tkn == TKN_hlvm && "Expecting hlvm element");
-  cur = cur->xmlChildrenNode;
+  cur = cur->children;
   if (skipBlanks(cur)) return;
-  parseBundle(cur);
+  AST::Bundle* bundle = parseBundle(cur);
+  node->setRoot(bundle);
 }
 
 // Implement the read interface to parse, validate, and convert the
@@ -189,8 +207,8 @@
   }
 
   // Parse the file, creating a Document tree
-  doc_ = xmlCtxtReadFile(ctxt, path_.c_str(), 0, 0);
-  if (!doc_) {
+  doc = xmlCtxtReadFile(ctxt, path.c_str(), 0, 0);
+  if (!doc) {
     error("Failed to parse the document");
     xmlRelaxNGFreeParserCtxt(rngparser);
     xmlRelaxNGFree(schema);
@@ -205,8 +223,8 @@
     xmlRelaxNGFreeParserCtxt(rngparser);
     xmlRelaxNGFree(schema);
     xmlFreeParserCtxt(ctxt);
-    xmlFreeDoc(doc_);
-    doc_ = 0;
+    xmlFreeDoc(doc);
+    doc = 0;
     return;
   }
 
@@ -214,12 +232,13 @@
   xmlRelaxNGSetValidStructuredErrors(validation, ValidationHandler, this);
 
   // Validate the document with the schema
-  if (xmlRelaxNGValidateDoc(validation, doc_)) {
+  if (xmlRelaxNGValidateDoc(validation, doc)) {
     error("Document didn't pass RNG schema validation");
     xmlRelaxNGFreeParserCtxt(rngparser);
     xmlRelaxNGFree(schema);
     xmlFreeParserCtxt(ctxt);
-    xmlFreeDoc(doc_);
+    xmlFreeDoc(doc);
+    doc = 0;
     xmlRelaxNGFreeValidCtxt(validation);
     return;
   }
@@ -230,12 +249,14 @@
   xmlRelaxNGFree(schema);
   xmlFreeParserCtxt(ctxt);
   xmlRelaxNGFreeValidCtxt(validation);
+  xmlFreeDoc(doc);
+  doc = 0;
 }
 
-AST::Node*
+AST::AST*
 XMLReaderImpl::get()
 {
-  return node_;
+  return node;
 }
 
 

Modified: hlvm/trunk/hlvm/Writer/Writer.h
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Writer/Writer.h?rev=38050&r1=38049&r2=38050&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Writer/Writer.h (original)
+++ hlvm/trunk/hlvm/Writer/Writer.h Sat Jul  7 18:59:19 2007
@@ -31,14 +31,14 @@
 #define XPS_WRITER_WRITER_H
 
 namespace hlvm {
-namespace AST { class Node; }
+namespace AST { class AST; }
 
   class Writer
   {
   public:
     /// This method writes the entire content of the AST to the writer's
     /// destination
-    virtual void write(AST::Node* source) = 0;
+    virtual void write(AST::AST* source) = 0;
   };
 }
 #endif

Modified: hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp?rev=38050&r1=38049&r2=38050&view=diff

==============================================================================
--- hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp (original)
+++ hlvm/trunk/hlvm/Writer/XML/XMLWriter.cpp Sat Jul  7 18:59:19 2007
@@ -28,7 +28,7 @@
 //===----------------------------------------------------------------------===//
 
 #include <hlvm/Writer/XML/XMLWriter.h>
-#include <hlvm/AST/Node.h>
+#include <hlvm/AST/AST.h>
 #include <hlvm/AST/Bundle.h>
 #include <iostream>
 #include <cassert>
@@ -70,7 +70,7 @@
 class XMLWriterImpl : public XMLWriter {
   ostream_indent ind_;
   std::ostream& out_;
-  AST::Node* node_;
+  AST::AST* node_;
 public:
   XMLWriterImpl(std::ostream& out)
     : ind_(out), out_(out), node_(0)
@@ -81,7 +81,7 @@
     out_.flush();
   }
 
-  virtual void write(AST::Node* node);
+  virtual void write(AST::AST* node);
 
 private:
   inline void putHeader();
@@ -127,9 +127,7 @@
 XMLWriterImpl::putHeader() 
 {
   out_ << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
-  out_ << "<!DOCTYPE hlvm PUBLIC \"-//HLVM/DTD HLVM 1.0//EN\" ";
-  out_ << "\"http://hlvm.org/src/hlvm/Reader/XML/HLVM.rng\">\n";
-  out_ << "<hlvm>";
+  out_ << "<hlvm xmlns=\"http://hlvm.org/src/hlvm/Reader/XML/HLVM.rng\">";
   ind_.in(true);
 }
 
@@ -143,10 +141,11 @@
 inline void 
 XMLWriterImpl::put(AST::Bundle* b)
 {
-  out_ << "<bundle pubId=\"" << b->getName() << "\">";
+  out_ << "<bundle pubid=\"" << b->getName() << "\">";
   ind_.in(true);
   ind_.out(true);
   out_ << "</bundle>";
+  ind_.out(false);
 }
 
 void
@@ -269,11 +268,11 @@
 }
 
 void
-XMLWriterImpl::write(AST::Node* node) 
+XMLWriterImpl::write(AST::AST* ast) 
 {
-  node_ = node;
+  node_ = ast;
   putHeader();
-  put(node);
+  put(ast->getRoot());
   putFooter();
 }
 

Modified: hlvm/trunk/test/xml2xml/bundle.hlx
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/test/xml2xml/bundle.hlx?rev=38050&r1=38049&r2=38050&view=diff

==============================================================================
--- hlvm/trunk/test/xml2xml/bundle.hlx (original)
+++ hlvm/trunk/test/xml2xml/bundle.hlx Sat Jul  7 18:59:19 2007
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <hlvm xmlns="http://hlvm.org/src/hlvm/Reader/XML/HLVM.rng">
  <bundle pubid="name">
+
  </bundle>
 </hlvm>

Modified: hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp
URL: http://llvm.org/viewvc/llvm-project/hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp?rev=38050&r1=38049&r2=38050&view=diff

==============================================================================
--- hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp (original)
+++ hlvm/trunk/tools/hlvm-xml2xml/hlvm-xml2xml.cpp Sat Jul  7 18:59:19 2007
@@ -91,7 +91,7 @@
     XMLReader* rdr = XMLReader::create(InputFilename);
     XMLWriter* wrtr = XMLWriter::create(*Out);
     rdr->read();
-    AST::Node* node = rdr->get();
+    AST::AST* node = rdr->get();
     if (node) {
       wrtr->write(node);
     }





More information about the llvm-commits mailing list