Stabel

Check-in [99cbabf9ff]
Login
Overview
Comment:Fail integer parsing when it contains an non-digit character.
Timelines: family | ancestors | descendants | both | integer-syntax-improvements
Files: files | file ages | folders
SHA3-256: 99cbabf9ff59c0d348bb834301e1cea4f867e805f24c672c568edc5e0909b0f0
User & Date: robin.hansen on 2021-09-13 18:27:49
Other Links: branch diff | manifest | tags
Context
2021-09-13
18:37
Add more contexts. check-in: b9aa49ac53 user: robin.hansen tags: integer-syntax-improvements
18:27
Fail integer parsing when it contains an non-digit character. check-in: 99cbabf9ff user: robin.hansen tags: integer-syntax-improvements
2021-09-11
10:21
Parser now understands negative ints. Added test todos to guide future work. check-in: 33fb3fd0a1 user: robin.hansen tags: integer-syntax-improvements
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Modified src/Stabel/Parser.elm from [ed07e3440f] to [d93f30a5a3].

164
165
166
167
168
169
170





171
172
173
174
175
176
177
            , expecting = ExpectedInt
            }
        |= Parser.oneOf
            [ Parser.succeed True
                |. Parser.symbol (Token "-" UnknownError)
            , Parser.succeed False
            ]





        |> Parser.andThen helper


sourceLocationParser : Parser SourceLocation
sourceLocationParser =
    Parser.succeed SourceLocation
        |= Parser.getRow







>
>
>
>
>







164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
            , expecting = ExpectedInt
            }
        |= Parser.oneOf
            [ Parser.succeed True
                |. Parser.symbol (Token "-" UnknownError)
            , Parser.succeed False
            ]
        |. Parser.oneOf
            [ Parser.chompIf (\c -> Set.member c whitespaceChars) ExpectedWhitespace
            , Parser.succeed ()
                |. Parser.end ExpectedEndOfFile
            ]
        |> Parser.andThen helper


sourceLocationParser : Parser SourceLocation
sourceLocationParser =
    Parser.succeed SourceLocation
        |= Parser.getRow

Modified tests/Test/Parser.elm from [c7d0ff484a] to [a306bec1ba].

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
            in
            [ fuzz positiveIntFuzzer "Positive int" <|
                \num ->
                    expectParseInt (String.fromInt num) num
            , fuzz negativeIntFuzzer "Negative ints" <|
                \num ->
                    expectParseInt (String.fromInt (num * -1) ++ "-") num
            , Test.todo "something like 43x should fail as it is not a valid int"
            , Test.todo "positive number constant must fit in signed 32-bit int"
            , Test.todo "negative number constant must fit in signed 32-bit int"
            , Test.todo "cannot start with leading 0"
            , Test.todo "can contain underscores as seperators"
            , Test.todo "numbers cannot begin with underscores"
            , Test.todo "numbers cannot end with underscores"
            ]
        ]


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


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







<
<
<
<

<
<












1531
1532
1533
1534
1535
1536
1537




1538


1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
            in
            [ fuzz positiveIntFuzzer "Positive int" <|
                \num ->
                    expectParseInt (String.fromInt num) num
            , fuzz negativeIntFuzzer "Negative ints" <|
                \num ->
                    expectParseInt (String.fromInt (num * -1) ++ "-") num




            , Test.todo "can contain underscores as seperators"


            ]
        ]


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 [64b2513855].

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
                        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.todo "cannot start with leading 0"
            , Test.todo "numbers cannot begin with underscores"
            , Test.todo "numbers cannot end with underscores"
            , 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
        Err errors ->