What to use for adding syntax for hierarcical trees, metaclasses, tokenize.py or PLY?

glomde tbrkic at yahoo.com
Tue May 9 07:57:01 EDT 2006


Hi I would like to extend python so that you could create hiercical
tree structures (XML, HTML etc) easier and that resulting code would be
more readable.

The syntax i would like is something like the below:

# Example creating html tree

'*!*' is an operator that creates an new node
'*=*' is an operator that sets an attribute.

bodyNode = body()

*!* bodyNode:
      *=* color = blue
      *=* bg  = white
      for i in headings:
             *!* H1(heading[i]):


This would translate to something like this in python:
bodyNode = body()
if True:
   bodyNode.attr['color'] = blue
   bodyNode.attr['bg'] = white
   for i in headings:
             if True:
                bodyNode.append(H1(heading[i]))

I think that with the added syntax you get better overview on how your
tree looks like.

But the thing is how to implement my added syntax. I dont want to mess
with Python source code to add the syntax. So I searched a bit on the
net and think there might be three alternatves.

1. MetaClasses. I tried to understand them but my head almost exploded
:-). But my conclusion was that it is not possible with metaclasses.
Since this is a real syntax change.

2. Tokenize.py. I modify tokenize.py to recognize my new operators
'#!#' and '#=#' and then I write a parser that exports the code. This
option I understand somewhat how to implement. But I would reduce my
own code for the parser, so is there any parser that can handle
tokenize.py input? So that I could somehow write rules for my code.

3. Use PLY or any other python parser. Write rules for my language. But
would I need to write    rules that can handle the whole python
languager? Then this seems to be overkill. I would just
like to recognize my added syntax and convert that. So I would really
like to have a rule file that handles python and then add rules for my
syntax. Are there any Python rule file for the python language and a
python parser for it? Then I could just add my syntax to the rule file
and be able to create the output.

I would be very thankfull for any hints or thougts on how to do it.

Best regards,

G




More information about the Python-list mailing list