no-context / moo

Optimised tokenizer/lexer generator! 🐄 Uses /y for performance. Moo.
BSD 3-Clause "New" or "Revised" License
817 stars 65 forks source link

The difference between 'text' and 'value' fields in token info? #90

Closed gavvvr closed 5 years ago

gavvvr commented 5 years ago

Hi. I was wondering what is the difference between 'text' and 'value' in token info? From my experience they are always equal. Are there any situations where they can have any difference? Please describe the difference in ReadMe. 'text' field is not mentioned here

nathan commented 5 years ago

The fields differ if you provide a transform, e.g.:

const moo = require('moo')

const lexer = moo.compile({
  ws: /[ \t]+/,
  string: {match: /"(?:\\["\\]|[^\n"\\])*"/, value: s => s.slice(1, -1)},
})

lexer.reset('"test"')
lexer.next() /* { value: 'test', text: '"test"', ... } */

We should update the Token Info section to include text and explain the difference.

gavvvr commented 5 years ago

Thanks for the explanation. Agree, it should be added to readme

tjvr commented 5 years ago

I've added a new section to the Readme.

Thanks for your question!