-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
createToken shortcut functions #1029
Comments
An interesting reference would be the Java Lexer Implemented as part of Prettier Java HighLights |
Interesting. So do you think it'd make sense to add some of these shortcuts to the main API? |
Not sure, I am not sure its a good idea to have multiple ways to do things, additionally it is more consistent to have most of the APIs using the JavaScript Config Pattern. and I don't think it is a huge advantage to save a few property names... I think end users would benefit more from other upgrades to the tokenizer, for example:
|
I will be closing this for now as I think it would be quite easy to any end user to implement this themselves if they want and it would also be confusing to provide multiple ways to accomplish the same thing so I would rather stick with the more verbose but also extensible and consistent config object style API. Thanks for the suggestion and feedback, it is appreciated 👍 Also note that if you want a really concise API for token/lexer definitions it is always possible to build @HoldYourWaffle if you are interested in "good first issues" improvements in the API area |
I agree that there isn't much value to adding this, I just figured "why not suggest it" 😄 Lexdoc looks really promising, maybe we could add a reference to it somewhere in the tutorial? |
It is just a similar tiny method that does |
Now that I've read the issue with a not-very-tired-head it indeed looks very simple. Unfortunately I don't have the time to help right now due to deadline, but if it's still open when I do have time I'll be sure to take a look 😄 |
Great, take your time no rush 😄 |
The current
createToken
function does exactly what it should do, but it's pretty clunky to use (especially when you have a lot of tokens).I propose adding 2 extra helper functions to make creating your lexicon less painful:
createToken shorthand
Definition:
As you can see it's just a simple shorthand for the 2 properties all tokens will need. Most tokens don't need any extra configuration, saving a lot of space and increasing readability. With this helper the full lexicon from the tutorial page can be shortened to this:
I don't want to change the existing API, I just want to add an overload for convenience.
createKeyword
Almost every language has keywords, and all keywords have two things in common:
longer_alt
option should be set to anIdentifier
-like token (if such a token exists)This repetition is ground for the other helper function I want to propose:
The
identifierToken
parameter is needed to set thelonger_alt
option. It's optional because not every language might have such anIdentifier
-like token. There's also acaseSensitive
option to toggle thei
option for the generated regex pattern, SQL has caseinsensitive keywords for example.The
name
property of the generated token would be the first-capitalized version of thekeyword
parameter, as per the 'standard'.This extra helper allows the lexicon from the tutorial page to be shortened even further to just this:
I feel like there should be a way to set the
Identifier
-like token only once for all keywords, but I can't figure out how such a system would work. Perhaps someone with more JS experience can tune in on that.The tutorial page is probably not the best example for the usefulness of these shorthands, the benefit grows massively the more tokens (especially keywords) you add (like when you want to put in a full SQL language).
I always define these shorthand functions myself, but I figured they could be useful for more people.
The text was updated successfully, but these errors were encountered: