|
| 1 | +/** |
| 2 | + * Redundant Unit returns are necessary when running in JS. |
| 3 | + * Functions which do not explicitly return a Unit value will return `undefined` in JS. |
| 4 | + */ |
| 5 | +@file:Suppress("RedundantUnitExpression") |
| 6 | + |
1 | 7 | package com.kgbier.graphql.parser |
2 | 8 |
|
3 | 9 | import com.kgbier.graphql.parser.structure.Maybe |
4 | | -import com.kgbier.graphql.parser.substring.Substring |
5 | 10 |
|
6 | 11 | internal object Parsers { |
7 | 12 |
|
8 | | - val always = object : Parser<Unit> { |
9 | | - override fun run(str: Substring) = Unit |
10 | | - } |
| 13 | + val always: Parser<Unit> = Parser { Unit } |
11 | 14 |
|
12 | | - fun <A> always(a: A) = object : Parser<A> { |
13 | | - override fun run(str: Substring) = a |
14 | | - } |
| 15 | + fun <Output> always(constant: Output): Parser<Output> = Parser { constant } |
15 | 16 |
|
16 | | - fun <A> never() = object : Parser<A> { |
17 | | - override fun run(str: Substring): A? = null |
18 | | - } |
| 17 | + fun <Output> never(): Parser<Output> = Parser { null } |
19 | 18 |
|
20 | | - fun <A> deferred(f: () -> Parser<A>) = object : Parser<A> { |
21 | | - override fun run(str: Substring): A? = f().parse(str) |
22 | | - } |
| 19 | + fun <Output> deferred(provideParser: () -> Parser<Output>): Parser<Output> = |
| 20 | + Parser { provideParser()(it) } |
23 | 21 |
|
24 | | - fun <A> zeroOrMore(p: Parser<A>) = zeroOrMore<A, Unit>(p, null) |
25 | | - |
26 | | - fun <A, B> zeroOrMore(p: Parser<A>, separatedBy: Parser<B>?) = object : Parser<List<A>> { |
27 | | - override fun run(str: Substring): List<A> { |
28 | | - var remainderState = str.state |
29 | | - val matches = mutableListOf<A>() |
30 | | - while (true) { |
31 | | - val match = p.parse(str) ?: break |
32 | | - remainderState = str.state |
33 | | - matches.add(match) |
34 | | - if (separatedBy != null) { |
35 | | - separatedBy.parse(str) ?: return matches |
36 | | - } |
| 22 | + fun <Output> maybe(parser: Parser<Output>): Parser<Maybe<Output>> = |
| 23 | + Parser { Maybe(parser(it)) } |
| 24 | + |
| 25 | + fun <Output> zeroOrMore(parser: Parser<Output>) = |
| 26 | + zeroOrMore<Output, Unit>(parser, null) |
| 27 | + |
| 28 | + fun <Output, SeparatedBy> zeroOrMore( |
| 29 | + parser: Parser<Output>, |
| 30 | + separatedBy: Parser<SeparatedBy>?, |
| 31 | + ): Parser<List<Output>> = Parser { |
| 32 | + var remainderState = it.state |
| 33 | + val matches = mutableListOf<Output>() |
| 34 | + while (true) { |
| 35 | + val match = parser(it) ?: break |
| 36 | + remainderState = it.state |
| 37 | + matches.add(match) |
| 38 | + if (separatedBy != null) { |
| 39 | + separatedBy(it) ?: return@Parser matches |
37 | 40 | } |
38 | | - str.state = remainderState |
39 | | - return matches |
40 | 41 | } |
| 42 | + it.state = remainderState |
| 43 | + |
| 44 | + matches |
41 | 45 | } |
42 | 46 |
|
43 | | - fun <A> oneOrMore(p: Parser<A>) = oneOrMore<A, Unit>(p, null) |
| 47 | + fun <Output> oneOrMore(parser: Parser<Output>) = |
| 48 | + oneOrMore<Output, Unit>(parser, null) |
44 | 49 |
|
45 | | - fun <A, B> oneOrMore(p: Parser<A>, separatedBy: Parser<B>?) = zeroOrMore(p, separatedBy).flatMap { |
| 50 | + fun <Output, SeparatedBy> oneOrMore( |
| 51 | + parser: Parser<Output>, |
| 52 | + separatedBy: Parser<SeparatedBy>?, |
| 53 | + ) = zeroOrMore(parser, separatedBy).flatMap { |
46 | 54 | if (it.isEmpty()) never() else always(it) |
47 | 55 | } |
48 | 56 |
|
49 | | - fun <A> oneOf(ps: List<Parser<out A>>) = object : Parser<A> { |
50 | | - override fun run(str: Substring): A? { |
51 | | - for (p in ps) { |
52 | | - val match = p.run(str) |
53 | | - if (match != null) return match |
54 | | - } |
55 | | - return null |
| 57 | + fun <Output> oneOf(parsers: List<Parser<out Output>>): Parser<Output> = Parser { |
| 58 | + for (p in parsers) { |
| 59 | + val match = p(it) |
| 60 | + if (match != null) return@Parser match |
56 | 61 | } |
57 | | - } |
58 | 62 |
|
59 | | - fun <A> notOneOf(ps: List<Parser<A>>) = object : Parser<Unit> { |
60 | | - override fun run(str: Substring): Unit? { |
61 | | - for (p in ps) { |
62 | | - val match = p.run(str) |
63 | | - if (match != null) return null |
64 | | - } |
65 | | - return Unit |
66 | | - } |
| 63 | + null |
67 | 64 | } |
68 | 65 |
|
69 | | - fun <A> maybe(p: Parser<A>) = object : Parser<Maybe<A>> { |
70 | | - override fun run(str: Substring): Maybe<A> { |
71 | | - val match = p.parse(str) |
72 | | - return Maybe(match) |
| 66 | + fun <Output> notOneOf(parsers: List<Parser<Output>>): Parser<Unit> = Parser { |
| 67 | + for (p in parsers) { |
| 68 | + val match = p(it) |
| 69 | + if (match != null) return@Parser null |
73 | 70 | } |
| 71 | + Unit |
74 | 72 | } |
75 | 73 |
|
76 | | - val integer = object : Parser<Int> { |
77 | | - override fun run(str: Substring): Int? { |
78 | | - val result = str.takeWhile { it.isDigit() } |
79 | | - return if (result.isNotEmpty()) { |
80 | | - str.advance(result.length) |
81 | | - result.toString().toInt() |
82 | | - } else null |
83 | | - } |
| 74 | + val int: Parser<Int> = Parser { |
| 75 | + val result = it.takeWhile { it.isDigit() } |
| 76 | + if (result.isNotEmpty()) { |
| 77 | + it.advance(result.length) |
| 78 | + result.toString().toIntOrNull() |
| 79 | + } else null |
84 | 80 | } |
85 | 81 |
|
86 | | - val character = object : Parser<Char> { |
87 | | - override fun run(str: Substring): Char? { |
88 | | - return if (str.isNotEmpty()) { |
89 | | - val result = str.first() |
90 | | - str.advance() |
91 | | - result |
92 | | - } else null |
93 | | - } |
| 82 | + val char: Parser<Char> = Parser { |
| 83 | + if (it.isNotEmpty()) { |
| 84 | + val result = it.first() |
| 85 | + it.advance() |
| 86 | + result |
| 87 | + } else null |
94 | 88 | } |
95 | 89 |
|
96 | | - fun character(char: Char) = object : Parser<Char> { |
97 | | - override fun run(str: Substring): Char? { |
98 | | - return if (str.isNotEmpty() && str.first() == char) { |
99 | | - str.advance() |
100 | | - char |
101 | | - } else null |
102 | | - } |
| 90 | + fun character(char: Char): Parser<Char> = Parser { |
| 91 | + if (it.isNotEmpty() && it.first() == char) { |
| 92 | + it.advance() |
| 93 | + char |
| 94 | + } else null |
103 | 95 | } |
104 | 96 |
|
105 | | - fun literal(literal: String) = object : Parser<Unit> { |
106 | | - override fun run(str: Substring): Unit? { |
107 | | - return if (str.startsWith(literal)) { |
108 | | - str.advance(literal.length) |
109 | | - } else null |
110 | | - } |
| 97 | + fun literal(literal: String): Parser<Unit> = Parser { |
| 98 | + if (it.startsWith(literal)) { |
| 99 | + it.advance(literal.length) |
| 100 | + Unit |
| 101 | + } else null |
111 | 102 | } |
112 | 103 |
|
113 | 104 | // TODO: consider `Substring` instead of `String`? |
114 | | - fun prefix(predicate: (Char) -> Boolean) = object : Parser<String> { |
115 | | - override fun run(str: Substring): String? { |
116 | | - val result = str.takeWhile(predicate) |
117 | | - return if (result.isNotEmpty()) { |
118 | | - str.advance(result.length) |
119 | | - result.toString() |
120 | | - } else null |
121 | | - } |
| 105 | + fun prefix(predicate: (Char) -> Boolean): Parser<String> = Parser { |
| 106 | + val result = it.takeWhile(predicate) |
| 107 | + if (result.isNotEmpty()) { |
| 108 | + it.advance(result.length) |
| 109 | + result.toString() |
| 110 | + } else null |
122 | 111 | } |
123 | 112 | } |
0 commit comments