📄 parser.h
字号:
#ifndef __PARSER_H_#define __PARSER_H_#include <stdio.h>#include <stdlib.h>#include <string>#include <map>//Project includes #include "TemplateContainer.h"#include "TemplateObject.h"#include "TemplateError.h"typedef enum {ID,STRING,EQ,LT,GT,VZ,SLASH,SPACE,ESCAPE,OTHER} token_type;struct token { string image; token_type type; int line,pos; // line and position of token's first symbol };struct TmplTag { int begin_line,begin_pos; string name; map<string,string> args;};typedef void (*tag_handler)(TemplateContainer *t,TemplateObject *,TmplTag &tag,bool,FILE *);class Parser {public: Parser(); Parser(TemplateContainer *t,TemplateObject *curr,string end_tag,FILE *out_stream=stdout); void addHandler(string tag,tag_handler handler); void setTemplate(TemplateContainer *t); void setCurrent(TemplateObject *curr); void setEndTag(string &end_tag); void setOutStream(FILE *out_stream); void setActive(bool active) { is_active=active; } void parse();private: TemplateContainer *tmpl; TemplateObject *current; string end_tag; FILE *out; map<string,tag_handler> tag_handlers; // map of pointers to functions to be called when tag occured bool is_active; private: token getToken(); inline char nextpos(); inline void print(token t); inline void step_back();};inline char Parser::nextpos() { return tmpl->next();}inline void Parser::print(token t) { if(is_active) { if(t.type==STRING) { fputs("\"",out); fputs(t.image.c_str(),out); fputs("\"",out); } else fputs(t.image.c_str(),out); }}inline void Parser::step_back() { tmpl->prev(); }// Error handling ...class ParserError: public TemplateError {public: ParserError():TemplateError() { } ParserError(string error,token t):TemplateError(error) { char buf[256]; snprintf(buf,256,"At %d:%d> ",t.line,t.pos); error_msg=string(buf)+error; } ParserError(string error,TmplTag t) : TemplateError(error) { char buf[256]; map<string,string>::const_iterator ci=t.args.begin(); snprintf(buf,255,"%d:%d",t.begin_line,t.begin_pos); error_msg=string("At ")+buf+"> "+error+"\n"; error_msg+="Here's a list of arguments of "+t.name+":\n"; while(ci!=t.args.end()) { error_msg+=ci->first+"="+ci->second+"\n"; ci++; } }}; #endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -