Doxygen
TemplateLexer类 参考

Lexer class for turning a template into a list of tokens 更多...

+ TemplateLexer 的协作图:

Public 成员函数

 TemplateLexer (const TemplateEngine *engine, const QCString &fileName, const QCString &data)
 
void tokenize (TemplateTokenStream &tokens)
 
void setOpenCloseCharacters (char openChar, char closeChar)
 

Private 成员函数

void addToken (TemplateTokenStream &tokens, const QCString &data, int line, int startPos, int endPos, TemplateToken::Type type)
 
void reset ()
 

Private 属性

const TemplateEnginem_engine = 0
 
QCString m_fileName
 
QCString m_data
 
char m_openChar = 0
 
char m_closeChar = 0
 

详细描述

Lexer class for turning a template into a list of tokens

在文件 template.cpp4805 行定义.

构造及析构函数说明

◆ TemplateLexer()

TemplateLexer::TemplateLexer ( const TemplateEngine engine,
const QCString fileName,
const QCString data 
)

在文件 template.cpp4824 行定义.

4824  :
4825  m_engine(engine), m_fileName(fileName), m_data(data)
4826 {
4827  m_openChar='{';
4828  m_closeChar='}';
4829 }

引用了 m_closeChar , 以及 m_openChar.

成员函数说明

◆ addToken()

void TemplateLexer::addToken ( TemplateTokenStream tokens,
const QCString data,
int  line,
int  startPos,
int  endPos,
TemplateToken::Type  type 
)
private

在文件 template.cpp5034 行定义.

5038 {
5039  if (startPos<endPos)
5040  {
5041  int len = endPos-startPos;
5042  QCString text = data.mid(startPos,len);
5043  if (type!=TemplateToken::Text) text = text.stripWhiteSpace();
5044  tokens.push_back(std::make_unique<TemplateToken>(type,text,line));
5045  }
5046 }

引用了 QCString::mid(), QCString::stripWhiteSpace() , 以及 TemplateToken::Text.

被这些函数引用 tokenize().

◆ reset()

void TemplateLexer::reset ( )
private

◆ setOpenCloseCharacters()

void TemplateLexer::setOpenCloseCharacters ( char  openChar,
char  closeChar 
)
inline

在文件 template.cpp4810 行定义.

4811  { m_openChar=openChar; m_closeChar=closeChar; }

引用了 m_closeChar , 以及 m_openChar.

被这些函数引用 TemplateImpl::TemplateImpl().

◆ tokenize()

void TemplateLexer::tokenize ( TemplateTokenStream tokens)

在文件 template.cpp4831 行定义.

4832 {
4833  enum LexerStates
4834  {
4835  StateText,
4836  StateBeginTemplate,
4837  StateTag,
4838  StateEndTag,
4839  StateComment,
4840  StateEndComment,
4841  StateMaybeVar,
4842  StateVariable,
4843  StateEndVariable
4844  };
4845 
4846  if (m_data.isEmpty()) return;
4847  const char *p=m_data.data();
4848  int state=StateText;
4849  int pos=0;
4850  int lastTokenPos=0;
4851  int startLinePos=0;
4852  bool emptyOutputLine=TRUE;
4853  int line=1;
4854  char c;
4855  int markStartPos=-1;
4856  for (;(c=*p);p++,pos++)
4857  {
4858  switch (state)
4859  {
4860  case StateText:
4861  if (c==m_openChar) // {{ or {% or {# or something else
4862  {
4863  state=StateBeginTemplate;
4864  }
4865  else if (c!=' ' && c!='\t' && c!='\n') // non-whitespace text
4866  {
4867  emptyOutputLine=FALSE;
4868  }
4869  break;
4870  case StateBeginTemplate:
4871  switch (c)
4872  {
4873  case '%': // {%
4874  state=StateTag;
4875  markStartPos=pos-1;
4876  break;
4877  case '#': // {#
4878  state=StateComment;
4879  markStartPos=pos-1;
4880  break;
4881  case '{': // {{
4882  if (m_openChar=='{')
4883  {
4884  state=StateMaybeVar;
4885  }
4886  else
4887  {
4888  state=StateVariable;
4889  }
4890  markStartPos=pos-1;
4891  break;
4892  default:
4893  state=StateText;
4894  emptyOutputLine=FALSE;
4895  break;
4896  }
4897  break;
4898  case StateTag:
4899  if (c=='\n')
4900  {
4901  warn(m_fileName,line,"unexpected new line inside %c%%...%%%c block",m_openChar,m_closeChar);
4903  }
4904  else if (c=='%') // %} or something else
4905  {
4906  state=StateEndTag;
4907  }
4908  break;
4909  case StateEndTag:
4910  if (c==m_closeChar) // %}
4911  {
4912  // found tag!
4913  state=StateText;
4914  addToken(tokens,m_data,line,lastTokenPos,
4915  emptyOutputLine ? startLinePos : markStartPos,
4917  addToken(tokens,m_data,line,markStartPos+2,
4918  pos-1,TemplateToken::Block);
4919  lastTokenPos = pos+1;
4920  }
4921  else // something else
4922  {
4923  if (c=='\n')
4924  {
4925  warn(m_fileName,line,"unexpected new line inside %c%%...%%%c block",m_openChar,m_closeChar);
4927  }
4928  state=StateTag;
4929  }
4930  break;
4931  case StateComment:
4932  if (c=='\n')
4933  {
4934  warn(m_fileName,line,"unexpected new line inside %c#...#%c block",m_openChar,m_closeChar);
4936  }
4937  else if (c=='#') // #} or something else
4938  {
4939  state=StateEndComment;
4940  }
4941  break;
4942  case StateEndComment:
4943  if (c==m_closeChar) // #}
4944  {
4945  // found comment tag!
4946  state=StateText;
4947  addToken(tokens,m_data,line,lastTokenPos,
4948  emptyOutputLine ? startLinePos : markStartPos,
4950  lastTokenPos = pos+1;
4951  }
4952  else // something else
4953  {
4954  if (c=='\n')
4955  {
4956  warn(m_fileName,line,"unexpected new line inside %c#...#%c block",m_openChar,m_closeChar);
4958  }
4959  state=StateComment;
4960  }
4961  break;
4962  case StateMaybeVar:
4963  switch (c)
4964  {
4965  case '#': // {{#
4966  state=StateComment;
4967  markStartPos=pos-1;
4968  break;
4969  case '%': // {{%
4970  state=StateTag;
4971  markStartPos=pos-1;
4972  break;
4973  default: // {{
4974  state=StateVariable;
4975  break;
4976  }
4977  break;
4978  case StateVariable:
4979  emptyOutputLine=FALSE; // assume a variable expands to content
4980  if (c=='\n')
4981  {
4982  warn(m_fileName,line,"unexpected new line inside %c{...}%c block",m_openChar,m_closeChar);
4984  }
4985  else if (c=='}') // }} or something else
4986  {
4987  state=StateEndVariable;
4988  }
4989  break;
4990  case StateEndVariable:
4991  if (c==m_closeChar) // }}
4992  {
4993  // found variable tag!
4994  state=StateText;
4995  addToken(tokens,m_data,line,lastTokenPos,
4996  emptyOutputLine ? startLinePos : markStartPos,
4998  addToken(tokens,m_data,line,markStartPos+2,
4999  pos-1,TemplateToken::Variable);
5000  lastTokenPos = pos+1;
5001  }
5002  else // something else
5003  {
5004  if (c=='\n')
5005  {
5006  warn(m_fileName,line,"unexpected new line inside %c{...}%c block",m_openChar,m_closeChar);
5008  }
5009  state=StateVariable;
5010  }
5011  break;
5012  }
5013  if (c=='\n') // new line
5014  {
5015  state=StateText;
5016  startLinePos=pos+1;
5017  // if the current line only contain commands and whitespace,
5018  // then skip it in the output by moving lastTokenPos
5019  if (markStartPos!=-1 && emptyOutputLine) lastTokenPos = startLinePos;
5020  // reset markers
5021  markStartPos=-1;
5022  line++;
5023  emptyOutputLine=TRUE;
5024  }
5025  }
5026  if (lastTokenPos<pos)
5027  {
5028  addToken(tokens,m_data,line,
5029  lastTokenPos,pos,
5031  }
5032 }

引用了 addToken(), TemplateToken::Block, QCString::data(), FALSE, QCString::isEmpty(), m_closeChar, m_data, m_engine, m_fileName, m_openChar, TemplateEngine::printIncludeContext(), TemplateToken::Text, TRUE, TemplateToken::Variable , 以及 warn().

被这些函数引用 TemplateImpl::TemplateImpl().

类成员变量说明

◆ m_closeChar

char TemplateLexer::m_closeChar = 0
private

在文件 template.cpp4821 行定义.

被这些函数引用 setOpenCloseCharacters(), TemplateLexer() , 以及 tokenize().

◆ m_data

QCString TemplateLexer::m_data
private

在文件 template.cpp4819 行定义.

被这些函数引用 tokenize().

◆ m_engine

const TemplateEngine* TemplateLexer::m_engine = 0
private

在文件 template.cpp4817 行定义.

被这些函数引用 tokenize().

◆ m_fileName

QCString TemplateLexer::m_fileName
private

在文件 template.cpp4818 行定义.

被这些函数引用 tokenize().

◆ m_openChar

char TemplateLexer::m_openChar = 0
private

在文件 template.cpp4820 行定义.

被这些函数引用 setOpenCloseCharacters(), TemplateLexer() , 以及 tokenize().


该类的文档由以下文件生成:
QCString::isEmpty
bool isEmpty() const
Returns TRUE iff the string is empty
Definition: qcstring.h:144
TemplateEngine::printIncludeContext
void printIncludeContext(const QCString &fileName, int line) const
Prints the current template file include stack
Definition: template.cpp:5401
warn
void warn(const QCString &file, int line, const char *fmt,...)
Definition: message.cpp:151
TemplateLexer::addToken
void addToken(TemplateTokenStream &tokens, const QCString &data, int line, int startPos, int endPos, TemplateToken::Type type)
Definition: template.cpp:5034
QCString::stripWhiteSpace
QCString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition: qcstring.h:243
TemplateLexer::m_openChar
char m_openChar
Definition: template.cpp:4820
TemplateLexer::m_engine
const TemplateEngine * m_engine
Definition: template.cpp:4817
TemplateLexer::m_fileName
QCString m_fileName
Definition: template.cpp:4818
TemplateLexer::m_closeChar
char m_closeChar
Definition: template.cpp:4821
TRUE
#define TRUE
Definition: qcstring.h:36
TemplateToken::Text
@ Text
Definition: template.cpp:1955
QCString::mid
QCString mid(size_t index, size_t len=static_cast< size_t >(-1)) const
Definition: qcstring.h:224
TemplateToken::Block
@ Block
Definition: template.cpp:1955
QCString::data
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string
Definition: qcstring.h:153
TemplateToken::Variable
@ Variable
Definition: template.cpp:1955
TemplateLexer::m_data
QCString m_data
Definition: template.cpp:4819
FALSE
#define FALSE
Definition: qcstring.h:33
QCString
This is an alternative implementation of QCString.
Definition: qcstring.h:108