Public Member Functions | Private Member Functions | Private Attributes | List of all members
TemplateLexer Class Reference

Lexer class for turning a template into a list of tokens. More...

Public Member Functions

 TemplateLexer (const TemplateEngine *engine, const QCString &fileName, const QCString &data)
 
void tokenize (QList< TemplateToken > &tokens)
 
void setOpenCloseCharacters (char openChar, char closeChar)
 

Private Member Functions

void addToken (QList< TemplateToken > &tokens, const char *data, int line, int startPos, int endPos, TemplateToken::Type type)
 
void reset ()
 

Private Attributes

const TemplateEnginem_engine
 
QCString m_fileName
 
QCString m_data
 
char m_openChar
 
char m_closeChar
 

Detailed Description

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

Definition at line 4415 of file template.cpp.

Constructor & Destructor Documentation

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

Definition at line 4434 of file template.cpp.

4434  :
4435  m_engine(engine), m_fileName(fileName), m_data(data)
4436 {
4437  m_openChar='{';
4438  m_closeChar='}';
4439 }
QCString m_data
Definition: template.cpp:4429
QCString m_fileName
Definition: template.cpp:4428
const TemplateEngine * m_engine
Definition: template.cpp:4427

Member Function Documentation

void TemplateLexer::addToken ( QList< TemplateToken > &  tokens,
const char *  data,
int  line,
int  startPos,
int  endPos,
TemplateToken::Type  type 
)
private

Definition at line 4644 of file template.cpp.

4648 {
4649  if (startPos<endPos)
4650  {
4651  int len = endPos-startPos+1;
4652  QCString text(len);
4653  qstrncpy(text.rawData(),data+startPos,len);
4654  if (type!=TemplateToken::Text) text = text.stripWhiteSpace();
4655  tokens.append(new TemplateToken(type,text,line));
4656  }
4657 }
char * qstrncpy(char *dst, const char *src, uint len)
Definition: qcstring.cpp:557
void append(const type *d)
Definition: qlist.h:73
Class representing a lexical token in a template.
Definition: template.cpp:2226
void line(double t, double *p, double &x, double &y, double &z)
void TemplateLexer::reset ( )
private
void TemplateLexer::setOpenCloseCharacters ( char  openChar,
char  closeChar 
)
inline

Definition at line 4420 of file template.cpp.

4421  { m_openChar=openChar; m_closeChar=closeChar; }
void TemplateLexer::tokenize ( QList< TemplateToken > &  tokens)

Definition at line 4441 of file template.cpp.

4442 {
4443  enum LexerStates
4444  {
4445  StateText,
4446  StateBeginTemplate,
4447  StateTag,
4448  StateEndTag,
4449  StateComment,
4450  StateEndComment,
4451  StateMaybeVar,
4452  StateVariable,
4453  StateEndVariable
4454  };
4455 
4456  const char *p=m_data.data();
4457  if (p==0) return;
4458  int state=StateText;
4459  int pos=0;
4460  int lastTokenPos=0;
4461  int startLinePos=0;
4462  bool emptyOutputLine=TRUE;
4463  int line=1;
4464  char c;
4465  int markStartPos=-1;
4466  for (;(c=*p);p++,pos++)
4467  {
4468  switch (state)
4469  {
4470  case StateText:
4471  if (c==m_openChar) // {{ or {% or {# or something else
4472  {
4473  state=StateBeginTemplate;
4474  }
4475  else if (c!=' ' && c!='\t' && c!='\n') // non-whitepace text
4476  {
4477  emptyOutputLine=FALSE;
4478  }
4479  break;
4480  case StateBeginTemplate:
4481  switch (c)
4482  {
4483  case '%': // {%
4484  state=StateTag;
4485  markStartPos=pos-1;
4486  break;
4487  case '#': // {#
4488  state=StateComment;
4489  markStartPos=pos-1;
4490  break;
4491  case '{': // {{
4492  if (m_openChar=='{')
4493  {
4494  state=StateMaybeVar;
4495  }
4496  else
4497  {
4498  state=StateVariable;
4499  }
4500  markStartPos=pos-1;
4501  break;
4502  default:
4503  state=StateText;
4504  emptyOutputLine=FALSE;
4505  break;
4506  }
4507  break;
4508  case StateTag:
4509  if (c=='\n')
4510  {
4511  warn(m_fileName,line,"unexpected new line inside %c%%...%%%c block",m_openChar,m_closeChar);
4513  }
4514  else if (c=='%') // %} or something else
4515  {
4516  state=StateEndTag;
4517  }
4518  break;
4519  case StateEndTag:
4520  if (c==m_closeChar) // %}
4521  {
4522  // found tag!
4523  state=StateText;
4524  addToken(tokens,m_data.data(),line,lastTokenPos,
4525  emptyOutputLine ? startLinePos : markStartPos,
4527  addToken(tokens,m_data.data(),line,markStartPos+2,
4528  pos-1,TemplateToken::Block);
4529  lastTokenPos = pos+1;
4530  }
4531  else // something else
4532  {
4533  if (c=='\n')
4534  {
4535  warn(m_fileName,line,"unexpected new line inside %c%%...%%%c block",m_openChar,m_closeChar);
4537  }
4538  state=StateTag;
4539  }
4540  break;
4541  case StateComment:
4542  if (c=='\n')
4543  {
4544  warn(m_fileName,line,"unexpected new line inside %c#...#%c block",m_openChar,m_closeChar);
4546  }
4547  else if (c=='#') // #} or something else
4548  {
4549  state=StateEndComment;
4550  }
4551  break;
4552  case StateEndComment:
4553  if (c==m_closeChar) // #}
4554  {
4555  // found comment tag!
4556  state=StateText;
4557  addToken(tokens,m_data.data(),line,lastTokenPos,
4558  emptyOutputLine ? startLinePos : markStartPos,
4560  lastTokenPos = pos+1;
4561  }
4562  else // something else
4563  {
4564  if (c=='\n')
4565  {
4566  warn(m_fileName,line,"unexpected new line inside %c#...#%c block",m_openChar,m_closeChar);
4568  }
4569  state=StateComment;
4570  }
4571  break;
4572  case StateMaybeVar:
4573  switch (c)
4574  {
4575  case '#': // {{#
4576  state=StateComment;
4577  markStartPos=pos-1;
4578  break;
4579  case '%': // {{%
4580  state=StateTag;
4581  markStartPos=pos-1;
4582  break;
4583  default: // {{
4584  state=StateVariable;
4585  break;
4586  }
4587  break;
4588  case StateVariable:
4589  emptyOutputLine=FALSE; // assume a variable expands to content
4590  if (c=='\n')
4591  {
4592  warn(m_fileName,line,"unexpected new line inside %c{...}%c block",m_openChar,m_closeChar);
4594  }
4595  else if (c=='}') // }} or something else
4596  {
4597  state=StateEndVariable;
4598  }
4599  break;
4600  case StateEndVariable:
4601  if (c==m_closeChar) // }}
4602  {
4603  // found variable tag!
4604  state=StateText;
4605  addToken(tokens,m_data.data(),line,lastTokenPos,
4606  emptyOutputLine ? startLinePos : markStartPos,
4608  addToken(tokens,m_data.data(),line,markStartPos+2,
4609  pos-1,TemplateToken::Variable);
4610  lastTokenPos = pos+1;
4611  }
4612  else // something else
4613  {
4614  if (c=='\n')
4615  {
4616  warn(m_fileName,line,"unexpected new line inside %c{...}%c block",m_openChar,m_closeChar);
4618  }
4619  state=StateVariable;
4620  }
4621  break;
4622  }
4623  if (c=='\n') // new line
4624  {
4625  state=StateText;
4626  startLinePos=pos+1;
4627  // if the current line only contain commands and whitespace,
4628  // then skip it in the output by moving lastTokenPos
4629  if (markStartPos!=-1 && emptyOutputLine) lastTokenPos = startLinePos;
4630  // reset markers
4631  markStartPos=-1;
4632  line++;
4633  emptyOutputLine=TRUE;
4634  }
4635  }
4636  if (lastTokenPos<pos)
4637  {
4638  addToken(tokens,m_data.data(),line,
4639  lastTokenPos,pos,
4641  }
4642 }
const bool FALSE
Definition: qglobal.h:370
void printIncludeContext(const char *fileName, int line) const
Definition: template.cpp:4991
QCString m_data
Definition: template.cpp:4429
QCString m_fileName
Definition: template.cpp:4428
p
Definition: test.py:223
const char * data() const
Definition: qcstring.h:207
void warn(const char *file, int line, const char *fmt,...)
Definition: message.cpp:183
void line(double t, double *p, double &x, double &y, double &z)
void addToken(QList< TemplateToken > &tokens, const char *data, int line, int startPos, int endPos, TemplateToken::Type type)
Definition: template.cpp:4644
const TemplateEngine * m_engine
Definition: template.cpp:4427
const bool TRUE
Definition: qglobal.h:371

Member Data Documentation

char TemplateLexer::m_closeChar
private

Definition at line 4431 of file template.cpp.

QCString TemplateLexer::m_data
private

Definition at line 4429 of file template.cpp.

const TemplateEngine* TemplateLexer::m_engine
private

Definition at line 4427 of file template.cpp.

QCString TemplateLexer::m_fileName
private

Definition at line 4428 of file template.cpp.

char TemplateLexer::m_openChar
private

Definition at line 4430 of file template.cpp.


The documentation for this class was generated from the following file: