Skip to content

Commit 30c6878

Browse files
committed
feat(v2): better README
1 parent ddb0e8f commit 30c6878

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,38 @@ const parsing = floorCombinator.parse(stream)
8585
assertEquals(4, parsing.value, 'Floor parsing')
8686
```
8787

88+
## Parsing tokens
89+
90+
You can parse a stream of tokens, not only characters. Let's parse a date from
91+
tokens.
92+
93+
```js
94+
import { Stream, C, F, GenLex } from '@masala/parser'
95+
96+
const genlex = new GenLex()
97+
98+
const [slash] = genlex.keywords(['/'])
99+
// 1100 is the precedence of the token
100+
const number = genlex.tokenize(N.digits(), 'number', 1100)
101+
102+
let dateParser = number
103+
.then(slash.drop())
104+
.then(number)
105+
.then(slash.drop())
106+
.then(number)
107+
.map(([day, , month, year]) => ({
108+
day: day,
109+
month: month,
110+
year: year,
111+
}))
112+
```
113+
114+
You will then be able to combine this date parser with other parsers that use
115+
the tokens.
116+
117+
Overall, using GenLex and tokens is more efficient than using characters for
118+
complex grammars.
119+
88120
## Explanations
89121

90122
We create small simple parsers, with a set of utilities (`C`, `N`, `optrep()`,

0 commit comments

Comments
 (0)