diff --git a/common/parser-service/src/main/scala/org/enso/ParserService.scala b/common/parser-service/src/main/scala/org/enso/ParserService.scala index 6d1a26b1e06..bee20f43d62 100644 --- a/common/parser-service/src/main/scala/org/enso/ParserService.scala +++ b/common/parser-service/src/main/scala/org/enso/ParserService.scala @@ -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) diff --git a/common/syntax/definition/src/main/scala/org/enso/data/Index.scala b/common/syntax/definition/src/main/scala/org/enso/data/Index.scala index cbee2685809..2783f1b737c 100644 --- a/common/syntax/definition/src/main/scala/org/enso/data/Index.scala +++ b/common/syntax/definition/src/main/scala/org/enso/data/Index.scala @@ -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 { diff --git a/common/syntax/definition/src/main/scala/org/enso/data/Span.scala b/common/syntax/definition/src/main/scala/org/enso/data/Span.scala index 43bcc6e7650..397f586092b 100644 --- a/common/syntax/definition/src/main/scala/org/enso/data/Span.scala +++ b/common/syntax/definition/src/main/scala/org/enso/data/Span.scala @@ -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) } } diff --git a/common/syntax/specialization/js/src/main/scala/org/enso/syntax/text/Main.scala b/common/syntax/specialization/js/src/main/scala/org/enso/syntax/text/Main.scala index 5f621b03c81..4dda902ed3f 100644 --- a/common/syntax/specialization/js/src/main/scala/org/enso/syntax/text/Main.scala +++ b/common/syntax/specialization/js/src/main/scala/org/enso/syntax/text/Main.scala @@ -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 diff --git a/common/syntax/specialization/shared/src/main/scala/org/enso/syntax/text/Parser.scala b/common/syntax/specialization/shared/src/main/scala/org/enso/syntax/text/Parser.scala index 0dbd014a356..538eb5be39c 100644 --- a/common/syntax/specialization/shared/src/main/scala/org/enso/syntax/text/Parser.scala +++ b/common/syntax/specialization/shared/src/main/scala/org/enso/syntax/text/Parser.scala @@ -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 ////