467,122 Members | 1,292 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,122 developers. It's quick & easy.

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

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

May 10 '06 #1
  • viewed: 1195
Share:
2 Replies
glomde wrote:
<questions on how to extend Python syntax>

You should look at the compiler package and at the new AST in Python
2.5.
For easy things tokenize could be enough, though. Notice that in Python
2.5 tokenize
grew a new 'untokenize' function which is pretty useful. See
http://docs.python.org/dev/lib/module-tokenize.html for examples.
Michele Simionato

May 10 '06 #2
"glomde" <tb****@yahoo.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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.


It's not quite so syntactically sugary, but it looks like much of the
functionality you are looking for is in elementTree, which will be
incorporated into 2.5 (or you can download and install yourself from
http://www.effbot.org/downloads/#elementtree).

-- Paul
May 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

54 posts views Thread by Brandon J. Van Every | last post: by
699 posts views Thread by mike420@ziplip.com | last post: by
7 posts views Thread by Michele Simionato | last post: by
24 posts views Thread by Xah Lee | last post: by
16 posts views Thread by qwweeeit | last post: by
4 posts views Thread by Kelvin@!!! | last post: by
53 posts views Thread by Deniz Bahar | last post: by
1 post views Thread by Tim | last post: by
1 post views Thread by Alan Isaac | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.