-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from alephium/mut_binding
Mutable binding
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
.../main/scala/org/alephium/ralph/lsp/access/compiler/parser/soft/MutableBindingParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.alephium.ralph.lsp.access.compiler.parser.soft | ||
|
||
import fastparse._ | ||
import fastparse.NoWhitespace.noWhitespaceImplicit | ||
import org.alephium.ralph.lsp.access.compiler.message.SourceIndexExtra.range | ||
import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.{SoftAST, Token} | ||
|
||
private object MutableBindingParser { | ||
|
||
/** Syntax: mut [identifier] */ | ||
def parseOrFail[Unknown: P]: P[SoftAST.MutableBinding] = | ||
P { | ||
Index ~ | ||
TokenParser.parseOrFail(Token.Mut) ~ | ||
SpaceParser.parseOrFail ~ | ||
IdentifierParser.parseOrFail ~ | ||
Index | ||
} map { | ||
case (from, mut, space, identifier, to) => | ||
SoftAST.MutableBinding( | ||
index = range(from, to), | ||
mut = mut, | ||
space = space, | ||
identifier = identifier | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...rc/test/scala/org/alephium/ralph/lsp/access/compiler/parser/soft/MutableBindingSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2024 The Alephium Authors | ||
// This file is part of the alephium project. | ||
// | ||
// The library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the library. If not, see http://www.gnu.org/licenses/. | ||
|
||
package org.alephium.ralph.lsp.access.compiler.parser.soft | ||
|
||
import org.alephium.ralph.lsp.access.compiler.parser.soft.TestParser._ | ||
import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.SoftAST | ||
import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.TestSoftAST._ | ||
import org.alephium.ralph.lsp.access.util.TestCodeUtil._ | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
import org.scalatest.OptionValues._ | ||
|
||
class MutableBindingSpec extends AnyWordSpec with Matchers { | ||
|
||
"succeed" when { | ||
"an identifier is set a mut" in { | ||
val annotation = | ||
parseMutableBinding("mut variable") | ||
|
||
annotation shouldBe | ||
SoftAST.MutableBinding( | ||
index = indexOf(">>mut variable<<"), | ||
mut = Mut(indexOf(">>mut<< variable")), | ||
space = SpaceOne(indexOf("mut>> <<variable")), | ||
identifier = Identifier(indexOf("mut >>variable<<"), "variable") | ||
) | ||
} | ||
|
||
"an identifier in a tuple is set a mut" in { | ||
val body = | ||
parseSoft("(a, b, mut variable)") | ||
|
||
body.parts should have size 1 | ||
val tuple = body.parts.head.part.asInstanceOf[SoftAST.Tuple] | ||
|
||
tuple.headExpression shouldBe defined | ||
tuple.tailExpressions should have size 2 // there are two tail expressions | ||
val lastExpression = tuple.tailExpressions.last.expression.asInstanceOf[SoftAST.MutableBinding] // test the last expression i.e. `mut variable` | ||
|
||
lastExpression shouldBe | ||
SoftAST.MutableBinding( | ||
index = indexOf("(a, b, >>mut variable<<)"), | ||
mut = Mut(indexOf("(a, b, >>mut<< variable)")), | ||
space = SpaceOne(indexOf("(a, b, mut>> <<variable)")), | ||
identifier = Identifier(indexOf("(a, b, mut >>variable<<)"), "variable") | ||
) | ||
} | ||
|
||
"the binding is documented" when { | ||
"tuple" in { | ||
val body = | ||
parseSoft { | ||
"""( | ||
|a, | ||
|b, | ||
|// documentation line 1 | ||
|// documentation line 2 | ||
|mut variable | ||
|) | ||
|""".stripMargin | ||
} | ||
|
||
body.parts should have size 1 | ||
val tuple = body.parts.head.part.asInstanceOf[SoftAST.Tuple] | ||
|
||
tuple.headExpression shouldBe defined | ||
tuple.tailExpressions should have size 2 // there are two tail expressions | ||
val lastExpression = tuple.tailExpressions.last.expression.asInstanceOf[SoftAST.MutableBinding] // test the last expression i.e. `mut variable` | ||
|
||
lastExpression.mut.documentation.value.toCode() shouldBe | ||
"""// documentation line 1 | ||
|// documentation line 2 | ||
|""".stripMargin | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters