/** @file parser.h * * macros suitable for writing parsers. * */ /******************************************************************************/ #ifndef PARSER_H #define PARSER_H /** * return from a parser function. setting state for reentry. */ #define PARSER_RETURN(state, n) \ do { (state)=__LINE__; return (n); case __LINE__: ; } while(0) /** * wrap parser code with these macros to create a re-entry state machine. * @see PARSER_END */ #define PARSER_BEGIN(state) switch((state)) { parser_entry: case 0: /** * wrap parser code with these macros to create a re-entry state machine. * @see PARSER_BEGIN */ #define PARSER_END } /** * wait until the condition is true, forces parser function to return n until * cond is true. */ #define PARSER_UNTIL(state, n, cond) while(!(cond)) { PARSER_RETURN(state, n); } /** * restart the parser. */ #define PARSER_RESTART(state) do { (state)=0; goto parser_entry; } while(0) /** * macro used for appending a character to a buffer. */ #define PARSER_BUFFER_APPEND(buf, ofs, max, ch) \ do { if((ofs)<(max)) (buf)[(ofs)++]=ch; } while(0) #endif