-
Notifications
You must be signed in to change notification settings - Fork 3
/
package.scala
40 lines (36 loc) · 1007 Bytes
/
package.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package gql
import cats.parse._
import cats._
import cats.data._
import cats.implicits._
import io.circe._
package object parser {
final case class ParseError(
caret: Caret,
prettyError: Eval[String]
) {
lazy val asGraphQL = {
import io.circe.syntax._
JsonObject(
"message" -> "could not parse query".asJson,
"locations" -> Json.arr(Json.obj("line" -> caret.line.asJson, "column" -> caret.col.asJson)),
"error" -> prettyError.value.asJson
)
}
}
def parse(str: String): Either[ParseError, NonEmptyList[QueryParser.ExecutableDefinition]] = {
QueryParser.executableDefinition.rep
.parseAll(str)
.leftMap { err =>
val offset = err.failedAtOffset
val left = str.take(offset)
val nls = left.split("\n")
val line = nls.size
val col = nls.last.size
ParseError(
Caret(line, col, offset),
Eval.later(ParserUtil.errorMessage(str, err))
)
}
}
}