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

Skipping whitespace tokens #156

Open
jarble opened this issue May 18, 2021 · 3 comments
Open

Skipping whitespace tokens #156

jarble opened this issue May 18, 2021 · 3 comments

Comments

@jarble
Copy link

jarble commented May 18, 2021

Is it possible to skip tokens when defining a lexer?
I want to split a string into a list of tokens without whitespace, but I don't know if Moo can do this:

Input string:

  • "while ( a < 3 ) { a += 1; }"

List of tokens:

  • ["while","(","a","<","3",")","{","a","+=","1",",";","}"]
@nathan
Copy link
Collaborator

nathan commented May 18, 2021

const moo = require('moo')
const lex = moo.compile({
  ws: {match: /\p{White_Space}+/u, lineBreaks: true},
  word: /\p{XID_Start}\p{XID_Continue}*/u,
  op: moo.fallback,
})
;[...lex.reset('while ( a < 3 ) { a += 1; }')]
.filter(t => t.type !== 'ws')
.map(t => t.value)

@jarble
Copy link
Author

jarble commented May 18, 2021

@nathan The documentation doesn't describe this feature: does it need to be updated?

@tjvr
Copy link
Collaborator

tjvr commented May 18, 2021

The documentation needs to be updated to document moo.fallback (see #112).

As for the rest, I think Nathan's just demonstrating that since a moo lexer object is an Iterator, you can use filter() and map() which are built-in to JavaScript.

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