Subversion Repositories spk

Rev

Rev 1 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
  defines bod_text_parser - class that can parse bod syntax at general level
*/

#ifndef BOD_TEXT_PARSER_INCLUDED
#define BOD_TEXT_PARSER_INCLUDED

#include "../common/ext_list.h"

class bod_text_parser
{
        public:
                enum ParseFlags
                {
                        none=0,
                        parseIgnoreSlash=1,
                        parseEqual=2,
                        parseStdBrackets=4,
                        parseQuotedStrings=8,
                        parseBODComments=16,
                        parseCComments=32,
                        parseOperators=64
                };
                
                class Error
                {
                        private:
                                static const char *m_messages[];
                        public:
                                enum Code {
                                        errNone,
                                        errNewLineInConstant
                                };
                                Code code;
                                int col, line;
                                
                                Error() { col=line=-1; code=errNone; } 
                                
                                const char* message() const { return m_messages[code]; }
                };
                
                /*enum Error
                {
                        errNone,
                        errNewLineInConstant
                };*/
                
                struct token
                {
                        /* 
                                before messing with the order of the enums, 
                                make sure it will not break X3_TOKEN* declarations in x3objects.h 
                        */
                        enum Type{
                                t_openCrBracket,
                                t_closeCrBracket,
                                t_semicolon,
                                t_colon,
                                t_equal,
                                t_openInstrBlock,
                                t_closeInstrBlock,
                                t_hdrinfo,
                                t_openStdBracket,
                                t_closeStdBracket,
                                t_slash,
                                t_dot,
                                t_plus,
                                t_text,
                                t_quotedString
                        };
                        
                        static const char* specialChars[];
                        static char tabWidth;
                        
                        const char *text;

                        int line;
                        short col;
                        
                        Type type;
                        
                        token() { text=0; tabWidth=4; }
                        token(const token& t) { text=t.text; line=t.line; col=t.col; type=t.type; }
                        virtual ~token() { }
                        
                        const char* getText() const;
                        
                };
                
        private:
                size_t m_lastPos;
                size_t m_newPos;
                
                const char *m_pszBuffer;
                size_t m_buffLen;
                int m_lineIdx;
                
                Error m_error;
                ParseFlags m_parseFlags;
                
                bool testFlag(ParseFlags f) const { return (parseFlags() & f) > 0; }
                
                bool parseLine(char *line, int idx);
        
                void error(int line, int col, Error::Code code) 
                { 
                        m_error.code=code; 
                        m_error.col=col; m_error.line=line;
                        // set the last pos to end so eof() will return true
                        if(code!=Error::errNone)
                                m_lastPos=m_buffLen; 
                }
        
        public:
                char * nextLine(); // public because of use in x3objects which is hack anyway
                
        public:
                typedef ext::list<token*> TokenList;
                typedef TokenList::iterator iterator;
                TokenList tokens;
                
                bod_text_parser() 
                { 
                        tabWidth(4);
                        m_lastPos=0; m_newPos=0;
                        m_pszBuffer=0;
                        m_buffLen=0;
                        m_lineIdx=0;
                        parseFlags(none);
                }
                
                ~bod_text_parser()
                {
                        for(iterator &it=tokens.begin(); it!=tokens.end(); ++it){
                                delete *it;
                        }
                }
                
                Error error() const { return m_error; }
                
                bool eof() const { return m_lastPos >= m_buffLen; }
                
                void tabWidth(int width) { token::tabWidth=width; }
                int tabWidth() const { return token::tabWidth; }
                
                void parseFlags(ParseFlags newf) { m_parseFlags=newf; }
                ParseFlags parseFlags() const { return m_parseFlags; }
                
                void preParseBuffer(char *pszBuffer, size_t size);
                size_t parseBuffer(size_t limit=-1);
};

#endif // !defined(BOD_TEXT_PARSER_INCLUDED)