Skip to content

Commit 04c1a39

Browse files
committed
Reformat and cleanup
1 parent d04145d commit 04c1a39

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

src/commonMain/kotlin/com/kgbier/graphql/parser/GraphQl.kt

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.kgbier.graphql.parser.Parsers.never
1010
import com.kgbier.graphql.parser.Parsers.notOneOf
1111
import com.kgbier.graphql.parser.Parsers.oneOf
1212
import com.kgbier.graphql.parser.Parsers.oneOrMore
13+
import com.kgbier.graphql.parser.Parsers.predicate
1314
import com.kgbier.graphql.parser.Parsers.zeroOrMore
1415
import com.kgbier.graphql.parser.structure.*
1516

@@ -23,7 +24,7 @@ internal class GraphQl {
2324
val sourceChar = char
2425

2526
// name -> '[_A-Za-z][_0-9A-Za-z]'
26-
val name = Parsers.prefix { it.isLetterOrDigit() || it == '_' }
27+
val name = predicate { it.isLetterOrDigit() || it == '_' }
2728
.flatMap { if (it.isNotEmpty() && !it.first().isDigit()) always(it) else never() }
2829

2930
// whiteSpace -> [ '\s' '\t' ]
@@ -101,8 +102,21 @@ internal class GraphQl {
101102
*/
102103

103104
// value -> [ variable intValue floatValue stringValue booleanValue nullValue listValue objectValue ]
104-
var value = deferred { valueDeferred }
105-
var valueDeferred: Parser<Value> = never()
105+
val value: Parser<Value> = deferred {
106+
oneOf(
107+
listOf(
108+
variableValue,
109+
stringValue,
110+
objectValue,
111+
listValue,
112+
nullValue,
113+
booleanValue,
114+
enumValue,
115+
floatValue,
116+
intValue
117+
)
118+
)
119+
}
106120

107121
// negativeSign -> '-'
108122
val negativeSign = character('-')
@@ -321,8 +335,15 @@ internal class GraphQl {
321335
*/
322336

323337
// type -> [ namedType listType nonNullType ]
324-
val type = deferred { typeDeferred }
325-
var typeDeferred: Parser<String> = never()
338+
val type: Parser<String> = deferred {
339+
oneOf(
340+
listOf(
341+
nonNullType,
342+
listType,
343+
namedType
344+
)
345+
)
346+
}
326347

327348
// namedType -> name
328349
val namedType = name
@@ -427,8 +448,13 @@ internal class GraphQl {
427448
*/
428449

429450
// selection -> [ field fragmentSpread inlineFragment ]
430-
val selection = deferred { selectionDeferred }
431-
var selectionDeferred: Parser<Selection> = never()
451+
val selection: Parser<Selection> = deferred {
452+
oneOf(listOf(
453+
field.map { SelectionField(it) },
454+
fragmentSpread.map { SelectionFragmentSpread(it) },
455+
inlineFragment.map { SelectionInlineFragment(it) }
456+
))
457+
}
432458

433459
// selectionSet -> " '{' { selection } '}' "
434460
val selectionSet = zip(
@@ -586,34 +612,4 @@ internal class GraphQl {
586612
// document -> { definition }
587613
val document = oneOrMore(definition, tokenSeparator)
588614
.map { Document(it) }
589-
590-
init {
591-
valueDeferred = oneOf(
592-
listOf(
593-
variableValue,
594-
stringValue,
595-
objectValue,
596-
listValue,
597-
nullValue,
598-
booleanValue,
599-
enumValue,
600-
floatValue,
601-
intValue
602-
)
603-
)
604-
605-
typeDeferred = oneOf(
606-
listOf(
607-
nonNullType,
608-
listType,
609-
namedType
610-
)
611-
)
612-
613-
selectionDeferred = oneOf(listOf(
614-
field.map { SelectionField(it) },
615-
fragmentSpread.map { SelectionFragmentSpread(it) },
616-
inlineFragment.map { SelectionInlineFragment(it) }
617-
))
618-
}
619615
}

src/commonMain/kotlin/com/kgbier/graphql/parser/Parsers.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ internal object Parsers {
1616

1717
fun <Output> never(): Parser<Output> = Parser { null }
1818

19-
fun <Output> deferred(provideParser: () -> Parser<Output>): Parser<Output> =
19+
inline fun <Output> deferred(crossinline provideParser: () -> Parser<Output>): Parser<Output> =
2020
Parser { provideParser()(it) }
2121

2222
fun <Output> maybe(parser: Parser<Output>): Parser<Maybe<Output>> =
2323
Parser { Maybe(parser(it)) }
2424

25-
fun <Output> zeroOrMore(parser: Parser<Output>) =
25+
fun <Output> zeroOrMore(parser: Parser<Output>): Parser<List<Output>> =
2626
zeroOrMore<Output, Unit>(parser, null)
2727

2828
fun <Output, SeparatedBy> zeroOrMore(
@@ -44,13 +44,13 @@ internal object Parsers {
4444
matches
4545
}
4646

47-
fun <Output> oneOrMore(parser: Parser<Output>) =
47+
fun <Output> oneOrMore(parser: Parser<Output>): Parser<List<Output>> =
4848
oneOrMore<Output, Unit>(parser, null)
4949

5050
fun <Output, SeparatedBy> oneOrMore(
5151
parser: Parser<Output>,
5252
separatedBy: Parser<SeparatedBy>?,
53-
) = zeroOrMore(parser, separatedBy).flatMap {
53+
): Parser<List<Output>> = zeroOrMore(parser, separatedBy).flatMap {
5454
if (it.isEmpty()) never() else always(it)
5555
}
5656

@@ -68,6 +68,7 @@ internal object Parsers {
6868
val match = p(it)
6969
if (match != null) return@Parser null
7070
}
71+
7172
Unit
7273
}
7374

@@ -101,8 +102,9 @@ internal object Parsers {
101102
} else null
102103
}
103104

104-
// TODO: consider `Substring` instead of `String`?
105-
fun prefix(predicate: (Char) -> Boolean): Parser<String> = Parser {
105+
inline fun predicate(
106+
crossinline predicate: (Char) -> Boolean,
107+
): Parser<String> = Parser {
106108
val result = it.takeWhile(predicate)
107109
if (result.isNotEmpty()) {
108110
it.advance(result.length)

0 commit comments

Comments
 (0)