Skip to content

Commit

Permalink
parser: understand binary literal
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Sep 29, 2022
1 parent cba4253 commit 2830197
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,22 @@ final class LexerTests extends AnyFunSuite with Matchers {
import ParserHarness._

test("numbers") {
Set("-1", "0", "1", "10", "999", "9", "0x42", "042", "-1.1", "0.1", "999.999", "0.000000", "0xAC").foreach(str =>
shouldParseAs(str, TsLexer.numericLiteral)(TsLexer.NumericLit(str)),
)
Set(
"-1",
"0",
"1",
"10",
"999",
"9",
"0x42",
"042",
"-1.1",
"0.1",
"999.999",
"0.000000",
"0xAC",
"0b0000000000000000000000000000001",
).foreach(str => shouldParseAs(str, TsLexer.numericLiteral)(TsLexer.NumericLit(str)))
}

test("comment") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2976,7 +2976,7 @@ export {};
shouldParseAs(content, TsParser.tsMember)(
TsMemberIndex(
NoComments,
true,
isReadOnly = true,
TsProtectionLevel.Default,
Indexing.Single(TsQIdent(IArray(TsIdentSimple("entrypoint")))),
Some(TsTypeLiteral(TsLiteral.Bool(true))),
Expand Down Expand Up @@ -3011,7 +3011,7 @@ export {};
shouldParseAs(content, TsParser.tsDeclTypeAlias)(
TsDeclTypeAlias(
NoComments,
true,
declared = true,
TsIdentSimple("AddPrefixToKeys"),
IArray(
TsTypeParam(NoComments, TsIdentSimple("Prefix"), Some(TsTypeRef.string), None),
Expand Down Expand Up @@ -3161,4 +3161,24 @@ export {};
),
)
}

test("binary literal") {
shouldParseAs(
"""const DiscreteEventPriority = 0b0000000000000000000000000000001""".stripMargin,
TsParser.tsDeclVars,
)(
IArray(
TsDeclVar(
NoComments,
declared = false,
readOnly = true,
TsIdentSimple("DiscreteEventPriority"),
None,
Some(TsExpr.Literal(TsLiteral.Num("0b0000000000000000000000000000001"))),
Zero,
CodePath.NoPath,
),
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ object TsLexer extends Lexical with StdTokens with ParserHelpers with ImplicitCo
}

val numericLiteral: Parser[NumericLit] = {
val binaryNumericLiteral: Parser[NumericLit] =
'0' ~> (Parser('b') | 'B') ~> stringOf1(Parser('0') | '1') ^^ (s => NumericLit("0b" + s))

val hexNumericLiteral: Parser[NumericLit] =
'0' ~> (Parser('x') | 'X') ~> stringOf1(
digit | 'a' | 'A' | 'b' | 'B' | 'c' | 'C' | 'd' | 'D' | 'e' | 'E' | 'f' | 'F',
) ^^ (s => NumericLit("0x" + s))

val decimal = stringOf1(digit | '.' | '-' | 'e') <~ 'n'.? ^^ NumericLit // yeah yeah, good enough for us

hexNumericLiteral | decimal.filter(_.chars.exists(_.isDigit))
binaryNumericLiteral | hexNumericLiteral | decimal.filter(_.chars.exists(_.isDigit))
}

val stringLiteral: Parser[StringLit] = {
Expand Down

0 comments on commit 2830197

Please sign in to comment.