Pass id map to parser calls. (#556)

This commit is contained in:
Josef 2020-02-25 13:11:20 +01:00 committed by GitHub
parent d394888a5a
commit 083fa0e4a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 24 deletions

View File

@ -36,13 +36,13 @@ case class ParserService() extends Server with Protocol {
def serializeAst(ast: AST.Module): String = ast.toJson().noSpaces
def runParser(program: String, ids: Seq[((Int, Int), AST.ID)]): AST.Module =
new Parser().run(new Reader(program), Parser.idMap(ids))
def runParser(program: String, ids: Parser.IDMap): AST.Module =
new Parser().run(new Reader(program), ids)
def handleRequest(request: Request): Response = {
request match {
case ParseRequest(program, idsJson) =>
val ids = decode[Seq[((Int, Int), AST.ID)]](idsJson).getOrElse {
val ids = Parser.idMapFromJson(idsJson).getOrElse {
throw new Exception("Could not decode IDMap from json.")
}
val ast = runParser(program, ids)

View File

@ -1,18 +1,18 @@
package org.enso.data
/** Strongly typed index position in a container. */
case class Index(index: Int) extends AnyVal with Ordered[Index] {
def +(offset: Size): Index = Index(index + offset.value)
def -(offset: Size): Index = Index(index - offset.value)
case class Index(value: Int) extends AnyVal with Ordered[Index] {
def +(offset: Size): Index = Index(value + offset.value)
def -(offset: Size): Index = Index(value - offset.value)
/** Span between two text positions. Operands order is irrelevant. */
def <->(that: Index): Span =
if (index <= that.index) Span(this, Size(that.index - index))
else Span(that, Size(index - that.index))
if (value <= that.value) Span(this, Size(that.value - value))
else Span(that, Size(value - that.value))
def asSize: Size = Size(index)
def asSize: Size = Size(value)
def compare(rhs: Index): Int = index compare rhs.index
def compare(rhs: Index): Int = value compare rhs.value
}
object Index {

View File

@ -3,13 +3,13 @@ package org.enso.data
import org.enso.syntax.text.AST
/** Strongly typed span in a container. */
case class Span(begin: Index, length: Size) extends Ordered[Span] {
case class Span(index: Index, size: Size) extends Ordered[Span] {
override def compare(that: Span): Int =
(begin -> that.length).compare(that.begin -> length)
(index -> that.size).compare(that.index -> size)
/** Index of the first element past the span */
def end: Index = begin + length
def end: Index = index + size
}
@ -25,6 +25,6 @@ object Span {
implicit class StringOps(text: String) {
def substring(span: Span): String =
text.substring(span.begin.index, span.end.index)
text.substring(span.index.value, span.end.value)
}
}

View File

@ -1,9 +1,7 @@
package org.enso.syntax.text
import org.enso.flexer.Reader
import org.enso.syntax.text
import io.circe.parser._
import scala.scalajs.js.annotation._
import scala.scalajs.js
@ -11,11 +9,10 @@ object Parse {
@JSExportTopLevel("parse")
def parse(program: String, idsJson: String): String = {
try {
val ids = decode[Seq[((Int, Int), AST.ID)]](idsJson).getOrElse {
val ids = Parser.idMapFromJson(idsJson).getOrElse {
throw new Exception("Could not decode IDMap from json.")
}
val ast = new text.Parser().run(new Reader(program), text.Parser.idMap(ids))
ast.toJson().noSpacesSortKeys
new Parser().run(new Reader(program), ids).toJson().noSpacesSortKeys
} catch {
// FIXME We wrap the error message in JavaScriptException, so that javascript
// can display it. This is no longer needed in scalajs 1.0

View File

@ -3,9 +3,7 @@ package org.enso.syntax.text
import java.util.UUID
import cats.Foldable
import org.enso.data.Index
import org.enso.data.List1
import org.enso.data.Size
import org.enso.data.Span
import org.enso.flexer
import org.enso.flexer.Reader
@ -17,6 +15,9 @@ import org.enso.syntax.text.ast.meta.Builtin
import org.enso.syntax.text.prec.Macro
import org.enso.syntax.text.spec.ParserDef
import cats.implicits._
import io.circe
import io.circe.generic.auto._
import io.circe.parser._
////////////////////////////////
@ -346,15 +347,15 @@ class Parser {
}
object Parser {
type IDMap = Seq[(Span, AST.ID)]
private val newEngine = flexer.Parser.compile(ParserDef())
def apply(): Parser = new Parser()
def idMap(ids: Seq[((Int, Int), AST.ID)]): IDMap = ids.map {
case ((ix, len), id) => (Span(Index(ix), Size(len)), id)
}
def idMapFromJson(json: String): Either[circe.Error, IDMap] =
decode[IDMap](json)
//// Exceptions ////