-
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 #354 from alephium/b_string_parser
`BStringParser`
- Loading branch information
Showing
8 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...ess/src/main/scala/org/alephium/ralph/lsp/access/compiler/parser/soft/BStringParser.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,31 @@ | ||
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 BStringParser { | ||
|
||
def parseOrFail[Unknown: P]: P[SoftAST.BString] = | ||
P { | ||
Index ~ | ||
TokenParser.parseOrFail(Token.B) ~ | ||
SpaceParser.parseOrFail.? ~ | ||
TokenParser.parseOrFail(Token.Tick) ~ | ||
TextParser.parseOrFail(Token.Tick).? ~ | ||
TokenParser.parse(Token.Tick) ~ | ||
Index | ||
} map { | ||
case (from, b, postBSpace, startTick, text, endTick, to) => | ||
SoftAST.BString( | ||
index = range(from, to), | ||
b = b, | ||
postBSpace = postBSpace, | ||
startTick = startTick, | ||
text = text, | ||
endTick = endTick | ||
) | ||
} | ||
|
||
} |
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
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
167 changes: 167 additions & 0 deletions
167
...ccess/src/test/scala/org/alephium/ralph/lsp/access/compiler/parser/soft/BStringSpec.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,167 @@ | ||
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, Token} | ||
import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.TestSoftAST._ | ||
import org.alephium.ralph.lsp.access.util.TestCodeUtil._ | ||
import org.alephium.ralph.lsp.access.util.TestFastParse.assertIsFastParseError | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
import org.scalatest.OptionValues._ | ||
|
||
class BStringSpec extends AnyWordSpec with Matchers { | ||
|
||
"b alone" should { | ||
"not parse as String literal" in { | ||
// this is because "b" is not a reserved token. | ||
// "b" followed by tick is required for string literal. | ||
assertIsFastParseError { | ||
parseBString("b") | ||
} | ||
} | ||
} | ||
|
||
"b followed by a tick" should { | ||
"parse as string literal" in { | ||
// this is successfully parsed and the ending tick is reported as missing. | ||
val string = | ||
parseBString("b`") // closing tick is missing | ||
|
||
string shouldBe | ||
SoftAST.BString( | ||
index = indexOf(">>b`<<"), | ||
b = B(indexOf(">>b<<`")), | ||
postBSpace = None, | ||
startTick = Tick(indexOf("b>>`<<")), | ||
text = None, | ||
endTick = SoftAST.TokenExpected(indexOf("b`>><<"), Token.Tick) | ||
) | ||
} | ||
} | ||
|
||
"empty string" in { | ||
val string = | ||
parseBString("b``") | ||
|
||
string shouldBe | ||
SoftAST.BString( | ||
index = indexOf(">>b``<<"), | ||
b = B(indexOf(">>b<<``")), | ||
postBSpace = None, | ||
startTick = Tick(indexOf("b>>`<<`")), | ||
text = None, | ||
endTick = Tick(indexOf("b`>>`<<")) | ||
) | ||
} | ||
|
||
"spaces only" in { | ||
val string = | ||
parseBString("b ` `") | ||
|
||
string shouldBe | ||
SoftAST.BString( | ||
index = indexOf(">>b ` `<<"), | ||
b = B(indexOf(">>b<< ` `")), | ||
postBSpace = Some(SpaceOne(indexOf("b>> <<` `"))), | ||
startTick = Tick(indexOf("b >>`<< `")), | ||
text = Some(SoftAST.CodeString(indexOf("b `>> <<`"), " ")), | ||
endTick = Tick(indexOf("b ` >>`<<")) | ||
) | ||
} | ||
|
||
"comment exist before b" in { | ||
val string = | ||
parseBString { | ||
"""// this is a byte string | ||
|b ` `""".stripMargin | ||
} | ||
|
||
// Simply check that the comment exists before b. | ||
// No need to assert the comment AST, CommentsSpec should assert this. | ||
val comments = | ||
string.b.documentation.value | ||
|
||
comments.index shouldBe | ||
indexOf { | ||
""">>// this is a byte string | ||
|<<b ` `""".stripMargin | ||
} | ||
|
||
comments.toCode() shouldBe | ||
"""// this is a byte string | ||
|""".stripMargin | ||
|
||
} | ||
|
||
"comment exist before tick" in { | ||
val string = | ||
parseBString { | ||
"""b | ||
|// this is a byte string | ||
|` `""".stripMargin | ||
} | ||
|
||
// Check that the comment exists before b. | ||
// No need to assert the comment AST, CommentsSpec should assert this. | ||
val comments = | ||
string.startTick.documentation.value | ||
|
||
comments.index shouldBe | ||
indexOf { | ||
"""b | ||
|>>// this is a byte string | ||
|<<` `""".stripMargin | ||
} | ||
|
||
comments.toCode() shouldBe | ||
"""// this is a byte string | ||
|""".stripMargin | ||
|
||
} | ||
|
||
"string characters exist" when { | ||
"closing tick is missing" should { | ||
"parse the entire string" in { | ||
val string = | ||
parseBString { | ||
"""b` this is | ||
| | ||
|a string value | ||
|""".stripMargin | ||
} | ||
|
||
string.text.value shouldBe | ||
SoftAST.CodeString( | ||
index = indexOf { | ||
"""b`>> this is | ||
| | ||
|a string value | ||
|<<""".stripMargin | ||
}, | ||
text = """ this is | ||
| | ||
|a string value | ||
|""".stripMargin | ||
) | ||
|
||
string.text.value.text shouldBe | ||
""" this is | ||
| | ||
|a string value | ||
|""".stripMargin | ||
|
||
string.endTick shouldBe | ||
SoftAST.TokenExpected( | ||
index = indexOf { | ||
"""b` this is | ||
| | ||
|a string value | ||
|>><<""".stripMargin | ||
}, | ||
token = Token.Tick | ||
) | ||
} | ||
} | ||
} | ||
|
||
} |
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