Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Node around QualifiedNameRef #165

Open
wants to merge 4 commits into
base: v8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/Elm/Parser/Declarations.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Elm.Parser.Declarations exposing (caseBlock, caseStatement, caseStatements, declaration, expression, function, functionArgument, functionSignature, letBlock, letBody, letExpression, signature)

import Combine exposing (Parser, choice, lazy, many, maybe, modifyState, or, sepBy1, string, succeed, withLocation)
import Elm.Parser.DestructurePatterns as DestructurPatterns exposing (destructurPattern)
import Elm.Parser.DestructurePatterns as DestructurPatterns exposing (destructurePattern)
import Elm.Parser.Infix as Infix
import Elm.Parser.Layout as Layout
import Elm.Parser.Node as Node
Expand Down Expand Up @@ -149,7 +149,7 @@ portDeclaration =

functionArgument : Parser State (Node DestructurePattern)
functionArgument =
DestructurPatterns.destructurPattern
DestructurPatterns.destructurePattern



Expand Down Expand Up @@ -502,7 +502,7 @@ letBody =
let
blockElement : Parser State LetDeclaration
blockElement =
destructurPattern
destructurePattern
|> Combine.andThen
(\(Node r p) ->
case p of
Expand Down
10 changes: 5 additions & 5 deletions src/Elm/Parser/DestructurePatterns.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Elm.Parser.DestructurePatterns exposing (destructurPattern)
module Elm.Parser.DestructurePatterns exposing (destructurePattern)

import Combine exposing (Parser, between, lazy, many, maybe, parens, sepBy, string)
import Elm.Parser.Base as Base
Expand Down Expand Up @@ -27,8 +27,8 @@ tryToCompose x =
)


destructurPattern : Parser State (Node DestructurePattern)
destructurPattern =
destructurePattern : Parser State (Node DestructurePattern)
destructurePattern =
composablePattern |> Combine.andThen tryToCompose


Expand All @@ -37,7 +37,7 @@ parensPattern =
Combine.lazy
(\() ->
Node.parser
(parens (sepBy (string ",") (Layout.maybeAroundBothSides destructurPattern))
(parens (sepBy (string ",") (Layout.maybeAroundBothSides destructurePattern))
|> Combine.map
(\c ->
case c of
Expand Down Expand Up @@ -100,7 +100,7 @@ qualifiedPattern consumeArgs =
(\args ->
Node
(Range.combine (range :: List.map (\(Node r _) -> r) args))
(NamedPattern_ (QualifiedNameRef mod name) args)
(NamedPattern_ (Node range (QualifiedNameRef mod name)) args)
)
)

Expand Down
2 changes: 1 addition & 1 deletion src/Elm/Parser/Patterns.elm
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ qualifiedPattern consumeArgs =
(\args ->
Node
(Range.combine (range :: List.map (\(Node r _) -> r) args))
(NamedPattern (QualifiedNameRef mod name) args)
(NamedPattern (Node range (QualifiedNameRef mod name)) args)
)
)

Expand Down
68 changes: 16 additions & 52 deletions src/Elm/Syntax/DestructurePattern.elm
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
module Elm.Syntax.DestructurePattern exposing
( moduleNames
( DestructurePattern(..)
, encode, decoder
, DestructurePattern(..)
)

{-|


# Destructur pattern Syntax

This syntax represents patterns used for destructuring data.
{-| This syntax represents patterns used for destructuring data.
For example:

Just x as someMaybe
Expand All @@ -18,12 +12,7 @@ For example:

# Types

@docs DestructurPattern


## Functions

@docs moduleNames
@docs DestructurePattern


## Serialization
Expand All @@ -33,7 +22,7 @@ For example:
-}

import Elm.Json.Util exposing (decodeTyped, encodeTyped)
import Elm.Syntax.ModuleName as ModuleName exposing (ModuleName)
import Elm.Syntax.ModuleName as ModuleName
import Elm.Syntax.Node as Node exposing (Node)
import Elm.Syntax.Pattern exposing (QualifiedNameRef)
import Json.Decode as JD exposing (Decoder)
Expand All @@ -58,45 +47,16 @@ type DestructurePattern
| TuplePattern_ (List (Node DestructurePattern))
| RecordPattern_ (List (Node String))
| VarPattern_ String
| NamedPattern_ QualifiedNameRef (List (Node DestructurePattern))
| NamedPattern_ (Node QualifiedNameRef) (List (Node DestructurePattern))
| AsPattern_ (Node DestructurePattern) (Node String)
| ParenthesizedPattern_ (Node DestructurePattern)


{-| Get all the modules names that are used in the pattern (and its nested patterns).
Use this to collect qualified patterns, such as `Maybe.Just x`.
-}
moduleNames : DestructurePattern -> List ModuleName
moduleNames p =
let
recur =
Node.value >> moduleNames
in
case p of
TuplePattern_ xs ->
List.concatMap recur xs

RecordPattern_ _ ->
[]

NamedPattern_ qualifiedNameRef subPatterns ->
qualifiedNameRef.moduleName :: List.concatMap recur subPatterns

AsPattern_ inner _ ->
recur inner

ParenthesizedPattern_ inner ->
recur inner

_ ->
[]



-- Serialization


{-| Encode a `DestructurPattern` syntax element to JSON.
{-| Encode a `DestructurePattern` syntax element to JSON.
-}
encode : DestructurePattern -> Value
encode pattern =
Expand Down Expand Up @@ -132,10 +92,14 @@ encode pattern =
encodeTyped "named" <|
JE.object
[ ( "qualified"
, JE.object
[ ( "moduleName", ModuleName.encode qualifiedNameRef.moduleName )
, ( "name", JE.string qualifiedNameRef.name )
]
, Node.encode
(\{ moduleName, name } ->
JE.object
[ ( "moduleName", ModuleName.encode moduleName )
, ( "name", JE.string name )
]
)
qualifiedNameRef
)
, ( "patterns", JE.list (Node.encode encode) patterns )
]
Expand All @@ -155,7 +119,7 @@ encode pattern =
)


{-| JSON decoder for a `DestructurPattern` syntax element.
{-| JSON decoder for a `DestructurePattern` syntax element.
-}
decoder : Decoder DestructurePattern
decoder =
Expand All @@ -167,7 +131,7 @@ decoder =
, ( "tuple", JD.field "value" (JD.list (Node.decoder decoder)) |> JD.map TuplePattern_ )
, ( "record", JD.field "value" (JD.list (Node.decoder JD.string)) |> JD.map RecordPattern_ )
, ( "var", JD.field "value" JD.string |> JD.map VarPattern_ )
, ( "named", JD.map2 NamedPattern_ (JD.field "qualified" decodeQualifiedNameRef) (JD.field "patterns" (JD.list (Node.decoder decoder))) )
, ( "named", JD.map2 NamedPattern_ (JD.field "qualified" (Node.decoder decodeQualifiedNameRef)) (JD.field "patterns" (JD.list (Node.decoder decoder))) )
, ( "as", JD.map2 AsPattern_ (JD.field "pattern" (Node.decoder decoder)) (JD.field "name" (Node.decoder JD.string)) )
, ( "parentisized", JD.map ParenthesizedPattern_ (JD.field "value" (Node.decoder decoder)) )
]
Expand Down
59 changes: 11 additions & 48 deletions src/Elm/Syntax/Pattern.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Elm.Syntax.Pattern exposing
( Pattern(..), QualifiedNameRef
, moduleNames
, encode, decoder
)

Expand All @@ -21,19 +20,14 @@ For example:
@docs Pattern, QualifiedNameRef


## Functions

@docs moduleNames


## Serialization

@docs encode, decoder

-}

import Elm.Json.Util exposing (decodeTyped, encodeTyped)
import Elm.Syntax.ModuleName as ModuleName exposing (ModuleName)
import Elm.Syntax.ModuleName as ModuleName
import Elm.Syntax.Node as Node exposing (Node)
import Json.Decode as JD exposing (Decoder)
import Json.Encode as JE exposing (Value)
Expand Down Expand Up @@ -70,7 +64,7 @@ type Pattern
| UnConsPattern (Node Pattern) (Node Pattern)
| ListPattern (List (Node Pattern))
| VarPattern String
| NamedPattern QualifiedNameRef (List (Node Pattern))
| NamedPattern (Node QualifiedNameRef) (List (Node Pattern))
| AsPattern (Node Pattern) (Node String)
| ParenthesizedPattern (Node Pattern)

Expand All @@ -83,41 +77,6 @@ type alias QualifiedNameRef =
}


{-| Get all the modules names that are used in the pattern (and its nested patterns).
Use this to collect qualified patterns, such as `Maybe.Just x`.
-}
moduleNames : Pattern -> List ModuleName
moduleNames p =
let
recur =
Node.value >> moduleNames
in
case p of
TuplePattern xs ->
List.concatMap recur xs

RecordPattern _ ->
[]

UnConsPattern left right ->
recur left ++ recur right

ListPattern xs ->
List.concatMap recur xs

NamedPattern qualifiedNameRef subPatterns ->
qualifiedNameRef.moduleName :: List.concatMap recur subPatterns

AsPattern inner _ ->
recur inner

ParenthesizedPattern inner ->
recur inner

_ ->
[]



-- Serialization

Expand Down Expand Up @@ -201,10 +160,14 @@ encode pattern =
encodeTyped "named" <|
JE.object
[ ( "qualified"
, JE.object
[ ( "moduleName", ModuleName.encode qualifiedNameRef.moduleName )
, ( "name", JE.string qualifiedNameRef.name )
]
, Node.encode
(\{ moduleName, name } ->
JE.object
[ ( "moduleName", ModuleName.encode moduleName )
, ( "name", JE.string name )
]
)
qualifiedNameRef
)
, ( "patterns", JE.list (Node.encode encode) patterns )
]
Expand Down Expand Up @@ -242,7 +205,7 @@ decoder =
, ( "uncons", JD.map2 UnConsPattern (JD.field "left" (Node.decoder decoder)) (JD.field "right" (Node.decoder decoder)) )
, ( "list", JD.field "value" (JD.list (Node.decoder decoder)) |> JD.map ListPattern )
, ( "var", JD.field "value" JD.string |> JD.map VarPattern )
, ( "named", JD.map2 NamedPattern (JD.field "qualified" decodeQualifiedNameRef) (JD.field "patterns" (JD.list (Node.decoder decoder))) )
, ( "named", JD.map2 NamedPattern (JD.field "qualified" (Node.decoder decodeQualifiedNameRef)) (JD.field "patterns" (JD.list (Node.decoder decoder))) )
, ( "as", JD.map2 AsPattern (JD.field "pattern" (Node.decoder decoder)) (JD.field "name" (Node.decoder JD.string)) )
, ( "parentisized", JD.map ParenthesizedPattern (JD.field "value" (Node.decoder decoder)) )
]
Expand Down
4 changes: 2 additions & 2 deletions src/Elm/Writer.elm
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ writePattern (Node _ p) =
VarPattern var ->
string var

NamedPattern qnr others ->
NamedPattern (Node _ qnr) others ->
spaced
[ writeQualifiedNameRef qnr
, spaced (List.map writePattern others)
Expand Down Expand Up @@ -637,7 +637,7 @@ writeDestructurPattern (Node _ p) =
VarPattern_ var ->
string var

NamedPattern_ qnr others ->
NamedPattern_ (Node _ qnr) others ->
spaced
[ writeQualifiedNameRef qnr
, spaced (List.map writeDestructurPattern others)
Expand Down
Loading