Skip to content

Commit

Permalink
Update Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tjvr committed Mar 31, 2018
1 parent 00d5c6f commit 5456cf6
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,12 @@ It'll automatically compile them into regular expressions, escaping them where n
```js
moo.compile({
IDEN: {match: /[a-zA-Z]+/, keywords: {
KW: ['while', 'if', 'else', 'moo', 'cows']),
}},
IDEN: {
match: /[a-zA-Z]+/,
type: moo.keywords({
KW: ['while', 'if', 'else', 'moo', 'cows']),
}),
},
SPACE: {match: /\s+/, lineBreaks: true},
})
```
Expand Down Expand Up @@ -337,10 +340,14 @@ Use [itt](https://github.com/nathan/itt)'s iteration tools with Moo.
```
Transform
---------
Transforms
----------
The **value** transform takes the matched `text`, and returns the `value` which should be stored in the Token object. By default, the `text` of a Token is the same as the `value`.
Moo doesn't allow capturing groups in RegExps: use value() instead.
Moo doesn't allow capturing groups, but you can supply a transform function, `value()`, which will be called on the value before storing it in the Token object.
```js
moo.compile({
Expand All @@ -353,6 +360,24 @@ Moo doesn't allow capturing groups, but you can supply a transform function, `va
})
```
The **type** transform takes the matched `text` and returns the `type`. By default, the rule name is used.
```js
const caseInsensitiveKeywords = map => {
const transform = moo.keywords(map)
return text => transform(text.toLowerCase())
}

const lexer = moo.compile({
identifier: {
match: /[a-zA-Z]+/,
type: caseInsensitiveKeywords({
keyword: ['class', 'def'],
}),
},
})
```
Contributing
------------
Expand Down

0 comments on commit 5456cf6

Please sign in to comment.