Skip to content
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

Context sensitive tokens #1027

Closed
keyvan-m-sadeghi opened this issue Aug 31, 2019 · 11 comments
Closed

Context sensitive tokens #1027

keyvan-m-sadeghi opened this issue Aug 31, 2019 · 11 comments

Comments

@keyvan-m-sadeghi
Copy link

keyvan-m-sadeghi commented Aug 31, 2019

Following the discussions on #993 and #1023, I'm wondering if it'd make sense to have an option for stateful tokens.

const stringStartAndEnd = createToken({
  effect: ({ insideString }) => ({ insideString: !insideString }),
  pattern: /'/
});

const literal = createToken({
  gate: ({ insideString }) => insideString,
  pattern: /[^']*/
});

For this to work, the lexer would be initiated with a state variable, an empty object at first.
If present, the effect function of a token is invoked whenever encountered during lexing:

state = { ...state, ...token.effect(state)) };

The gate property of a token is called with the state variable before a match.

@bd82 does this make any sense?

@HoldYourWaffle
Copy link
Contributor

I'm not sure I completely understand what you're suggesting, but this looks very similar to multi-mode lexers to me.

@bd82
Copy link
Member

bd82 commented Aug 31, 2019

Sounds like multi mode Lexing too.
You can implement even more complex logic during lexing using custom token patterns.

@keyvan-m-sadeghi
Copy link
Author

Thanks @HoldYourWaffle and @bd82, that's right. Sorry for being a noob ;)

@HoldYourWaffle
Copy link
Contributor

Fellow noobs should help each other out right 😉

@bd82 Maybe it's a good idea to make multi-mode lexing more prevalent in the documentation? There isn't really a tutorial page for it (although the linked example is plenty to understand how it works) and I didn't find the page until you linked to it from another issue. Perhaps we could add a section to the lexer tutorial page that mentions it?

@bd82
Copy link
Member

bd82 commented Sep 1, 2019

Perhaps we could add a section to the lexer tutorial page that mentions it?

It seems like this feature is more popular than I thought...

Having a full guide e.g:

Would be best but that it a fair bit of work to create...

Perhaps (as suggested) a small note could be added in the lexer tutorial to aid discover-ability.

@HoldYourWaffle
Copy link
Contributor

I'd love to add one, but the 'Edit on github' link results in a 404 😅

@bd82
Copy link
Member

bd82 commented Sep 1, 2019

The move to mono repo structure broke the links.
Add packages/chevrotain to the link, e.g:
https://github.com/SAP/chevrotain/edit/master/packages/chevrotain/docs/guide/generating_syntax_diagrams.md

I will sort it out later.

@bd82
Copy link
Member

bd82 commented Sep 1, 2019

The edit links are now fixed.

But you may want to edit them on a local dev env due to enforcement of prettier formatting.

@HoldYourWaffle
Copy link
Contributor

I created a pull request with an added note.

The move to mono repo structure broke the links.

I saw something about a 'transitioning to a mono repo' before, and I've always wondered: what kinds of chevrotain were in a different repository? The packages folder has just 1 subdirectory, and I can't think of anything easily "splittable" in this library.

@bd82
Copy link
Member

bd82 commented Sep 2, 2019

I saw something about a 'transitioning to a mono repo' before, and I've always wondered: what kinds of chevrotain were in a different repository? The packages folder has just 1 subdirectory, and I can't think of anything easily "splittable" in this library.

  • The examples directory includes multiple packages (private ones).
  • The Lexer could at one point be split out.
  • The Diagrams generation could also be split out.

@HoldYourWaffle
Copy link
Contributor

That makes sense, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants