Fixing parsing function calls in If expression (#508)

This commit is contained in:
Dima 2022-04-29 15:12:40 +03:00 committed by GitHub
parent e548578752
commit 2ff870dd9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 8 deletions

View File

@ -87,14 +87,14 @@ object Token {
val exclamation: P[Unit] = P.char('!')
val `[]` : P[Unit] = P.string("[]")
val `[` : P[Unit] = P.char('[') <* ` `.?
val `*[` : P[Unit] = P.string("*[").surroundedBy(` `.?)
val `?[` : P[Unit] = P.string("?[").surroundedBy(` `.?)
val `*[` : P[Unit] = P.string("*[") <* ` `.?
val `?[` : P[Unit] = P.string("?[") <* ` `.?
val `]` : P[Unit] = ` `.?.with1 *> P.char(']')
val `` : P[Unit] = P.char('')
val `⊥` : P[Unit] = P.char('⊥')
val `∅` : P[Unit] = P.char('∅')
val `(` : P[Unit] = P.char('(').surroundedBy(` `.?)
val `)` : P[Unit] = P.char(')').surroundedBy(` `.?)
val `(` : P[Unit] = P.char('(') <* ` `.?
val `)` : P[Unit] = ` `.?.with1 *> P.char(')')
val `()` : P[Unit] = P.string("()")
val ` -> ` : P[Unit] = P.string("->").surroundedBy(` `.?)
val ` <- ` : P[Unit] = P.string("<-").surroundedBy(` `.?)

View File

@ -123,10 +123,10 @@ object ArrowTypeToken {
}
def `arrowWithNames`(argTypeP: P[TypeToken[Span.S]]): P[ArrowTypeToken[Span.S]] =
(((`(`.lift <* `/s*`) ~ comma0(
(((` `.?.with1 *> `(`.lift <* `/s*`) ~ comma0(
(Name.p.map(Option(_)) ~ (` : ` *> (argTypeP | argTypeP.between(`(`, `)`))))
.surroundedBy(`/s*`)
) <* (`/s*` *> `)`)) ~
) <* (`/s*` *> `)` <* ` `.?)) ~
(` -> ` *> comma(DataTypeToken.`datatypedef`)).?).map { case ((point, args), res) =>
ArrowTypeToken(point, args, res.toList.flatMap(_.toList))
}

View File

@ -82,7 +82,8 @@ object CallArrowToken {
val callArrow: P[CallArrowToken[Span.S]] =
((Ability.dotted <* `.`).?.with1 ~
(Name.p
~ comma0(ValueToken.`value`.surroundedBy(`/s*`)).between(`(` <* `/s*`, `/s*` *> `)`))
~ comma0(ValueToken.`value`.surroundedBy(`/s*`))
.between(` `.?.with1 *> `(` <* `/s*`, `/s*` *> `)`))
.withContext(
"Missing braces '()' after the function call"
)).map { case (ab, (fn, args)) =>

View File

@ -2,7 +2,9 @@ package aqua.parser
import aqua.AquaSpec
import aqua.parser.expr.func.IfExpr
import aqua.parser.lexer.EqOp
import aqua.parser.lexer.InfixToken.Op.{Add, Sub}
import aqua.parser.lexer.{CallArrowToken, CollectionToken, EqOp, InfixToken}
import aqua.parser.lexer.CollectionToken.Mode.OptionMode
import cats.Id
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
@ -35,5 +37,49 @@ class IfExprSpec extends AnyFlatSpec with Matchers with AquaSpec {
parseIf("if a!5 == b[3]") should be(
IfExpr[Id](toVarIndex("a", 5), EqOp[Id](true), toVarIndex("b", 3))
)
parseIf("if Op.identity(\"str\") == \"a\"") should be(
IfExpr[Id](
CallArrowToken[Id](Some(toAb("Op")), toName("identity"), toStr("str") :: Nil),
EqOp[Id](true),
toStr("a")
)
)
parseIf("if Op.identity(\"str\") != Op.identity(\"str\")") should be(
IfExpr[Id](
CallArrowToken[Id](Some(toAb("Op")), toName("identity"), toStr("str") :: Nil),
EqOp[Id](false),
CallArrowToken[Id](Some(toAb("Op")), toName("identity"), toStr("str") :: Nil)
)
)
parseIf("if 2 - 3 != Op.identity(4) + 5") should be(
IfExpr[Id](
InfixToken[Id](toNumber(2), toNumber(3), Sub),
EqOp[Id](false),
InfixToken[Id](
CallArrowToken[Id](Some(toAb("Op")), toName("identity"), toNumber(4) :: Nil),
toNumber(5),
Add
)
)
)
parseIf("if funcCall(3) == funcCall2(4)") should be(
IfExpr[Id](
CallArrowToken[Id](None, toName("funcCall"), toNumber(3) :: Nil),
EqOp[Id](true),
CallArrowToken[Id](None, toName("funcCall2"), toNumber(4) :: Nil)
)
)
parseIf("if ?[\"a\"] == ?[\"a\"]") should be(
IfExpr[Id](
CollectionToken[Id](OptionMode, toStr("a") :: Nil),
EqOp[Id](true),
CollectionToken[Id](OptionMode, toStr("a") :: Nil)
)
)
}
}