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

Add keywordsCaseInsensitive option #78

Closed
wants to merge 1 commit into from

Conversation

camullen
Copy link

I added an option to use case insensitive keywords, but by default they are case sensitive, and the fast string lookup function is unaffected. Essentially, when keywordsCaseInsensitive is true, the keywords themselves are converted to upper case when creating the fast string lookup function, and the function converts values it is searching for to upper case before matching.

The option works as follows:

   let lexer = compile({
      identifier: {
        match: /[a-zA-Z]+/,
        keywordsCaseInsensitive: true,
        keywords: {
          'kw-class': 'class',
          'kw-def': 'def',
          'kw-if': 'if',
        },
      },
      space: {match: /\s+/, lineBreaks: true},
    })

@duartealexf
Copy link

Oh I really need this... is anyone considering it? ... @tjvr ?

@tjvr
Copy link
Collaborator

tjvr commented Sep 19, 2018

Thanks for your contribution!

I'm going to close this in favour of the general-purpose type transform added in #85.

I need to document it better, but you can now do something like this:

const caseInsensitiveKeywords = defs => {
  const keywords = moo.keywords(defs)
  return value => keywords(value.toLower())
}

let lexer = compile({
  identifier: {
    match: /[a-zA-Z]+/,
    type: caseInsensitiveKeywords({
      'kw-class': 'class',
      'kw-def': 'def',
      'kw-if': 'if',
    }),
  },
  space: {match: /\s+/, lineBreaks: true},
})

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

Successfully merging this pull request may close these issues.

None yet

3 participants