473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
2 1348
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
7
by: Michele Simionato | last post by:
So far, I have not installed Prothon, nor I have experience with Io, Self or other prototype-based languages. Still, from the discussion on the mailing list, I have got the strong impression that...
24
by: Xah Lee | last post by:
in computer languages, often a function definition looks like this: subroutine f (x1, x2, ...) { variables ... do this or that } in advanced languages such as LISP family, it is not uncommon...
16
by: qwweeeit | last post by:
In analysing a very big application (pysol) made of almost 100 sources, I had the need to remove comments. Removing the comments which take all the line is straightforward... Instead for the...
4
by: Kelvin | last post by:
hi: in C, we can use strtok() to tokenize a char* but i can't find any similar member function of string that can tokenize a string so how so i tokenize a string in C++? do it the C way? ...
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
1
by: Tim | last post by:
I ran into a problem with a script i was playing with to check code indents and need some direction. It seems to depend on if tabsize is set to 4 in editor and spaces and tabs indents are mixed on...
1
by: Alan Isaac | last post by:
Forman's book is out of print. Is there a good substitute? Thanks, Alan Isaac
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.