lexing nested parenthesis (for a Python Unix Shell)

Dave Cinege dcinege at psychosis.com
Wed Jul 31 17:30:40 EDT 2002


On Wednesday 31 July 2002 16:32, Bengt Richter wrote:

> 	if 1 and (var1 or qm('-d /etc/')):
>
> would already be legal Python.

That's not the point. I'm not making legal Python but a 'short hand'
subset, specifically a Python Unix Shell (aka bourne shell replacement)

The idea right now is to parse and replace the short hand 
with python constructs that are predefined, and then let python
run all of it. It's either that or I basically handle ALL the
parsing and pretty much recreate the wheel.

You see I only want to deal with 'my' subset...python will then run
(via compile()) and handle the remaining grammer, indentation, etc.

Things get a bit more difficult in interactive mode, but I feel this
is still the best route.

> Maybe a few examples with un-nested and nested parens (and nested ?(...)
> constructs??) together with what Python you would like to have them
> transformed to would get you some useful help.

I haven't speced it all out yet, but I'm pretty much decided I want to
contain most all thing within ()'s and prefix the first ( with an identifier.

To put things in perspective:
	In bash sh:	[ -d /etc/ ]
	In pysh:	=(-d /etc/)  (Maybe =('-d /etc/') )

	At runtime it will be parsed and replaced by:
	pysh_test('-d', '/etc/')

Somethings will not be so easy as this, as they will not be a
simple function name replacement. It can get ugly when I need
to work recursivly through nested functions. I need to work
on the next item first so I know how to handle output.

IE if the return is normally a list, and it's nested in
what requires and string, I have to account for that.

In bash:
	for line in $(cat *.py); do echo $line; done	# Yep time to retire this POS

In pysh
	for $line in !(cat *(*.py)): print $line ;;	# Ain't it pretty?

	FYI
	  $   == variable prefix (I might be able to avoid using this, dunno)
	  !() == Command Substitution
	  *() == Shell glob (might become seemless, ie I search for glob chars!)
	  ;;  == explict newline
Parsed to python:
	for line in pysh_cmdsub_inpath('cat',pysh_ListToArgStr(pysh_glob('*.py'))):
		print line  	# You can visualize the pysh functions...

> 'Better' is a waste of time unless we're working on the real problem ;-)

The problem is Python already works. : > I KNOW how I can do all this, I just
don't feel like writing a complete parser if I can reuse something Python 
itself already uses for parsing.

-- 
The time is now 22:48 (Totalitarian)  -  http://www.ccops.org/





More information about the Python-list mailing list