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

G

glomde

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):


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 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
 
M

Michele Simionato

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
 
P

Paul McGuire

glomde said:
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
 
G

glomde

Thanks,

but the thing is that I want to have the syntactical sugar.
And I think that you could use elementtree or xist or any
other python tree structure module at the bottom. It could
be quite generic.

And with the syntactical sugar I find it a lot more readable.
A little bit like yaml, but you have the power of python aswell.

For example with syntactical sugar:

# build a tree structure
root = ET.Element("html")
*!*root:
*!*head("head"):
*!*title("title):
*=*text = "Page Title"
*!*body("body"):
*=*bgcolor = "#ffffff"
*=*text = "Hello, World!"

This would then be this with element tree package.


# build a tree structure
root = ET.Element("html")
head = ET.SubElement(root, "head")
title = ET.SubElement(head, "title")
title.text = "Page Title"
body = ET.SubElement(root, "body")
body.set("bgcolor", "#ffffff")
body.text = "Hello, World!"

I find it a lot more readable with the syntactical sugar.
So I would really like to add this syntax.

/T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top