Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
simerplaha committed Jan 20, 2025
1 parent 5bf42ea commit d6ac479
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.{SoftAST, Token}

private object BlockParser {

def parseOrFail[Unknown: P]: P[SoftAST.BlockClause] =
parse(required = false)

def parse[Unknown: P]: P[SoftAST.BlockClause] =
parse(required = true)

def parseOrFail[Unknown: P]: P[SoftAST.BlockClause] =
parse(required = false)

def body[Unknown: P]: P[SoftAST.BlockBody] =
body()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ private object GroupParser {
TokenParser.parse(required, open) ~
SpaceParser.parseOrFail.? ~
Index ~
expression.? ~
expression(open, close).? ~
SpaceParser.parseOrFail.? ~
tail.rep ~
tail(open, close).rep ~
TokenParser.parse(close) ~
Index
} map {
Expand Down Expand Up @@ -108,12 +108,14 @@ private object GroupParser {
*
* @return An instance of [[SoftAST.GroupTail]].
*/
private def tail[Unknown: P]: P[SoftAST.GroupTail] =
private def tail[Unknown: P, O <: Token, C <: Token](
open: O,
close: C): P[SoftAST.GroupTail] =
P {
Index ~
TokenParser.parseOrFail(Token.Comma) ~
SpaceParser.parseOrFail.? ~
expression ~
expression(open, close) ~
SpaceParser.parseOrFail.? ~
Index
} map {
Expand All @@ -127,9 +129,12 @@ private object GroupParser {
)
}

private def expression[Unknown: P]: P[SoftAST.ExpressionAST] =
private def expression[Unknown: P, O <: Token, C <: Token](
open: O,
close: C): P[SoftAST.ExpressionAST] =
P {
TypeAssignmentParser.parseOrFail |
GroupParser.parseOrFail(open, close) |
TypeAssignmentParser.parseOrFail |
AssignmentParser.parseOrFail |
InfixCallParser.parseOrFail |
MethodCallParser.parseOrFail |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,39 @@ class GroupParserSpec extends AnyWordSpec with Matchers {
bbb.tpe.toCode() shouldBe "(tuple1, tuple2)"
}

"nested tuples" in {
val tuple = parseTuple("(a, (b, c))")

tuple.headExpression.value shouldBe a[SoftAST.Identifier]

tuple.tailExpressions should have size 1
val lastTuple = tuple.tailExpressions.head

lastTuple shouldBe
SoftAST.GroupTail(
index = indexOf("(a>>, (b, c)<<)"),
comma = Comma(indexOf("(a>>,<< (b, c))")),
preExpressionSpace = Some(SpaceOne(indexOf("(a,>> <<(b, c))"))),
expression = SoftAST.Group(
index = indexOf("(a, >>(b, c)<<)"),
openToken = OpenParen(indexOf("(a, >>(<<b, c))")),
preHeadExpressionSpace = None,
headExpression = Some(Identifier(indexOf("(a, (>>b<<, c))"), "b")),
postHeadExpressionSpace = None,
tailExpressions = Seq(
SoftAST.GroupTail(
index = indexOf("(a, (b>>, c<<))"),
comma = Comma(indexOf("(a, (b>>,<< c))")),
preExpressionSpace = Some(SpaceOne(indexOf("(a, (b,>> <<c))"))),
expression = Identifier(indexOf("(a, (b, >>c<<))"), "c"),
postExpressionSpace = None
)
),
closeToken = CloseParen(indexOf("(a, (b, c>>)<<)"))
),
postExpressionSpace = None
)

}

}

0 comments on commit d6ac479

Please sign in to comment.