-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NumberParser
#351
Merged
Merged
NumberParser
#351
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 46 additions & 0 deletions
46
...cess/src/main/scala/org/alephium/ralph/lsp/access/compiler/parser/soft/NumberParser.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,46 @@ | ||
package org.alephium.ralph.lsp.access.compiler.parser.soft | ||
|
||
import fastparse._ | ||
import fastparse.NoWhitespace.noWhitespaceImplicit | ||
import org.alephium.ralph.lsp.access.compiler.message.SourceIndexExtra._ | ||
import org.alephium.ralph.lsp.access.compiler.parser.soft.ast.{SoftAST, Token} | ||
|
||
object NumberParser { | ||
|
||
def parseOrFail[Unknown: P]: P[SoftAST.Number] = | ||
P { | ||
Index ~ | ||
CommentParser.parseOrFail.? ~ | ||
number ~ | ||
SpaceParser.parseOrFail.? ~ | ||
TokenParser.parseOrFail(Token.AlphLowercase).? ~ | ||
Index | ||
} map { | ||
case (from, documentation, digits, postDigitSpace, unit, to) => | ||
SoftAST.Number( | ||
index = range(from, to), | ||
documentation = documentation, | ||
number = digits, | ||
space = postDigitSpace, | ||
unit = unit | ||
) | ||
} | ||
|
||
private def number[Unknown: P]: P[SoftAST.CodeString] = | ||
P { | ||
Index ~ | ||
isNumber ~ | ||
(!Token.AlphLowercase.lexeme ~ CharIn("[0-9a-zA-Z]._+\\-")).rep(1).! ~ | ||
Index | ||
} map { | ||
case (from, number, to) => | ||
SoftAST.CodeString( | ||
index = range(from, to), | ||
text = number | ||
) | ||
} | ||
|
||
private def isNumber[Unknown: P]: P[Unit] = | ||
P(&(CharIn("+\\-").? ~ CharIn("[0-9]"))) | ||
|
||
} |
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
216 changes: 216 additions & 0 deletions
216
.../src/test/scala/org/alephium/ralph/lsp/access/compiler/parser/soft/NumberParserSpec.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,216 @@ | ||
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 | ||
|
||
class NumberParserSpec extends AnyWordSpec with Matchers { | ||
|
||
def assertSimpleNumber(number: String) = | ||
parseNumber(number) shouldBe | ||
Number( | ||
index = indexOf(s">>$number<<"), | ||
text = number | ||
) | ||
|
||
"integer" in { | ||
assertSimpleNumber("10") | ||
assertSimpleNumber("-10") | ||
assertSimpleNumber("+10") | ||
} | ||
|
||
"typed" in { | ||
assertSimpleNumber("10u") | ||
assertSimpleNumber("10i") | ||
assertSimpleNumber("-10u") | ||
assertSimpleNumber("-10i") | ||
assertSimpleNumber("+10u") | ||
assertSimpleNumber("+10i") | ||
} | ||
|
||
"scientific" in { | ||
assertSimpleNumber("1e18") | ||
assertSimpleNumber("-1e18") | ||
assertSimpleNumber("+1e18") | ||
assertSimpleNumber("5.12e18") | ||
assertSimpleNumber("-5.12e18") | ||
assertSimpleNumber("+5.12e18") | ||
} | ||
|
||
"underscored" in { | ||
assertSimpleNumber("1_000_000_000") | ||
assertSimpleNumber("-1_000_000_000") | ||
assertSimpleNumber("+1_000_000_000") | ||
} | ||
|
||
"hex" in { | ||
assertSimpleNumber("0x12") | ||
assertSimpleNumber("-0x12") | ||
assertSimpleNumber("+0x12") | ||
} | ||
|
||
"unit" when { | ||
"invalid" in { | ||
assertSimpleNumber("1alp") | ||
assertSimpleNumber("-1alp") | ||
assertSimpleNumber("+1alp") | ||
|
||
assertSimpleNumber("1lph") | ||
assertSimpleNumber("-1lph") | ||
assertSimpleNumber("+1lph") | ||
|
||
assertSimpleNumber("1hpla") | ||
assertSimpleNumber("-1hpla") | ||
assertSimpleNumber("+1hpla") | ||
} | ||
|
||
"valid" when { | ||
"no spaces" when { | ||
def testWithUnit(numberOnly: String) = | ||
parseNumber(s"${numberOnly}alph") shouldBe | ||
SoftAST.Number( | ||
index = indexOf(s">>${numberOnly}alph<<"), | ||
documentation = None, | ||
number = SoftAST.CodeString( | ||
index = indexOf(s">>$numberOnly<<alph"), | ||
text = numberOnly | ||
), | ||
space = None, | ||
unit = Some(AlphLowercase(indexOf(s"$numberOnly>>alph<<"))) | ||
) | ||
|
||
"no sign" in { | ||
testWithUnit("1") | ||
testWithUnit("5.12e18") | ||
testWithUnit("0x12") | ||
} | ||
|
||
"positive" in { | ||
testWithUnit("+1") | ||
testWithUnit("+5.12e18") | ||
testWithUnit("+0x12") | ||
} | ||
|
||
"negative" in { | ||
testWithUnit("-1") | ||
testWithUnit("-5.12e18") | ||
testWithUnit("-0x12") | ||
} | ||
} | ||
|
||
"with space" when { | ||
def testWithUnit(numberOnly: String) = | ||
parseNumber(s"$numberOnly alph") shouldBe | ||
SoftAST.Number( | ||
index = indexOf(s">>$numberOnly alph<<"), | ||
documentation = None, | ||
number = SoftAST.CodeString( | ||
index = indexOf(s">>$numberOnly<< alph"), | ||
text = numberOnly | ||
), | ||
space = Some(SpaceOne(indexOf(s"$numberOnly>> <<alph"))), | ||
unit = Some(AlphLowercase(indexOf(s"$numberOnly >>alph<<"))) | ||
) | ||
|
||
"no sign" in { | ||
testWithUnit("1") | ||
testWithUnit("5.12e18") | ||
testWithUnit("0x12") | ||
} | ||
|
||
"positive" in { | ||
testWithUnit("+1") | ||
testWithUnit("+5.12e18") | ||
testWithUnit("+0x12") | ||
} | ||
|
||
"negative" in { | ||
testWithUnit("-1") | ||
testWithUnit("-5.12e18") | ||
testWithUnit("-0x12") | ||
} | ||
} | ||
|
||
"scientific number" when { | ||
"without space" when { | ||
"valid unit" in { | ||
parseNumber("1e-18alph") shouldBe | ||
SoftAST.Number( | ||
index = indexOf(s">>1e-18alph<<"), | ||
documentation = None, | ||
number = SoftAST.CodeString( | ||
index = indexOf(s">>1e-18<<alph"), | ||
text = "1e-18" | ||
), | ||
space = None, | ||
unit = Some(AlphLowercase(indexOf("1e-18>>alph<<"))) | ||
) | ||
} | ||
|
||
"invalid unit - 'alp' is typo" in { | ||
parseNumber("1e-18alp") shouldBe | ||
SoftAST.Number( | ||
index = indexOf(s">>1e-18alp<<"), | ||
documentation = None, | ||
number = SoftAST.CodeString( | ||
index = indexOf(s">>1e-18alp<<"), | ||
text = "1e-18alp" | ||
), | ||
space = None, | ||
unit = None | ||
) | ||
} | ||
} | ||
|
||
"with space" when { | ||
"valid unit" in { | ||
parseNumber("1e-18 alph") shouldBe | ||
SoftAST.Number( | ||
index = indexOf(s">>1e-18 alph<<"), | ||
documentation = None, | ||
number = SoftAST.CodeString( | ||
index = indexOf(s">>1e-18<< alph"), | ||
text = "1e-18" | ||
), | ||
space = Some(SpaceOne(indexOf("1e-18>> <<alph"))), | ||
unit = Some(AlphLowercase(indexOf("1e-18 >>alph<<"))) | ||
) | ||
} | ||
|
||
"invalid unit - 'alp' is typo" in { | ||
val body = parseSoft("1e-18 alp") | ||
body.parts should have size 2 | ||
|
||
val number = body.parts.head.part | ||
val alp = body.parts.last.part | ||
|
||
// Note: alp is not a unit. So it's not parsed as part of the number. | ||
number shouldBe | ||
SoftAST.Number( | ||
index = indexOf(s">>1e-18 << alp"), | ||
documentation = None, | ||
number = SoftAST.CodeString( | ||
index = indexOf(s">>1e-18<< alp"), | ||
text = "1e-18" | ||
), | ||
space = Some(SpaceOne(indexOf("1e-18>> <<alp"))), | ||
unit = None | ||
) | ||
|
||
// alp is stored as an identifier | ||
alp shouldBe | ||
Identifier( | ||
index = indexOf(s"1e-18 >>alp<<"), | ||
text = "alp" | ||
) | ||
} | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice tests as always