Stabel

Check-in [db0d5f7a64]
Login
Overview
Comment:Check that integers are within range of 32-bit signed.
Timelines: family | ancestors | descendants | both | integer-syntax-improvements
Files: files | file ages | folders
SHA3-256: db0d5f7a645871dd8460a370d2800c2e3f8c6b25b947ad47e947bf78f55d5ef0
User & Date: robin.hansen on 2021-09-16 13:05:17
Other Links: branch diff | manifest | tags
Context
2021-09-16
14:02
Hex-encoded integer literals. check-in: 0e154d19e7 user: robin.hansen tags: integer-syntax-improvements
13:05
Check that integers are within range of 32-bit signed. check-in: db0d5f7a64 user: robin.hansen tags: integer-syntax-improvements
2021-09-13
19:20
Allow underscores to group large integers. check-in: df946f0acc user: robin.hansen tags: integer-syntax-improvements
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Parser.elm from [55df2cef68] to [f0e2701f0c].

9
10
11
12
13
14
15

16
17
18
19
20
21
22
...
175
176
177
178
179
180
181
182

183
184
185
186
187






188
189
190
191
192
193
194
    , 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 (Context, Problem(..))
import Stabel.Parser.SourceLocation exposing (SourceLocation, SourceLocationRange)
import Stabel.Parser.Type
    exposing
................................................................................

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

    else
        case String.toInt digits of
            Just num ->
                Parser.succeed <|

                    if isNegative then
                        num * -1

                    else
                        num







            Nothing ->
                Parser.problem ExpectedInt


sourceLocationParser : Parser SourceLocation
sourceLocationParser =







>







 







|
>
|
|

|
|
>
>
>
>
>
>







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
...
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
    , TypeMatchValue(..)
    , run
    )

import Dict exposing (Dict)
import Dict.Extra as Dict
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
................................................................................

    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 =

Modified src/Stabel/Parser/Problem.elm from [5e7629c919] to [fffc0c5e37].

56
57
58
59
60
61
62

63
64
65
66
67
68
69
...
300
301
302
303
304
305
306



    | InvalidModulePath String
    | ModuleIsEmpty
    | BadDefinition String
    | UnknownEscapeSequence String
    | StringNotTerminated
    | IntegerBadLeadingZero
    | IntegerTrailingUnderscore



toString : String -> String -> DeadEnd Context Problem -> String
toString sourceRef source deadEnd =
    let
        contextExplination =
            contextStackExplination deadEnd.contextStack
................................................................................
            "This string never terminates. Expected to find a closing \"."

        IntegerBadLeadingZero ->
            "Integers cannot start with 0."

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










>







 







>
>
>
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
...
301
302
303
304
305
306
307
308
309
310
    | InvalidModulePath String
    | ModuleIsEmpty
    | BadDefinition String
    | UnknownEscapeSequence String
    | StringNotTerminated
    | IntegerBadLeadingZero
    | IntegerTrailingUnderscore
    | IntegerOutOfBounds


toString : String -> String -> DeadEnd Context Problem -> String
toString sourceRef source deadEnd =
    let
        contextExplination =
            contextStackExplination deadEnd.contextStack
................................................................................
            "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."

Modified tests/Test/Parser/Errors.elm from [71430dff1d] to [528443d252].

347
348
349
350
351
352
353
354









355









356
357
358
359
360
361
362
                        source =
                            """
                            def: src
                            : 100_
                            """
                    in
                    checkForError ((==) IntegerTrailingUnderscore) source
            , Test.todo "positive number constant must fit in signed 32-bit int"









            , Test.todo "negative number constant must fit in signed 32-bit int"









            ]
        ]


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







|
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>







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
                        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
            ]
        ]


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