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

Return multiple tokens #47

Open
MiSawa opened this issue Feb 19, 2022 · 1 comment
Open

Return multiple tokens #47

MiSawa opened this issue Feb 19, 2022 · 1 comment
Labels
feature New feature or request

Comments

@MiSawa
Copy link
Contributor

MiSawa commented Feb 19, 2022

Sometimes I want a lexer rule to be able to return multiple tokens, e.g. to emit a dummy token so parser can use it as an end-marker for some syntax. Maybe I should just use Lexer -> Vec<MyToken> and flatten it later, though it'd be great if this is supported by the library side.

@osa1
Copy link
Owner

osa1 commented Feb 20, 2022

I needed this once, but I don't remember for what and how I worked around not having it.

We probably don't want to return a Vec in all semantic actions as it will incur runtime costs to lexers that don't need this. We could use SmallVec<[Token; 1]> to avoid allocation in majority of the cases, but even then the lexer main loop (the Iterator implementation) will have to store the returned (by semantic actions) vectors, and return the vector elements when there are tokens in the vector, and continue with lexing if it's empty. This means next() will be slower whether you need to return multiple tokens or not.

Alternatively, we could provide a compile-time switch for this feature and only do this in lexer that need it.

@osa1 osa1 added the feature New feature or request label Feb 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants