Skip to content

Commit 0499ec2

Browse files
committed
Updated lexer to catch invalid integer literals
1 parent a6d3a2a commit 0499ec2

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

compiler/src/Lexer.x

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ $graphic = $printable # $white
3333
@sym = $alpha_ [$alpha $digit \_ \']*
3434
@string = \" ($printable # \")* \"
3535
@label = \`\{ ($printable # \})* \}\`
36-
36+
@declit = $digit[\_$digit]*
37+
@binlit = 0[bB]$bindigit[\_$bindigit]*
38+
@octlit = 0[oO]$octdigit[\_$octdigit]*
39+
@hexlit = 0[xX]$hexdigit[\_$hexdigit]*
3740

3841
tokens:-
3942
-- Whitespace insensitive
@@ -112,11 +115,11 @@ tokens:-
112115
<state_dclabel> "#root-integrity" { mkL TokenDCRootInteg }
113116
<state_dclabel> "#null-confidentiality" { mkL TokenDCNullConf }
114117
<state_dclabel> "#null-integrity" { mkL TokenDCNullInteg }
115-
<0> $digit[\_$digit]* { mkLs (\s -> TokenNum (read (filter (/='_') s))) }
116-
-- TODO: Improve lexing of the below integer literal matches. 2025-08-24; ASL.
117-
<0> 0[bB]$bindigit[\_$bindigit]* { mkLs (\s -> TokenNum (fst (head (readBin (filter (/='_') (drop 2 s)))))) }
118-
<0> 0[oO]$octdigit[\_$octdigit]* { mkLs (\s -> TokenNum (fst (head (readOct (filter (/='_') (drop 2 s)))))) }
119-
<0> 0[xX]$hexdigit[\_$hexdigit]* { mkLs (\s -> TokenNum (fst (head (readHex (filter (/='_') (drop 2 s)))))) }
118+
<0> @declit { mkLs (\s -> TokenNum (read (filter (/='_') s))) }
119+
<0> @binlit { mkLs (\s -> TokenNum (fst (head (readBin (filter (/='_') (drop 2 s)))))) }
120+
<0> @octlit { mkLs (\s -> TokenNum (fst (head (readOct (filter (/='_') (drop 2 s)))))) }
121+
<0> @hexlit { mkLs (\s -> TokenNum (fst (head (readHex (filter (/='_') (drop 2 s)))))) }
122+
<0> (@declit|@binlit|@octlit|@hexlit)@sym { \(_, _, _, s) _ -> lexerError ("Invalid literal " ++ s) }
120123
<0> [\<][\<] { mkL TokenBinShiftLeft }
121124
<0> [\>][\>] { mkL TokenBinShiftRight }
122125
<0> [\~][\>][\>] { mkL TokenBinZeroShiftRight }
@@ -393,7 +396,6 @@ lexerError msg =
393396
where
394397
trim = reverse . dropWhile (== ' ') . reverse . dropWhile (== ' ')
395398

396-
397399
-- we use a custom version of monadScan so that we have full
398400
-- control over the error reporting; this one is based on
399401
-- the built-in alexMonadScan

tests/cmp/int-lit1.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Parse Error:
2+
Invalid literal 0x_A
3+
at 1:5 before end of line
File renamed without changes.

tests/cmp/int-lit2.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Parse Error:
2+
Invalid literal 1_x_0
3+
at 1:6 before end of line

tests/cmp/int-lit2.trp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1_x_0

0 commit comments

Comments
 (0)