Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions compiler/src/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import Control.Monad.Except
import Data.Char (isSpace, toLower)
import Data.List (dropWhileEnd)
import Data.Char ( chr )
import Numeric ( readDec )
import Numeric ( readDec, readBin, readOct, readHex )
import Control.Monad (when)
}

%wrapper "monadUserState"

$bindigit = [01]
$octdigit = 0-7
$hexdigit = [0-9A-Fa-f]
$digit = 0-9
$alpha = [a-zA-Z]
$alpha_ = [$alpha \_]
Expand All @@ -30,7 +33,10 @@ $graphic = $printable # $white
@sym = $alpha_ [$alpha $digit \_ \']*
@string = \" ($printable # \")* \"
@label = \`\{ ($printable # \})* \}\`

@declit = $digit[\_$digit]*
@binlit = 0[bB]$bindigit[\_$bindigit]*
@octlit = 0[oO]$octdigit[\_$octdigit]*
@hexlit = 0[xX]$hexdigit[\_$hexdigit]*

tokens:-
-- Whitespace insensitive
Expand Down Expand Up @@ -109,7 +115,12 @@ tokens:-
<state_dclabel> "#root-integrity" { mkL TokenDCRootInteg }
<state_dclabel> "#null-confidentiality" { mkL TokenDCNullConf }
<state_dclabel> "#null-integrity" { mkL TokenDCNullInteg }
<0> $digit+ { mkLs (\s -> TokenNum (read s)) }
-- Integer literal parsing inspired by https://github.com/ocaml/ocaml/blob/trunk/parsing/lexer.mll
<0> @declit { mkLs (\s -> TokenNum (read (filter (/='_') s))) }
<0> @binlit { mkLs (\s -> TokenNum (fst (head (readBin (filter (/='_') (drop 2 s)))))) }
<0> @octlit { mkLs (\s -> TokenNum (fst (head (readOct (filter (/='_') (drop 2 s)))))) }
<0> @hexlit { mkLs (\s -> TokenNum (fst (head (readHex (filter (/='_') (drop 2 s)))))) }
<0> (@declit|@binlit|@octlit|@hexlit)@sym { \(_, _, _, s) _ -> lexerError ("Invalid literal " ++ s) }
<0> [\<][\<] { mkL TokenBinShiftLeft }
<0> [\>][\>] { mkL TokenBinShiftRight }
<0> [\~][\>][\>] { mkL TokenBinZeroShiftRight }
Expand Down Expand Up @@ -386,9 +397,6 @@ lexerError msg =
where
trim = reverse . dropWhile (== ' ') . reverse . dropWhile (== ' ')




-- we use a custom version of monadScan so that we have full
-- control over the error reporting; this one is based on
-- the built-in alexMonadScan
Expand Down
3 changes: 3 additions & 0 deletions tests/cmp/int-lit1.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Parse Error:
Invalid literal 0x_A
at 1:5 before end of line
1 change: 1 addition & 0 deletions tests/cmp/int-lit1.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0x_A
3 changes: 3 additions & 0 deletions tests/cmp/int-lit2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Parse Error:
Invalid literal 1_x_0
at 1:6 before end of line
1 change: 1 addition & 0 deletions tests/cmp/int-lit2.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1_x_0
3 changes: 3 additions & 0 deletions tests/cmp/int-lit3.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Parse Error:
Invalid literal 0b12
at 1:5 before end of line
1 change: 1 addition & 0 deletions tests/cmp/int-lit3.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0b12
3 changes: 3 additions & 0 deletions tests/cmp/int-lit4.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Parse Error:
Invalid literal 0o8
at 1:4 before end of line
1 change: 1 addition & 0 deletions tests/cmp/int-lit4.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0o8
2 changes: 2 additions & 0 deletions tests/rt/pos/core/int-lit1.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2025-08-24T19:08:18.468Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
>>> Main thread finished with value: 1000000@{}%{}
1 change: 1 addition & 0 deletions tests/rt/pos/core/int-lit1.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1_000_000
2 changes: 2 additions & 0 deletions tests/rt/pos/core/int-lit2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2025-08-24T19:08:15.976Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
>>> Main thread finished with value: 27@{}%{}
1 change: 1 addition & 0 deletions tests/rt/pos/core/int-lit2.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0b11011
2 changes: 2 additions & 0 deletions tests/rt/pos/core/int-lit3.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2025-08-24T19:08:15.975Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
>>> Main thread finished with value: 511@{}%{}
1 change: 1 addition & 0 deletions tests/rt/pos/core/int-lit3.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0o777
2 changes: 2 additions & 0 deletions tests/rt/pos/core/int-lit4.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2025-08-24T19:08:23.402Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
>>> Main thread finished with value: 175@{}%{}
1 change: 1 addition & 0 deletions tests/rt/pos/core/int-lit4.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0xaf
2 changes: 2 additions & 0 deletions tests/rt/pos/core/int-lit5.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2025-08-24T19:08:23.450Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
>>> Main thread finished with value: 5@{}%{}
1 change: 1 addition & 0 deletions tests/rt/pos/core/int-lit5.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0b1__0__1
2 changes: 2 additions & 0 deletions tests/rt/pos/core/int-lit6.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2025-08-24T19:08:26.140Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
>>> Main thread finished with value: 299@{}%{}
1 change: 1 addition & 0 deletions tests/rt/pos/core/int-lit6.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0o4_5_____3
2 changes: 2 additions & 0 deletions tests/rt/pos/core/int-lit7.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2025-08-24T19:08:26.048Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
>>> Main thread finished with value: 174@{}%{}
1 change: 1 addition & 0 deletions tests/rt/pos/core/int-lit7.trp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0xA______________E_____
Loading