Skip to content

Commit

Permalink
LF: parsing interface primitives (#11825)
Browse files Browse the repository at this point in the history
Continue work start in #11797

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da authored Nov 23, 2021
1 parent f2aa09c commit 1610d97
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ private[parser] class ExprParser[P](parserParameters: ParserParameters[P]) {
eToTextTypeConName |
eThrow |
eCallInterface |
eToInterface |
eFromInterface |
(id ^? builtinFunctions) ^^ EBuiltin |
experimental |
caseOf |
Expand Down Expand Up @@ -221,6 +223,18 @@ private[parser] class ExprParser[P](parserParameters: ParserParameters[P]) {
private lazy val eToTextTypeConName: Parser[Expr] =
`type_rep` ~>! argTyp ^^ ETypeRep

private lazy val eToInterface: Parser[Expr] =
`to_interface` ~! `@` ~> fullIdentifier ~ `@` ~ fullIdentifier ~ expr0 ^^ {
case ifaceId ~ _ ~ tmplId ~ e =>
EToInterface(ifaceId, tmplId, e)
}

private lazy val eFromInterface: Parser[Expr] =
`from_interface` ~! `@` ~> fullIdentifier ~ `@` ~ fullIdentifier ~ expr0 ^^ {
case ifaceId ~ _ ~ tmplId ~ e =>
EFromInterface(ifaceId, tmplId, e)
}

private lazy val pattern: Parser[CasePat] =
primCon ^^ CPPrimCon |
(`nil` ^^^ CPNil) |
Expand Down Expand Up @@ -326,7 +340,7 @@ private[parser] class ExprParser[P](parserParameters: ParserParameters[P]) {
)

private lazy val eCallInterface: Parser[ECallInterface] =
Id("icall") ~! `@` ~> fullIdentifier ~ id ~ expr0 ^^ { case ifaceId ~ name ~ body =>
`icall` ~! `@` ~> fullIdentifier ~ id ~ expr0 ^^ { case ifaceId ~ name ~ body =>
ECallInterface(interfaceId = ifaceId, methodName = name, value = body)
}

Expand Down Expand Up @@ -396,16 +410,32 @@ private[parser] class ExprParser[P](parserParameters: ParserParameters[P]) {
UpdateCreate(t, e)
}

private lazy val updateCreateInterface =
Id("create_by_interface") ~! `@` ~> fullIdentifier ~ expr0 ^^ { case iface ~ e =>
UpdateCreateInterface(iface, e)
}

private lazy val updateFetch =
Id("fetch") ~! `@` ~> fullIdentifier ~ expr0 ^^ { case t ~ e =>
UpdateFetch(t, e)
}

private lazy val updateFetchInterface =
Id("fetch_by_interface") ~! `@` ~> fullIdentifier ~ expr0 ^^ { case iface ~ e =>
UpdateFetchInterface(iface, e)
}

private lazy val updateExercise =
Id("exercise") ~! `@` ~> fullIdentifier ~ id ~ expr0 ~ expr0 ^^ { case t ~ choice ~ cid ~ arg =>
UpdateExercise(t, choice, cid, arg)
}

private lazy val updateExerciseInterface =
Id("exercise_by_interface") ~! `@` ~> fullIdentifier ~ id ~ expr0 ~ expr0 ^^ {
case iface ~ choice ~ cid ~ arg =>
UpdateExerciseInterface(iface, choice, cid, arg)
}

private lazy val updateExerciseByKey =
Id("exercise_by_key") ~! `@` ~> fullIdentifier ~ id ~ expr0 ~ expr0 ^^ {
case t ~ choice ~ key ~ arg => UpdateExerciseByKey(t, choice, key, arg)
Expand Down Expand Up @@ -438,8 +468,11 @@ private[parser] class ExprParser[P](parserParameters: ParserParameters[P]) {
updatePure |
updateBlock |
updateCreate |
updateCreateInterface |
updateFetch |
updateFetchInterface |
updateExercise |
updateExerciseInterface |
updateExerciseByKey |
updateFetchByKey |
updateLookupByKey |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ private[parser] object Lexer extends RegexParsers {
"from_any_exception" -> `from_any_exception`,
"throw" -> `throw`,
"catch" -> `catch`,
"to_interface" -> `to_interface`,
"from_interface" -> `from_interface`,
"icall" -> `icall`,
)

val token: Parser[Token] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ private[parser] object Token {
case object `from_any_exception` extends Token
case object `throw` extends Token
case object `catch` extends Token
case object `to_interface` extends Token
case object `from_interface` extends Token
case object `icall` extends Token

final case class Id(s: String) extends Token
final case class ContractId(s: String) extends Token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,14 @@ class ParsersSpec extends AnyWordSpec with ScalaCheckPropertyChecks with Matcher
ECallInterface(I.tycon, n"method", e"body"),
"icall @'-pkgId-':Mod:I method body" ->
ECallInterface(I.tycon, n"method", e"body"),
"to_interface @Mod:T @Mod:I body" ->
EToInterface(T.tycon, I.tycon, e"body"),
"to_interface @'-pkgId-':Mod:T @'-pkgId-':Mod:I body" ->
EToInterface(T.tycon, I.tycon, e"body"),
"from_interface @Mod:T @Mod:I body" ->
EFromInterface(T.tycon, I.tycon, e"body"),
"from_interface @'-pkgId-':Mod:T @'-pkgId-':Mod:I body" ->
EFromInterface(T.tycon, I.tycon, e"body"),
)

forEvery(testCases)((stringToParse, expectedExp) =>
Expand Down Expand Up @@ -416,10 +424,16 @@ class ParsersSpec extends AnyWordSpec with ScalaCheckPropertyChecks with Matcher
),
"create @Mod:T e" ->
UpdateCreate(T.tycon, e"e"),
"create_by_interface @Mod:I e" ->
UpdateCreateInterface(I.tycon, e"e"),
"fetch @Mod:T e" ->
UpdateFetch(T.tycon, e"e"),
"fetch_by_interface @Mod:I e" ->
UpdateFetchInterface(I.tycon, e"e"),
"exercise @Mod:T Choice cid arg" ->
UpdateExercise(T.tycon, n"Choice", e"cid", e"arg"),
"exercise_by_interface @Mod:I Choice cid arg" ->
UpdateExerciseInterface(I.tycon, n"Choice", e"cid", e"arg"),
"exercise_by_key @Mod:T Choice key arg" ->
UpdateExerciseByKey(T.tycon, n"Choice", e"key", e"arg"),
"fetch_by_key @Mod:T e" ->
Expand Down Expand Up @@ -845,18 +859,20 @@ class ParsersSpec extends AnyWordSpec with ScalaCheckPropertyChecks with Matcher
"from_any_exception",
"throw",
"catch",
"to_interface",
"from_interface",
)

private val modName = DottedName.assertFromString("Mod")

private def qualify(s: String) =
Identifier(defaultPackageId, QualifiedName(modName, DottedName.assertFromString(s)))

private val T: TTyCon = TTyCon(qualify("T"))
private val R: TTyCon = TTyCon(qualify("R"))
private val E: TTyCon = TTyCon(qualify("E"))
private val I: TTyCon = TTyCon(qualify("I"))
private val R: TTyCon = TTyCon(qualify("R"))
private val RIntBool = TypeConApp(R.tycon, ImmArray(t"Int64", t"Bool"))
private val T: TTyCon = TTyCon(qualify("T"))
private val α: TVar = TVar(n"a")
private val β: TVar = TVar(n"b")

Expand Down

0 comments on commit 1610d97

Please sign in to comment.