Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C++

Parsing XML in C++ using the YARD Parser

Rate me:
Please Sign up or sign in to vote.
4.79/5 (23 votes)
21 Dec 20046 min read 87K   1.2K   39  
Provides a set of tools for building XML parsers in C++ using the YARD recursive descent parser.
// released into the public domain 
// by Christopher Diggins 2004 
// http://www.cdiggins.com 

#ifndef RULES_HPP_INCLUDED 
#define RULES_HPP_INCLUDED  

#include "re_ops.hpp"

namespace yard 
{
  template<char C>
  struct MatchChar
  {
    static bool Accept(ParserInputStream<char>& stream) {
      return (stream.AdvancePast(C));
    }
  };

  template<char C0, char C1>
  struct MatchCharRange
  {
    static bool Accept(ParserInputStream<char>& stream) {
      char ch = stream.GetElem();
      if ((ch >= C0) && (ch <= C1)) {
        stream.GotoNext();         
        return true;
      }
      return false;
    }
  };
  
  template<typename String_T>
  struct MatchString 
  {  
    static bool Accept(ParserInputStream<char>& stream) {      
      char const* p = String_T::GetString();
      for (int i = 0; p[i] != NULL; ++i) { 
        if (stream.AtEnd()) return false;
        char ch = stream.GetElem();
        stream.GotoNext();        
        if (ch != p[i]) return false;
      }
      return true;
    }
  };
 
  template<typename String_T> // expected to be lower-case 
  struct MatchStringNoCase 
  {  
    static bool Accept(ParserInputStream<char>& stream) {      
      char const* p = String_T::GetString();
      for (int i = 0; p[i] != NULL; ++i) { 
        if (stream.AtEnd()) return false;
        char ch = stream.GetElem();
        stream.GotoNext();                
        if (ch < 96) ch += 32; 
        if (ch != p[i]) return false;
      }
      return true;
    }
  };
  
  struct MatchWSpace : public 
    re_or<
      re_or<
        MatchChar<' '>, 
        MatchChar<'\t'> 
      >, 
      re_or<
        MatchChar<'\n'>, 
        MatchChar<'\r'> 
      > 
    >
    { };

  typedef MatchCharRange<'a', 'z'> MatchLowerCaseLetter;
  typedef MatchCharRange<'A', 'Z'> MatchUpperCaseLetter; 
  typedef MatchCharRange<'0', '9'> MatchNumber;
  
  typedef re_or<MatchLowerCaseLetter, MatchUpperCaseLetter> MatchLetter;
  typedef re_or<MatchLetter, MatchChar<'\''> > MatchWordChar;
  typedef re_plus<MatchWordChar> MatchWord;
  typedef re_or<MatchLetter, MatchChar<'_'> > MatchIdentFirstChar;
  typedef re_or<MatchIdentFirstChar, MatchNumber> MatchIdentOtherChar;
  typedef re_and<MatchIdentFirstChar, re_star<MatchIdentOtherChar> > MatchIdent;
}

#endif // #ifndef RULES_HPP_INCLUDED 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer Ara 3D
Canada Canada
I am the designer of the Plato programming language and I am the founder of Ara 3D. I can be reached via email at cdiggins@gmail.com

Comments and Discussions