Stabel

Check-in [946bd10827]
Login
Overview
Comment:Improved integer syntax. [55a5b57c3b].
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA3-256: 946bd108273cc26d13a2f7fb8e9de790f348d8ff6e1fbad8858a030671ee3aa3
User & Date: robin.hansen on 2021-09-16 14:30:02
Other Links: manifest | tags
Context
2021-09-16
14:30
Improved integer syntax. [55a5b57c3b]. Leaf check-in: 946bd10827 user: robin.hansen tags: trunk
14:28
Bit-encoded integer literals. Closed-Leaf check-in: 317c5ea0b3 user: robin.hansen tags: integer-syntax-improvements
2021-09-11
09:42
elm-review fixes. check-in: 7c41ddbc55 user: robin.hansen tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified elm.json from [6ed21b8cff] to [1fc5ced1fe].

5
6
7
8
9
10
11

12
13
14
15
16
17
18
..
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    ],
    "elm-version": "0.19.1",
    "dependencies": {
        "direct": {
            "elm/core": "1.0.5",
            "elm/json": "1.1.3",
            "elm/parser": "1.1.0",

            "elm-community/dict-extra": "2.4.0",
            "elm-community/list-extra": "8.3.0",
            "elm-community/result-extra": "2.4.0",
            "zwilias/elm-utf-tools": "2.0.1"
        },
        "indirect": {}
    },
................................................................................
    "test-dependencies": {
        "direct": {
            "elm-community/string-extra": "4.0.1",
            "elm-explorations/test": "1.2.2"
        },
        "indirect": {
            "elm/html": "1.0.0",
            "elm/random": "1.0.0",
            "elm/regex": "1.0.0",
            "elm/time": "1.0.0",
            "elm/virtual-dom": "1.0.2"
        }
    }
}







>







 







<






5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
..
20
21
22
23
24
25
26

27
28
29
30
31
32
    ],
    "elm-version": "0.19.1",
    "dependencies": {
        "direct": {
            "elm/core": "1.0.5",
            "elm/json": "1.1.3",
            "elm/parser": "1.1.0",
            "elm/random": "1.0.0",
            "elm-community/dict-extra": "2.4.0",
            "elm-community/list-extra": "8.3.0",
            "elm-community/result-extra": "2.4.0",
            "zwilias/elm-utf-tools": "2.0.1"
        },
        "indirect": {}
    },
................................................................................
    "test-dependencies": {
        "direct": {
            "elm-community/string-extra": "4.0.1",
            "elm-explorations/test": "1.2.2"
        },
        "indirect": {
            "elm/html": "1.0.0",

            "elm/regex": "1.0.0",
            "elm/time": "1.0.0",
            "elm/virtual-dom": "1.0.2"
        }
    }
}

Modified src/Stabel/Parser.elm from [acca630c3a] to [e30ba16980].

6
7
8
9
10
11
12

13
14

15

16
17
18
19
20
21
22
23
24
25
26
...
133
134
135
136
137
138
139
140




141
142
143
144
145
146
147
148


149






150
151
152
153




154









155
156
157
158
159






160




























































































































161
162
163
164
165
166
167
...
270
271
272
273
274
275
276

277
278
279
280
281
282
283
284

285
286
287
288
289
290
291
...
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
...
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
...
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
....
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
....
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
....
1231
1232
1233
1234
1235
1236
1237

1238
1239
1240
1241
1242
1243
1244
1245

1246
1247
1248
1249
1250
1251
1252
    , TypeDefinition
    , TypeDefinitionMembers(..)
    , TypeMatch(..)
    , TypeMatchValue(..)
    , run
    )


import Dict exposing (Dict)
import Dict.Extra as Dict

import Parser.Advanced as Parser exposing ((|.), (|=), Token(..))

import Set exposing (Set)
import Stabel.Parser.AssociatedFunctionSignature as AssociatedFunctionSignature exposing (AssociatedFunctionSignature)
import Stabel.Parser.ModuleDefinition as ModuleDefinition exposing (ModuleDefinition)
import Stabel.Parser.Problem as Problem exposing (..)
import Stabel.Parser.SourceLocation exposing (SourceLocation, SourceLocationRange)
import Stabel.Parser.Type
    exposing
        ( FunctionSignature
        , PossiblyQualifiedType(..)
        , PossiblyQualifiedTypeOrStackRange(..)
        )
................................................................................
    Set.fromList
        [ ' '
        , '\n'
        , '\u{000D}'
        , '\t'
        ]






{-| The builtin int parser has a bug where it commits when it comes across an 'e'
-}
intParser : Parser Int
intParser =
    let
        helper text =
            case String.toInt text of
                Just num ->


                    Parser.succeed num







                Nothing ->
                    Parser.problem ExpectedInt
    in




    Parser.variable









        { start = Char.isDigit
        , inner = Char.isDigit
        , reserved = Set.empty
        , expecting = ExpectedInt
        }






        |> Parser.andThen helper






























































































































sourceLocationParser : Parser SourceLocation
sourceLocationParser =
    Parser.succeed SourceLocation
        |= Parser.getRow
        |= Parser.getCol
................................................................................
textParser =
    Parser.chompWhile (\c -> not <| Set.member c whitespaceChars)
        |> Parser.getChompedString


stringParser : Parser String
stringParser =

    Parser.oneOf
        [ Parser.succeed stripMultilineStringWhitespace
            |. Parser.symbol (Token "\"\"\"" UnknownError)
            |= Parser.loop (Just "") multilineStringParserLoop
        , Parser.succeed identity
            |. Parser.symbol (Token "\"" UnknownError)
            |= Parser.loop (Just "") stringParserLoop
        ]



stringParserLoop : Maybe String -> Parser (Parser.Step (Maybe String) String)
stringParserLoop maybeStr =
    case maybeStr of
        Nothing ->
            -- Work around bug in elm/parser
................................................................................
            ]
        |. Parser.end ExpectedEndOfFile
        |> Parser.andThen checkIfEmpty


moduleDefinitionParser : Parser ModuleDefinition
moduleDefinitionParser =
    Parser.inContext ModuleDefinition
        (Parser.succeed identity
            |. Parser.keyword (Token "defmodule:" UnknownError)
            |. noiseParser
            |= Parser.loop ModuleDefinition.emptyDefinition moduleDefinitionMetaParser
            |> Parser.map ModuleDefinition.Defined
        )


moduleDefinitionMetaParser :
    ModuleDefinition.Definition
    -> Parser (Parser.Step ModuleDefinition.Definition ModuleDefinition.Definition)
moduleDefinitionMetaParser def =
    Parser.oneOf
        [ Parser.inContext AliasKeyword
            (Parser.succeed (\alias value -> Parser.Loop { def | aliases = Dict.insert alias value def.aliases })
                |. Parser.keyword (Token "alias:" UnknownError)
                |. noiseParser
                |= symbolParser
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
            )
        , Parser.inContext ImportKeyword
            (Parser.succeed (\mod vals -> Parser.Loop { def | imports = Dict.insert mod vals def.imports })
                |. Parser.keyword (Token "import:" UnknownError)
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
                |= Parser.loop [] symbolImplListParser
                |. noiseParser
            )
        , Parser.inContext ExposingKeyword
            (Parser.succeed (\exposings -> Parser.Loop { def | exposes = exposings })
                |. Parser.keyword (Token "exposing:" UnknownError)
                |. noiseParser
                |= (Parser.loop [] symbolImplListParser |> Parser.map Set.fromList)
                |. noiseParser
            )
        , Parser.succeed (\str -> Parser.Loop { def | documentation = str })
................................................................................
                    |= Parser.loop emptyDef functionMetadataParser
                    |= sourceLocationParser


functionMetadataParser : FunctionDefinition -> Parser (Parser.Step FunctionDefinition FunctionDefinition)
functionMetadataParser def =
    Parser.oneOf
        [ Parser.inContext TypeKeyword <|
            Parser.succeed (\typeSign -> Parser.Loop { def | typeSignature = AssociatedFunctionSignature.UserProvided typeSign })
                |. Parser.keyword (Token "type:" UnknownError)
                |. noiseParser
                |= typeSignatureParser
        , Parser.inContext DocKeyword <|
            Parser.succeed (\str -> Parser.Loop { def | documentation = str })
                |. Parser.keyword (Token "doc:" UnknownError)
                |. noiseParser
                |= stringParser
                |. noiseParser
        , Parser.inContext AliasKeyword <|
            Parser.succeed (\alias value -> Parser.Loop { def | aliases = Dict.insert alias value def.aliases })
                |. Parser.keyword (Token "alias:" UnknownError)
                |. noiseParser
                |= symbolParser
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
        , Parser.inContext ImportKeyword <|
            Parser.succeed (\mod vals -> Parser.Loop { def | imports = Dict.insert mod vals def.imports })
                |. Parser.keyword (Token "import:" UnknownError)
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
                |= Parser.loop [] symbolImplListParser
                |. noiseParser
        , Parser.inContext ImplementationKeyword <|
            Parser.succeed (\impl -> Parser.Loop { def | implementation = SoloImpl impl })
                |. Parser.keyword (Token ":" UnknownError)
                |. noiseParser
                |= implementationParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser
            |> Parser.andThen Parser.problem
................................................................................
                MultiImpl whens _ ->
                    MultiImpl whens impl

                SoloImpl _ ->
                    MultiImpl [] impl
    in
    Parser.oneOf
        [ Parser.inContext TypeKeyword <|
            Parser.succeed (\typeSign -> Parser.Loop { def | typeSignature = AssociatedFunctionSignature.UserProvided typeSign })
                |. Parser.keyword (Token "type:" UnknownError)
                |. noiseParser
                |= typeSignatureParser
        , Parser.inContext DocKeyword <|
            Parser.succeed (\str -> Parser.Loop { def | documentation = str })
                |. Parser.keyword (Token "doc:" UnknownError)
                |. noiseParser
                |= stringParser
                |. noiseParser
        , Parser.inContext ElseKeyword <|
            Parser.succeed (\impl -> Parser.Loop { def | implementation = setDefaultImpl impl })
                |. Parser.keyword (Token "else:" UnknownError)
                |. noiseParser
                |= implementationParser
        , Parser.inContext AliasKeyword <|
            Parser.succeed (\alias value -> Parser.Loop { def | aliases = Dict.insert alias value def.aliases })
                |. Parser.keyword (Token "alias:" UnknownError)
                |. noiseParser
                |= symbolParser
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
        , Parser.inContext ImportKeyword <|
            Parser.succeed (\mod vals -> Parser.Loop { def | imports = Dict.insert mod vals def.imports })
                |. Parser.keyword (Token "import:" UnknownError)
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
                |= Parser.loop [] symbolImplListParser
                |. noiseParser
        , Parser.inContext ImplementationKeyword <|
            Parser.succeed (\type_ impl -> Parser.Loop { def | implementation = addWhenImpl ( type_, impl ) })
                |. Parser.keyword (Token ":" UnknownError)
                |. noiseParser
                |= typeMatchParser
                |. noiseParser
                |= implementationParser
        , Parser.succeed UnknownMetadata
................................................................................
                            setter.name
                            setter.sourceLocationRange

                ( Nothing, Nothing ) ->
                    Parser.succeed name
    in
    Parser.oneOf
        [ Parser.inContext MemberKeyword <|
            Parser.succeed (\name type_ -> Parser.Loop { result | members = ( name, type_ ) :: result.members })
                |. Parser.symbol (Token ":" UnknownError)
                |. noiseParser
                |= Parser.andThen alreadyDefinedCheck symbolParser
                |. noiseParser
                |= typeRefParser
        , Parser.inContext DocKeyword <|
            Parser.succeed (\str -> Parser.Loop { result | documentation = str })
                |. Parser.keyword (Token "doc:" UnknownError)
                |. noiseParser
                |= stringParser
                |. noiseParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser
................................................................................


unionTypeMemberParser :
    UnionMembersParserResult
    -> Parser (Parser.Step UnionMembersParserResult UnionMembersParserResult)
unionTypeMemberParser result =
    Parser.oneOf
        [ Parser.inContext MemberKeyword <|
            Parser.succeed (\type_ -> Parser.Loop { result | types = type_ :: result.types })
                |. Parser.symbol (Token ":" UnknownError)
                |. noiseParser
                |= typeRefParser
        , Parser.inContext DocKeyword <|
            Parser.succeed (\str -> Parser.Loop { result | documentation = str })
                |. Parser.keyword (Token "doc:" UnknownError)
                |. noiseParser
                |= stringParser
                |. noiseParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser
................................................................................
            |= sourceLocationParser
            |. Parser.symbol (Token "[" ExpectedLeftBracket)
            |. noiseParser
            |= implementationParser
            |. Parser.symbol (Token "]" ExpectedRightBracket)
            |= sourceLocationParser
            |. noiseParser

        , Parser.succeed (\startLoc arrContent endLoc -> Parser.Loop (ArrayLiteral (SourceLocationRange startLoc endLoc) arrContent :: nodes))
            |= sourceLocationParser
            |. Parser.symbol (Token "{" ExpectedLeftCurly)
            |. noiseParser
            |= implementationParser
            |. Parser.symbol (Token "}" ExpectedRightCurly)
            |= sourceLocationParser
            |. noiseParser

        , Parser.succeed (\startLoc strContent endLoc -> Parser.Loop (StringLiteral (SourceLocationRange startLoc endLoc) strContent :: nodes))
            |= sourceLocationParser
            |= stringParser
            -- stringParser chomps the final "
            |= sourceLocationParser
            |. noiseParser
        , Parser.succeed (Parser.Done (List.reverse nodes))







>


>

>



|







 








>
>
>
>




<
<
<
<
>
>
|
>
>
>
>
>
>
|
<
<
<
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
|
|
|
|
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
|
|
|
|
|
|
|
|
>







 







|













|








|








|







 







|




|





|







|







|







 







|




|





|




|







|







|







 







|






|







 







|




|







 







>
|
|
|
|
|
|
|
|
>







6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151




152
153
154
155
156
157
158
159
160
161



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
...
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
...
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
...
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
....
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
....
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
....
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
....
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
    , TypeDefinition
    , TypeDefinitionMembers(..)
    , TypeMatch(..)
    , TypeMatchValue(..)
    , run
    )

import Bitwise
import Dict exposing (Dict)
import Dict.Extra as Dict
import List.Extra as List
import Parser.Advanced as Parser exposing ((|.), (|=), Token(..))
import Random
import Set exposing (Set)
import Stabel.Parser.AssociatedFunctionSignature as AssociatedFunctionSignature exposing (AssociatedFunctionSignature)
import Stabel.Parser.ModuleDefinition as ModuleDefinition exposing (ModuleDefinition)
import Stabel.Parser.Problem as Problem exposing (Context, Problem(..))
import Stabel.Parser.SourceLocation exposing (SourceLocation, SourceLocationRange)
import Stabel.Parser.Type
    exposing
        ( FunctionSignature
        , PossiblyQualifiedType(..)
        , PossiblyQualifiedTypeOrStackRange(..)
        )
................................................................................
    Set.fromList
        [ ' '
        , '\n'
        , '\u{000D}'
        , '\t'
        ]



-- Int parsing


{-| The builtin int parser has a bug where it commits when it comes across an 'e'
-}
intParser : Parser Int
intParser =




    Parser.inContext Problem.IntegerLiteral
        (Parser.oneOf
            [ Parser.succeed identity
                |. Parser.symbol (Token "0b" UnknownError)
                |= Parser.variable
                    { start = isBit
                    , inner = isBit
                    , reserved = Set.empty
                    , expecting = ExpectedBitInt
                    }



                |. whiteSpaceOrEnd
                |> Parser.andThen intBitParserHelper
            , Parser.succeed identity
                |. Parser.symbol (Token "0x" UnknownError)
                |= Parser.variable
                    { start = isHexDigit
                    , inner = isHexDigit
                    , reserved = Set.empty
                    , expecting = ExpectedHexInt
                    }
                |. whiteSpaceOrEnd
                |> Parser.andThen intHexParserHelper
            , Parser.succeed Tuple.pair
                |= Parser.variable
                    { start = Char.isDigit
                    , inner = \c -> Char.isDigit c || c == '_'
                    , reserved = Set.empty
                    , expecting = ExpectedInt
                    }
                |= Parser.oneOf
                    [ Parser.succeed True
                        |. Parser.symbol (Token "-" UnknownError)
                    , Parser.succeed False
                    ]
                |. whiteSpaceOrEnd
                |> Parser.andThen intParserHelper
            ]
        )


whiteSpaceOrEnd : Parser ()
whiteSpaceOrEnd =
    Parser.oneOf
        [ Parser.chompIf (\c -> Set.member c whitespaceChars) ExpectedWhitespace
        , Parser.succeed ()
            |. Parser.end ExpectedEndOfFile
        ]



-- Bit integers


isBit : Char -> Bool
isBit char =
    char == '0' || char == '1'


intBitParserHelper : String -> Parser Int
intBitParserHelper text =
    if String.length text > 32 then
        Parser.problem IntegerBitOutOfBounds

    else
        String.foldl bitCharFolder 0 text
            |> Parser.succeed


bitCharFolder : Char -> Int -> Int
bitCharFolder char num =
    num
        |> Bitwise.shiftLeftBy 1
        |> Bitwise.or (bitCharToNum char)


bitCharToNum : Char -> Int
bitCharToNum char =
    case char of
        '1' ->
            1

        '0' ->
            0

        _ ->
            -1



-- Hex integers


isHexDigit : Char -> Bool
isHexDigit char =
    List.member (Char.toUpper char) hexDigits


hexDigits : List Char
hexDigits =
    [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ]


intHexParserHelper : String -> Parser Int
intHexParserHelper text =
    if String.length text > 8 then
        Parser.problem IntegerHexOutOfBounds

    else
        String.foldl hexCharFolder 0 text
            |> Parser.succeed


hexCharFolder : Char -> Int -> Int
hexCharFolder char num =
    num
        |> Bitwise.shiftLeftBy 4
        |> Bitwise.or (hexCharToNum char)


hexCharToNum : Char -> Int
hexCharToNum char =
    List.elemIndex (Char.toUpper char) hexDigits
        |> Maybe.withDefault -1



-- Base 10 integers


intParserHelper : ( String, Bool ) -> Parser Int
intParserHelper ( text, isNegative ) =
    let
        digits =
            String.replace "_" "" text
    in
    if String.endsWith "_" text then
        Parser.problem IntegerTrailingUnderscore

    else if String.length digits > 1 && String.startsWith "0" digits then
        Parser.problem IntegerBadLeadingZero

    else
        case String.toInt digits of
            Just num ->
                let
                    actualNumber =
                        if isNegative then
                            num * -1

                        else
                            num
                in
                if actualNumber > Random.maxInt || actualNumber < Random.minInt then
                    Parser.problem IntegerOutOfBounds

                else
                    Parser.succeed actualNumber

            Nothing ->
                Parser.problem ExpectedInt


sourceLocationParser : Parser SourceLocation
sourceLocationParser =
    Parser.succeed SourceLocation
        |= Parser.getRow
        |= Parser.getCol
................................................................................
textParser =
    Parser.chompWhile (\c -> not <| Set.member c whitespaceChars)
        |> Parser.getChompedString


stringParser : Parser String
stringParser =
    Parser.inContext Problem.StringLiteral
        (Parser.oneOf
            [ Parser.succeed stripMultilineStringWhitespace
                |. Parser.symbol (Token "\"\"\"" UnknownError)
                |= Parser.loop (Just "") multilineStringParserLoop
            , Parser.succeed identity
                |. Parser.symbol (Token "\"" UnknownError)
                |= Parser.loop (Just "") stringParserLoop
            ]
        )


stringParserLoop : Maybe String -> Parser (Parser.Step (Maybe String) String)
stringParserLoop maybeStr =
    case maybeStr of
        Nothing ->
            -- Work around bug in elm/parser
................................................................................
            ]
        |. Parser.end ExpectedEndOfFile
        |> Parser.andThen checkIfEmpty


moduleDefinitionParser : Parser ModuleDefinition
moduleDefinitionParser =
    Parser.inContext Problem.ModuleDefinition
        (Parser.succeed identity
            |. Parser.keyword (Token "defmodule:" UnknownError)
            |. noiseParser
            |= Parser.loop ModuleDefinition.emptyDefinition moduleDefinitionMetaParser
            |> Parser.map ModuleDefinition.Defined
        )


moduleDefinitionMetaParser :
    ModuleDefinition.Definition
    -> Parser (Parser.Step ModuleDefinition.Definition ModuleDefinition.Definition)
moduleDefinitionMetaParser def =
    Parser.oneOf
        [ Parser.inContext Problem.AliasKeyword
            (Parser.succeed (\alias value -> Parser.Loop { def | aliases = Dict.insert alias value def.aliases })
                |. Parser.keyword (Token "alias:" UnknownError)
                |. noiseParser
                |= symbolParser
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
            )
        , Parser.inContext Problem.ImportKeyword
            (Parser.succeed (\mod vals -> Parser.Loop { def | imports = Dict.insert mod vals def.imports })
                |. Parser.keyword (Token "import:" UnknownError)
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
                |= Parser.loop [] symbolImplListParser
                |. noiseParser
            )
        , Parser.inContext Problem.ExposingKeyword
            (Parser.succeed (\exposings -> Parser.Loop { def | exposes = exposings })
                |. Parser.keyword (Token "exposing:" UnknownError)
                |. noiseParser
                |= (Parser.loop [] symbolImplListParser |> Parser.map Set.fromList)
                |. noiseParser
            )
        , Parser.succeed (\str -> Parser.Loop { def | documentation = str })
................................................................................
                    |= Parser.loop emptyDef functionMetadataParser
                    |= sourceLocationParser


functionMetadataParser : FunctionDefinition -> Parser (Parser.Step FunctionDefinition FunctionDefinition)
functionMetadataParser def =
    Parser.oneOf
        [ Parser.inContext Problem.TypeKeyword <|
            Parser.succeed (\typeSign -> Parser.Loop { def | typeSignature = AssociatedFunctionSignature.UserProvided typeSign })
                |. Parser.keyword (Token "type:" UnknownError)
                |. noiseParser
                |= typeSignatureParser
        , Parser.inContext Problem.DocKeyword <|
            Parser.succeed (\str -> Parser.Loop { def | documentation = str })
                |. Parser.keyword (Token "doc:" UnknownError)
                |. noiseParser
                |= stringParser
                |. noiseParser
        , Parser.inContext Problem.AliasKeyword <|
            Parser.succeed (\alias value -> Parser.Loop { def | aliases = Dict.insert alias value def.aliases })
                |. Parser.keyword (Token "alias:" UnknownError)
                |. noiseParser
                |= symbolParser
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
        , Parser.inContext Problem.ImportKeyword <|
            Parser.succeed (\mod vals -> Parser.Loop { def | imports = Dict.insert mod vals def.imports })
                |. Parser.keyword (Token "import:" UnknownError)
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
                |= Parser.loop [] symbolImplListParser
                |. noiseParser
        , Parser.inContext Problem.ImplementationKeyword <|
            Parser.succeed (\impl -> Parser.Loop { def | implementation = SoloImpl impl })
                |. Parser.keyword (Token ":" UnknownError)
                |. noiseParser
                |= implementationParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser
            |> Parser.andThen Parser.problem
................................................................................
                MultiImpl whens _ ->
                    MultiImpl whens impl

                SoloImpl _ ->
                    MultiImpl [] impl
    in
    Parser.oneOf
        [ Parser.inContext Problem.TypeKeyword <|
            Parser.succeed (\typeSign -> Parser.Loop { def | typeSignature = AssociatedFunctionSignature.UserProvided typeSign })
                |. Parser.keyword (Token "type:" UnknownError)
                |. noiseParser
                |= typeSignatureParser
        , Parser.inContext Problem.DocKeyword <|
            Parser.succeed (\str -> Parser.Loop { def | documentation = str })
                |. Parser.keyword (Token "doc:" UnknownError)
                |. noiseParser
                |= stringParser
                |. noiseParser
        , Parser.inContext Problem.ElseKeyword <|
            Parser.succeed (\impl -> Parser.Loop { def | implementation = setDefaultImpl impl })
                |. Parser.keyword (Token "else:" UnknownError)
                |. noiseParser
                |= implementationParser
        , Parser.inContext Problem.AliasKeyword <|
            Parser.succeed (\alias value -> Parser.Loop { def | aliases = Dict.insert alias value def.aliases })
                |. Parser.keyword (Token "alias:" UnknownError)
                |. noiseParser
                |= symbolParser
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
        , Parser.inContext Problem.ImportKeyword <|
            Parser.succeed (\mod vals -> Parser.Loop { def | imports = Dict.insert mod vals def.imports })
                |. Parser.keyword (Token "import:" UnknownError)
                |. noiseParser
                |= modulePathStringParser
                |. noiseParser
                |= Parser.loop [] symbolImplListParser
                |. noiseParser
        , Parser.inContext Problem.ImplementationKeyword <|
            Parser.succeed (\type_ impl -> Parser.Loop { def | implementation = addWhenImpl ( type_, impl ) })
                |. Parser.keyword (Token ":" UnknownError)
                |. noiseParser
                |= typeMatchParser
                |. noiseParser
                |= implementationParser
        , Parser.succeed UnknownMetadata
................................................................................
                            setter.name
                            setter.sourceLocationRange

                ( Nothing, Nothing ) ->
                    Parser.succeed name
    in
    Parser.oneOf
        [ Parser.inContext Problem.MemberKeyword <|
            Parser.succeed (\name type_ -> Parser.Loop { result | members = ( name, type_ ) :: result.members })
                |. Parser.symbol (Token ":" UnknownError)
                |. noiseParser
                |= Parser.andThen alreadyDefinedCheck symbolParser
                |. noiseParser
                |= typeRefParser
        , Parser.inContext Problem.DocKeyword <|
            Parser.succeed (\str -> Parser.Loop { result | documentation = str })
                |. Parser.keyword (Token "doc:" UnknownError)
                |. noiseParser
                |= stringParser
                |. noiseParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser
................................................................................


unionTypeMemberParser :
    UnionMembersParserResult
    -> Parser (Parser.Step UnionMembersParserResult UnionMembersParserResult)
unionTypeMemberParser result =
    Parser.oneOf
        [ Parser.inContext Problem.MemberKeyword <|
            Parser.succeed (\type_ -> Parser.Loop { result | types = type_ :: result.types })
                |. Parser.symbol (Token ":" UnknownError)
                |. noiseParser
                |= typeRefParser
        , Parser.inContext Problem.DocKeyword <|
            Parser.succeed (\str -> Parser.Loop { result | documentation = str })
                |. Parser.keyword (Token "doc:" UnknownError)
                |. noiseParser
                |= stringParser
                |. noiseParser
        , Parser.succeed UnknownMetadata
            |= definitionMetadataParser
................................................................................
            |= sourceLocationParser
            |. Parser.symbol (Token "[" ExpectedLeftBracket)
            |. noiseParser
            |= implementationParser
            |. Parser.symbol (Token "]" ExpectedRightBracket)
            |= sourceLocationParser
            |. noiseParser
        , Parser.inContext Problem.ArrayLiteral
            (Parser.succeed (\startLoc arrContent endLoc -> Parser.Loop (ArrayLiteral (SourceLocationRange startLoc endLoc) arrContent :: nodes))
                |= sourceLocationParser
                |. Parser.symbol (Token "{" ExpectedLeftCurly)
                |. noiseParser
                |= implementationParser
                |. Parser.symbol (Token "}" ExpectedRightCurly)
                |= sourceLocationParser
                |. noiseParser
            )
        , Parser.succeed (\startLoc strContent endLoc -> Parser.Loop (StringLiteral (SourceLocationRange startLoc endLoc) strContent :: nodes))
            |= sourceLocationParser
            |= stringParser
            -- stringParser chomps the final "
            |= sourceLocationParser
            |. noiseParser
        , Parser.succeed (Parser.Done (List.reverse nodes))

Modified src/Stabel/Parser/Problem.elm from [c2792cae36] to [6449cf85de].

23
24
25
26
27
28
29



30
31
32
33


34
35
36
37
38
39
40
..
51
52
53
54
55
56
57





58
59
60
61
62
63
64
65
66
67
68
69
70
71
..
92
93
94
95
96
97
98
99
100
101



102
103
104
105
106
107
108
109
110
111
112
113

114
115
116
117
118
119
120
...
151
152
153
154
155
156
157









158
159
160
161
162
163
164
...
182
183
184
185
186
187
188






189
190
191
192
193
194
195
...
276
277
278
279
280
281
282















    | ImportKeyword
    | ExposingKeyword
    | TypeKeyword
    | DocKeyword
    | MemberKeyword
    | ImplementationKeyword
    | ElseKeyword





type Problem
    = ExpectedInt


    | ExpectedSymbol
    | ExpectedMetadata
    | ExpectedGeneric
    | ExpectedType
    | UnknownError
    | ExpectedForwardSlash
    | ExpectedWhitespace
................................................................................
    | TypeAlreadyDefined String SourceLocationRange
    | UnknownMetadata String
    | InvalidModulePath String
    | ModuleIsEmpty
    | BadDefinition String
    | UnknownEscapeSequence String
    | StringNotTerminated







toString : String -> String -> DeadEnd Context Problem -> String
toString sourceRef source deadEnd =
    let
        contextExplination =
            contextStackExplination deadEnd

        lineOfProblem =
            deadEnd.row

        lineOfContext =
            firstContextRow deadEnd
                |> Maybe.withDefault lineOfProblem
................................................................................
    , codeBlock
    , problemDetail
    ]
        |> List.filter (not << String.isEmpty)
        |> String.join "\n\n"


contextStackExplination : DeadEnd Context Problem -> String
contextStackExplination deadEnd =
    case deadEnd.contextStack of



        contextFrame :: [] ->
            "I came across a problem while parsing the "
                ++ contextToString contextFrame.context

        contextFrame1 :: contextFrame2 :: _ ->
            "I came across a problem while parsing the "
                ++ contextToString contextFrame1.context
                ++ " of the "
                ++ contextToString contextFrame2.context

        _ ->
            "I came across a problem"



contextToString : Context -> String
contextToString context =
    case context of
        ModuleDefinition ->
            "module definition"
................................................................................

        ImplementationKeyword ->
            "implementation"

        ElseKeyword ->
            "else branch"











firstContextRow : DeadEnd Context Problem -> Maybe Int
firstContextRow deadEnd =
    case deadEnd.contextStack of
        frame :: _ ->
            case frame.context of
                FunctionDefinition loc _ ->
................................................................................

problemToString : String -> Problem -> String
problemToString source problem =
    case problem of
        ExpectedInt ->
            "Expected to find an integer"







        ExpectedSymbol ->
            "Expected to find a symbol"

        ExpectedMetadata ->
            "Expected to find a keyword"

        ExpectedGeneric ->
................................................................................
        UnknownEscapeSequence seq ->
            "'"
                ++ seq
                ++ "' is not a valid escape sequence. Expected either \\n, \\t, \\\\ or \\\"."

        StringNotTerminated ->
            "This string never terminates. Expected to find a closing \"."






















>
>
>




>
>







 







>
>
>
>
>






|







 







|
|
|
>
>
>




|
|
|
|
|
|
|
|
>







 







>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
..
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
...
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
...
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
    | ImportKeyword
    | ExposingKeyword
    | TypeKeyword
    | DocKeyword
    | MemberKeyword
    | ImplementationKeyword
    | ElseKeyword
    | IntegerLiteral
    | ArrayLiteral
    | StringLiteral


type Problem
    = ExpectedInt
    | ExpectedHexInt
    | ExpectedBitInt
    | ExpectedSymbol
    | ExpectedMetadata
    | ExpectedGeneric
    | ExpectedType
    | UnknownError
    | ExpectedForwardSlash
    | ExpectedWhitespace
................................................................................
    | TypeAlreadyDefined String SourceLocationRange
    | UnknownMetadata String
    | InvalidModulePath String
    | ModuleIsEmpty
    | BadDefinition String
    | UnknownEscapeSequence String
    | StringNotTerminated
    | IntegerBadLeadingZero
    | IntegerTrailingUnderscore
    | IntegerOutOfBounds
    | IntegerHexOutOfBounds
    | IntegerBitOutOfBounds


toString : String -> String -> DeadEnd Context Problem -> String
toString sourceRef source deadEnd =
    let
        contextExplination =
            contextStackExplination deadEnd.contextStack

        lineOfProblem =
            deadEnd.row

        lineOfContext =
            firstContextRow deadEnd
                |> Maybe.withDefault lineOfProblem
................................................................................
    , codeBlock
    , problemDetail
    ]
        |> List.filter (not << String.isEmpty)
        |> String.join "\n\n"


contextStackExplination : List { row : Int, col : Int, context : Context } -> String
contextStackExplination contextStack =
    case contextStack of
        [] ->
            "I came across a problem"

        contextFrame :: [] ->
            "I came across a problem while parsing the "
                ++ contextToString contextFrame.context

        _ ->
            let
                contextStrings =
                    contextStack
                        |> List.map (.context >> contextToString)
                        |> String.join " of the "
            in
            "I came across a problem while parsing the "
                ++ contextStrings


contextToString : Context -> String
contextToString context =
    case context of
        ModuleDefinition ->
            "module definition"
................................................................................

        ImplementationKeyword ->
            "implementation"

        ElseKeyword ->
            "else branch"

        IntegerLiteral ->
            "integer"

        ArrayLiteral ->
            "array"

        StringLiteral ->
            "string"


firstContextRow : DeadEnd Context Problem -> Maybe Int
firstContextRow deadEnd =
    case deadEnd.contextStack of
        frame :: _ ->
            case frame.context of
                FunctionDefinition loc _ ->
................................................................................

problemToString : String -> Problem -> String
problemToString source problem =
    case problem of
        ExpectedInt ->
            "Expected to find an integer"

        ExpectedHexInt ->
            "Expected to find an hex-encoded integer"

        ExpectedBitInt ->
            "Expected to find an bit-encoded integer"

        ExpectedSymbol ->
            "Expected to find a symbol"

        ExpectedMetadata ->
            "Expected to find a keyword"

        ExpectedGeneric ->
................................................................................
        UnknownEscapeSequence seq ->
            "'"
                ++ seq
                ++ "' is not a valid escape sequence. Expected either \\n, \\t, \\\\ or \\\"."

        StringNotTerminated ->
            "This string never terminates. Expected to find a closing \"."

        IntegerBadLeadingZero ->
            "Integers cannot start with 0."

        IntegerTrailingUnderscore ->
            "Integers cannot end with an underscore."

        IntegerOutOfBounds ->
            "Integers must fit within a signed 32-bit number."

        IntegerHexOutOfBounds ->
            "Hex-encoded integers can contain at most 8 characters (4 bytes, 32-bits)."

        IntegerBitOutOfBounds ->
            "Bit-encoded integers can contain at most 32 characters (32-bits)."

Modified tests/Test/Parser.elm from [0383b8f873] to [e8a011aa46].

1
2
3
4
5


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
....
1492
1493
1494
1495
1496
1497
1498








1499























































module Test.Parser exposing (..)

import Dict
import Dict.Extra as Dict
import Expect


import Stabel.Parser as AST exposing (..)
import Stabel.Parser.AssociatedFunctionSignature as AssociatedFunctionSignature
import Stabel.Parser.ModuleDefinition as ModuleDefinition
import Stabel.Parser.SourceLocation
    exposing
        ( SourceLocation
        , SourceLocationRange
        , emptyRange
        )
import Stabel.Parser.Type as AST exposing (..)
import Test exposing (Test, describe, test)
import Test.Parser.Util
    exposing
        ( addFunctionsForStructs
        , compileRetainLocations
        , expectAst
        , expectCompiles
        )
................................................................................
                        """
                        This is a test
                          with some indentation
                        Let's see how it turns out
                        """
                        "This is a test\n  with some indentation\nLet's see how it turns out"
            ]








        ]




























































>
>










|







 







>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
....
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
module Test.Parser exposing (..)

import Dict
import Dict.Extra as Dict
import Expect
import Fuzz exposing (Fuzzer)
import Random
import Stabel.Parser as AST exposing (..)
import Stabel.Parser.AssociatedFunctionSignature as AssociatedFunctionSignature
import Stabel.Parser.ModuleDefinition as ModuleDefinition
import Stabel.Parser.SourceLocation
    exposing
        ( SourceLocation
        , SourceLocationRange
        , emptyRange
        )
import Stabel.Parser.Type as AST exposing (..)
import Test exposing (Test, describe, fuzz, test)
import Test.Parser.Util
    exposing
        ( addFunctionsForStructs
        , compileRetainLocations
        , expectAst
        , expectCompiles
        )
................................................................................
                        """
                        This is a test
                          with some indentation
                        Let's see how it turns out
                        """
                        "This is a test\n  with some indentation\nLet's see how it turns out"
            ]
        , describe "Integers" <|
            let
                expectParseInt input output =
                    let
                        source =
                            """
                            def: test
                            : """ ++ input

                        expectedAst =
                            { sourceReference = ""
                            , moduleDefinition = ModuleDefinition.Undefined
                            , types = Dict.empty
                            , functions =
                                Dict.fromListBy .name
                                    [ { name = "test"
                                      , typeSignature = AssociatedFunctionSignature.NotProvided
                                      , sourceLocationRange = Nothing
                                      , documentation = ""
                                      , aliases = Dict.empty
                                      , imports = Dict.empty
                                      , implementation =
                                            SoloImpl
                                                [ AST.Integer emptyRange output
                                                ]
                                      }
                                    ]
                            }
                    in
                    expectAst source expectedAst
            in
            [ fuzz positiveIntFuzzer "Positive int" <|
                \num ->
                    expectParseInt (String.fromInt num) num
            , fuzz negativeIntFuzzer "Negative ints" <|
                \num ->
                    expectParseInt (String.fromInt (num * -1) ++ "-") num
            , test "can contain underscores as seperators" <|
                \_ ->
                    expectParseInt "10_000" 10000
            , test "can be in Hex format" <|
                \_ ->
                    expectParseInt "0xFA0" 0x0FA0
            , test "can be in Hex format (second example)" <|
                \_ ->
                    expectParseInt "0xe3cc" 0xE3CC
            , test "can be in bit format" <|
                \_ ->
                    expectParseInt "0b101" 5
            , test "can be in bit format (second example)" <|
                \_ ->
                    expectParseInt "0b111001" 57
            ]
        ]


positiveIntFuzzer : Fuzzer Int
positiveIntFuzzer =
    Fuzz.intRange 0 Random.maxInt


negativeIntFuzzer : Fuzzer Int
negativeIntFuzzer =
    Fuzz.intRange Random.minInt -1

Modified tests/Test/Parser/Errors.elm from [a51137eb07] to [9aaaba3e40].

316
317
318
319
320
321
322




































































































































323
324
325
326
327
328
329
                        source =
                            """
                           def: src
                           : \"\"\"bad string"""
                    in
                    checkForError stringNotTerminated source
            ]




































































































































        ]


checkForError : (Problem -> Bool) -> String -> Expectation
checkForError fn source =
    case compile source of
        Err errors ->







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
                        source =
                            """
                           def: src
                           : \"\"\"bad string"""
                    in
                    checkForError stringNotTerminated source
            ]
        , describe "Integers"
            [ test "something like 43x should fail as it is not a valid int" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 43x
                            """
                    in
                    checkForError ((==) ExpectedWhitespace) source
            , test "cannot start with leading 0" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 010
                            """
                    in
                    checkForError ((==) IntegerBadLeadingZero) source
            , test "numbers cannot end with underscores" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 100_
                            """
                    in
                    checkForError ((==) IntegerTrailingUnderscore) source
            , test "positive number constant must fit in signed 32-bit int" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 3_000_000_000
                            """
                    in
                    checkForError ((==) IntegerOutOfBounds) source
            , test "negative number constant must fit in signed 32-bit int" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 3_000_000_000-
                            """
                    in
                    checkForError ((==) IntegerOutOfBounds) source
            , test "Hex numbers cannot be negative" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 0xAA00FF-
                            """
                    in
                    checkForError ((==) ExpectedWhitespace) source
            , test "Hex numbers cannot use underscores" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 0xAA_00FF
                            """
                    in
                    checkForError ((==) ExpectedWhitespace) source
            , test "Hex numbers must contain at least one member" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 0x
                            """
                    in
                    checkForError ((==) ExpectedHexInt) source
            , test "Hex numbers cannot contain more than 8 characters (4 bytes, 32 bits)" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 0xAABBCCDDE
                            """
                    in
                    checkForError ((==) IntegerHexOutOfBounds) source
            , test "Bit numbers cannot be negative" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 0b0101-
                            """
                    in
                    checkForError ((==) ExpectedWhitespace) source
            , test "Bit numbers cannot use underscores" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 0b11_00
                            """
                    in
                    checkForError ((==) ExpectedWhitespace) source
            , test "Bit numbers must contain at least one member" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 0b
                            """
                    in
                    checkForError ((==) ExpectedBitInt) source
            , test "Bit numbers cannot contain more than 32 characters (32 bits)" <|
                \_ ->
                    let
                        source =
                            """
                            def: src
                            : 0b010101010101010101010101010101011
                            """
                    in
                    checkForError ((==) IntegerBitOutOfBounds) source
            ]
        ]


checkForError : (Problem -> Bool) -> String -> Expectation
checkForError fn source =
    case compile source of
        Err errors ->