Skip to content

[Proposal] Add an alternatnive, more explicit way of using the library. #95

@MinekPo1

Description

@MinekPo1

Motivation

  1. 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.

  1. 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.

  1. 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() and Lexer.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions