I've been working on an alternative compiler front-end for Python where all syntax is parsed via macros. I'm finally to the point with its development that I can start work on a superset of the Python language where macros are an integral component.
My problem is that I can't come up with a pythonic macro definition syntax. I've posted several examples in two different syntaxes in answers below. Can anyone come up with a better syntax? It doesn't have to build off the syntax I've proposed in any way -- I'm completely open here. Any comments, suggestions, etc would be helpful, as would alternative syntaxes showing the examples I've posted.
A note about the macro structure, as seen in the examples I've posted: The use of MultiLine/MLMacro and Partial/PartialMacro tell the parser how the macro is applied. If it's multiline, the macro will match multiple line lists; generally used for constructs. If it's partial, the macro will match code in the middle of a list; generally used for operators.
After thinking about it a while a few days ago, and coming up with nothing worth posting, I came back to it now and came up with some syntax I rather like, because it nearly looks like python:
Var()
instead of simpleVar
)Pass the name of elements as a "keyword parameter" to items we want a name for. It should still be easy to find all the names in the parser, since this syntax definition anyway needs to be interpreted in some way to fill the macro classes syntax variable.
needs to be converted to fill the syntax variable of the resulting macro class.
The internal syntax representation could also look the same:
The internal syntax classes like
OneOrMore
would follow this pattern to allow subitems and an optional name:When the macro matches, you just collect all items that have a name and pass them as keyword parameters to the handler function:
The handler function would then be defined like this:
I added the syntaxtree as a first parameter that is always passed, so you wouldn't need to have any named items if you just want to do very basic stuff on the syntax tree.
Also, if you don't like the decorators, why not add the macro type like a "base class"?
IfMacro
would then look like this:And in the internal representation:
I think this would give a quite flexible system. Main advantages:
None
in the handlerSeveral("abc", min=3, max=5, name="a")
. I think this could also be used to add default values to optional elements likeOptional("step", Var(), name="step", default=1)
.I'm not sure about the quote/unquote syntax with "quote:" and "$", but some syntax for this is needed, since it makes life much easier if you don't have to manually write syntax trees. Probably its a good idea to require (or just permit?) parenthesis for "$", so that you can insert more complicated syntax parts, if you want. Like
$(Stmt(a, b, c))
.The ToMacro would look something like this:
This is the macro syntax I've come up with for my Python superset.
Print macro:
If macro:
X to Y [inclusive] [step Z] macro:
My primary issue with this is that the syntax block is unclear, particularly the 'to' line in the last example. I'm also not a big fan of using decorators to differentiate macro types.
This is the current mechanism for defining syntax by use of a standard Python class.
Print macro:
If/elif/else macro class:
X to Y [inclusive] [step Z] macro class:
My issues with this design is that things are very verbose and don't feel pythonic in the least. In addition, the lack of quotation ability makes complex macros difficult.
I'm posting a bit of floating ideas to see if it inspires. I don't know much python, and I'm not using real python syntax, but it beats nothing :p
More ideas:
Implementing Perl quote style, but in Python! ( its a very bad implementation, and note: whitespace is significant in the rule )
You might consider looking at how Boo (a .NET-based language with a syntax largely inspired by Python) implements macros, as described at http://boo.codehaus.org/Syntactic+Macros.
Incorporating BNF