Subject: Re: simple templating system
From: D.Hendriks (Dennis) <[email protected]>
To: [email protected] <[email protected]>
Date: Tue Nov 24 2009 08:09:23 GMT+0100 (CET)
> Hello Alessandro,
>
> This rule:
>
> > def t_OTHER(t):
> > r'[^ $]+'
> > return t
>
> is a negative rule. It matches way to much. You should probably specify
> it in a positive way. I recommend introducing a t_ID to scan identifiers.
>
> Also, take a look at http://www.dabeaz.com/ply/ply.html (sections 4.3
> and 4.4). This explains how to do lexing of identifiers vs keywords.
>
> Best,
> Dennis
>
Thanks Dennis.
I solved with this code:
....
tokens = (
'OTHER', 'FUNCTION'
)
literals = [ '(',')' ]
def t_FUNCTION(t):
r'\$FUNCTION'
return t
def t_OTHER(t):
r'[^ $\(\)]+'
return t
t_ignore = " \t"
...
def p_expression(p):
'''expression : function expression
| OTHER expression
| parexpression expression
| empty'''
p[0] = ' '.join([str(x) for x in p[1:] ])
def p_parexpression(p):
'''parexpression : '(' expression ')' '''
p[0] = "( %s )" % p[2]
def p_function(p):
"function : FUNCTION parexpression "
p[0]= '<%s>' % p[1] + p[2] # some modifs.
def p_empty(p):
'empty :'
p[0] = ''
.....
I think I must use the negative rule because I want a template system.
Now I understand why most templating language are using particular
tokens to go into a "second state" where you can use the templating
language. For example:
bla bla bla
${
if ..
for...
..
}$
bla bla
The "${" and the "$}" simplify the parsing very much!
Alessandro
--
You received this message because you are subscribed to the Google Groups
"ply-hack" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/ply-hack?hl=en.