-
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 #351 from alephium/number_parser
`NumberParser`
- Loading branch information
Showing
7 changed files
with
302 additions
and
2 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
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