Skip to content

Commit

Permalink
Merge pull request #355 from alephium/number_follow_compiler
Browse files Browse the repository at this point in the history
Follow rules in `Lexer.number` parser
  • Loading branch information
simerplaha authored Jan 16, 2025
2 parents 57a9478 + 1d1be2c commit 22401c2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ object NumberParser {
private def number[Unknown: P]: P[SoftAST.CodeString] =
P {
Index ~
isNumber ~
(!Token.AlphLowercase.lexeme ~ CharIn("[0-9a-zA-Z]._+\\-")).rep(1).! ~
numberOrHex.! ~
Index
} map {
case (from, number, to) =>
Expand All @@ -40,7 +39,34 @@ object NumberParser {
)
}

private def isNumber[Unknown: P]: P[Unit] =
P(&(CharIn("+\\-").? ~ CharIn("[0-9]")))
/**
* Parses characters that <u><b>could</b></u> (not strict) form a number.
*
* Follows rules defined by the parser [[org.alephium.ralph.Lexer.integer]].
* This is to handle all cases that are compiled successfully in ralphc.
*
* For example, the following are valid numbers in ralphc:
*
* {{{
* let num0 = 1_
* let num1 = 1_._
* let num2 = 1_.____0
* let num3 = 1_._0___0
* }}}
*
* Current parser code in [[org.alephium.ralph.Lexer.integer]], update accordingly:
* {{{
* Index ~ (CharsWhileIn("0-9_") ~ ("." ~ CharsWhileIn("0-9_")).? ~
* ("e" ~ "-".? ~ CharsWhileIn("0-9")).?).! ~
* CharsWhileIn(" ", 0).! ~ token(Keyword.alph).?.!
* }}}
*/
private def numberOrHex[Unknown: P]: P[Unit] =
P {
CharIn("\\+", "\\-").? ~
CharsWhileIn("0-9_") ~
("." ~ CharsWhileIn("0-9_")).? ~
(!Token.AlphLowercase.lexeme ~ (StringIn("e-", "E-") | CharIn("0-9a-zA-Z_"))).rep
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,43 @@ class NumberParserSpec extends AnyWordSpec with Matchers {
)
}
}
}
}
}

"infix numbers with tail negative signs" should {
"not be parsed as a number, they are infix operations" in {
// These are not numbers, they are infix operations.
// The minus sign within the number is for scientific numbers `e-` only.
Seq(
"1-1",
"1- 1"
) foreach {
infixOperation =>
// it only parses the first number 1, ignoring the sign and the tail number.
parseNumberNoCodeCheck(infixOperation) shouldBe
Number(
index = indexOf(">>1<<"),
text = "1"
)

// parser it from the root SoftParser parses it as an infix operation and not a number.
val infix = parseSoft(infixOperation)
infix.parts should have size 1
infix.parts.head.part shouldBe a[SoftAST.InfixExpression]
}
}
}

"special cases" in {

/**
* See documentation of [[NumberParser.numberOrHex]]
*/
assertSimpleNumber("1_")
assertSimpleNumber("1_._")
assertSimpleNumber("1_.____0")
assertSimpleNumber("1_._0___0")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ object TestParser {
def parseNumber(code: String): SoftAST.Number =
runSoftParser(NumberParser.parseOrFail(_))(code)

/** Run the parser but perform no input code check */
def parseNumberNoCodeCheck(code: String): SoftAST.Number =
runAnyParser(NumberParser.parseOrFail(_))(code)

def parseBString(code: String): SoftAST.BString =
runSoftParser(BStringParser.parseOrFail(_))(code)

Expand Down

0 comments on commit 22401c2

Please sign in to comment.