Stabel

Check-in [33fb3fd0a1]
Login
Overview
Comment:Parser now understands negative ints. Added test todos to guide future work.
Timelines: family | ancestors | descendants | both | integer-syntax-improvements
Files: files | file ages | folders
SHA3-256: 33fb3fd0a1f28d4588582dad7dadde4373404f4475adb1b0b3175d54b6c51828
User & Date: robin.hansen on 2021-09-11 10:21:07
Other Links: branch diff | manifest | tags
Context
2021-09-13
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
09:42
elm-review fixes. check-in: 7c41ddbc55 user: robin.hansen tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

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

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


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







|


|
>
>
>
>
>




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







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


{-| The builtin int parser has a bug where it commits when it comes across an 'e'
-}
intParser : Parser Int
intParser =
    let
        helper ( text, isNegative ) =
            case String.toInt text of
                Just num ->
                    Parser.succeed <|
                        if isNegative then
                            num * -1

                        else
                            num

                Nothing ->
                    Parser.problem ExpectedInt
    in
    Parser.succeed Tuple.pair
        |= Parser.variable
            { start = Char.isDigit
            , inner = Char.isDigit
            , reserved = Set.empty
            , 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

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

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