-
Notifications
You must be signed in to change notification settings - Fork 112
Description
Motivation
- Readability
The abstract meta-programing features, require the reader to be acquainted to the library. While acceptable in most environments, in some this is a down side.
- Third party tools
While somewhat an extension of readability, I feel like its also important to mention. In some environments, these third party tools can be required and long comments disabling parts of these tools are not only looked down on, but also can prevent the tools from checking the code the developer wrote.
- Personal preference
For some, more explicit aliases can be preferable. Like how some people prefer tabs over spaces (haha).
Potential solution
sly.explicit
This new optional sub-module would include explicit aliases for existing meta-programing syntaxes.
sly.explicit.TokenType()
Replaces:
class SomeLexer(Lexer):
...
ABC_TOKEN = r'[abc]'
ABC_TOKEN['a'] = 'A_TOKEN'
With:
class SomeLexer(Lexer):
...
ABC_TOKEN = TokenType(r'abc')
ABC_TOKEN['a'] = TokenType(name='A_TOKEN')
sly.explicit.add_action()
,sly.explicit.add_rule()
Aliases for _
.
Replaces:
class SomeLexer(Lexer):
...
@_(r'\d')
def NUMBER(self,t):
...
...
class SomeParser(Parser):
...
@_("A_TOKEN LETTER")
def rule(self, p):
...
With
class SomeLexer(Lexer):
...
@add_action(r'\d')
def NUMBER(self,t):
...
...
class SomeParser(Parser):
...
@add_rule("A_TOKEN ABC_TOKEN")
def rule(self, p):
...
The main difference between the two is if the positional argument is of type Token
or YaccProduction
.
Extensive type annotation for interface functions.
This would not only apply to the new explicit interface, but also to the existing one, improving self documentation.
Aside from the members of sly.explicit
this would also include:
Parser.parse()
andLexer.tokenize()
Parser.error()
- Members such as
Parser.tokens
,Token.value
etc.
Final thoughts
I understand, that meta-programing in this libraries spirit and that the problems I laid out in the motivation paragraph are known of and considered low (or even lower) priority. I however think, that this proposed alternative interface, would allow for whom these problems are important to solve them.
Potentially, the note outlined in Contributing.md could be reformatted to allow changes within this submodule.
I look forward to any suggestions and hopefully the go-ahead for me to implement this.