|
5 | 5 | A nice parser combinator library for Kotlin
|
6 | 6 |
|
7 | 7 | ```kotlin
|
8 |
| - val booleanGrammar = object : Grammar<BooleanExpression>() { |
9 |
| - val id by token("\\w+") |
10 |
| - val not by token("!") |
11 |
| - val and by token("&") |
12 |
| - val or by token("|") |
13 |
| - val ws by token("\\s+", ignore = true) |
14 |
| - val lpar by token("\\(") |
15 |
| - val rpar by token("\\)") |
16 |
| - |
17 |
| - val term = |
18 |
| - (id use { Variable(text) }) or |
19 |
| - (-not * parser(this::term) map { (Not(it) }) or |
20 |
| - (-lpar * parser(this::rootParser) * -rpar) |
21 |
| - |
22 |
| - val andChain = leftAssociative(term, and) { l, _, r -> And(l, r) } |
23 |
| - val rootParser = leftAssociative(andChain, or) { l, _, r -> Or(l, r) } |
24 |
| - } |
25 |
| - |
26 |
| - val ast = booleanGrammar.parseToEnd("a & !b | b & (!a | c)") |
| 8 | +val booleanGrammar = object : Grammar<BooleanExpression>() { |
| 9 | + val id by token("\\w+") |
| 10 | + val not by token("!") |
| 11 | + val and by token("&") |
| 12 | + val or by token("|") |
| 13 | + val ws by token("\\s+", ignore = true) |
| 14 | + val lpar by token("\\(") |
| 15 | + val rpar by token("\\)") |
| 16 | + |
| 17 | + val term = |
| 18 | + (id use { Variable(text) }) or |
| 19 | + (-not * parser(this::term) map { (Not(it) }) or |
| 20 | + (-lpar * parser(this::rootParser) * -rpar) |
| 21 | + |
| 22 | + val andChain = leftAssociative(term, and) { l, _, r -> And(l, r) } |
| 23 | + val rootParser = leftAssociative(andChain, or) { l, _, r -> Or(l, r) } |
| 24 | +} |
| 25 | + |
| 26 | +val ast = booleanGrammar.parseToEnd("a & !b | b & (!a | c)") |
27 | 27 | ```
|
28 | 28 |
|
29 | 29 | ### Using with Gradle
|
|
0 commit comments