My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 4636 of file template.cpp.

Constructor & Destructor Documentation

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

Definition at line 4655 of file template.cpp.

References m_closeChar, and m_openChar.

:
m_engine(engine), m_fileName(fileName), m_data(data)
{
}

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 4865 of file template.cpp.

References TemplateToken::Text.

Referenced by tokenize().

{
if (startPos<endPos)
{
int len = endPos-startPos+1;
QCString text(len);
qstrncpy(text.rawData(),data+startPos,len);
if (type!=TemplateToken::Text) text = text.stripWhiteSpace();
tokens.append(new TemplateToken(type,text,line));
}
}
void TemplateLexer::reset ( )
private
void TemplateLexer::setOpenCloseCharacters ( char  openChar,
char  closeChar 
)
inline

Definition at line 4641 of file template.cpp.

References m_closeChar, and m_openChar.

Referenced by TemplateImpl::TemplateImpl().

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

Definition at line 4662 of file template.cpp.

References addToken(), TemplateToken::Block, m_closeChar, m_data, m_engine, m_fileName, m_openChar, TemplateEngine::printIncludeContext(), TemplateToken::Text, TemplateToken::Variable, and warn().

Referenced by TemplateImpl::TemplateImpl().

{
enum LexerStates
{
StateText,
StateBeginTemplate,
StateTag,
StateEndTag,
StateComment,
StateEndComment,
StateMaybeVar,
StateVariable,
StateEndVariable
};
const char *p=m_data.data();
if (p==0) return;
int state=StateText;
int pos=0;
int lastTokenPos=0;
int startLinePos=0;
bool emptyOutputLine=TRUE;
int line=1;
char c;
int markStartPos=-1;
for (;(c=*p);p++,pos++)
{
switch (state)
{
case StateText:
if (c==m_openChar) // {{ or {% or {# or something else
{
state=StateBeginTemplate;
}
else if (c!=' ' && c!='\t' && c!='\n') // non-whitepace text
{
emptyOutputLine=FALSE;
}
break;
case StateBeginTemplate:
switch (c)
{
case '%': // {%
state=StateTag;
markStartPos=pos-1;
break;
case '#': // {#
state=StateComment;
markStartPos=pos-1;
break;
case '{': // {{
if (m_openChar=='{')
{
state=StateMaybeVar;
}
else
{
state=StateVariable;
}
markStartPos=pos-1;
break;
default:
state=StateText;
emptyOutputLine=FALSE;
break;
}
break;
case StateTag:
if (c=='\n')
{
warn(m_fileName,line,"unexpected new line inside %c%%...%%%c block",m_openChar,m_closeChar);
}
else if (c=='%') // %} or something else
{
state=StateEndTag;
}
break;
case StateEndTag:
if (c==m_closeChar) // %}
{
// found tag!
state=StateText;
addToken(tokens,m_data.data(),line,lastTokenPos,
emptyOutputLine ? startLinePos : markStartPos,
addToken(tokens,m_data.data(),line,markStartPos+2,
lastTokenPos = pos+1;
}
else // something else
{
if (c=='\n')
{
warn(m_fileName,line,"unexpected new line inside %c%%...%%%c block",m_openChar,m_closeChar);
}
state=StateTag;
}
break;
case StateComment:
if (c=='\n')
{
warn(m_fileName,line,"unexpected new line inside %c#...#%c block",m_openChar,m_closeChar);
}
else if (c=='#') // #} or something else
{
state=StateEndComment;
}
break;
case StateEndComment:
if (c==m_closeChar) // #}
{
// found comment tag!
state=StateText;
addToken(tokens,m_data.data(),line,lastTokenPos,
emptyOutputLine ? startLinePos : markStartPos,
lastTokenPos = pos+1;
}
else // something else
{
if (c=='\n')
{
warn(m_fileName,line,"unexpected new line inside %c#...#%c block",m_openChar,m_closeChar);
}
state=StateComment;
}
break;
case StateMaybeVar:
switch (c)
{
case '#': // {{#
state=StateComment;
markStartPos=pos-1;
break;
case '%': // {{%
state=StateTag;
markStartPos=pos-1;
break;
default: // {{
state=StateVariable;
break;
}
break;
case StateVariable:
emptyOutputLine=FALSE; // assume a variable expands to content
if (c=='\n')
{
warn(m_fileName,line,"unexpected new line inside %c{...}%c block",m_openChar,m_closeChar);
}
else if (c=='}') // }} or something else
{
state=StateEndVariable;
}
break;
case StateEndVariable:
if (c==m_closeChar) // }}
{
// found variable tag!
state=StateText;
addToken(tokens,m_data.data(),line,lastTokenPos,
emptyOutputLine ? startLinePos : markStartPos,
addToken(tokens,m_data.data(),line,markStartPos+2,
lastTokenPos = pos+1;
}
else // something else
{
if (c=='\n')
{
warn(m_fileName,line,"unexpected new line inside %c{...}%c block",m_openChar,m_closeChar);
}
state=StateVariable;
}
break;
}
if (c=='\n') // new line
{
state=StateText;
startLinePos=pos+1;
// if the current line only contain commands and whitespace,
// then skip it in the output by moving lastTokenPos
if (markStartPos!=-1 && emptyOutputLine) lastTokenPos = startLinePos;
// reset markers
markStartPos=-1;
line++;
emptyOutputLine=TRUE;
}
}
if (lastTokenPos<pos)
{
addToken(tokens,m_data.data(),line,
lastTokenPos,pos,
}
}

Member Data Documentation

char TemplateLexer::m_closeChar
private

Definition at line 4652 of file template.cpp.

Referenced by setOpenCloseCharacters(), TemplateLexer(), and tokenize().

QCString TemplateLexer::m_data
private

Definition at line 4650 of file template.cpp.

Referenced by tokenize().

const TemplateEngine* TemplateLexer::m_engine
private

Definition at line 4648 of file template.cpp.

Referenced by tokenize().

QCString TemplateLexer::m_fileName
private

Definition at line 4649 of file template.cpp.

Referenced by tokenize().

char TemplateLexer::m_openChar
private

Definition at line 4651 of file template.cpp.

Referenced by setOpenCloseCharacters(), TemplateLexer(), and tokenize().


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