Hunter的大杂烩 技术学习笔记

2005-09-15

html模板引擎之yacc语法分析

Filed under: HHTE — hunter @ 11:51 am

%{
/*
* Copyright (c) 2005 Hunter (whywhathow at hotmail dot com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the Hunter nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*!
* \file hhte_template.l
* \brief H html template engine’s yacc parser, some code from php3.
* \author HunterLiu
* \date 2005-9-15
* \version 1.1
*/

#include “hhte_struct.h”
#include “hhte_template.tab.h”
#include “h_html_template.h”
#include
#include

#define YYDEBUG 1
extern CTemplate g_cHTemplate;
extern void end_hhte();
//void yyerror(const char *s);
void yyerror(const char *s)
{
fprintf(stderr, “%s\n”, s);
}

%}

%union {
char *sval;
}

%token HHTE_LOOP
%token HHTE_ECHO
%token HHTE_DONE
%token END_HHTE
%token INLINE_HTML
%token HHTE_STRING

%type label
%type var

%error-verbose
%%

statement_list:
statement_list statement
| /* empty */
;
statement:
‘{‘ statement_list ‘}’
| INLINE_HTML {
boost::shared_ptr cSymbol(new CStringSymbol($1));
g_cHTemplate.getSymbolManager()->addSymbol(cSymbol);
free($1);
}
| HHTE_LOOP label {
boost::shared_ptr cSymbol(new CLoopSymbol($2));
g_cHTemplate.getSymbolManager()->addSymbol(cSymbol);
free($2);
}
statement_list HHTE_DONE { g_cHTemplate.getSymbolManager()->setState(SYMBOL_DONE); }
| HHTE_ECHO var end_statement {
boost::shared_ptr cSymbol(new CTagSymbol($2));
g_cHTemplate.getSymbolManager()->addSymbol(cSymbol);
free($2);
}
| END_HHTE { end_hhte(); }
;

end_statement:
‘;’
| END_HHTE { end_hhte(); }
;

var:
‘$’ HHTE_STRING { $$=$2; }
;

label: ‘:’HHTE_STRING {
$$=$2;
}
;

%%

html模板引擎之lex词法分析

Filed under: HHTE — hunter @ 11:50 am

%{
/*
* Copyright (c) 2005 Hunter (whywhathow at hotmail dot com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the Hunter nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*!
* \file hhte_template.l
* \brief H html template engine’s lex parser, some code from php3.
* \author HunterLiu
* \date 2005-9-15
* \version 1.1
*/
%}

%x IN_HHTE

%{

#include “hhte_template.tab.h”
#include “h_html_template.h”
#include

#define YYDEBUG 1
#undef YY_INPUT
#define YY_INPUT(buf,retval,max) ( retval = hhte_yyinput(buf, max) )

extern CTemplate g_cHTemplate;
int iCurrentPtr = 0;
int hhte_yyinput(char *buf, int max_size)
{
int iBufferSize = (max_size if (iBufferSize>0)
{
string sTmpString = g_cHTemplate.getBuffer(iCurrentPtr, max_size);
memcpy(buf, sTmpString.c_str(), iBufferSize);
iCurrentPtr += iBufferSize;
}
return iBufferSize;
}

void end_hhte()
{
BEGIN(INITIAL);
}

%}

LNUM [0-9]+
DNUM [0-9]*[\.][0-9]+
HNUM “0x”[0-9a-fA-F]+
LABEL [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
WHITESPACE [ \n\r\t]+
TOKENS [;:,.\[\]()|^&+-/*=%!~$<>{}?@]
ENCAPSED_TOKENS [\[\]\\$]
ESCAPED_AND_WHITESPACE [\n\t\r #`’.:;,()|^&+-/*=%!~<>{}?@]+

%option lex-compat
%option 8bit

%%

“loop” {
return HHTE_LOOP;
}

“done” {
return HHTE_DONE;
}

“echo” {
return HHTE_ECHO;
}

{TOKENS} {
return yytext[0];
}

([^< ]|"<"[^%]){1,400} { yylval.sval = strdup(yytext); return INLINE_HTML; } "< %" { BEGIN(IN_HHTE); } "%>” {

return END_HHTE;
}

{LABEL} {

yylval.sval = strdup(yytext);
return HHTE_STRING;
}

{WHITESPACE} {

; /* 忽略空格 */
}

“/*”([^*]|[*]+[^/])*”*/” {
; /* 忽略注释 */
}

<> {
fprintf(stderr, “…………\n”);
yyterminate();
}

Powered by WordPress