Skip to content

Commit

Permalink
refactor parser error handling to use new result types
Browse files Browse the repository at this point in the history
  • Loading branch information
decioferreira committed Feb 11, 2025
1 parent b815a58 commit 2ccf4bf
Show file tree
Hide file tree
Showing 19 changed files with 297 additions and 212 deletions.
4 changes: 2 additions & 2 deletions src/Builder/Elm/Outline.elm
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,10 @@ boundParser bound tooLong =
col + len
in
if len < bound then
Ok (P.POk P.Consumed (String.slice pos end src) (P.State src end end indent row newCol))
P.Cok (String.slice pos end src) (P.State src end end indent row newCol)

else
Err (P.PErr P.Consumed row newCol (\_ _ -> tooLong))
P.Cerr row newCol (\_ _ -> tooLong)


srcDirEncoder : SrcDir -> Encode.Value
Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/Elm/Constraint.elm
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ parser =
P.Parser <|
\((P.State _ _ _ _ row col) as state) ->
if V.compare lower higher == LT then
Ok (P.POk P.Empty (Range lower loOp hiOp higher) state)
P.Eok (Range lower loOp hiOp higher) state

else
Err (P.PErr P.Empty row col (\_ _ -> InvalidRange lower higher))
P.Eerr row col (\_ _ -> InvalidRange lower higher)
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion src/Compiler/Elm/Docs.elm
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ chompUntilDocs =
newState =
P.State src newPos end indent newRow newCol
in
Ok (P.POk P.Consumed isDocs newState)
P.Cok isDocs newState
)


Expand Down
4 changes: 2 additions & 2 deletions src/Compiler/Elm/Kernel.elm
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ parseChunks vtable enums fields =
chompChunks vtable enums fields src pos end row col pos []
in
if newPos == end then
Ok (P.POk P.Consumed chunks (P.State src newPos end indent newRow newCol))
P.Cok chunks (P.State src newPos end indent newRow newCol)

else
Err (P.PErr P.Consumed row col toError)
P.Cerr row col toError
)


Expand Down
6 changes: 3 additions & 3 deletions src/Compiler/Elm/ModuleName.elm
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ parser =
newState =
P.State src newPos end indent row newCol
in
Ok (P.POk P.Consumed (String.slice pos newPos src) newState)
P.Cok (String.slice pos newPos src) newState

else if col == newCol then
Err (P.PErr P.Empty row newCol Tuple.pair)
P.Eerr row newCol Tuple.pair

else
Err (P.PErr P.Consumed row newCol Tuple.pair)
P.Cerr row newCol Tuple.pair
)


Expand Down
8 changes: 4 additions & 4 deletions src/Compiler/Elm/Package.elm
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ parseName isGoodStart isGoodInner =
P.Parser <|
\(P.State src pos end indent row col) ->
if pos >= end then
Err (P.PErr P.Empty row col Tuple.pair)
P.Eerr row col Tuple.pair

else
let
Expand All @@ -298,7 +298,7 @@ parseName isGoodStart isGoodInner =
P.unsafeIndex src pos
in
if not (isGoodStart word) then
Err (P.PErr P.Empty row col Tuple.pair)
P.Eerr row col Tuple.pair

else
let
Expand All @@ -319,10 +319,10 @@ parseName isGoodStart isGoodInner =
newState =
P.State src newPos end indent row newCol
in
Ok (P.POk P.Consumed (String.slice pos newPos src) newState)
P.Cok (String.slice pos newPos src) newState

else
Err (P.PErr P.Consumed row newCol Tuple.pair)
P.Cerr row newCol Tuple.pair


isLower : Char -> Bool
Expand Down
8 changes: 4 additions & 4 deletions src/Compiler/Elm/Version.elm
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ numberParser =
P.Parser <|
\(P.State src pos end indent row col) ->
if pos >= end then
Err (P.PErr P.Empty row col Tuple.pair)
P.Eerr row col Tuple.pair

else
let
Expand All @@ -195,7 +195,7 @@ numberParser =
newState =
P.State src (pos + 1) end indent row (col + 1)
in
Ok (P.POk P.Consumed 0 newState)
P.Cok 0 newState

else if isDigit word then
let
Expand All @@ -206,10 +206,10 @@ numberParser =
newState =
P.State src newPos end indent row (col + (newPos - pos))
in
Ok (P.POk P.Consumed total newState)
P.Cok total newState

else
Err (P.PErr P.Empty row col Tuple.pair)
P.Eerr row col Tuple.pair


chompWord16 : String -> Int -> Int -> Int -> ( Int, Int )
Expand Down
26 changes: 13 additions & 13 deletions src/Compiler/Json/Decode.elm
Original file line number Diff line number Diff line change
Expand Up @@ -709,13 +709,13 @@ pString start =
newState =
P.State src newPos end indent newRow newCol
in
Ok (P.POk P.Consumed snp newState)
P.Cok snp newState

BadString problem ->
Err (P.PErr P.Consumed newRow newCol (StringProblem problem))
P.Cerr newRow newCol (StringProblem problem)

else
Err (P.PErr P.Empty row col start)
P.Eerr row col start


type StringStatus
Expand Down Expand Up @@ -838,15 +838,15 @@ spaces =
eatSpaces src pos end row col
in
if pos == newPos then
Ok (P.POk P.Empty () state)
P.Eok () state

else
let
newState : P.State
newState =
P.State src newPos end indent newRow newCol
in
Ok (P.POk P.Consumed () newState)
P.Cok () newState


eatSpaces : String -> Int -> Int -> Row -> Col -> ( Int, Row, Col )
Expand Down Expand Up @@ -882,7 +882,7 @@ pInt =
P.Parser <|
\(P.State src pos end indent row col) ->
if pos >= end then
Err (P.PErr P.Empty row col Start)
P.Eerr row col Start

else
let
Expand All @@ -891,7 +891,7 @@ pInt =
P.unsafeIndex src pos
in
if not (isDecimalDigit word) then
Err (P.PErr P.Empty row col Start)
P.Eerr row col Start

else if word == '0' then
let
Expand All @@ -910,16 +910,16 @@ pInt =
P.unsafeIndex src pos1
in
if isDecimalDigit word1 then
Err (P.PErr P.Consumed row (col + 1) NoLeadingZeros)
P.Cerr row (col + 1) NoLeadingZeros

else if word1 == '.' then
Err (P.PErr P.Consumed row (col + 1) NoFloats)
P.Cerr row (col + 1) NoFloats

else
Ok (P.POk P.Consumed (Int 0) newState)
P.Cok (Int 0) newState

else
Ok (P.POk P.Consumed (Int 0) newState)
P.Cok (Int 0) newState

else
let
Expand All @@ -937,10 +937,10 @@ pInt =
newState =
P.State src newPos end indent row (col + len)
in
Ok (P.POk P.Consumed (Int n) newState)
P.Cok (Int n) newState

BadIntEnd ->
Err (P.PErr P.Consumed row (col + len) NoFloats)
P.Cerr row (col + len) NoFloats


type IntStatus
Expand Down
23 changes: 17 additions & 6 deletions src/Compiler/Parse/Declaration.elm
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,26 @@ chompMatchingName expectedName =
in
P.Parser <|
\((P.State _ _ _ _ sr sc) as state) ->
Result.andThen
(\(P.POk status name ((P.State _ _ _ _ er ec) as newState)) ->
case parserL state of
P.Cok name ((P.State _ _ _ _ er ec) as newState) ->
if expectedName == name then
Ok (P.POk status (A.At (A.Region (A.Position sr sc) (A.Position er ec)) name) newState)
P.Cok (A.At (A.Region (A.Position sr sc) (A.Position er ec)) name) newState

else
Err (P.PErr status sr sc (E.DeclDefNameMatch name))
)
(parserL state)
P.Cerr sr sc (E.DeclDefNameMatch name)

P.Eok name ((P.State _ _ _ _ er ec) as newState) ->
if expectedName == name then
P.Eok (A.At (A.Region (A.Position sr sc) (A.Position er ec)) name) newState

else
P.Eerr sr sc (E.DeclDefNameMatch name)

P.Cerr r c t ->
P.Cerr r c t

P.Eerr r c t ->
P.Eerr r c t



Expand Down
23 changes: 17 additions & 6 deletions src/Compiler/Parse/Expression.elm
Original file line number Diff line number Diff line change
Expand Up @@ -814,15 +814,26 @@ chompMatchingName expectedName =
in
P.Parser <|
\((P.State _ _ _ _ sr sc) as state) ->
Result.andThen
(\(P.POk status name ((P.State _ _ _ _ er ec) as newState)) ->
case parserL state of
P.Cok name ((P.State _ _ _ _ er ec) as newState) ->
if expectedName == name then
Ok (P.POk status (A.At (A.Region (A.Position sr sc) (A.Position er ec)) name) newState)
P.Cok (A.At (A.Region (A.Position sr sc) (A.Position er ec)) name) newState

else
Err (P.PErr status sr sc (E.DefNameMatch name))
)
(parserL state)
P.Cerr sr sc (E.DefNameMatch name)

P.Eok name ((P.State _ _ _ _ er ec) as newState) ->
if expectedName == name then
P.Eok (A.At (A.Region (A.Position sr sc) (A.Position er ec)) name) newState

else
P.Eerr sr sc (E.DefNameMatch name)

P.Cerr r c t ->
P.Cerr r c t

P.Eerr r c t ->
P.Eerr r c t



Expand Down
32 changes: 16 additions & 16 deletions src/Compiler/Parse/Keyword.elm
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ subscription_ toError =
s =
P.State src pos12 end indent row (col + 12)
in
Ok (P.POk P.Consumed () s)
P.Cok () s

else
Err (P.PErr P.Empty row col toError)
P.Eerr row col toError



Expand All @@ -222,10 +222,10 @@ k2 w1 w2 toError =
s =
P.State src pos2 end indent row (col + 2)
in
Ok (P.POk P.Consumed () s)
P.Cok () s

else
Err (P.PErr P.Empty row col toError)
P.Eerr row col toError


k3 : Char -> Char -> Char -> (Row -> Col -> x) -> Parser x ()
Expand All @@ -249,10 +249,10 @@ k3 w1 w2 w3 toError =
s =
P.State src pos3 end indent row (col + 3)
in
Ok (P.POk P.Consumed () s)
P.Cok () s

else
Err (P.PErr P.Empty row col toError)
P.Eerr row col toError


k4 : Char -> Char -> Char -> Char -> (Row -> Col -> x) -> Parser x ()
Expand All @@ -277,10 +277,10 @@ k4 w1 w2 w3 w4 toError =
s =
P.State src pos4 end indent row (col + 4)
in
Ok (P.POk P.Consumed () s)
P.Cok () s

else
Err (P.PErr P.Empty row col toError)
P.Eerr row col toError


k5 : Char -> Char -> Char -> Char -> Char -> (Row -> Col -> x) -> Parser x ()
Expand All @@ -306,10 +306,10 @@ k5 w1 w2 w3 w4 w5 toError =
s =
P.State src pos5 end indent row (col + 5)
in
Ok (P.POk P.Consumed () s)
P.Cok () s

else
Err (P.PErr P.Empty row col toError)
P.Eerr row col toError


k6 : Char -> Char -> Char -> Char -> Char -> Char -> (Row -> Col -> x) -> Parser x ()
Expand All @@ -336,10 +336,10 @@ k6 w1 w2 w3 w4 w5 w6 toError =
s =
P.State src pos6 end indent row (col + 6)
in
Ok (P.POk P.Consumed () s)
P.Cok () s

else
Err (P.PErr P.Empty row col toError)
P.Eerr row col toError


k7 : Char -> Char -> Char -> Char -> Char -> Char -> Char -> (Row -> Col -> x) -> Parser x ()
Expand Down Expand Up @@ -367,10 +367,10 @@ k7 w1 w2 w3 w4 w5 w6 w7 toError =
s =
P.State src pos7 end indent row (col + 7)
in
Ok (P.POk P.Consumed () s)
P.Cok () s

else
Err (P.PErr P.Empty row col toError)
P.Eerr row col toError


k8 : Char -> Char -> Char -> Char -> Char -> Char -> Char -> Char -> (Row -> Col -> x) -> Parser x ()
Expand Down Expand Up @@ -399,7 +399,7 @@ k8 w1 w2 w3 w4 w5 w6 w7 w8 toError =
s =
P.State src pos8 end indent row (col + 8)
in
Ok (P.POk P.Consumed () s)
P.Cok () s

else
Err (P.PErr P.Empty row col toError)
P.Eerr row col toError
Loading

0 comments on commit 2ccf4bf

Please sign in to comment.