diff --git a/CHANGELOG.md b/CHANGELOG.md index bebe9535d..bfde3f3ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,75 @@ ## [Unreleased] +- Removed the deprecated `Elm.Parser.parse` function, and renamed `Elm.Parser.parseToFile` to `Elm.Parser.parse` (in other words, `Elm.Parser.parse`'s type changed to be the same as `parseToFile`) + +- Removed APIs: + - `Elm.Writer` module + - `Elm.Interface` module + - `Elm.Dependency` module + - `Elm.Processing` and `Elm.RawFile` modules + - All `encode` and `decode` functions + +- Changed `Elm.Syntax.Declaration.Declaration`: + - Removed `Destructuring` variant (it was not possible to get) + - `PortDeclaration` documentation's is now attached to the declaration + - `PortDeclaration Signature` -> `PortDeclaration Port` where `type alias Port = { documentation : Maybe (Node Documentation), signature : Signature }` + - The comment for the port declaration is now removed from the `Elm.Syntax.File.File.comments` field. + +- Add a new module `Elm.Syntax.DeconstructPattern` which mimics `Elm.Syntax.Pattern` but for patterns in functions and let declarations. + - This removes the need to handle impossible patterns. For instance, you can't find a string pattern in `someFn (X "impossible") = x` in a function declaration. + +- Changed `Elm.Syntax.Expression.Expression`: + - **WARNING (will not result in compilation error)** Stripped the `.` in the `String` stored in `RecordAccessFunction` (for code like `.field`) + - Example: `RecordAccessFunction ".field"` -> `RecordAccessFunction "field"` + - Renamed `Literal` to `StringLiteral` + - Added information to `String` literal as to whether `"""` or `"` was used: + - `Literal String` -> `StringLiteral StringLiteralType String` + - Added `type StringLiteralType = TripleQuote | SingleQuote` + - Renamed `Application` to `FunctionCall` + - `Application (List (Node Expression))` -> `FunctionCall (Node Expression) (Node Expression) (List (Node Expression))` (function is separated, and takes a non-empty list of arguments) + - Renamed `RecordUpdateExpression` to `RecordUpdate` + - `RecordUpdateExpression (Node String) (List (Node RecordSetter))` -> `RecordUpdate (Node String) (Node RecordSetter) (List (Node RecordSetter))` (takes a non-empty list of fields) + - Renamed `TupledExpression` to `TupleExpression` + - Renamed `IfBlock` to `If` + - Renamed `Floatable` to `FloatLiteral` + - Renamed `Integer` to `IntegerLiteral` + - Renamed `Hex` to `HexLiteral` + - Renamed `ListExpr` to `ListLiteral` + - `OperatorApplication` + - Renamed `OperatorApplication` to `Operation` + - Removed the `InfixDirection` field + - `OperatorApplication String InfixDirection (Node Expression) (Node Expression)` -> `Operation String (Node Expression) (Node Expression)` + - Renamed `RecordExpr` to `Record` + - Renamed `CaseExpression` to `Case` + - Renamed `LetExpression` to `Let` + - Renamed `GLSLExpression` to `GLSL` + - `UnitExpr` -> `TupleExpression []` + - `ParenthesizedExpression x` -> `TupleExpression [ x ]` + - Removed `Elm.Syntax.Expression.Cases` type alias (it was `type alias Cases = List Case`) + - `Elm.Syntax.Expression.CaseBlock`'s `cases : List Case` field is split into `firstCase : Case` and `restOfCases : List Case` (takes a non-empty list of cases) + - Removed `Operator` (it was not possible to get) + - Renamed predicate functions `isLambda`, `isLet`, `isIfElse`, `isCase`, `isOperatorApplication` + +- Changed `Elm.Syntax.Pattern.Pattern`: + - Removed `FloatPattern` (it was not possible to get) + - Added information to `StringPattern` as to whether `"""` or `"` was used: + - `StringPattern String` -> `StringPattern StringLiteralType String` + +- Changed `Elm.Syntax.TypeAnnotation.TypeAnnotation`: + - `Tupled` -> `Tuple` + - `Unit` -> `Tuple []` + - Renamed `Typed` to `Type` + - `Elm.Syntax.TypeAnnotation.Type`'s `constructors : List (Node ValueConstructor)` field is split into `firstConstructor : Node ValueConstructor` and `restOfConstructors : List (Node ValueConstructor)` (takes a non-empty list of constructors) + - Renamed `GenericType` to `Var` + +- Changed `Elm.Syntax.Exposing.Exposing`: + - `Explicit (List (Node TopLevelExpose))` -> `Explicit (Node TopLevelExpose) (List (Node TopLevelExpose))` (takes a non-empty list of elements) + +- Added module `Elm.Syntax.StringLiteralType` containing `type StringLiteralType = TripleQuote | SingleQuote` + +- Removed deprecated `Elm.Syntax.Range.emptyRange`, use `Elm.Syntax.Range.empty` instead. + ## [7.3.4] - 2024-07-26 Parsing is faster by ~90%. A big thank you to [@lue-bird](https://github.com/lue-bird) for finding and introducing a huge amount of performance improvements. diff --git a/elm.json b/elm.json index 0b83ce37f..3a96a7ff0 100644 --- a/elm.json +++ b/elm.json @@ -5,14 +5,10 @@ "license": "MIT", "version": "7.3.4", "exposed-modules": [ - "Elm.Dependency", - "Elm.Interface", "Elm.Parser", - "Elm.Processing", - "Elm.RawFile", - "Elm.Writer", "Elm.Syntax.Comments", "Elm.Syntax.Declaration", + "Elm.Syntax.DestructurePattern", "Elm.Syntax.Documentation", "Elm.Syntax.Exposing", "Elm.Syntax.Expression", @@ -23,8 +19,11 @@ "Elm.Syntax.ModuleName", "Elm.Syntax.Node", "Elm.Syntax.Pattern", + "Elm.Syntax.Port", "Elm.Syntax.Range", "Elm.Syntax.Signature", + "Elm.Syntax.StringLiteralType", + "Elm.Syntax.TypeAlias", "Elm.Syntax.TypeAlias", "Elm.Syntax.TypeAnnotation", "Elm.Syntax.Type" @@ -32,11 +31,9 @@ "elm-version": "0.19.0 <= v < 0.20.0", "dependencies": { "elm/core": "1.0.0 <= v < 2.0.0", - "elm/json": "1.0.0 <= v < 2.0.0", "elm/parser": "1.0.0 <= v < 2.0.0", "miniBill/elm-unicode": "1.0.2 <= v < 2.0.0", - "rtfeldman/elm-hex": "1.0.0 <= v < 2.0.0", - "stil4m/structured-writer": "1.0.1 <= v < 2.0.0" + "rtfeldman/elm-hex": "1.0.0 <= v < 2.0.0" }, "test-dependencies": { "elm-explorations/test": "2.0.0 <= v < 3.0.0" diff --git a/review/suppressed/NoDeprecated.json b/review/suppressed/NoDeprecated.json deleted file mode 100644 index 10c022460..000000000 --- a/review/suppressed/NoDeprecated.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "version": 1, - "automatically created by": "elm-review suppress", - "learn more": "elm-review suppress --help", - "suppressions": [ - { "count": 2, "filePath": "tests/Elm/ProcessingTests.elm" } - ] -} diff --git a/review/suppressed/NoImportingEverything.json b/review/suppressed/NoImportingEverything.json deleted file mode 100644 index dc62dffc5..000000000 --- a/review/suppressed/NoImportingEverything.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "version": 1, - "automatically created by": "elm-review suppress", - "learn more": "elm-review suppress --help", - "suppressions": [ - { "count": 13, "filePath": "src/Elm/Writer.elm" } - ] -} diff --git a/src/Elm/Dependency.elm b/src/Elm/Dependency.elm deleted file mode 100644 index ecef51bfe..000000000 --- a/src/Elm/Dependency.elm +++ /dev/null @@ -1,37 +0,0 @@ -module Elm.Dependency exposing (Dependency, Version) - -{-| This module contains types regarding dependencies of a codebase. -To gain the most information of a codebase, information of the dependencies may be required. -For example, what operators does it define, or what constructors are defined for a custom type. - - -## Types - -@docs Dependency, Version - --} - -import Dict exposing (Dict) -import Elm.Interface exposing (Interface) -import Elm.Syntax.ModuleName exposing (ModuleName) - - -{-| Record that represents a dependency. For example: - - { name = "elm/core" - , version = "1.0.0" - , interfaces = Dict.fromList [ ( "Basics", basicsInterface ), ... ] - } - --} -type alias Dependency = - { name : String - , version : Version - , interfaces : Dict ModuleName Interface - } - - -{-| Alias for a version string. For example "1.2.3". --} -type alias Version = - String diff --git a/src/Elm/Interface.elm b/src/Elm/Interface.elm deleted file mode 100644 index a1858ac9b..000000000 --- a/src/Elm/Interface.elm +++ /dev/null @@ -1,244 +0,0 @@ -module Elm.Interface exposing - ( Interface, Exposed(..) - , build, exposesAlias, exposesFunction, operators - ) - -{-| A type that represents the interface for an Elm module. -You can see this as a trimmed down version of a file that only contains the header (`module X exposing (..)`) and some small set of additional data. - - -## Types - -@docs Interface, Exposed - - -## Functions - -@docs build, exposesAlias, exposesFunction, operators - --} - -import Dict exposing (Dict) -import Elm.Internal.RawFile as InternalRawFile -import Elm.RawFile as RawFile -import Elm.Syntax.Declaration exposing (Declaration(..)) -import Elm.Syntax.Exposing exposing (Exposing(..), TopLevelExpose(..)) -import Elm.Syntax.Expression exposing (FunctionImplementation) -import Elm.Syntax.File exposing (File) -import Elm.Syntax.Infix exposing (Infix, InfixDirection(..)) -import Elm.Syntax.Module as Module -import Elm.Syntax.Node as Node exposing (Node(..)) - - -{-| An interface is just a list of 'things' that are exposed by a module. - - [ Type "Color" [ "Red", "Blue" ], Function "asRgb" ] - --} -type alias Interface = - List Exposed - - -{-| Union type for the things that a module can expose. These are `Function`s, `CustomType`s, and `Alias`es. - -Elm core packages can also define `Operator`s, and thus we take that into account as well. -The `Infix` type alias will contain all the information regarding the operator - --} -type Exposed - = Function String - | CustomType ( String, List String ) - | Alias String - | Operator Infix - - -{-| A function to check whether an `Interface` exposes an certain type alias. --} -exposesAlias : String -> Interface -> Bool -exposesAlias k interface = - List.any - (\x -> - case x of - Alias l -> - k == l - - _ -> - False - ) - interface - - -{-| Check whether an `Interface` exposes an function. - - exposesFunction "A" [ Function "A", CustomType "B", [ "C" ] ] == True - exposesFunction "B" [ Function "A", CustomType "B", [ "C" ] ] == False - exposesFunction "<" [ Infix { operator = "<" , ... } ] == True - exposesFunction "A" [ Alias "A" ] == False - --} -exposesFunction : String -> Interface -> Bool -exposesFunction k interface = - List.any - (\x -> - case x of - Function l -> - k == l - - CustomType ( _, constructors ) -> - List.member k constructors - - Operator inf -> - Node.value inf.operator == k - - Alias _ -> - False - ) - interface - - -{-| Retrieve all operators exposed by the `Interface` --} -operators : Interface -> List Infix -operators = - List.filterMap - (\interface -> - case interface of - Operator operator -> - Just operator - - _ -> - Nothing - ) - - -{-| Build an interface from a file --} -build : RawFile.RawFile -> Interface -build (InternalRawFile.Raw file) = - let - fileDefinitionList : List ( String, Exposed ) - fileDefinitionList = - fileToDefinitions file - in - case Module.exposingList (Node.value file.moduleDefinition) of - Explicit x -> - buildInterfaceFromExplicit x (Dict.fromList fileDefinitionList) - - All _ -> - List.map Tuple.second fileDefinitionList - - -buildInterfaceFromExplicit : List (Node TopLevelExpose) -> Dict String Exposed -> Interface -buildInterfaceFromExplicit x exposedDict = - List.filterMap - (\(Node _ expose) -> - case expose of - InfixExpose k -> - Dict.get k exposedDict - - TypeOrAliasExpose s -> - Dict.get s exposedDict - |> Maybe.map (ifCustomType (\( name, _ ) -> CustomType ( name, [] ))) - - FunctionExpose s -> - Just <| Function s - - TypeExpose exposedType -> - case exposedType.open of - Nothing -> - Just <| CustomType ( exposedType.name, [] ) - - Just _ -> - Dict.get exposedType.name exposedDict - ) - x - - -ifCustomType : (( String, List String ) -> Exposed) -> Exposed -> Exposed -ifCustomType f i = - case i of - CustomType t -> - f t - - _ -> - i - - -fileToDefinitions : File -> List ( String, Exposed ) -fileToDefinitions file = - let - allDeclarations : List ( String, Exposed ) - allDeclarations = - List.filterMap - (\(Node _ decl) -> - case decl of - CustomTypeDeclaration t -> - Just ( Node.value t.name, CustomType ( Node.value t.name, t.constructors |> List.map (Node.value >> .name >> Node.value) ) ) - - AliasDeclaration a -> - Just ( Node.value a.name, Alias <| Node.value a.name ) - - PortDeclaration p -> - Just ( Node.value p.name, Function (Node.value p.name) ) - - FunctionDeclaration f -> - let - declaration : FunctionImplementation - declaration = - Node.value f.declaration - - name : String - name = - Node.value declaration.name - in - Just ( name, Function <| name ) - - InfixDeclaration i -> - Just ( Node.value i.operator, Operator i ) - - Destructuring _ _ -> - Nothing - ) - file.declarations - in - -- I really don't understand what this part of the function does. - allDeclarations - |> List.map - (\( name, _ ) -> - -- AFAIK, it's not possible to have two declarations with the same name - -- in the same file, so this seems pointless. - allDeclarations - |> List.filter (\( otherDeclarationName, _ ) -> otherDeclarationName == name) - ) - |> List.filterMap resolveGroup - - -resolveGroup : List ( String, Exposed ) -> Maybe ( String, Exposed ) -resolveGroup g = - case g of - [] -> - Nothing - - [ x ] -> - Just x - - [ ( n1, t1 ), ( _, t2 ) ] -> - getValidOperatorInterface t1 t2 - |> Maybe.map (\a -> ( n1, a )) - - _ -> - Nothing - - -getValidOperatorInterface : Exposed -> Exposed -> Maybe Exposed -getValidOperatorInterface t1 t2 = - case ( t1, t2 ) of - ( Operator x, Operator y ) -> - if Node.value x.precedence == 5 && Node.value x.direction == Left then - Just <| Operator y - - else - Just <| Operator x - - _ -> - Nothing diff --git a/src/Elm/Internal/RawFile.elm b/src/Elm/Internal/RawFile.elm deleted file mode 100644 index 24cd95be9..000000000 --- a/src/Elm/Internal/RawFile.elm +++ /dev/null @@ -1,12 +0,0 @@ -module Elm.Internal.RawFile exposing (RawFile(..), fromFile) - -import Elm.Syntax.File exposing (File) - - -type RawFile - = Raw File - - -fromFile : File -> RawFile -fromFile = - Raw diff --git a/src/Elm/Json/Util.elm b/src/Elm/Json/Util.elm deleted file mode 100644 index 6dc8a4975..000000000 --- a/src/Elm/Json/Util.elm +++ /dev/null @@ -1,29 +0,0 @@ -module Elm.Json.Util exposing (decodeTyped, encodeTyped) - -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) - - -encodeTyped : String -> Value -> Value -encodeTyped x v = - JE.object - [ ( "type", JE.string x ) - , ( x, v ) - ] - - -decodeTyped : List ( String, Decoder a ) -> Decoder a -decodeTyped opts = - JD.lazy - (\() -> - JD.field "type" JD.string - |> JD.andThen - (\t -> - case List.filter (\( opt, _ ) -> opt == t) opts |> List.head of - Just m -> - JD.field (Tuple.first m) <| Tuple.second m - - Nothing -> - JD.fail ("No decoder for type: " ++ t) - ) - ) diff --git a/src/Elm/Parser.elm b/src/Elm/Parser.elm index 639c353b4..d503e7535 100644 --- a/src/Elm/Parser.elm +++ b/src/Elm/Parser.elm @@ -1,42 +1,20 @@ -module Elm.Parser exposing - ( parseToFile - , parse - ) +module Elm.Parser exposing (parse) {-| -@docs parseToFile @docs parse -} -import Elm.Internal.RawFile as InternalRawFile import Elm.Parser.File exposing (file) -import Elm.RawFile exposing (RawFile) import Elm.Syntax.File exposing (File) import Parser import ParserFast -{-| **@deprecated** Use [`parseToFile`](#parseToFile) instead, which is simpler and doesn't require post-processing. -Since v7.3.3, post-processing is unnecessary. - -Parse some text as if it is an Elm source file. -When parsing fails, the result will contain a list of errors indicating what went wrong (and/or where). -If it succeeds, you will get a `RawFile`. -This `RawFile` will require some post-processing to properly setup documentation and ensure that operator precedence is applied correctly (based on dependencies). -To process a `RawFile`, check out the `Processing` module. - --} -parse : String -> Result (List Parser.DeadEnd) RawFile -parse input = - parseToFile input - |> Result.map InternalRawFile.fromFile - - {-| Parse some text as if it is an Elm source file. When parsing fails, the result will contain a list of errors indicating what went wrong (and/or where). -} -parseToFile : String -> Result (List Parser.DeadEnd) File -parseToFile input = +parse : String -> Result (List Parser.DeadEnd) File +parse input = ParserFast.run file input diff --git a/src/Elm/Parser/Declarations.elm b/src/Elm/Parser/Declarations.elm index 65df12b9e..2c1c17bed 100644 --- a/src/Elm/Parser/Declarations.elm +++ b/src/Elm/Parser/Declarations.elm @@ -1,17 +1,17 @@ module Elm.Parser.Declarations exposing (declaration) import Elm.Parser.Comments as Comments +import Elm.Parser.DestructurePatterns as DestructurePatterns import Elm.Parser.Expression exposing (expression) import Elm.Parser.Layout as Layout import Elm.Parser.Node as Node -import Elm.Parser.Patterns as Patterns import Elm.Parser.Tokens as Tokens import Elm.Parser.TypeAnnotation as TypeAnnotation exposing (typeAnnotation, typeAnnotationNoFnExcludingTypedWithArguments) import Elm.Syntax.Declaration as Declaration exposing (Declaration) +import Elm.Syntax.DestructurePattern exposing (DestructurePattern) import Elm.Syntax.Expression exposing (Expression) import Elm.Syntax.Infix as Infix import Elm.Syntax.Node as Node exposing (Node(..)) -import Elm.Syntax.Pattern exposing (Pattern) import Elm.Syntax.Range exposing (Location) import Elm.Syntax.Signature exposing (Signature) import Elm.Syntax.Type exposing (ValueConstructor) @@ -128,9 +128,8 @@ declarationWithDocumentation = { documentation = Just documentation , name = typeDeclarationAfterDocumentation.name , generics = typeDeclarationAfterDocumentation.parameters - , constructors = - typeDeclarationAfterDocumentation.headVariant - :: List.reverse typeDeclarationAfterDocumentation.tailVariantsReverse + , firstConstructor = typeDeclarationAfterDocumentation.headVariant + , restOfConstructors = List.reverse typeDeclarationAfterDocumentation.tailVariantsReverse } ) } @@ -159,17 +158,22 @@ declarationWithDocumentation = (Node typeAnnotationRange _) = portDeclarationAfterName.typeAnnotation in - { comments = - Rope.one documentation - |> Rope.filledPrependTo afterDocumentation.comments + { comments = afterDocumentation.comments , syntax = Node - { start = portDeclarationAfterName.startLocation + { start = start , end = typeAnnotationRange.end } (Declaration.PortDeclaration - { name = portDeclarationAfterName.name - , typeAnnotation = portDeclarationAfterName.typeAnnotation + { documentation = Just documentation + , signature = + Node + { start = (Node.range portDeclarationAfterName.name).start + , end = typeAnnotationRange.end + } + { name = portDeclarationAfterName.name + , typeAnnotation = portDeclarationAfterName.typeAnnotation + } } ) } @@ -195,7 +199,7 @@ type DeclarationAfterDocumentation { typeAnnotation : Node TypeAnnotation , implementationName : Node String } - , arguments : List (Node Pattern) + , arguments : List (Node DestructurePattern) , expression : Node Expression } | TypeDeclarationAfterDocumentation @@ -396,7 +400,7 @@ functionDeclarationWithoutDocumentation = |> ParserFast.andThen identity -parameterPatternsEqual : Parser (WithComments (List (Node Pattern))) +parameterPatternsEqual : Parser (WithComments (List (Node DestructurePattern))) parameterPatternsEqual = ParserWithComments.until Tokens.equal (ParserFast.map2 @@ -405,7 +409,7 @@ parameterPatternsEqual = , syntax = patternResult.syntax } ) - Patterns.patternNotDirectlyComposing + DestructurePatterns.destructurePattern Layout.maybeLayout ) @@ -495,8 +499,15 @@ portDeclarationWithoutDocumentation = , end = end } (Declaration.PortDeclaration - { name = name - , typeAnnotation = typeAnnotationResult.syntax + { documentation = Nothing + , signature = + Node + { start = nameRange.start + , end = end + } + { name = name + , typeAnnotation = typeAnnotationResult.syntax + } } ) } @@ -629,9 +640,8 @@ typeOrTypeAliasDefinitionWithoutDocumentation = { documentation = Nothing , name = typeDeclarationAfterDocumentation.name , generics = typeDeclarationAfterDocumentation.parameters - , constructors = - typeDeclarationAfterDocumentation.headVariant - :: List.reverse typeDeclarationAfterDocumentation.tailVariantsReverse + , firstConstructor = typeDeclarationAfterDocumentation.headVariant + , restOfConstructors = List.reverse typeDeclarationAfterDocumentation.tailVariantsReverse } , end = end } diff --git a/src/Elm/Parser/DestructurePatterns.elm b/src/Elm/Parser/DestructurePatterns.elm new file mode 100644 index 000000000..1ff53d24b --- /dev/null +++ b/src/Elm/Parser/DestructurePatterns.elm @@ -0,0 +1,273 @@ +module Elm.Parser.DestructurePatterns exposing (destructurePattern) + +import Elm.Parser.Layout as Layout +import Elm.Parser.Node as Node +import Elm.Parser.Tokens as Tokens +import Elm.Syntax.DestructurePattern exposing (DestructurePattern(..)) +import Elm.Syntax.Node as Node exposing (Node(..)) +import ParserFast exposing (Parser) +import ParserWithComments exposing (WithComments) +import Rope + + +type PatternComposedWith + = PatternComposedWithNothing () + | PatternComposedWithAs (Node String) + + +subDestructurePattern : Parser (WithComments (Node DestructurePattern)) +subDestructurePattern = + ParserFast.lazy (\() -> composablePatternTryToCompose) + + +composablePatternTryToCompose : Parser (WithComments (Node DestructurePattern)) +composablePatternTryToCompose = + ParserFast.map3 + (\x commentsAfterLeft maybeComposedWithResult -> + { comments = + x.comments + |> Rope.prependTo commentsAfterLeft + |> Rope.prependTo maybeComposedWithResult.comments + , syntax = + case maybeComposedWithResult.syntax of + PatternComposedWithNothing () -> + x.syntax + + PatternComposedWithAs anotherName -> + Node.combine AsPattern_ x.syntax anotherName + } + ) + composablePattern + Layout.maybeLayout + maybeComposedWith + + +maybeComposedWith : Parser { comments : ParserWithComments.Comments, syntax : PatternComposedWith } +maybeComposedWith = + ParserFast.oneOf + [ ParserFast.map2 + (\commentsAfterAs name -> + { comments = commentsAfterAs + , syntax = PatternComposedWithAs name + } + ) + (ParserFast.keywordFollowedBy "as" Layout.maybeLayout) + (Node.parserCore Tokens.functionName) + , ParserFast.succeed { comments = Rope.empty, syntax = PatternComposedWithNothing () } + ] + + +parensPattern : Parser (WithComments (Node DestructurePattern)) +parensPattern = + ParserFast.map2 + (\commentsBeforeHead contentResult -> + { comments = + commentsBeforeHead + |> Rope.prependTo contentResult.comments + , syntax = contentResult.syntax + } + ) + (ParserFast.symbolFollowedBy "(" Layout.maybeLayout) + -- yes, ( ) is a valid pattern but not a valid type or expression + (ParserFast.oneOf2 + (ParserFast.map3 + (\headResult commentsAfterHead tailResult -> + { comments = + headResult.comments + |> Rope.prependTo commentsAfterHead + |> Rope.prependTo tailResult.comments + , syntax = + case tailResult.syntax of + [] -> + ParenthesizedPattern_ headResult.syntax + + _ -> + TuplePattern_ (headResult.syntax :: tailResult.syntax) + } + ) + subDestructurePattern + Layout.maybeLayout + (ParserWithComments.until + Tokens.parensEnd + (ParserFast.symbolFollowedBy "," + (Layout.maybeAroundBothSides subDestructurePattern) + ) + ) + ) + (ParserFast.symbol ")" { comments = Rope.empty, syntax = UnitPattern_ }) + ) + |> Node.parser + + +varPattern : Parser (WithComments (Node DestructurePattern)) +varPattern = + Tokens.functionName + |> ParserFast.mapWithStartAndEndPosition + (\start var end -> + { comments = Rope.empty + , syntax = + Node { start = start, end = end } (VarPattern_ var) + } + ) + + +composablePattern : Parser (WithComments (Node DestructurePattern)) +composablePattern = + ParserFast.oneOf + [ varPattern + , qualifiedPatternWithConsumeArgs + , allPattern + , unitPattern + , parensPattern + , recordPattern + ] + + +destructurePattern : Parser (WithComments (Node DestructurePattern)) +destructurePattern = + ParserFast.oneOf + [ varPattern + , qualifiedPatternWithoutConsumeArgs + , allPattern + , unitPattern + , parensPattern + , recordPattern + ] + + +allPattern : Parser (WithComments (Node DestructurePattern)) +allPattern = + ParserFast.symbol "_" { comments = Rope.empty, syntax = AllPattern_ } + |> Node.parser + + +unitPattern : Parser (WithComments (Node DestructurePattern)) +unitPattern = + ParserFast.symbol "()" { comments = Rope.empty, syntax = UnitPattern_ } + |> Node.parser + + +maybeDotTypeNamesTuple : ParserFast.Parser (Maybe ( List String, String )) +maybeDotTypeNamesTuple = + ParserFast.orSucceed + (ParserFast.map2 + (\startName afterStartName -> + case afterStartName of + Nothing -> + Just ( [], startName ) + + Just ( qualificationAfter, unqualified ) -> + Just ( startName :: qualificationAfter, unqualified ) + ) + (ParserFast.symbolFollowedBy "." Tokens.typeName) + (ParserFast.lazy (\() -> maybeDotTypeNamesTuple)) + ) + Nothing + + +qualifiedPatternWithConsumeArgs : Parser (WithComments (Node DestructurePattern)) +qualifiedPatternWithConsumeArgs = + ParserFast.map3 + (\startName afterStartName args -> + { comments = args.comments + , syntax = + NamedPattern_ + (case afterStartName of + Nothing -> + { moduleName = [], name = startName } + + Just ( qualificationAfter, unqualified ) -> + { moduleName = startName :: qualificationAfter, name = unqualified } + ) + args.syntax + } + ) + Tokens.typeName + maybeDotTypeNamesTuple + (ParserWithComments.many + (ParserFast.map2 + (\commentsBefore arg -> + { comments = arg.comments |> Rope.prependTo commentsBefore + , syntax = arg.syntax + } + ) + (Layout.maybeLayout |> ParserFast.backtrackable) + destructurePattern + ) + ) + |> Node.parser + + +qualifiedPatternWithoutConsumeArgs : Parser (WithComments (Node DestructurePattern)) +qualifiedPatternWithoutConsumeArgs = + ParserFast.mapWithStartAndEndPosition + (\start name end -> + { comments = Rope.empty + , syntax = + Node { start = start, end = end } (NamedPattern_ name []) + } + ) + (ParserFast.map2 + (\firstName after -> + case after of + Nothing -> + { moduleName = [], name = firstName } + + Just ( qualificationAfter, unqualified ) -> + { moduleName = firstName :: qualificationAfter, name = unqualified } + ) + Tokens.typeName + maybeDotTypeNamesTuple + ) + + +recordPattern : Parser (WithComments (Node DestructurePattern)) +recordPattern = + ParserFast.map2 + (\commentsBeforeElements maybeElements -> + case maybeElements of + Nothing -> + { comments = commentsBeforeElements + , syntax = patternRecordEmpty + } + + Just elements -> + { comments = commentsBeforeElements |> Rope.prependTo elements.comments + , syntax = RecordPattern_ elements.syntax + } + ) + (ParserFast.symbolFollowedBy "{" Layout.maybeLayout) + (ParserFast.oneOf2 + (ParserFast.map4 + (\head commentsAfterHead tail () -> + Just + { comments = + commentsAfterHead + |> Rope.prependTo tail.comments + , syntax = head :: tail.syntax + } + ) + (Node.parserCore Tokens.functionName) + Layout.maybeLayout + (ParserWithComments.many + (ParserFast.map3 + (\beforeName name afterName -> + { comments = beforeName |> Rope.prependTo afterName + , syntax = name + } + ) + (ParserFast.symbolFollowedBy "," Layout.maybeLayout) + (Node.parserCore Tokens.functionName) + Layout.maybeLayout + ) + ) + Tokens.curlyEnd + ) + (ParserFast.symbol "}" Nothing) + ) + |> Node.parser + + +patternRecordEmpty : DestructurePattern +patternRecordEmpty = + RecordPattern_ [] diff --git a/src/Elm/Parser/Expose.elm b/src/Elm/Parser/Expose.elm index 8549951d6..36ea0af3a 100644 --- a/src/Elm/Parser/Expose.elm +++ b/src/Elm/Parser/Expose.elm @@ -41,9 +41,8 @@ exposingListInner = |> Rope.prependTo tailElements.comments , syntax = Explicit - (headElement.syntax - :: tailElements.syntax - ) + headElement.syntax + tailElements.syntax } ) exposable diff --git a/src/Elm/Parser/Expression.elm b/src/Elm/Parser/Expression.elm index 5add7524c..729f7006f 100644 --- a/src/Elm/Parser/Expression.elm +++ b/src/Elm/Parser/Expression.elm @@ -1,15 +1,15 @@ module Elm.Parser.Expression exposing (expression) +import Elm.Parser.DestructurePatterns as DestructurePatterns import Elm.Parser.Layout as Layout import Elm.Parser.Node as Node import Elm.Parser.Numbers import Elm.Parser.Patterns as Patterns import Elm.Parser.Tokens as Tokens import Elm.Parser.TypeAnnotation as TypeAnnotation +import Elm.Syntax.DestructurePattern exposing (DestructurePattern) import Elm.Syntax.Expression as Expression exposing (Case, Expression(..), LetDeclaration(..), RecordSetter) -import Elm.Syntax.Infix as Infix import Elm.Syntax.Node as Node exposing (Node(..)) -import Elm.Syntax.Pattern exposing (Pattern) import Elm.Syntax.Range exposing (Location) import Elm.Syntax.Signature exposing (Signature) import ParserFast exposing (Parser) @@ -25,7 +25,7 @@ subExpression = , unqualifiedFunctionReferenceExpression , literalExpression , numberExpression - , tupledExpression + , tupleExpression , listOrGlslExpression , recordExpression , caseExpression @@ -137,7 +137,7 @@ glslExpressionAfterOpeningSquareBracket = { start = { row = start.row, column = start.column - 6 } , end = { row = end.row, column = end.column + 2 } } - (GLSLExpression s) + (GLSL s) } ) (ParserFast.Advanced.loop "" untilGlslEnd) @@ -187,14 +187,14 @@ expressionAfterOpeningSquareBracket = ) Layout.maybeLayout (ParserFast.oneOf2 - (ParserFast.symbol "]" { comments = Rope.empty, syntax = ListExpr [] }) + (ParserFast.symbol "]" { comments = Rope.empty, syntax = ListLiteral [] }) (ParserFast.map4 (\head commentsAfterHead tail () -> { comments = head.comments |> Rope.prependTo commentsAfterHead |> Rope.prependTo tail.comments - , syntax = ListExpr (head.syntax :: tail.syntax) + , syntax = ListLiteral (head.syntax :: tail.syntax) } ) expression @@ -243,10 +243,10 @@ recordContentsCurlyEnd = , syntax = case afterNameBeforeFields.syntax of RecordUpdateFirstSetter firstField -> - RecordUpdateExpression nameNode (firstField :: tailFields.syntax) + RecordUpdate nameNode firstField tailFields.syntax FieldsFirstValue firstFieldValue -> - RecordExpr (Node.combine Tuple.pair nameNode firstFieldValue :: tailFields.syntax) + Record (Node.combine Tuple.pair nameNode firstFieldValue :: tailFields.syntax) } ) (Node.parserCore Tokens.functionName) @@ -278,7 +278,7 @@ recordContentsCurlyEnd = recordFields (Layout.maybeLayoutUntilIgnored ParserFast.symbolFollowedBy "}") ) - (ParserFast.symbol "}" { comments = Rope.empty, syntax = RecordExpr [] }) + (ParserFast.symbol "}" { comments = Rope.empty, syntax = Record [] }) type RecordFieldsOrUpdateAfterName @@ -327,9 +327,9 @@ recordSetterNodeWithLayout = literalExpression : Parser (WithComments (Node Expression)) literalExpression = ParserFast.mapWithStartAndEndPosition - (\start string end -> + (\start ( stringLiteralType, string ) end -> { comments = Rope.empty - , syntax = Node { start = start, end = end } (Literal string) + , syntax = Node { start = start, end = end } (StringLiteral stringLiteralType string) } ) Tokens.singleOrTripleQuotedStringLiteral @@ -362,7 +362,8 @@ lambdaExpression = , syntax = Node { start = start, end = expressionRange.end } (LambdaExpression - { args = lambda.args + { firstArg = lambda.firstArg + , restOfArgs = lambda.restOfArgs , expression = lambda.expression } ) @@ -370,18 +371,19 @@ lambdaExpression = ) (ParserFast.map5 (\commentsAfterBackslash firstArg commentsAfterFirstArg secondUpArgs expressionResult -> - { args = firstArg.syntax :: secondUpArgs.syntax - , comments = + { comments = commentsAfterBackslash |> Rope.prependTo firstArg.comments |> Rope.prependTo commentsAfterFirstArg |> Rope.prependTo secondUpArgs.comments |> Rope.prependTo expressionResult.comments , expression = expressionResult.syntax + , firstArg = firstArg.syntax + , restOfArgs = secondUpArgs.syntax } ) (ParserFast.symbolFollowedBy "\\" Layout.maybeLayout) - Patterns.patternNotDirectlyComposing + DestructurePatterns.destructurePattern Layout.maybeLayout (ParserWithComments.until Tokens.arrowRight @@ -393,7 +395,7 @@ lambdaExpression = , syntax = patternResult.syntax } ) - Patterns.patternNotDirectlyComposing + DestructurePatterns.destructurePattern Layout.maybeLayout ) ) @@ -425,9 +427,10 @@ caseExpression = in firstCaseExpressionRange.end } - (CaseExpression + (Case { expression = caseBlock.casedExpression - , cases = caseBlock.firstCase :: List.reverse caseBlock.lastToSecondCase + , firstCase = caseBlock.firstCase + , restOfCases = List.reverse caseBlock.lastToSecondCase } ) } @@ -515,7 +518,7 @@ letExpression = { comments = letBlock.comments , syntax = Node { start = start, end = expressionRange.end } - (LetExpression + (Let { declarations = letBlock.declarations , expression = letBlock.expression } @@ -612,7 +615,7 @@ letDestructuringDeclaration = (LetDestructuring pattern.syntax expressionResult.syntax) } ) - Patterns.patternNotDirectlyComposing + DestructurePatterns.destructurePattern Layout.maybeLayout (ParserFast.symbolFollowedBy "=" Layout.maybeLayout) expression @@ -726,7 +729,7 @@ letFunction = |> ParserFast.andThen identity -parameterPatternsEqual : Parser (WithComments (List (Node Pattern))) +parameterPatternsEqual : Parser (WithComments (List (Node DestructurePattern))) parameterPatternsEqual = ParserWithComments.until Tokens.equal (ParserFast.map2 @@ -735,7 +738,7 @@ parameterPatternsEqual = , syntax = patternResult.syntax } ) - Patterns.patternNotDirectlyComposing + DestructurePatterns.destructurePattern Layout.maybeLayout ) @@ -749,9 +752,9 @@ numberExpression = } ) (Elm.Parser.Numbers.floatOrIntOrHex - Floatable - Integer - Hex + FloatLiteral + IntegerLiteral + HexLiteral ) @@ -766,7 +769,7 @@ ifBlockExpression = { comments = ifBlock.comments , syntax = Node { start = start, end = ifFalseRange.end } - (IfBlock + (If ifBlock.condition ifBlock.ifTrue ifBlock.ifFalse @@ -890,14 +893,14 @@ recordAccessFunctionExpression = { comments = Rope.empty , syntax = Node { start = start, end = end } - (RecordAccessFunction ("." ++ field)) + (RecordAccessFunction field) } ) (ParserFast.symbolFollowedBy "." Tokens.functionName) -tupledExpression : Parser (WithComments (Node Expression)) -tupledExpression = +tupleExpression : Parser (WithComments (Node Expression)) +tupleExpression = ParserFast.symbolFollowedBy "(" (ParserFast.oneOf (ParserFast.mapWithEndPosition @@ -905,7 +908,7 @@ tupledExpression = { comments = Rope.empty , syntax = Node { start = { row = end.row, column = end.column - 2 }, end = end } - UnitExpr + unit } ) (ParserFast.symbol ")" ()) @@ -928,6 +931,11 @@ tupledExpression = ) +unit : Expression +unit = + TupleExpression [] + + expressionPrefixOperatorMinus : Expression expressionPrefixOperatorMinus = PrefixOperator "-" @@ -974,7 +982,7 @@ tupledExpressionInnerAfterOpeningParens = commentsBeforeFirstPart |> Rope.prependTo firstPart.comments |> Rope.prependTo commentsAfterFirstPart - , syntax = ParenthesizedExpression firstPart.syntax + , syntax = TupleExpression [ firstPart.syntax ] } _ -> @@ -983,7 +991,7 @@ tupledExpressionInnerAfterOpeningParens = |> Rope.prependTo firstPart.comments |> Rope.prependTo commentsAfterFirstPart |> Rope.prependTo tailPartsReverse.comments - , syntax = TupledExpression (firstPart.syntax :: List.reverse tailPartsReverse.syntax) + , syntax = TupleExpression (firstPart.syntax :: List.reverse tailPartsReverse.syntax) } ) Layout.maybeLayout @@ -1112,14 +1120,12 @@ applyExtensionRight extensionRight ((Node { start } left) as leftNode) = ExtendRightByApplication ((Node { end } _) as right) -> Node { start = start, end = end } - (Expression.Application - (case left of - Expression.Application (called :: firstArg :: secondArgUp) -> - called :: firstArg :: (secondArgUp ++ [ right ]) + (case left of + Expression.FunctionCall called firstArg restOfArgs -> + Expression.FunctionCall called firstArg (restOfArgs ++ [ right ]) - _ -> - [ leftNode, right ] - ) + _ -> + Expression.FunctionCall leftNode right [] ) ExtendRightByOperation extendRightOperation -> @@ -1128,7 +1134,7 @@ applyExtensionRight extensionRight ((Node { start } left) as leftNode) = extendRightOperation.expression in Node { start = start, end = end } - (OperatorApplication extendRightOperation.symbol extendRightOperation.direction leftNode right) + (Operation extendRightOperation.symbol leftNode right) abovePrecedence0 : Parser (WithComments ExtensionRight) @@ -1206,7 +1212,7 @@ infixLeft precedence possibilitiesForPrecedence symbol = possibilitiesForPrecedence (ParserFast.symbolFollowedBy symbol) (\right -> - ExtendRightByOperation { symbol = symbol, direction = Infix.Left, expression = right } + ExtendRightByOperation { symbol = symbol, expression = right } ) @@ -1216,7 +1222,7 @@ infixNonAssociative precedence possibilitiesForPrecedence symbol = possibilitiesForPrecedence (ParserFast.symbolFollowedBy symbol) (\right -> - ExtendRightByOperation { symbol = symbol, direction = Infix.Non, expression = right } + ExtendRightByOperation { symbol = symbol, expression = right } ) @@ -1229,7 +1235,7 @@ infixRight precedence possibilitiesForPrecedenceMinus1 symbol = possibilitiesForPrecedenceMinus1 (ParserFast.symbolFollowedBy symbol) (\right -> - ExtendRightByOperation { symbol = symbol, direction = Infix.Right, expression = right } + ExtendRightByOperation { symbol = symbol, expression = right } ) @@ -1257,7 +1263,7 @@ infixLeftSubtraction precedence possibilitiesForPrecedence = ) ) (\right -> - ExtendRightByOperation { symbol = "-", direction = Infix.Left, expression = right } + ExtendRightByOperation { symbol = "-", expression = right } ) @@ -1289,6 +1295,6 @@ postfix precedence operator = type ExtensionRight - = ExtendRightByOperation { symbol : String, direction : Infix.InfixDirection, expression : Node Expression } + = ExtendRightByOperation { symbol : String, expression : Node Expression } | ExtendRightByApplication (Node Expression) | ExtendRightByRecordAccess (Node String) diff --git a/src/Elm/Parser/Patterns.elm b/src/Elm/Parser/Patterns.elm index 4840b78e9..d1da3142e 100644 --- a/src/Elm/Parser/Patterns.elm +++ b/src/Elm/Parser/Patterns.elm @@ -1,4 +1,4 @@ -module Elm.Parser.Patterns exposing (pattern, patternNotDirectlyComposing) +module Elm.Parser.Patterns exposing (pattern) import Elm.Parser.Layout as Layout import Elm.Parser.Node as Node @@ -235,10 +235,10 @@ stringPattern : Parser (WithComments (Node Pattern)) stringPattern = Tokens.singleOrTripleQuotedStringLiteral |> ParserFast.mapWithStartAndEndPosition - (\start string end -> + (\start ( stringLiteralType, string ) end -> { comments = Rope.empty , syntax = - Node { start = start, end = end } (StringPattern string) + Node { start = start, end = end } (StringPattern stringLiteralType string) } ) diff --git a/src/Elm/Parser/Tokens.elm b/src/Elm/Parser/Tokens.elm index d0ed9a657..4a1957e73 100644 --- a/src/Elm/Parser/Tokens.elm +++ b/src/Elm/Parser/Tokens.elm @@ -22,6 +22,7 @@ module Elm.Parser.Tokens exposing import Char import Char.Extra +import Elm.Syntax.StringLiteralType exposing (StringLiteralType(..)) import Hex import ParserFast import ParserFast.Advanced @@ -110,14 +111,17 @@ characterLiteral = (ParserFast.symbol "'" ()) -singleOrTripleQuotedStringLiteral : ParserFast.Parser String +singleOrTripleQuotedStringLiteral : ParserFast.Parser ( StringLiteralType, String ) singleOrTripleQuotedStringLiteral = ParserFast.symbolFollowedBy "\"" (ParserFast.oneOf2 (ParserFast.symbolFollowedBy "\"\"" (ParserFast.Advanced.loop "" tripleQuotedStringLiteralStep) + |> ParserFast.map (\str -> ( TripleQuote, str )) + ) + (ParserFast.Advanced.loop "" stringLiteralHelper + |> ParserFast.map (\str -> ( SingleQuote, str )) ) - (ParserFast.Advanced.loop "" stringLiteralHelper) ) diff --git a/src/Elm/Parser/TypeAnnotation.elm b/src/Elm/Parser/TypeAnnotation.elm index c3fec280f..ccf3e81de 100644 --- a/src/Elm/Parser/TypeAnnotation.elm +++ b/src/Elm/Parser/TypeAnnotation.elm @@ -70,7 +70,7 @@ parensTypeAnnotation : Parser (WithComments (Node TypeAnnotation)) parensTypeAnnotation = ParserFast.symbolFollowedBy "(" (ParserFast.oneOf2 - (ParserFast.symbol ")" { comments = Rope.empty, syntax = TypeAnnotation.Unit }) + (ParserFast.symbol ")" { comments = Rope.empty, syntax = TypeAnnotation.Tuple [] }) (ParserFast.map4 (\commentsBeforeFirstPart firstPart commentsAfterFirstPart lastToSecondPart -> { comments = @@ -88,7 +88,7 @@ parensTypeAnnotation = firstPartValue _ -> - TypeAnnotation.Tupled (firstPart.syntax :: List.reverse lastToSecondPart.syntax) + TypeAnnotation.Tuple (firstPart.syntax :: List.reverse lastToSecondPart.syntax) } ) Layout.maybeLayout @@ -123,7 +123,7 @@ genericTypeAnnotation = { comments = Rope.empty , syntax = Node { start = start, end = end } - (TypeAnnotation.GenericType var) + (TypeAnnotation.Var var) } ) @@ -264,7 +264,7 @@ typedTypeAnnotationWithoutArguments = { comments = Rope.empty , syntax = Node range - (TypeAnnotation.Typed (Node range name) []) + (TypeAnnotation.Type (Node range name) []) } ) (ParserFast.map2 @@ -304,7 +304,7 @@ typedTypeAnnotationWithArguments = ParserFast.map2 (\nameNode args -> { comments = args.comments - , syntax = TypeAnnotation.Typed nameNode args.syntax + , syntax = TypeAnnotation.Type nameNode args.syntax } ) (ParserFast.mapWithStartAndEndPosition diff --git a/src/Elm/Processing.elm b/src/Elm/Processing.elm deleted file mode 100644 index 10e64f6ff..000000000 --- a/src/Elm/Processing.elm +++ /dev/null @@ -1,72 +0,0 @@ -module Elm.Processing exposing - ( ProcessContext - , init, addFile, addDependency, process - ) - -{-| Processing raw files with the context of other files and dependencies. - -Since v7.3.3, post-processing is unnecessary. -Use [`Elm.Parser.parseToFile`](Elm-Parser#parseToFile) instead. - - -## Types - -@docs ProcessContext - - -## Functions - -@docs init, addFile, addDependency, process - --} - -import Dict exposing (Dict) -import Elm.Dependency exposing (Dependency) -import Elm.Interface as Interface exposing (Interface) -import Elm.Internal.RawFile as InternalRawFile -import Elm.RawFile as RawFile -import Elm.Syntax.File exposing (File) -import Elm.Syntax.ModuleName exposing (ModuleName) - - -{-| Opaque type to hold context for the processing --} -type ProcessContext - = ProcessContext ModuleIndexInner - - -type alias ModuleIndexInner = - Dict ModuleName Interface - - -{-| Initialise an empty context --} -init : ProcessContext -init = - ProcessContext Dict.empty - - -{-| Add a file to the context that may be a dependency for the file that will be processed. --} -addFile : RawFile.RawFile -> ProcessContext -> ProcessContext -addFile file (ProcessContext context) = - ProcessContext - (Dict.insert - (RawFile.moduleName file) - (Interface.build file) - context - ) - - -{-| Add a whole dependency with its modules to the context. --} -addDependency : Dependency -> ProcessContext -> ProcessContext -addDependency dep (ProcessContext x) = - ProcessContext (Dict.union dep.interfaces x) - - -{-| Process a rawfile with a context. --} -process : ProcessContext -> RawFile.RawFile -> File -process _ (InternalRawFile.Raw file) = - file diff --git a/src/Elm/RawFile.elm b/src/Elm/RawFile.elm deleted file mode 100644 index 5fb99954b..000000000 --- a/src/Elm/RawFile.elm +++ /dev/null @@ -1,61 +0,0 @@ -module Elm.RawFile exposing - ( RawFile - , moduleName, imports - , encode, decoder - ) - -{-| - -@docs RawFile - -@docs moduleName, imports - - -## Serialization - -@docs encode, decoder - --} - -import Elm.Internal.RawFile as InternalRawFile -import Elm.Syntax.File as File -import Elm.Syntax.Import exposing (Import) -import Elm.Syntax.Module as Module -import Elm.Syntax.ModuleName exposing (ModuleName) -import Elm.Syntax.Node as Node -import Json.Decode as JD exposing (Decoder) -import Json.Encode exposing (Value) - - -{-| A Raw file --} -type alias RawFile = - InternalRawFile.RawFile - - -{-| Retrieve the module name for a raw file --} -moduleName : RawFile -> ModuleName -moduleName (InternalRawFile.Raw file) = - Module.moduleName <| Node.value file.moduleDefinition - - -{-| Encode a `RawFile` syntax element to JSON. --} -imports : RawFile -> List Import -imports (InternalRawFile.Raw file) = - List.map Node.value file.imports - - -{-| Encode a file to a value --} -encode : RawFile -> Value -encode (InternalRawFile.Raw file) = - File.encode file - - -{-| JSON decoder for a `RawFile` syntax element. --} -decoder : Decoder RawFile -decoder = - JD.map InternalRawFile.Raw File.decoder diff --git a/src/Elm/Syntax/Comments.elm b/src/Elm/Syntax/Comments.elm index 0588e56c5..30578742e 100644 --- a/src/Elm/Syntax/Comments.elm +++ b/src/Elm/Syntax/Comments.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.Comments exposing - ( Comment - , encode, decoder - ) +module Elm.Syntax.Comments exposing (Comment) {-| This syntax represents both single and multi line comments in Elm. For example: @@ -21,32 +18,10 @@ module Elm.Syntax.Comments exposing @docs Comment - -## Serialization - -@docs encode, decoder - -} -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) - {-| Type representing the comment syntax -} type alias Comment = String - - -{-| Encode a `Comment` syntax element to JSON. --} -encode : Comment -> Value -encode = - JE.string - - -{-| JSON decoder for a `Comment` syntax element. --} -decoder : Decoder Comment -decoder = - JD.string diff --git a/src/Elm/Syntax/Declaration.elm b/src/Elm/Syntax/Declaration.elm index a8459c817..4e03dc62a 100644 --- a/src/Elm/Syntax/Declaration.elm +++ b/src/Elm/Syntax/Declaration.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.Declaration exposing - ( Declaration(..) - , encode, decoder - ) +module Elm.Syntax.Declaration exposing (Declaration(..)) {-| Syntax for the different top-level declarations in Elm. These can be one of the following (all declared in `Declaration`): @@ -10,7 +7,6 @@ These can be one of the following (all declared in `Declaration`): - Custom types: `type Color = Blue | Red` - Type aliases: `type alias Status = Int` - Port declaration: `port sendMessage: String -> Cmd msg` - - Destructuring: `{name, age} = person` - Infix declarations. You will probably not need this, while only core packages can define these. @@ -18,23 +14,13 @@ These can be one of the following (all declared in `Declaration`): @docs Declaration - -## Serialization - -@docs encode, decoder - -} -import Elm.Json.Util exposing (decodeTyped, encodeTyped) -import Elm.Syntax.Expression as Expression exposing (Expression, Function) -import Elm.Syntax.Infix as Infix exposing (Infix) -import Elm.Syntax.Node as Node exposing (Node) -import Elm.Syntax.Pattern as Pattern exposing (Pattern) -import Elm.Syntax.Signature as Signature exposing (Signature) -import Elm.Syntax.Type as Type exposing (Type) -import Elm.Syntax.TypeAlias as TypeAlias exposing (TypeAlias) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.Expression exposing (Function) +import Elm.Syntax.Infix exposing (Infix) +import Elm.Syntax.Port exposing (Port) +import Elm.Syntax.Type exposing (Type) +import Elm.Syntax.TypeAlias exposing (TypeAlias) {-| Custom type that represents all different top-level declarations. @@ -43,57 +29,5 @@ type Declaration = FunctionDeclaration Function | AliasDeclaration TypeAlias | CustomTypeDeclaration Type - | PortDeclaration Signature + | PortDeclaration Port | InfixDeclaration Infix - | Destructuring (Node Pattern) (Node Expression) - - - --- Serialization - - -{-| Encode a `Declaration` syntax element to JSON. --} -encode : Declaration -> Value -encode decl = - case decl of - FunctionDeclaration function -> - encodeTyped "function" (Expression.encodeFunction function) - - AliasDeclaration typeAlias -> - encodeTyped "typeAlias" (TypeAlias.encode typeAlias) - - CustomTypeDeclaration typeDeclaration -> - encodeTyped "typedecl" (Type.encode typeDeclaration) - - PortDeclaration sig -> - encodeTyped "port" (Signature.encode sig) - - InfixDeclaration inf -> - encodeTyped "infix" - (Infix.encode inf) - - Destructuring pattern expression -> - encodeTyped "destructuring" - (JE.object - [ ( "pattern", Node.encode Pattern.encode pattern ) - , ( "expression", Node.encode Expression.encode expression ) - ] - ) - - -{-| JSON decoder for a `Declaration` syntax element. --} -decoder : Decoder Declaration -decoder = - JD.lazy - (\() -> - decodeTyped - [ ( "function", Expression.functionDecoder |> JD.map FunctionDeclaration ) - , ( "typeAlias", TypeAlias.decoder |> JD.map AliasDeclaration ) - , ( "typedecl", Type.decoder |> JD.map CustomTypeDeclaration ) - , ( "port", Signature.decoder |> JD.map PortDeclaration ) - , ( "infix", Infix.decoder |> JD.map InfixDeclaration ) - , ( "destructuring", JD.map2 Destructuring (JD.field "pattern" (Node.decoder Pattern.decoder)) (JD.field "expression" (Node.decoder Expression.decoder)) ) - ] - ) diff --git a/src/Elm/Syntax/DestructurePattern.elm b/src/Elm/Syntax/DestructurePattern.elm new file mode 100644 index 000000000..dcc4ce941 --- /dev/null +++ b/src/Elm/Syntax/DestructurePattern.elm @@ -0,0 +1,42 @@ +module Elm.Syntax.DestructurePattern exposing (DestructurePattern(..)) + +{-| + + +# Destructure pattern Syntax + +This syntax represents patterns used for destructuring data. +For example: + + Just x as someMaybe + {name, age} + +@docs DestructurePattern + +-} + +import Elm.Syntax.Node exposing (Node) +import Elm.Syntax.Pattern exposing (QualifiedNameRef) + + +{-| Custom type for all destructuring patterns such as: + + - `AllPattern_`: `_` + - `UnitPattern_`: `()` + - `TuplePattern_`: `(a, b)` + - `RecordPattern_`: `{name, age}` + - `VarPattern_`: `x` + - `NamedPattern_`: `Just _` + - `AsPattern_`: `_ as x` + - `ParenthesizedPattern_`: `( _ )` + +-} +type DestructurePattern + = AllPattern_ + | UnitPattern_ + | TuplePattern_ (List (Node DestructurePattern)) + | RecordPattern_ (List (Node String)) + | VarPattern_ String + | NamedPattern_ QualifiedNameRef (List (Node DestructurePattern)) + | AsPattern_ (Node DestructurePattern) (Node String) + | ParenthesizedPattern_ (Node DestructurePattern) diff --git a/src/Elm/Syntax/Documentation.elm b/src/Elm/Syntax/Documentation.elm index 003f6c3ac..45f5fe6f2 100644 --- a/src/Elm/Syntax/Documentation.elm +++ b/src/Elm/Syntax/Documentation.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.Documentation exposing - ( Documentation - , encode, decoder - ) +module Elm.Syntax.Documentation exposing (Documentation) {-| This syntax represents documentation comments in Elm. @@ -10,16 +7,8 @@ module Elm.Syntax.Documentation exposing @docs Documentation - -## Serialization - -@docs encode, decoder - -} -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) - {-| Type representing the documentation syntax -} @@ -28,18 +17,4 @@ type alias Documentation = --- Serialization - - -{-| Encode a `Documentation` syntax element to JSON. --} -encode : Documentation -> Value -encode = - JE.string - - -{-| JSON decoder for a `Documentation` syntax element. --} -decoder : Decoder Documentation -decoder = - JD.string +-- diff --git a/src/Elm/Syntax/Exposing.elm b/src/Elm/Syntax/Exposing.elm index a9382323e..b07f2bfb8 100644 --- a/src/Elm/Syntax/Exposing.elm +++ b/src/Elm/Syntax/Exposing.elm @@ -1,7 +1,6 @@ module Elm.Syntax.Exposing exposing ( Exposing(..), TopLevelExpose(..), ExposedType , exposesFunction, operators - , encode, decoder ) {-| This syntax represents the exposing declaration for both imports and module headers. @@ -20,25 +19,17 @@ For example: @docs exposesFunction, operators - -## Serialization - -@docs encode, decoder - -} -import Elm.Json.Util exposing (decodeTyped, encodeTyped) -import Elm.Syntax.Node as Node exposing (Node(..)) -import Elm.Syntax.Range as Range exposing (Range) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.Node exposing (Node(..)) +import Elm.Syntax.Range exposing (Range) {-| Different kind of exposing declarations -} type Exposing = All Range - | Explicit (List (Node TopLevelExpose)) + | Explicit (Node TopLevelExpose) (List (Node TopLevelExpose)) {-| An exposed entity @@ -77,7 +68,7 @@ exposesFunction s exposure = All _ -> True - Explicit l -> + Explicit head rest -> List.any (\(Node _ value) -> case value of @@ -87,7 +78,7 @@ exposesFunction s exposure = _ -> False ) - l + (head :: rest) {-| Collect all operator names from a list of TopLevelExposes @@ -105,84 +96,3 @@ operator t = _ -> Nothing - - - --- Serialization - - -{-| Encode an `Exposing` syntax element to JSON. --} -encode : Exposing -> Value -encode exp = - case exp of - All r -> - encodeTyped "all" <| Range.encode r - - Explicit l -> - encodeTyped "explicit" (JE.list encodeTopLevelExpose l) - - -{-| JSON decoder for an `Exposing` syntax element. --} -decoder : Decoder Exposing -decoder = - decodeTyped - [ ( "all", Range.decoder |> JD.map All ) - , ( "explicit", JD.list topLevelExposeDecoder |> JD.map Explicit ) - ] - - -encodeTopLevelExpose : Node TopLevelExpose -> Value -encodeTopLevelExpose = - Node.encode - (\exp -> - case exp of - InfixExpose x -> - encodeTyped "infix" <| - JE.object - [ ( "name", JE.string x ) - ] - - FunctionExpose x -> - encodeTyped "function" <| - JE.object - [ ( "name", JE.string x ) - ] - - TypeOrAliasExpose x -> - encodeTyped "typeOrAlias" <| - JE.object - [ ( "name", JE.string x ) - ] - - TypeExpose exposedType -> - encodeTyped "typeexpose" (encodeExposedType exposedType) - ) - - -encodeExposedType : ExposedType -> Value -encodeExposedType { name, open } = - JE.object - [ ( "name", JE.string name ) - , ( "open", open |> Maybe.map Range.encode |> Maybe.withDefault JE.null ) - ] - - -topLevelExposeDecoder : Decoder (Node TopLevelExpose) -topLevelExposeDecoder = - Node.decoder - (decodeTyped - [ ( "infix", JD.map InfixExpose (JD.field "name" JD.string) ) - , ( "function", JD.map FunctionExpose (JD.field "name" JD.string) ) - , ( "typeOrAlias", JD.map TypeOrAliasExpose (JD.field "name" JD.string) ) - , ( "typeexpose", JD.map TypeExpose exposedTypeDecoder ) - ] - ) - - -exposedTypeDecoder : Decoder ExposedType -exposedTypeDecoder = - JD.map2 ExposedType - (JD.field "name" JD.string) - (JD.field "open" (JD.nullable Range.decoder)) diff --git a/src/Elm/Syntax/Expression.elm b/src/Elm/Syntax/Expression.elm index cb665f896..8471c046d 100644 --- a/src/Elm/Syntax/Expression.elm +++ b/src/Elm/Syntax/Expression.elm @@ -1,7 +1,6 @@ module Elm.Syntax.Expression exposing - ( Expression(..), Lambda, LetBlock, LetDeclaration(..), RecordSetter, CaseBlock, Cases, Case, Function, FunctionImplementation - , functionRange, isLambda, isLet, isIfElse, isCase, isOperatorApplication - , encode, encodeFunction, decoder, functionDecoder + ( Expression(..), Lambda, LetBlock, LetDeclaration(..), RecordSetter, CaseBlock, Case, Function, FunctionImplementation + , functionRange ) {-| This syntax represents all that you can express in Elm. @@ -10,30 +9,23 @@ Although it is a easy and simple language, you can express a lot! See the `Expre ## Types -@docs Expression, Lambda, LetBlock, LetDeclaration, RecordSetter, CaseBlock, Cases, Case, Function, FunctionImplementation +@docs Expression, Lambda, LetBlock, LetDeclaration, RecordSetter, CaseBlock, Case, Function, FunctionImplementation ## Functions -@docs functionRange, isLambda, isLet, isIfElse, isCase, isOperatorApplication - - -## Serialization - -@docs encode, encodeFunction, decoder, functionDecoder +@docs functionRange -} -import Elm.Json.Util exposing (decodeTyped, encodeTyped) -import Elm.Syntax.Documentation as Documentation exposing (Documentation) -import Elm.Syntax.Infix as Infix exposing (InfixDirection) -import Elm.Syntax.ModuleName as ModuleName exposing (ModuleName) +import Elm.Syntax.DestructurePattern exposing (DestructurePattern) +import Elm.Syntax.Documentation exposing (Documentation) +import Elm.Syntax.ModuleName exposing (ModuleName) import Elm.Syntax.Node as Node exposing (Node(..)) -import Elm.Syntax.Pattern as Pattern exposing (Pattern) +import Elm.Syntax.Pattern exposing (Pattern) import Elm.Syntax.Range exposing (Range) -import Elm.Syntax.Signature as Signature exposing (Signature) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.Signature exposing (Signature) +import Elm.Syntax.StringLiteralType exposing (StringLiteralType) {-| Type alias for a full function @@ -77,64 +69,58 @@ functionRange function = -} type alias FunctionImplementation = { name : Node String - , arguments : List (Node Pattern) + , arguments : List (Node DestructurePattern) , expression : Node Expression } {-| Custom type for all expressions such as: - - `Unit`: `()` - - `Application`: `add a b` - - `OperatorApplication`: `a + b` + - `StringLiteral`: `"text"` or `"""text"""` + - `CharLiteral`: `'a'` + - `IntegerLiteral`: `42` + - `HexLiteral`: `0x1F` + - `FloatLiteral`: `42.0` + - `Negation`: `-a` + - `ListLiteral`: `[ x, y ]` - `FunctionOrValue`: `add` or `True` - - `IfBlock`: `if a then b else c` + - `FunctionCall`: `add a b` + - `Operation`: `a + b` - `PrefixOperator`: `(+)` - - `Operator`: `+` (not possible to get in practice) - - `Integer`: `42` - - `Hex`: `0x1F` - - `Floatable`: `42.0` - - `Negation`: `-a` - - `Literal`: `"text"` - - `CharLiteral`: `'a'` - - `TupledExpression`: `(a, b)` or `(a, b, c)` - - `ParenthesizedExpression`: `(a)` - - `LetExpression`: `let a = 4 in a` - - `CaseExpression`: `case a of` followed by pattern matches + - `If`: `if a then b else c` + - `TupleExpression`: Something wrapped in parentheses like unit `()`, parentheses `(a)`, or a tuple `( a, b )` + - `Let`: `let a = 4 in a` + - `Case`: `case a of` followed by pattern matches - `LambdaExpression`: `(\a -> a)` - - `RecordExpr`: `{ name = "text" }` - - `ListExpr`: `[ x, y ]` + - `Record`: `{ name = "text" }` - `RecordAccess`: `a.name` - `RecordAccessFunction`: `.name` - - `RecordUpdateExpression`: `{ a | name = "text" }` - - `GLSLExpression`: `[glsl| ... |]` + - `RecordUpdate`: `{ a | name = "text" }` + - `GLSL`: `[glsl| ... |]` -} type Expression - = UnitExpr - | Application (List (Node Expression)) - | OperatorApplication String InfixDirection (Node Expression) (Node Expression) + = StringLiteral StringLiteralType String + | CharLiteral Char + | IntegerLiteral Int + | HexLiteral Int + | FloatLiteral Float + | Negation (Node Expression) + | ListLiteral (List (Node Expression)) | FunctionOrValue ModuleName String - | IfBlock (Node Expression) (Node Expression) (Node Expression) | PrefixOperator String - | Operator String - | Integer Int - | Hex Int - | Floatable Float - | Negation (Node Expression) - | Literal String - | CharLiteral Char - | TupledExpression (List (Node Expression)) - | ParenthesizedExpression (Node Expression) - | LetExpression LetBlock - | CaseExpression CaseBlock + | FunctionCall (Node Expression) (Node Expression) (List (Node Expression)) + | Operation String (Node Expression) (Node Expression) + | If (Node Expression) (Node Expression) (Node Expression) + | TupleExpression (List (Node Expression)) + | Let LetBlock + | Case CaseBlock | LambdaExpression Lambda - | RecordExpr (List (Node RecordSetter)) - | ListExpr (List (Node Expression)) + | Record (List (Node RecordSetter)) | RecordAccess (Node Expression) (Node String) | RecordAccessFunction String - | RecordUpdateExpression (Node String) (List (Node RecordSetter)) - | GLSLExpression String + | RecordUpdate (Node String) (Node RecordSetter) (List (Node RecordSetter)) + | GLSL String {-| Expression for setting a record field @@ -155,13 +141,14 @@ type alias LetBlock = -} type LetDeclaration = LetFunction Function - | LetDestructuring (Node Pattern) (Node Expression) + | LetDestructuring (Node DestructurePattern) (Node Expression) {-| Expression for a lambda -} type alias Lambda = - { args : List (Node Pattern) + { firstArg : Node DestructurePattern + , restOfArgs : List (Node DestructurePattern) , expression : Node Expression } @@ -170,7 +157,8 @@ type alias Lambda = -} type alias CaseBlock = { expression : Node Expression - , cases : Cases + , firstCase : Case + , restOfCases : List Case } @@ -178,418 +166,3 @@ type alias CaseBlock = -} type alias Case = ( Node Pattern, Node Expression ) - - -{-| Type alias for a list of cases --} -type alias Cases = - List Case - - -{-| Check whether an expression is a lambda-expression --} -isLambda : Expression -> Bool -isLambda e = - case e of - LambdaExpression _ -> - True - - _ -> - False - - -{-| Check whether an expression is a let-expression --} -isLet : Expression -> Bool -isLet e = - case e of - LetExpression _ -> - True - - _ -> - False - - -{-| Check whether an expression is an if-else-expression --} -isIfElse : Expression -> Bool -isIfElse e = - case e of - IfBlock _ _ _ -> - True - - _ -> - False - - -{-| Check whether an expression is a case-expression --} -isCase : Expression -> Bool -isCase e = - case e of - CaseExpression _ -> - True - - _ -> - False - - -{-| Check whether an expression is an operator application expression --} -isOperatorApplication : Expression -> Bool -isOperatorApplication e = - case e of - OperatorApplication _ _ _ _ -> - True - - _ -> - False - - - --- Serialization - - -{-| Encode an `Expression` syntax element to JSON. --} -encode : Expression -> Value -encode expr = - case expr of - UnitExpr -> - encodeTyped "unit" JE.null - - Application l -> - encodeTyped "application" (JE.list (Node.encode encode) l) - - OperatorApplication op dir left right -> - encodeTyped "operatorapplication" (encodeOperatorApplication op dir left right) - - FunctionOrValue moduleName name -> - encodeTyped "functionOrValue" - (JE.object - [ ( "moduleName", ModuleName.encode moduleName ) - , ( "name", JE.string name ) - ] - ) - - IfBlock c t e -> - encodeTyped "ifBlock" <| - JE.object - [ ( "clause", Node.encode encode c ) - , ( "then", Node.encode encode t ) - , ( "else", Node.encode encode e ) - ] - - PrefixOperator x -> - encodeTyped "prefixoperator" (JE.string x) - - Operator x -> - encodeTyped "operator" (JE.string x) - - Hex h -> - encodeTyped "hex" (JE.int h) - - Integer x -> - encodeTyped "integer" (JE.int x) - - Floatable x -> - encodeTyped "float" (JE.float x) - - Negation x -> - encodeTyped "negation" (Node.encode encode x) - - Literal x -> - encodeTyped "literal" (JE.string x) - - CharLiteral c -> - encodeTyped "charLiteral" (JE.string <| String.fromChar c) - - TupledExpression xs -> - encodeTyped "tupled" (JE.list (Node.encode encode) xs) - - ListExpr xs -> - encodeTyped "list" (JE.list (Node.encode encode) xs) - - ParenthesizedExpression x -> - encodeTyped "parenthesized" (Node.encode encode x) - - LetExpression x -> - encodeTyped "let" <| encodeLetBlock x - - CaseExpression x -> - encodeTyped "case" <| encodeCaseBlock x - - LambdaExpression x -> - encodeTyped "lambda" <| encodeLambda x - - RecordAccess exp name -> - encodeTyped "recordAccess" <| - JE.object - [ ( "expression", Node.encode encode exp ) - , ( "name", Node.encode JE.string name ) - ] - - RecordAccessFunction x -> - encodeTyped "recordAccessFunction" (JE.string x) - - RecordExpr xs -> - encodeTyped "record" (JE.list (Node.encode encodeRecordSetter) xs) - - RecordUpdateExpression name updates -> - encodeTyped "recordUpdate" (encodeRecordUpdate name updates) - - GLSLExpression x -> - encodeTyped "glsl" (JE.string x) - - -encodeOperatorApplication : String -> InfixDirection -> Node Expression -> Node Expression -> Value -encodeOperatorApplication operator direction left right = - JE.object - [ ( "operator", JE.string operator ) - , ( "direction", Infix.encodeDirection direction ) - , ( "left", Node.encode encode left ) - , ( "right", Node.encode encode right ) - ] - - -encodeLetBlock : LetBlock -> Value -encodeLetBlock { declarations, expression } = - JE.object - [ ( "declarations", JE.list (Node.encode encodeLetDeclaration) declarations ) - , ( "expression", Node.encode encode expression ) - ] - - -encodeRecordUpdate : Node String -> List (Node RecordSetter) -> Value -encodeRecordUpdate name updates = - JE.object - [ ( "name", Node.encode JE.string name ) - , ( "updates", JE.list (Node.encode encodeRecordSetter) updates ) - ] - - -encodeRecordSetter : RecordSetter -> Value -encodeRecordSetter ( field, expression ) = - JE.object - [ ( "field", Node.encode JE.string field ) - , ( "expression", Node.encode encode expression ) - ] - - -encodeLetDeclaration : LetDeclaration -> Value -encodeLetDeclaration letDeclaration = - case letDeclaration of - LetFunction f -> - encodeTyped "function" (encodeFunction f) - - LetDestructuring pattern expression -> - encodeTyped "destructuring" (encodeDestructuring pattern expression) - - -{-| Encode a `Function` syntax element to JSON. --} -encodeFunction : Function -> Value -encodeFunction { documentation, signature, declaration } = - JE.object - [ ( "documentation", Maybe.map (Node.encode Documentation.encode) documentation |> Maybe.withDefault JE.null ) - , ( "signature", Maybe.map (Node.encode Signature.encode) signature |> Maybe.withDefault JE.null ) - , ( "declaration", Node.encode encodeFunctionDeclaration declaration ) - ] - - -encodeFunctionDeclaration : FunctionImplementation -> Value -encodeFunctionDeclaration { name, arguments, expression } = - JE.object - [ ( "name", Node.encode JE.string name ) - , ( "arguments", JE.list (Node.encode Pattern.encode) arguments ) - , ( "expression", Node.encode encode expression ) - ] - - -encodeDestructuring : Node Pattern -> Node Expression -> Value -encodeDestructuring pattern expression = - JE.object - [ ( "pattern", Node.encode Pattern.encode pattern ) - , ( "expression", Node.encode encode expression ) - ] - - -encodeCaseBlock : CaseBlock -> Value -encodeCaseBlock { expression, cases } = - JE.object - [ ( "cases", JE.list encodeCase cases ) - , ( "expression", Node.encode encode expression ) - ] - - -encodeCase : Case -> Value -encodeCase ( pattern, expression ) = - JE.object - [ ( "pattern", Node.encode Pattern.encode pattern ) - , ( "expression", Node.encode encode expression ) - ] - - -encodeLambda : Lambda -> Value -encodeLambda { args, expression } = - JE.object - [ ( "patterns", JE.list (Node.encode Pattern.encode) args ) - , ( "expression", Node.encode encode expression ) - ] - - -decodeNested : Decoder (Node Expression) -decodeNested = - JD.lazy (\() -> Node.decoder decoder) - - -{-| JSON decoder for an `Expression` syntax element. --} -decoder : Decoder Expression -decoder = - JD.lazy - (\() -> - decodeTyped - [ ( "unit", JD.succeed UnitExpr ) - , ( "application", JD.list decodeNested |> JD.map Application ) - , ( "operatorapplication", decodeOperatorApplication ) - , ( "functionOrValue", JD.map2 FunctionOrValue (JD.field "moduleName" ModuleName.decoder) (JD.field "name" JD.string) ) - , ( "ifBlock", JD.map3 IfBlock (JD.field "clause" decodeNested) (JD.field "then" decodeNested) (JD.field "else" decodeNested) ) - , ( "prefixoperator", JD.string |> JD.map PrefixOperator ) - , ( "operator", JD.string |> JD.map Operator ) - , ( "hex", JD.int |> JD.map Hex ) - , ( "integer", JD.int |> JD.map Integer ) - , ( "float", JD.float |> JD.map Floatable ) - , ( "negation", decodeNested |> JD.map Negation ) - , ( "literal", JD.string |> JD.map Literal ) - , ( "charLiteral", decodeChar |> JD.map CharLiteral ) - , ( "tupled", JD.list decodeNested |> JD.map TupledExpression ) - , ( "list", JD.list decodeNested |> JD.map ListExpr ) - , ( "parenthesized", decodeNested |> JD.map ParenthesizedExpression ) - , ( "let", decodeLetBlock |> JD.map LetExpression ) - , ( "case", decodeCaseBlock |> JD.map CaseExpression ) - , ( "lambda", decodeLambda |> JD.map LambdaExpression ) - , ( "recordAccess", JD.map2 RecordAccess (JD.field "expression" decodeNested) (JD.field "name" (Node.decoder JD.string)) ) - , ( "recordAccessFunction", JD.string |> JD.map RecordAccessFunction ) - , ( "record", JD.list (Node.decoder decodeRecordSetter) |> JD.map RecordExpr ) - , ( "recordUpdate" - , JD.map2 RecordUpdateExpression - (JD.field "name" <| Node.decoder JD.string) - (JD.field "updates" (JD.list <| Node.decoder decodeRecordSetter)) - ) - , ( "glsl", JD.string |> JD.map GLSLExpression ) - ] - ) - - -decodeRecordSetter : Decoder RecordSetter -decodeRecordSetter = - JD.lazy - (\() -> - JD.map2 Tuple.pair - (JD.field "field" <| Node.decoder JD.string) - (JD.field "expression" decodeNested) - ) - - -decodeLambda : Decoder Lambda -decodeLambda = - JD.lazy - (\() -> - JD.map2 Lambda - (JD.field "patterns" (JD.list (Node.decoder Pattern.decoder))) - (JD.field "expression" decodeNested) - ) - - -decodeCaseBlock : Decoder CaseBlock -decodeCaseBlock = - JD.lazy - (\() -> - JD.map2 CaseBlock - (JD.field "expression" decodeNested) - (JD.field "cases" (JD.list decodeCase)) - ) - - -decodeCase : Decoder Case -decodeCase = - JD.lazy - (\() -> - JD.map2 Tuple.pair - (JD.field "pattern" (Node.decoder Pattern.decoder)) - (JD.field "expression" decodeNested) - ) - - -decodeLetBlock : Decoder LetBlock -decodeLetBlock = - JD.lazy - (\() -> - JD.map2 LetBlock - (JD.field "declarations" (JD.list decodeLetDeclaration)) - (JD.field "expression" decodeNested) - ) - - -decodeLetDeclaration : Decoder (Node LetDeclaration) -decodeLetDeclaration = - JD.lazy - (\() -> - Node.decoder - (decodeTyped - [ ( "function", JD.map LetFunction functionDecoder ) - , ( "destructuring", JD.map2 LetDestructuring (JD.field "pattern" (Node.decoder Pattern.decoder)) (JD.field "expression" decodeNested) ) - ] - ) - ) - - -decodeOperatorApplication : Decoder Expression -decodeOperatorApplication = - JD.lazy - (\() -> - JD.map4 OperatorApplication - (JD.field "operator" JD.string) - (JD.field "direction" Infix.decodeDirection) - (JD.field "left" decodeNested) - (JD.field "right" decodeNested) - ) - - -decodeChar : Decoder Char -decodeChar = - JD.string - |> JD.andThen - (\s -> - case String.uncons s of - Just ( c, _ ) -> - JD.succeed c - - Nothing -> - JD.fail "Not a char" - ) - - -{-| JSON decoder for an `Function` syntax element. --} -functionDecoder : Decoder Function -functionDecoder = - JD.lazy - (\() -> - JD.map3 Function - (JD.field "documentation" (JD.nullable <| Node.decoder Documentation.decoder)) - (JD.field "signature" (JD.nullable (Node.decoder Signature.decoder))) - (JD.field "declaration" (Node.decoder decodeFunctionDeclaration)) - ) - - -decodeFunctionDeclaration : Decoder FunctionImplementation -decodeFunctionDeclaration = - JD.lazy - (\() -> - JD.map3 FunctionImplementation - (JD.field "name" (Node.decoder JD.string)) - (JD.field "arguments" (JD.list (Node.decoder Pattern.decoder))) - (JD.field "expression" decodeNested) - ) diff --git a/src/Elm/Syntax/File.elm b/src/Elm/Syntax/File.elm index 0fa8ffb7d..8ec405d17 100644 --- a/src/Elm/Syntax/File.elm +++ b/src/Elm/Syntax/File.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.File exposing - ( File - , encode, decoder - ) +module Elm.Syntax.File exposing (File) {-| This syntax represents a whole Elm file. @@ -10,20 +7,13 @@ module Elm.Syntax.File exposing @docs File - -## Serialization - -@docs encode, decoder - -} -import Elm.Syntax.Comments as Comments exposing (Comment) -import Elm.Syntax.Declaration as Declaration exposing (Declaration) -import Elm.Syntax.Import as Import exposing (Import) -import Elm.Syntax.Module as Module exposing (Module) -import Elm.Syntax.Node as Node exposing (Node) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.Comments exposing (Comment) +import Elm.Syntax.Declaration exposing (Declaration) +import Elm.Syntax.Import exposing (Import) +import Elm.Syntax.Module exposing (Module) +import Elm.Syntax.Node exposing (Node) {-| Type annotation for a file @@ -34,26 +24,3 @@ type alias File = , declarations : List (Node Declaration) , comments : List (Node Comment) } - - -{-| Encode a `File` syntax element to JSON. --} -encode : File -> Value -encode { moduleDefinition, imports, declarations, comments } = - JE.object - [ ( "moduleDefinition", Node.encode Module.encode moduleDefinition ) - , ( "imports", JE.list (Node.encode Import.encode) imports ) - , ( "declarations", JE.list (Node.encode Declaration.encode) declarations ) - , ( "comments", JE.list (Node.encode Comments.encode) comments ) - ] - - -{-| JSON decoder for a `File` syntax element. --} -decoder : Decoder File -decoder = - JD.map4 File - (JD.field "moduleDefinition" (Node.decoder Module.decoder)) - (JD.field "imports" (JD.list (Node.decoder Import.decoder))) - (JD.field "declarations" (JD.list (Node.decoder Declaration.decoder))) - (JD.field "comments" (JD.list (Node.decoder Comments.decoder))) diff --git a/src/Elm/Syntax/Import.elm b/src/Elm/Syntax/Import.elm index 67cbc101e..9456de941 100644 --- a/src/Elm/Syntax/Import.elm +++ b/src/Elm/Syntax/Import.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.Import exposing - ( Import - , encode, decoder - ) +module Elm.Syntax.Import exposing (Import) {-| This syntax represents imports in Elm. For example: @@ -13,18 +10,11 @@ For example: @docs Import - -## Serialization - -@docs encode, decoder - -} -import Elm.Syntax.Exposing as Exposing exposing (Exposing) -import Elm.Syntax.ModuleName as ModuleName exposing (ModuleName) -import Elm.Syntax.Node as Node exposing (Node) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.Exposing exposing (Exposing) +import Elm.Syntax.ModuleName exposing (ModuleName) +import Elm.Syntax.Node exposing (Node) {-| Type alias representing an Import @@ -34,32 +24,3 @@ type alias Import = , moduleAlias : Maybe (Node ModuleName) , exposingList : Maybe (Node Exposing) } - - -{-| Encode a `Import` syntax element to JSON. --} -encode : Import -> Value -encode { moduleName, moduleAlias, exposingList } = - JE.object - [ ( "moduleName", Node.encode ModuleName.encode moduleName ) - , ( "moduleAlias" - , moduleAlias - |> Maybe.map (Node.encode ModuleName.encode) - |> Maybe.withDefault JE.null - ) - , ( "exposingList" - , exposingList - |> Maybe.map (Node.encode Exposing.encode) - |> Maybe.withDefault JE.null - ) - ] - - -{-| JSON decoder for a `Import` syntax element. --} -decoder : Decoder Import -decoder = - JD.map3 Import - (JD.field "moduleName" <| Node.decoder ModuleName.decoder) - (JD.field "moduleAlias" (JD.nullable <| Node.decoder ModuleName.decoder)) - (JD.field "exposingList" (JD.nullable <| Node.decoder Exposing.decoder)) diff --git a/src/Elm/Syntax/Infix.elm b/src/Elm/Syntax/Infix.elm index 6bdbf5808..7a6c97a29 100644 --- a/src/Elm/Syntax/Infix.elm +++ b/src/Elm/Syntax/Infix.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.Infix exposing - ( Infix, InfixDirection(..) - , encode, encodeDirection, decoder, decodeDirection - ) +module Elm.Syntax.Infix exposing (Infix, InfixDirection(..)) {-| @@ -10,16 +7,9 @@ module Elm.Syntax.Infix exposing @docs Infix, InfixDirection - -## Serialization - -@docs encode, encodeDirection, decoder, decodeDirection - -} -import Elm.Syntax.Node as Node exposing (Node) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.Node exposing (Node) {-| Type annotation for a infix definition @@ -38,63 +28,3 @@ type InfixDirection = Left | Right | Non - - -{-| Encode an infix --} -encode : Infix -> Value -encode inf = - JE.object - [ ( "direction", Node.encode encodeDirection inf.direction ) - , ( "precedence", Node.encode JE.int inf.precedence ) - , ( "operator", Node.encode JE.string inf.operator ) - , ( "function", Node.encode JE.string inf.function ) - ] - - -{-| Encode the infix direction --} -encodeDirection : InfixDirection -> Value -encodeDirection d = - case d of - Left -> - JE.string "left" - - Right -> - JE.string "right" - - Non -> - JE.string "non" - - -{-| Decode an infix --} -decoder : Decoder Infix -decoder = - JD.map4 Infix - (JD.field "direction" (Node.decoder decodeDirection)) - (JD.field "precedence" (Node.decoder JD.int)) - (JD.field "operator" (Node.decoder JD.string)) - (JD.field "function" (Node.decoder JD.string)) - - -{-| Decode a infix direction --} -decodeDirection : Decoder InfixDirection -decodeDirection = - JD.string - |> JD.andThen - (\v -> - case v of - "left" -> - JD.succeed Left - - "right" -> - JD.succeed Right - - "non" -> - JD.succeed Non - - _ -> - JD.fail "Invalid direction" - ) diff --git a/src/Elm/Syntax/Module.elm b/src/Elm/Syntax/Module.elm index cf54bbc7f..e64858211 100644 --- a/src/Elm/Syntax/Module.elm +++ b/src/Elm/Syntax/Module.elm @@ -1,7 +1,6 @@ module Elm.Syntax.Module exposing ( Module(..), DefaultModuleData, EffectModuleData , exposingList, moduleName, isPortModule, isEffectModule - , encode, decoder ) {-| This syntax represents module definitions in Elm. @@ -16,19 +15,11 @@ For example: @docs exposingList, moduleName, isPortModule, isEffectModule - -## Serialization - -@docs encode, decoder - -} -import Elm.Json.Util exposing (decodeTyped, encodeTyped) -import Elm.Syntax.Exposing as Exposing exposing (Exposing) -import Elm.Syntax.ModuleName as ModuleName exposing (ModuleName) +import Elm.Syntax.Exposing exposing (Exposing) +import Elm.Syntax.ModuleName exposing (ModuleName) import Elm.Syntax.Node as Node exposing (Node) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) {-| Union type for different kind of modules @@ -112,64 +103,4 @@ isEffectModule m = --- Serialization - - -{-| Encode a `Module` syntax element to JSON. --} -encode : Module -> Value -encode m = - case m of - NormalModule d -> - encodeTyped "normal" (encodeDefaultModuleData d) - - PortModule d -> - encodeTyped "port" (encodeDefaultModuleData d) - - EffectModule d -> - encodeTyped "effect" (encodeEffectModuleData d) - - -encodeEffectModuleData : EffectModuleData -> Value -encodeEffectModuleData moduleData = - JE.object - [ ( "moduleName", Node.encode ModuleName.encode moduleData.moduleName ) - , ( "exposingList", Node.encode Exposing.encode moduleData.exposingList ) - , ( "command", moduleData.command |> Maybe.map (Node.encode JE.string) |> Maybe.withDefault JE.null ) - , ( "subscription", moduleData.subscription |> Maybe.map (Node.encode JE.string) |> Maybe.withDefault JE.null ) - ] - - -encodeDefaultModuleData : DefaultModuleData -> Value -encodeDefaultModuleData moduleData = - JE.object - [ ( "moduleName", Node.encode ModuleName.encode moduleData.moduleName ) - , ( "exposingList", Node.encode Exposing.encode moduleData.exposingList ) - ] - - -{-| JSON decoder for a `Module` syntax element. --} -decoder : Decoder Module -decoder = - decodeTyped - [ ( "normal", decodeDefaultModuleData |> JD.map NormalModule ) - , ( "port", decodeDefaultModuleData |> JD.map PortModule ) - , ( "effect", decodeEffectModuleData |> JD.map EffectModule ) - ] - - -decodeDefaultModuleData : Decoder DefaultModuleData -decodeDefaultModuleData = - JD.map2 DefaultModuleData - (JD.field "moduleName" <| Node.decoder ModuleName.decoder) - (JD.field "exposingList" <| Node.decoder Exposing.decoder) - - -decodeEffectModuleData : Decoder EffectModuleData -decodeEffectModuleData = - JD.map4 EffectModuleData - (JD.field "moduleName" <| Node.decoder ModuleName.decoder) - (JD.field "exposingList" <| Node.decoder Exposing.decoder) - (JD.field "command" (JD.nullable <| Node.decoder JD.string)) - (JD.field "subscription" (JD.nullable <| Node.decoder JD.string)) +-- diff --git a/src/Elm/Syntax/ModuleName.elm b/src/Elm/Syntax/ModuleName.elm index f006134f5..8eb2bfa5d 100644 --- a/src/Elm/Syntax/ModuleName.elm +++ b/src/Elm/Syntax/ModuleName.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.ModuleName exposing - ( ModuleName - , encode, decoder - ) +module Elm.Syntax.ModuleName exposing (ModuleName) {-| This syntax represents the module names in Elm. These can be used for imports, module names (duh), and for qualified access. For example: @@ -21,16 +18,8 @@ For example: @docs ModuleName - -## Serialization - -@docs encode, decoder - -} -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) - {-| Base representation for a module name -} @@ -39,18 +28,4 @@ type alias ModuleName = --- Serialization - - -{-| Encode a `ModuleName` syntax element to JSON. --} -encode : ModuleName -> Value -encode = - JE.list JE.string - - -{-| JSON decoder for a `ModuleName` syntax element. --} -decoder : Decoder ModuleName -decoder = - JD.list JD.string +-- diff --git a/src/Elm/Syntax/Node.elm b/src/Elm/Syntax/Node.elm index a93bee11c..69ceef5e3 100644 --- a/src/Elm/Syntax/Node.elm +++ b/src/Elm/Syntax/Node.elm @@ -1,7 +1,6 @@ module Elm.Syntax.Node exposing ( Node(..) , empty, combine, range, value, map - , encode, decoder ) {-| Represents a `Node` of the AST (Abstract Syntax Tree). @@ -19,16 +18,9 @@ element of the tree was found. @docs empty, combine, range, value, map - -## Serialization - -@docs encode, decoder - -} import Elm.Syntax.Range as Range exposing (Range) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) {-| Base representation for a syntax node in a source file. @@ -72,22 +64,3 @@ range (Node r _) = value : Node a -> a value (Node _ v) = v - - -{-| Encode a `Node` into JSON --} -encode : (a -> Value) -> Node a -> Value -encode f (Node r v) = - JE.object - [ ( "range", Range.encode r ) - , ( "value", f v ) - ] - - -{-| A JSON decoder for `Node` --} -decoder : Decoder a -> Decoder (Node a) -decoder sub = - JD.map2 Node - (JD.field "range" Range.decoder) - (JD.field "value" sub) diff --git a/src/Elm/Syntax/Pattern.elm b/src/Elm/Syntax/Pattern.elm index 2843adfe2..ab02f1c8e 100644 --- a/src/Elm/Syntax/Pattern.elm +++ b/src/Elm/Syntax/Pattern.elm @@ -1,7 +1,6 @@ module Elm.Syntax.Pattern exposing ( Pattern(..), QualifiedNameRef , moduleNames - , encode, decoder ) {-| This syntax represents the patterns. @@ -20,18 +19,11 @@ For example: @docs moduleNames - -## Serialization - -@docs encode, decoder - -} -import Elm.Json.Util exposing (decodeTyped, encodeTyped) -import Elm.Syntax.ModuleName as ModuleName exposing (ModuleName) -import Elm.Syntax.Node as Node exposing (Node(..)) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.ModuleName exposing (ModuleName) +import Elm.Syntax.Node exposing (Node(..)) +import Elm.Syntax.StringLiteralType exposing (StringLiteralType) {-| Custom type for all patterns such as: @@ -57,10 +49,9 @@ type Pattern = AllPattern | UnitPattern | CharPattern Char - | StringPattern String + | StringPattern StringLiteralType String | IntPattern Int | HexPattern Int - | FloatPattern Float | TuplePattern (List (Node Pattern)) | RecordPattern (List (Node String)) | UnConsPattern (Node Pattern) (Node Pattern) @@ -110,163 +101,3 @@ moduleNames p = _ -> [] - - - --- Serialization - - -{-| Encode a `Pattern` syntax element to JSON. --} -encode : Pattern -> Value -encode pattern = - case pattern of - AllPattern -> - encodeTyped "all" (JE.object []) - - UnitPattern -> - encodeTyped "unit" (JE.object []) - - CharPattern c -> - encodeTyped "char" - (JE.object - [ ( "value", JE.string <| String.fromChar c ) - ] - ) - - StringPattern v -> - encodeTyped "string" - (JE.object - [ ( "value", JE.string v ) - ] - ) - - HexPattern h -> - encodeTyped "hex" - (JE.object - [ ( "value", JE.int h ) - ] - ) - - IntPattern i -> - encodeTyped "int" - (JE.object - [ ( "value", JE.int i ) - ] - ) - - FloatPattern f -> - encodeTyped "float" - (JE.object - [ ( "value", JE.float f ) - ] - ) - - TuplePattern patterns -> - encodeTyped "tuple" - (JE.object - [ ( "value", JE.list (Node.encode encode) patterns ) - ] - ) - - RecordPattern pointers -> - encodeTyped "record" - (JE.object - [ ( "value", JE.list (Node.encode JE.string) pointers ) - ] - ) - - UnConsPattern p1 p2 -> - encodeTyped "uncons" - (JE.object - [ ( "left", Node.encode encode p1 ) - , ( "right", Node.encode encode p2 ) - ] - ) - - ListPattern patterns -> - encodeTyped "list" - (JE.object - [ ( "value", JE.list (Node.encode encode) patterns ) - ] - ) - - VarPattern name -> - encodeTyped "var" - (JE.object - [ ( "value", JE.string name ) - ] - ) - - NamedPattern qualifiedNameRef patterns -> - encodeTyped "named" <| - JE.object - [ ( "qualified" - , JE.object - [ ( "moduleName", ModuleName.encode qualifiedNameRef.moduleName ) - , ( "name", JE.string qualifiedNameRef.name ) - ] - ) - , ( "patterns", JE.list (Node.encode encode) patterns ) - ] - - AsPattern destructured name -> - encodeTyped "as" <| - JE.object - [ ( "name", Node.encode JE.string name ) - , ( "pattern", Node.encode encode destructured ) - ] - - ParenthesizedPattern p1 -> - encodeTyped "parentisized" - (JE.object - [ ( "value", Node.encode encode p1 ) - ] - ) - - -{-| JSON decoder for a `Pattern` syntax element. --} -decoder : Decoder Pattern -decoder = - JD.lazy - (\() -> - decodeTyped - [ ( "all", JD.succeed AllPattern ) - , ( "unit", JD.succeed UnitPattern ) - , ( "char", JD.field "value" decodeChar |> JD.map CharPattern ) - , ( "string", JD.field "value" JD.string |> JD.map StringPattern ) - , ( "hex", JD.int |> JD.map HexPattern ) - , ( "int", JD.field "value" JD.int |> JD.map IntPattern ) - , ( "float", JD.field "value" JD.float |> JD.map FloatPattern ) - , ( "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 ) - , ( "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))) ) - , ( "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)) ) - ] - ) - - -decodeQualifiedNameRef : Decoder QualifiedNameRef -decodeQualifiedNameRef = - JD.map2 QualifiedNameRef - (JD.field "moduleName" ModuleName.decoder) - (JD.field "name" JD.string) - - -decodeChar : Decoder Char -decodeChar = - JD.string - |> JD.andThen - (\s -> - case String.uncons s of - Just ( c, _ ) -> - JD.succeed c - - Nothing -> - JD.fail "Not a char" - ) diff --git a/src/Elm/Syntax/Port.elm b/src/Elm/Syntax/Port.elm new file mode 100644 index 000000000..da56fb815 --- /dev/null +++ b/src/Elm/Syntax/Port.elm @@ -0,0 +1,32 @@ +module Elm.Syntax.Port exposing (Port) + +{-| This syntax represents ports. +For example: + + {-| This is a port. + -} + port send : String -> Cmd msg + + {-| This is another port. + -} + port scroll : (Move -> msg) -> Sub msg + + +## Types + +@docs Port + +-} + +import Elm.Syntax.Documentation exposing (Documentation) +import Elm.Syntax.Node exposing (Node) +import Elm.Syntax.Signature exposing (Signature) + + +{-| Information associated with a Port. +A bit meta, but you get the idea. All information that you can define in a type alias is embedded. +-} +type alias Port = + { documentation : Maybe (Node Documentation) + , signature : Node Signature + } diff --git a/src/Elm/Syntax/Range.elm b/src/Elm/Syntax/Range.elm index f65d767a5..5f91ef555 100644 --- a/src/Elm/Syntax/Range.elm +++ b/src/Elm/Syntax/Range.elm @@ -2,8 +2,6 @@ module Elm.Syntax.Range exposing ( Range, Location , empty, combine , compare, compareLocations - , encode, decoder - , emptyRange ) {-| @@ -25,21 +23,8 @@ See also [Basics.compare](https://package.elm-lang.org/packages/elm/core/latest/ @docs compare, compareLocations - -## Serialization - -@docs encode, decoder - - -## Deprecated - -@docs emptyRange - -} -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) - {-| Source location -} @@ -66,49 +51,6 @@ empty = } -{-| **@deprecated** Use [`empty`](#empty) instead. It does the same thing but the name is more Elm-y. - -Construct an empty range - --} -emptyRange : Range -emptyRange = - empty - - -{-| Encode a range --} -encode : Range -> Value -encode { start, end } = - JE.list JE.int - [ start.row - , start.column - , end.row - , end.column - ] - - -{-| Decode a range --} -decoder : Decoder Range -decoder = - JD.list JD.int - |> JD.andThen fromList - - -fromList : List Int -> Decoder Range -fromList input = - case input of - [ a, b, c, d ] -> - JD.succeed - { start = { row = a, column = b } - , end = { row = c, column = d } - } - - _ -> - JD.fail "Invalid input list" - - {-| Compute the largest area of a list of ranges. -} combine : List Range -> Range diff --git a/src/Elm/Syntax/Signature.elm b/src/Elm/Syntax/Signature.elm index cfff9e1f2..76ce36e4d 100644 --- a/src/Elm/Syntax/Signature.elm +++ b/src/Elm/Syntax/Signature.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.Signature exposing - ( Signature - , encode, decoder - ) +module Elm.Syntax.Signature exposing (Signature) {-| This syntax represents type signatures in Elm. @@ -14,17 +11,10 @@ For example : @docs Signature - -## Serialization - -@docs encode, decoder - -} -import Elm.Syntax.Node as Node exposing (Node) -import Elm.Syntax.TypeAnnotation as TypeAnnotation exposing (TypeAnnotation) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.Node exposing (Node) +import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation) {-| Type alias representing a signature in Elm. @@ -33,22 +23,3 @@ type alias Signature = { name : Node String , typeAnnotation : Node TypeAnnotation } - - -{-| Encode a `Signature` syntax element to JSON. --} -encode : Signature -> Value -encode { name, typeAnnotation } = - JE.object - [ ( "name", Node.encode JE.string name ) - , ( "typeAnnotation", Node.encode TypeAnnotation.encode typeAnnotation ) - ] - - -{-| JSON decoder for a `Signature` syntax element. --} -decoder : Decoder Signature -decoder = - JD.map2 Signature - (JD.field "name" (Node.decoder JD.string)) - (JD.field "typeAnnotation" (Node.decoder TypeAnnotation.decoder)) diff --git a/src/Elm/Syntax/StringLiteralType.elm b/src/Elm/Syntax/StringLiteralType.elm new file mode 100644 index 000000000..d84ec6357 --- /dev/null +++ b/src/Elm/Syntax/StringLiteralType.elm @@ -0,0 +1,14 @@ +module Elm.Syntax.StringLiteralType exposing (StringLiteralType(..)) + +{-| Information about quotes. + +@docs StringLiteralType + +-} + + +{-| Indicates whether a string literal is single (`"abc"`) or triple-quoted (`"""abc"""`). +-} +type StringLiteralType + = SingleQuote + | TripleQuote diff --git a/src/Elm/Syntax/Type.elm b/src/Elm/Syntax/Type.elm index bf1d16b40..2f0c5a89b 100644 --- a/src/Elm/Syntax/Type.elm +++ b/src/Elm/Syntax/Type.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.Type exposing - ( Type, ValueConstructor - , encode, decoder - ) +module Elm.Syntax.Type exposing (Type, ValueConstructor) {-| This syntax represents custom types. For example: @@ -17,18 +14,11 @@ For example: @docs Type, ValueConstructor - -## Serialization - -@docs encode, decoder - -} -import Elm.Syntax.Documentation as Documentation exposing (Documentation) -import Elm.Syntax.Node as Node exposing (Node) -import Elm.Syntax.TypeAnnotation as TypeAnnotation exposing (TypeAnnotation) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.Documentation exposing (Documentation) +import Elm.Syntax.Node exposing (Node) +import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation) {-| Type alias that defines the syntax for a custom type. @@ -38,7 +28,8 @@ type alias Type = { documentation : Maybe (Node Documentation) , name : Node String , generics : List (Node String) - , constructors : List (Node ValueConstructor) + , firstConstructor : Node ValueConstructor + , restOfConstructors : List (Node ValueConstructor) } @@ -48,45 +39,3 @@ type alias ValueConstructor = { name : Node String , arguments : List (Node TypeAnnotation) } - - - --- Serialization - - -{-| Encode a `Type` syntax element to JSON. --} -encode : Type -> Value -encode { documentation, name, generics, constructors } = - JE.object - [ ( "documentation", Maybe.map (Node.encode Documentation.encode) documentation |> Maybe.withDefault JE.null ) - , ( "name", Node.encode JE.string name ) - , ( "generics", JE.list (Node.encode JE.string) generics ) - , ( "constructors", JE.list (Node.encode encodeValueConstructor) constructors ) - ] - - -encodeValueConstructor : ValueConstructor -> Value -encodeValueConstructor { name, arguments } = - JE.object - [ ( "name", Node.encode JE.string name ) - , ( "arguments", JE.list (Node.encode TypeAnnotation.encode) arguments ) - ] - - -{-| JSON decoder for a `Type` syntax element. --} -decoder : Decoder Type -decoder = - JD.map4 Type - (JD.field "documentation" <| JD.nullable <| Node.decoder JD.string) - (JD.field "name" <| Node.decoder JD.string) - (JD.field "generics" (JD.list (Node.decoder JD.string))) - (JD.field "constructors" (JD.list (Node.decoder valueConstructorDecoder))) - - -valueConstructorDecoder : Decoder ValueConstructor -valueConstructorDecoder = - JD.map2 ValueConstructor - (JD.field "name" (Node.decoder JD.string)) - (JD.field "arguments" (JD.list (Node.decoder TypeAnnotation.decoder))) diff --git a/src/Elm/Syntax/TypeAlias.elm b/src/Elm/Syntax/TypeAlias.elm index 68df81b40..1f7f4aeae 100644 --- a/src/Elm/Syntax/TypeAlias.elm +++ b/src/Elm/Syntax/TypeAlias.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.TypeAlias exposing - ( TypeAlias - , encode, decoder - ) +module Elm.Syntax.TypeAlias exposing (TypeAlias) {-| This syntax represents type aliases. For example: @@ -18,18 +15,11 @@ For example: @docs TypeAlias - -## Serialization - -@docs encode, decoder - -} -import Elm.Syntax.Documentation as Documentation exposing (Documentation) -import Elm.Syntax.Node as Node exposing (Node) -import Elm.Syntax.TypeAnnotation as TypeAnnotation exposing (TypeAnnotation) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.Documentation exposing (Documentation) +import Elm.Syntax.Node exposing (Node) +import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation) {-| Type alias that defines the syntax for a type alias. @@ -44,27 +34,4 @@ type alias TypeAlias = --- Serialization - - -{-| Encode a `TypeAlias` syntax element to JSON. --} -encode : TypeAlias -> Value -encode { documentation, name, generics, typeAnnotation } = - JE.object - [ ( "documentation", Maybe.map (Node.encode Documentation.encode) documentation |> Maybe.withDefault JE.null ) - , ( "name", Node.encode JE.string name ) - , ( "generics", JE.list (Node.encode JE.string) generics ) - , ( "typeAnnotation", Node.encode TypeAnnotation.encode typeAnnotation ) - ] - - -{-| JSON decoder for a `Declaration` syntax element. --} -decoder : Decoder TypeAlias -decoder = - JD.map4 TypeAlias - (JD.field "documentation" (JD.nullable <| Node.decoder Documentation.decoder)) - (JD.field "name" <| Node.decoder JD.string) - (JD.field "generics" (JD.list <| Node.decoder JD.string)) - (JD.field "typeAnnotation" (Node.decoder TypeAnnotation.decoder)) +-- diff --git a/src/Elm/Syntax/TypeAnnotation.elm b/src/Elm/Syntax/TypeAnnotation.elm index 7d9cf2ab7..3a1a371aa 100644 --- a/src/Elm/Syntax/TypeAnnotation.elm +++ b/src/Elm/Syntax/TypeAnnotation.elm @@ -1,7 +1,4 @@ -module Elm.Syntax.TypeAnnotation exposing - ( TypeAnnotation(..), RecordDefinition, RecordField - , encode, decoder - ) +module Elm.Syntax.TypeAnnotation exposing (TypeAnnotation(..), RecordDefinition, RecordField) {-| This syntax represents the type annotation syntax. For example: @@ -13,36 +10,26 @@ For example: @docs TypeAnnotation, RecordDefinition, RecordField - -## Serialization - -@docs encode, decoder - -} -import Elm.Json.Util exposing (decodeTyped, encodeTyped) -import Elm.Syntax.ModuleName as ModuleName exposing (ModuleName) -import Elm.Syntax.Node as Node exposing (Node) -import Json.Decode as JD exposing (Decoder) -import Json.Encode as JE exposing (Value) +import Elm.Syntax.ModuleName exposing (ModuleName) +import Elm.Syntax.Node exposing (Node) {-| Custom type for different type annotations. For example: - - `GenericType`: `a` - - `Typed`: `Maybe (Int -> String)` - - `Unit`: `()` - - `Tuples`: `(a, b, c)` + - `Var`: `a` + - `Type`: `Maybe (Int -> String)` + - `Tuples`: `(a, b, c)` or Unit `()` - `Record`: `{ name : String}` - `GenericRecord`: `{ a | name : String}` - `FunctionTypeAnnotation`: `Int -> String` -} type TypeAnnotation - = GenericType String - | Typed (Node ( ModuleName, String )) (List (Node TypeAnnotation)) - | Unit - | Tupled (List (Node TypeAnnotation)) + = Var String + | Type (Node ( ModuleName, String )) (List (Node TypeAnnotation)) + | Tuple (List (Node TypeAnnotation)) | Record RecordDefinition | GenericRecord (Node String) (Node RecordDefinition) | FunctionTypeAnnotation (Node TypeAnnotation) (Node TypeAnnotation) @@ -58,133 +45,3 @@ type alias RecordDefinition = -} type alias RecordField = ( Node String, Node TypeAnnotation ) - - - --- Serialization - - -{-| Encode a `TypeAnnotation` syntax element to JSON. --} -encode : TypeAnnotation -> Value -encode typeAnnotation = - case typeAnnotation of - GenericType name -> - encodeTyped "generic" <| - JE.object - [ ( "value", JE.string name ) - ] - - Typed moduleNameAndName args -> - let - inner : ( ModuleName, String ) -> Value - inner ( mod, n ) = - JE.object - [ ( "moduleName", ModuleName.encode mod ) - , ( "name", JE.string n ) - ] - in - encodeTyped "typed" <| - JE.object - [ ( "moduleNameAndName", Node.encode inner moduleNameAndName ) - , ( "args", JE.list (Node.encode encode) args ) - ] - - Unit -> - encodeTyped "unit" (JE.object []) - - Tupled t -> - encodeTyped "tupled" <| - JE.object - [ ( "values", JE.list (Node.encode encode) t ) - ] - - FunctionTypeAnnotation left right -> - encodeTyped "function" <| - JE.object - [ ( "left", Node.encode encode left ) - , ( "right", Node.encode encode right ) - ] - - Record recordDefinition -> - encodeTyped "record" <| - JE.object - [ ( "value", encodeRecordDefinition recordDefinition ) - ] - - GenericRecord name recordDefinition -> - encodeTyped "genericRecord" <| - JE.object - [ ( "name", Node.encode JE.string name ) - , ( "values", Node.encode encodeRecordDefinition recordDefinition ) - ] - - -encodeRecordDefinition : RecordDefinition -> Value -encodeRecordDefinition = - JE.list (Node.encode encodeRecordField) - - -encodeRecordField : RecordField -> Value -encodeRecordField ( name, ref ) = - JE.object - [ ( "name", Node.encode JE.string name ) - , ( "typeAnnotation", Node.encode encode ref ) - ] - - -decodeModuleNameAndName : Decoder ( ModuleName, String ) -decodeModuleNameAndName = - JD.map2 Tuple.pair - (JD.field "moduleName" <| ModuleName.decoder) - (JD.field "name" <| JD.string) - - -{-| JSON decoder for a `TypeAnnotation` syntax element. --} -decoder : Decoder TypeAnnotation -decoder = - JD.lazy - (\() -> - decodeTyped - [ ( "generic", JD.map GenericType (JD.field "value" JD.string) ) - , ( "typed" - , JD.map2 Typed - (JD.field "moduleNameAndName" <| Node.decoder decodeModuleNameAndName) - (JD.field "args" (JD.list nestedDecoder)) - ) - , ( "unit", JD.succeed Unit ) - , ( "tupled", JD.map Tupled (JD.field "values" (JD.list nestedDecoder)) ) - , ( "function" - , JD.map2 FunctionTypeAnnotation - (JD.field "left" nestedDecoder) - (JD.field "right" nestedDecoder) - ) - , ( "record", JD.map Record (JD.field "value" recordDefinitionDecoder) ) - , ( "genericRecord" - , JD.map2 GenericRecord - (JD.field "name" <| Node.decoder JD.string) - (JD.field "values" <| Node.decoder recordDefinitionDecoder) - ) - ] - ) - - -nestedDecoder : Decoder (Node TypeAnnotation) -nestedDecoder = - JD.lazy (\() -> Node.decoder decoder) - - -recordDefinitionDecoder : Decoder RecordDefinition -recordDefinitionDecoder = - JD.lazy (\() -> JD.list <| Node.decoder recordFieldDecoder) - - -recordFieldDecoder : Decoder RecordField -recordFieldDecoder = - JD.lazy - (\() -> - JD.map2 Tuple.pair - (JD.field "name" <| Node.decoder JD.string) - (JD.field "typeAnnotation" nestedDecoder) - ) diff --git a/src/Elm/Writer.elm b/src/Elm/Writer.elm deleted file mode 100644 index 417ae9762..000000000 --- a/src/Elm/Writer.elm +++ /dev/null @@ -1,685 +0,0 @@ -module Elm.Writer exposing (write, writeFile, writePattern, writeExpression, writeTypeAnnotation, writeDeclaration) - -{-| Write a file to a string. - -**DEPRECATED:** In practice the writer is not very good, and this functionality will very likely be removed in the next major version. -We highly recommend using [the-sett/elm-syntax-dsl](https://package.elm-lang.org/packages/the-sett/elm-syntax-dsl/latest/) instead. - -@docs write, writeFile, writePattern, writeExpression, writeTypeAnnotation, writeDeclaration - --} - -import Elm.Syntax.Declaration exposing (..) -import Elm.Syntax.Documentation exposing (..) -import Elm.Syntax.Exposing exposing (..) -import Elm.Syntax.Expression exposing (..) -import Elm.Syntax.File exposing (..) -import Elm.Syntax.Import exposing (Import) -import Elm.Syntax.Infix exposing (..) -import Elm.Syntax.Module exposing (..) -import Elm.Syntax.ModuleName exposing (..) -import Elm.Syntax.Node as Node exposing (Node(..)) -import Elm.Syntax.Pattern exposing (..) -import Elm.Syntax.Range exposing (Range) -import Elm.Syntax.Signature exposing (Signature) -import Elm.Syntax.Type exposing (..) -import Elm.Syntax.TypeAlias exposing (..) -import Elm.Syntax.TypeAnnotation exposing (..) -import Hex -import List.Extra -import StructuredWriter as Writer exposing (..) - - -{-| Transform a writer to a string --} -write : Writer -> String -write = - Writer.write - - -{-| Write a file --} -writeFile : File -> Writer -writeFile file = - breaked - [ writeModule <| Node.value file.moduleDefinition - , breaked (List.map (Node.value >> writeImport) file.imports) - , breaked (List.map writeDeclaration file.declarations) - ] - - -writeModule : Module -> Writer -writeModule m = - case m of - NormalModule defaultModuleData -> - writeDefaultModuleData defaultModuleData - - PortModule defaultModuleData -> - spaced - [ string "port" - , writeDefaultModuleData defaultModuleData - ] - - EffectModule effectModuleData -> - writeEffectModuleData effectModuleData - - -writeDefaultModuleData : DefaultModuleData -> Writer -writeDefaultModuleData { moduleName, exposingList } = - spaced - [ string "module" - , writeModuleName <| Node.value moduleName - , writeExposureExpose <| Node.value exposingList - ] - - -writeEffectModuleData : EffectModuleData -> Writer -writeEffectModuleData { moduleName, exposingList, command, subscription } = - spaced - [ string "effect" - , string "module" - , writeModuleName <| Node.value moduleName - , writeWhere ( command, subscription ) - , writeExposureExpose <| Node.value exposingList - ] - - -writeWhere : ( Maybe (Node String), Maybe (Node String) ) -> Writer -writeWhere input = - case input of - ( Nothing, Nothing ) -> - epsilon - - ( Just x, Nothing ) -> - spaced - [ string "where { command =" - , string <| Node.value x - , string "}" - ] - - ( Nothing, Just x ) -> - spaced - [ string "where { subscription =" - , string <| Node.value x - , string "}" - ] - - ( Just x, Just y ) -> - spaced - [ string "where { command =" - , string <| Node.value x - , string ", subscription =" - , string <| Node.value y - , string "}" - ] - - -writeModuleName : ModuleName -> Writer -writeModuleName moduleName = - string (String.join "." moduleName) - - -writeExposureExpose : Exposing -> Writer -writeExposureExpose x = - case x of - All _ -> - string "exposing (..)" - - Explicit exposeList -> - let - diffLines : Bool - diffLines = - List.map Node.range exposeList - |> startOnDifferentLines - in - spaced - [ string "exposing" - , parensComma diffLines (List.map writeExpose exposeList) - ] - - -writeExpose : Node TopLevelExpose -> Writer -writeExpose (Node _ exp) = - case exp of - InfixExpose x -> - string ("(" ++ x ++ ")") - - FunctionExpose f -> - string f - - TypeOrAliasExpose t -> - string t - - TypeExpose { name, open } -> - case open of - Just _ -> - spaced - [ string name - , string "(..)" - ] - - Nothing -> - string name - - -startOnDifferentLines : List Range -> Bool -startOnDifferentLines xs = - List.length (List.Extra.unique (List.map (.start >> .row) xs)) > 1 - - -writeImport : Import -> Writer -writeImport { moduleName, moduleAlias, exposingList } = - spaced - [ string "import" - , writeModuleName <| Node.value moduleName - , maybe (Maybe.map (Node.value >> writeModuleName >> (\x -> spaced [ string "as", x ])) moduleAlias) - , maybe (Maybe.map (Node.value >> writeExposureExpose) exposingList) - ] - - -writeLetDeclaration : Node LetDeclaration -> Writer -writeLetDeclaration (Node _ letDeclaration) = - case letDeclaration of - LetFunction function -> - writeFunction function - - LetDestructuring pattern expression -> - writeDestructuring pattern expression - - -{-| Write a declaration --} -writeDeclaration : Node Declaration -> Writer -writeDeclaration (Node _ decl) = - case decl of - FunctionDeclaration function -> - writeFunction function - - AliasDeclaration typeAlias -> - writeTypeAlias typeAlias - - CustomTypeDeclaration type_ -> - writeType type_ - - PortDeclaration p -> - writePortDeclaration p - - InfixDeclaration i -> - writeInfix i - - Destructuring pattern expression -> - writeDestructuring pattern expression - - -writeFunction : Function -> Writer -writeFunction { documentation, signature, declaration } = - breaked - [ maybe (Maybe.map writeDocumentation documentation) - , maybe (Maybe.map (Node.value >> writeSignature) signature) - , writeFunctionImplementation <| Node.value declaration - ] - - -writeFunctionImplementation : FunctionImplementation -> Writer -writeFunctionImplementation declaration = - breaked - [ spaced - [ string <| Node.value declaration.name - , spaced (List.map writePattern declaration.arguments) - , string "=" - ] - , indent 4 (writeExpression declaration.expression) - ] - - -writeSignature : Signature -> Writer -writeSignature signature = - spaced - [ string <| Node.value signature.name - , string ":" - , writeTypeAnnotation signature.typeAnnotation - ] - - -writeDocumentation : Node Documentation -> Writer -writeDocumentation = - Node.value >> string - - -writeTypeAlias : TypeAlias -> Writer -writeTypeAlias typeAlias = - breaked - [ spaced - [ string "type alias" - , string <| Node.value typeAlias.name - , spaced (List.map (Node.value >> string) typeAlias.generics) - , string "=" - ] - , indent 4 (writeTypeAnnotation typeAlias.typeAnnotation) - ] - - -writeType : Type -> Writer -writeType type_ = - breaked - [ spaced - [ string "type" - , string <| Node.value type_.name - , spaced (List.map (Node.value >> string) type_.generics) - ] - , let - diffLines : Bool - diffLines = - List.map Node.range type_.constructors - |> startOnDifferentLines - in - indent 4 - (sepBy ( "= ", " | ", "" ) - diffLines - (List.map (Node.value >> writeValueConstructor) type_.constructors) - ) - ] - - -writeValueConstructor : ValueConstructor -> Writer -writeValueConstructor { name, arguments } = - spaced - ((string <| Node.value name) - :: List.map (wrapInSurroundingParentheses >> writeTypeAnnotation) arguments - ) - - -writePortDeclaration : Signature -> Writer -writePortDeclaration signature = - spaced [ string "port", writeSignature signature ] - - -writeInfix : Infix -> Writer -writeInfix { direction, precedence, operator, function } = - spaced - [ string "infix" - , case Node.value direction of - Left -> - string "left" - - Right -> - string "right" - - Non -> - string "non" - , string (String.fromInt (Node.value precedence)) - , string (Node.value operator) - , string "=" - , string (Node.value function) - ] - - -writeDestructuring : Node Pattern -> Node Expression -> Writer -writeDestructuring pattern expression = - breaked - [ spaced [ writePattern pattern, string "=" ] - , indent 4 (writeExpression expression) - ] - - -{-| Write a type annotation --} -writeTypeAnnotation : Node TypeAnnotation -> Writer -writeTypeAnnotation (Node _ typeAnnotation) = - case typeAnnotation of - GenericType s -> - string s - - Typed (Node _ ( moduleName, name )) args -> - spaced - ((string <| String.join "." (moduleName ++ [ name ])) - :: List.map (writeTypeAnnotation >> parensIfContainsSpaces) args - ) - - Unit -> - string "()" - - Tupled xs -> - parensComma False (List.map writeTypeAnnotation xs) - - Record xs -> - bracesComma False (List.map writeRecordField xs) - - GenericRecord name fields -> - spaced - [ string "{" - , string <| Node.value name - , string "|" - , sepByComma False (List.map writeRecordField <| Node.value fields) - , string "}" - ] - - FunctionTypeAnnotation left right -> - let - addParensForSubTypeAnnotation : Node TypeAnnotation -> Writer - addParensForSubTypeAnnotation type_ = - case type_ of - Node _ (FunctionTypeAnnotation _ _) -> - join [ string "(", writeTypeAnnotation type_, string ")" ] - - _ -> - writeTypeAnnotation type_ - in - spaced - [ addParensForSubTypeAnnotation left - , string "->" - , addParensForSubTypeAnnotation right - ] - - -writeRecordField : Node RecordField -> Writer -writeRecordField (Node _ ( name, ref )) = - spaced - [ string <| Node.value name - , string ":" - , writeTypeAnnotation ref - ] - - -{-| Writer an expression --} -writeExpression : Node Expression -> Writer -writeExpression (Node range inner) = - let - recurRangeHelper : Node Expression -> ( Range, Writer ) - recurRangeHelper (Node x y) = - ( x, writeExpression (Node x y) ) - - writeRecordSetter : RecordSetter -> ( Range, Writer ) - writeRecordSetter ( name, expr ) = - ( Node.range expr - , spaced [ string <| Node.value name, string "=", writeExpression expr ] - ) - - sepHelper : (Bool -> List Writer -> Writer) -> List ( Range, Writer ) -> Writer - sepHelper f l = - let - diffLines : Bool - diffLines = - List.map Tuple.first l - |> startOnDifferentLines - in - f diffLines (List.map Tuple.second l) - in - case inner of - UnitExpr -> - string "()" - - Application xs -> - case xs of - [] -> - epsilon - - [ x ] -> - writeExpression x - - x :: rest -> - spaced - [ writeExpression x - , sepHelper sepBySpace (List.map recurRangeHelper rest) - ] - - OperatorApplication x dir left right -> - case dir of - Left -> - sepHelper sepBySpace - [ ( Node.range left, writeExpression left ) - , ( range, spaced [ string x, writeExpression right ] ) - ] - - Right -> - sepHelper sepBySpace - [ ( Node.range left, spaced [ writeExpression left, string x ] ) - , ( Node.range right, writeExpression right ) - ] - - Non -> - sepHelper sepBySpace - [ ( Node.range left, spaced [ writeExpression left, string x ] ) - , ( Node.range right, writeExpression right ) - ] - - FunctionOrValue moduleName name -> - case moduleName of - [] -> - string name - - _ -> - join - [ writeModuleName <| moduleName - , string "." - , string <| name - ] - - IfBlock condition thenCase elseCase -> - breaked - [ spaced [ string "if", writeExpression condition, string "then" ] - , indent 2 (writeExpression thenCase) - , string "else" - , indent 2 (writeExpression elseCase) - ] - - PrefixOperator x -> - string ("(" ++ x ++ ")") - - Operator x -> - string x - - Hex h -> - join [ string "0x", string (Hex.toString h) ] - - Integer i -> - string (String.fromInt i) - - Floatable f -> - string (String.fromFloat f) - - Negation x -> - append (string "-") (writeExpression x) - - Literal s -> - string ("\"" ++ s ++ "\"") - - CharLiteral c -> - writeChar c - - TupledExpression t -> - join [ string "(", sepHelper sepByComma (List.map recurRangeHelper t), string ")" ] - - ParenthesizedExpression x -> - join [ string "(", writeExpression x, string ")" ] - - LetExpression letBlock -> - breaked - [ string "let" - , indent 2 (breaked (List.map writeLetDeclaration letBlock.declarations)) - , string "in" - , indent 2 (writeExpression letBlock.expression) - ] - - CaseExpression caseBlock -> - let - writeCaseBranch : ( Node Pattern, Node Expression ) -> Writer - writeCaseBranch ( pattern, expression ) = - indent 2 <| - breaked - [ spaced [ writePattern pattern, string "->" ] - , indent 2 (writeExpression expression) - ] - in - breaked - [ string "" - , spaced [ string "case", writeExpression caseBlock.expression, string "of" ] - , breaked (List.map writeCaseBranch caseBlock.cases) - , string "" - ] - - LambdaExpression lambda -> - spaced - [ join - [ string "\\" - , spaced (List.map writePattern lambda.args) - ] - , string "->" - , writeExpression lambda.expression - ] - - RecordExpr setters -> - sepHelper bracesComma (List.map (Node.value >> writeRecordSetter) setters) - - ListExpr xs -> - sepHelper bracketsComma (List.map recurRangeHelper xs) - - RecordAccess expression accessor -> - join [ writeExpression expression, string ".", string <| Node.value accessor ] - - RecordAccessFunction s -> - if String.startsWith "." s then - string s - - else - join [ string ".", string s ] - - RecordUpdateExpression name updates -> - spaced - [ string "{" - , string <| Node.value name - , string "|" - , sepHelper sepByComma (List.map (Node.value >> writeRecordSetter) updates) - , string "}" - ] - - GLSLExpression s -> - join - [ string "[glsl|" - , string s - , string "|]" - ] - - -escapeString : String -> String -escapeString = - String.replace "\"" "\\\"" - - -writeChar : Char -> Writer -writeChar c = - let - escape : String - escape = - if c == '\t' || c == '\'' || c == '\\' then - "\\" - - else - "" - in - string ("'" ++ escape ++ String.fromChar c ++ "'") - - -{-| Write a pattern --} -writePattern : Node Pattern -> Writer -writePattern (Node _ p) = - case p of - AllPattern -> - string "_" - - UnitPattern -> - string "()" - - CharPattern c -> - writeChar c - - StringPattern s -> - string ("\"" ++ escapeString s ++ "\"") - - HexPattern h -> - join [ string "0x", string (Hex.toString h) ] - - IntPattern i -> - string (String.fromInt i) - - FloatPattern f -> - string (String.fromFloat f) - - TuplePattern inner -> - parensComma False (List.map writePattern inner) - - RecordPattern inner -> - bracesComma False (List.map (Node.value >> string) inner) - - UnConsPattern left right -> - spaced [ writePattern left, string "::", writePattern right ] - - ListPattern inner -> - bracketsComma False (List.map writePattern inner) - - VarPattern var -> - string var - - NamedPattern qnr others -> - spaced - [ writeQualifiedNameRef qnr - , spaced (List.map writePattern others) - ] - - AsPattern innerPattern asName -> - spaced [ writePattern innerPattern, string "as", string <| Node.value asName ] - - ParenthesizedPattern innerPattern -> - spaced [ string "(", writePattern innerPattern, string ")" ] - - -writeQualifiedNameRef : QualifiedNameRef -> Writer -writeQualifiedNameRef { moduleName, name } = - case moduleName of - [] -> - string name - - _ -> - join - [ writeModuleName moduleName - , string "." - , string name - ] - - - --- Helpers - - -parensIfContainsSpaces : Writer -> Writer -parensIfContainsSpaces w = - if Writer.write w |> String.contains " " then - join [ string "(", w, string ")" ] - - else - w - - -wrapInSurroundingParentheses : Node TypeAnnotation -> Node TypeAnnotation -wrapInSurroundingParentheses node = - let - withParens : Node TypeAnnotation -> Node TypeAnnotation - withParens n = - Node.empty (Tupled [ n ]) - in - case Node.value node of - FunctionTypeAnnotation _ _ -> - withParens node - - Typed _ typeParameters -> - case typeParameters of - [] -> - node - - _ -> - withParens node - - _ -> - node diff --git a/src/List/Extra.elm b/src/List/Extra.elm index 54f7620bb..f18e57343 100644 --- a/src/List/Extra.elm +++ b/src/List/Extra.elm @@ -1,41 +1,12 @@ -module List.Extra exposing - ( find - , unique - ) +module List.Extra exposing (find) {-| Helper for List functions. find and unique taken from elm-community/list-extra. @docs find -@docs unique -} -{-| Remove duplicate values, keeping the first instance of each element which appears more than once. - - unique [ 0, 1, 1, 0, 1 ] - --> [ 0, 1 ] - --} -unique : List a -> List a -unique list = - uniqueHelp [] list [] - - -uniqueHelp : List a -> List a -> List a -> List a -uniqueHelp existing remaining accumulator = - case remaining of - [] -> - accumulator - - first :: rest -> - if List.member first existing then - uniqueHelp existing rest accumulator - - else - uniqueHelp (first :: existing) rest (first :: accumulator) - - find : (a -> Bool) -> List a -> Maybe a find predicate list = case list of diff --git a/tests/Elm/Parser/CaseExpressionTests.elm b/tests/Elm/Parser/CaseExpressionTests.elm index af198440f..72be318ec 100644 --- a/tests/Elm/Parser/CaseExpressionTests.elm +++ b/tests/Elm/Parser/CaseExpressionTests.elm @@ -5,6 +5,7 @@ import Elm.Parser.ParserWithCommentsTestUtil as ParserWithCommentsUtil import Elm.Syntax.Expression exposing (..) import Elm.Syntax.Node exposing (Node(..)) import Elm.Syntax.Pattern exposing (..) +import Elm.Syntax.StringLiteralType exposing (StringLiteralType(..)) import Expect import Test exposing (..) @@ -43,20 +44,21 @@ True -> 1""" False -> 2""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 3, column = 13 } } - (CaseExpression + (Case { expression = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } <| FunctionOrValue [] "f" - , cases = - [ ( Node { start = { row = 2, column = 3 }, end = { row = 2, column = 7 } } <| - NamedPattern (QualifiedNameRef [] "True") [] - , Node { start = { row = 2, column = 11 }, end = { row = 2, column = 12 } } <| - Integer 1 - ) - , ( Node { start = { row = 3, column = 3 }, end = { row = 3, column = 8 } } <| + , firstCase = + ( Node { start = { row = 2, column = 3 }, end = { row = 2, column = 7 } } <| + NamedPattern (QualifiedNameRef [] "True") [] + , Node { start = { row = 2, column = 11 }, end = { row = 2, column = 12 } } <| + IntegerLiteral 1 + ) + , restOfCases = + [ ( Node { start = { row = 3, column = 3 }, end = { row = 3, column = 8 } } <| NamedPattern (QualifiedNameRef [] "False") [] , Node { start = { row = 3, column = 12 }, end = { row = 3, column = 13 } } <| - Integer 2 + IntegerLiteral 2 ) ] } @@ -68,17 +70,17 @@ True -> 1""" Foo.Bar -> 1""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 2, column = 15 } } - (CaseExpression + (Case { expression = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } <| FunctionOrValue [] "f" - , cases = - [ ( Node { start = { row = 2, column = 3 }, end = { row = 2, column = 10 } } <| - NamedPattern (QualifiedNameRef [ "Foo" ] "Bar") [] - , Node { start = { row = 2, column = 14 }, end = { row = 2, column = 15 } } <| - Integer 1 - ) - ] + , firstCase = + ( Node { start = { row = 2, column = 3 }, end = { row = 2, column = 10 } } <| + NamedPattern (QualifiedNameRef [ "Foo" ] "Bar") [] + , Node { start = { row = 2, column = 14 }, end = { row = 2, column = 15 } } <| + IntegerLiteral 1 + ) + , restOfCases = [] } ) ) @@ -88,17 +90,17 @@ True -> 1""" x->1""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 2, column = 7 } } - (CaseExpression + (Case { expression = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } <| FunctionOrValue [] "f" - , cases = - [ ( Node { start = { row = 2, column = 3 }, end = { row = 2, column = 4 } } <| - VarPattern "x" - , Node { start = { row = 2, column = 6 }, end = { row = 2, column = 7 } } <| - Integer 1 - ) - ] + , firstCase = + ( Node { start = { row = 2, column = 3 }, end = { row = 2, column = 4 } } <| + VarPattern "x" + , Node { start = { row = 2, column = 6 }, end = { row = 2, column = 7 } } <| + IntegerLiteral 1 + ) + , restOfCases = [] } ) ) @@ -108,14 +110,15 @@ True -> 1""" False -> 2""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 2, column = 21 } } - (CaseExpression + (Case { expression = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } (FunctionOrValue [] "x") - , cases = - [ ( Node { start = { row = 1, column = 11 }, end = { row = 1, column = 15 } } (NamedPattern { moduleName = [], name = "True" } []) - , Node { start = { row = 1, column = 19 }, end = { row = 1, column = 20 } } (Integer 1) - ) - , ( Node { start = { row = 2, column = 11 }, end = { row = 2, column = 16 } } (NamedPattern { moduleName = [], name = "False" } []) - , Node { start = { row = 2, column = 20 }, end = { row = 2, column = 21 } } (Integer 2) + , firstCase = + ( Node { start = { row = 1, column = 11 }, end = { row = 1, column = 15 } } (NamedPattern { moduleName = [], name = "True" } []) + , Node { start = { row = 1, column = 19 }, end = { row = 1, column = 20 } } (IntegerLiteral 1) + ) + , restOfCases = + [ ( Node { start = { row = 2, column = 11 }, end = { row = 2, column = 16 } } (NamedPattern { moduleName = [], name = "False" } []) + , Node { start = { row = 2, column = 20 }, end = { row = 2, column = 21 } } (IntegerLiteral 2) ) ] } @@ -132,22 +135,23 @@ True -> 1""" _ -> 3""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 7, column = 15 } } - (CaseExpression + (Case { expression = Node { start = { row = 1, column = 6 } , end = { row = 1, column = 7 } } (FunctionOrValue [] "x") - , cases = - [ ( Node { start = { row = 2, column = 9 }, end = { row = 2, column = 39 } } (StringPattern "single line triple quote") - , Node { start = { row = 3, column = 13 }, end = { row = 3, column = 14 } } (Integer 1) - ) - , ( Node { start = { row = 4, column = 9 }, end = { row = 5, column = 28 } } (StringPattern "multi line\n triple quote") - , Node { start = { row = 6, column = 13 }, end = { row = 6, column = 14 } } (Integer 2) + , firstCase = + ( Node { start = { row = 2, column = 9 }, end = { row = 2, column = 39 } } (StringPattern TripleQuote "single line triple quote") + , Node { start = { row = 3, column = 13 }, end = { row = 3, column = 14 } } (IntegerLiteral 1) + ) + , restOfCases = + [ ( Node { start = { row = 4, column = 9 }, end = { row = 5, column = 28 } } (StringPattern TripleQuote "multi line\n triple quote") + , Node { start = { row = 6, column = 13 }, end = { row = 6, column = 14 } } (IntegerLiteral 2) ) , ( Node { start = { row = 7, column = 9 }, end = { row = 7, column = 10 } } AllPattern - , Node { start = { row = 7, column = 14 }, end = { row = 7, column = 15 } } (Integer 3) + , Node { start = { row = 7, column = 14 }, end = { row = 7, column = 15 } } (IntegerLiteral 3) ) ] } @@ -175,14 +179,15 @@ True -> 1""" 2""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 6, column = 6 } } - (CaseExpression + (Case { expression = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 9 } } (FunctionOrValue [] "msg") - , cases = - [ ( Node { start = { row = 2, column = 3 }, end = { row = 2, column = 12 } } (NamedPattern { moduleName = [], name = "Increment" } []) - , Node { start = { row = 3, column = 5 }, end = { row = 3, column = 6 } } (Integer 1) - ) - , ( Node { start = { row = 5, column = 3 }, end = { row = 5, column = 12 } } (NamedPattern { moduleName = [], name = "Decrement" } []) - , Node { start = { row = 6, column = 5 }, end = { row = 6, column = 6 } } (Integer 2) + , firstCase = + ( Node { start = { row = 2, column = 3 }, end = { row = 2, column = 12 } } (NamedPattern { moduleName = [], name = "Increment" } []) + , Node { start = { row = 3, column = 5 }, end = { row = 3, column = 6 } } (IntegerLiteral 1) + ) + , restOfCases = + [ ( Node { start = { row = 5, column = 3 }, end = { row = 5, column = 12 } } (NamedPattern { moduleName = [], name = "Decrement" } []) + , Node { start = { row = 6, column = 5 }, end = { row = 6, column = 6 } } (IntegerLiteral 2) ) ] } diff --git a/tests/Elm/Parser/DeclarationsTests.elm b/tests/Elm/Parser/DeclarationsTests.elm index 7b7a88aab..aae448bfa 100644 --- a/tests/Elm/Parser/DeclarationsTests.elm +++ b/tests/Elm/Parser/DeclarationsTests.elm @@ -3,11 +3,12 @@ module Elm.Parser.DeclarationsTests exposing (all) import Elm.Parser.Declarations exposing (..) import Elm.Parser.ParserWithCommentsTestUtil as ParserWithCommentsUtil exposing (parse) import Elm.Syntax.Declaration as Declaration exposing (..) -import Elm.Syntax.Expression exposing (..) -import Elm.Syntax.Infix as Infix exposing (InfixDirection(..)) +import Elm.Syntax.DestructurePattern exposing (DestructurePattern(..)) +import Elm.Syntax.Expression as Expression exposing (..) import Elm.Syntax.Node exposing (Node(..)) import Elm.Syntax.Pattern exposing (..) -import Elm.Syntax.TypeAnnotation exposing (..) +import Elm.Syntax.StringLiteralType exposing (StringLiteralType(..)) +import Elm.Syntax.TypeAnnotation as TypeAnnotation exposing (..) import Expect import Test exposing (..) @@ -62,7 +63,7 @@ foo = bar""" Node { start = { row = 1, column = 1 }, end = { row = 1, column = 9 } } { name = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } "foo" , arguments = [] - , expression = Node { start = { row = 1, column = 7 }, end = { row = 1, column = 9 } } (RecordExpr []) + , expression = Node { start = { row = 1, column = 7 }, end = { row = 1, column = 9 } } (Expression.Record []) } } ) @@ -84,10 +85,10 @@ foo = bar""" , declaration = Node { start = { row = 1, column = 1 }, end = { row = 7, column = 7 } } { name = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } "inc" - , arguments = [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 6 } } (VarPattern "x") ] + , arguments = [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 6 } } (VarPattern_ "x") ] , expression = Node { start = { row = 2, column = 3 }, end = { row = 7, column = 7 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 3, column = 5 }, end = { row = 5, column = 18 } } (LetFunction @@ -99,13 +100,13 @@ foo = bar""" , arguments = [] , expression = Node { start = { row = 4, column = 7 }, end = { row = 5, column = 18 } } - (CaseExpression + (Case { expression = Node { start = { row = 4, column = 12 }, end = { row = 4, column = 13 } } (FunctionOrValue [] "x") - , cases = - [ ( Node { start = { row = 5, column = 9 }, end = { row = 5, column = 13 } } (NamedPattern { moduleName = [], name = "True" } []) - , Node { start = { row = 5, column = 17 }, end = { row = 5, column = 18 } } (FunctionOrValue [] "z") - ) - ] + , firstCase = + ( Node { start = { row = 5, column = 9 }, end = { row = 5, column = 13 } } (NamedPattern { moduleName = [], name = "True" } []) + , Node { start = { row = 5, column = 17 }, end = { row = 5, column = 18 } } (FunctionOrValue [] "z") + ) + , restOfCases = [] } ) } @@ -142,13 +143,12 @@ foo = bar""" , declaration = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 14 } } { name = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } "inc" - , arguments = [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 6 } } (VarPattern "x") ] + , arguments = [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 6 } } (VarPattern_ "x") ] , expression = Node { start = { row = 1, column = 9 }, end = { row = 1, column = 14 } } - (OperatorApplication "+" - Infix.Left + (Operation "+" (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 10 } } (FunctionOrValue [] "x")) - (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 14 } } (Integer 1)) + (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 14 } } (IntegerLiteral 1)) ) } } @@ -172,7 +172,7 @@ foo = bar""" , arguments = [] , expression = Node { start = { row = 2, column = 2 }, end = { row = 5, column = 4 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 3, column = 3 }, end = { row = 3, column = 8 } } (LetFunction @@ -182,7 +182,7 @@ foo = bar""" Node { start = { row = 3, column = 3 }, end = { row = 3, column = 8 } } { name = Node { start = { row = 3, column = 3 }, end = { row = 3, column = 4 } } "b" , arguments = [] - , expression = Node { start = { row = 3, column = 7 }, end = { row = 3, column = 8 } } (Integer 1) + , expression = Node { start = { row = 3, column = 7 }, end = { row = 3, column = 8 } } (IntegerLiteral 1) } } ) @@ -220,21 +220,21 @@ foo = bar""" , arguments = [] , expression = Node { start = { row = 2, column = 2 }, end = { row = 5, column = 4 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 3, column = 3 }, end = { row = 3, column = 16 } } (LetDestructuring (Node { start = { row = 3, column = 3 }, end = { row = 3, column = 9 } } - (TuplePattern - [ Node { start = { row = 3, column = 4 }, end = { row = 3, column = 5 } } (VarPattern "b") - , Node { start = { row = 3, column = 7 }, end = { row = 3, column = 8 } } (VarPattern "c") + (TuplePattern_ + [ Node { start = { row = 3, column = 4 }, end = { row = 3, column = 5 } } (VarPattern_ "b") + , Node { start = { row = 3, column = 7 }, end = { row = 3, column = 8 } } (VarPattern_ "c") ] ) ) (Node { start = { row = 3, column = 10 }, end = { row = 3, column = 16 } } - (TupledExpression - [ Node { start = { row = 3, column = 11 }, end = { row = 3, column = 12 } } (Integer 1) - , Node { start = { row = 3, column = 14 }, end = { row = 3, column = 15 } } (Integer 2) + (TupleExpression + [ Node { start = { row = 3, column = 11 }, end = { row = 3, column = 12 } } (IntegerLiteral 1) + , Node { start = { row = 3, column = 14 }, end = { row = 3, column = 15 } } (IntegerLiteral 2) ] ) ) @@ -262,13 +262,13 @@ foo = bar""" , arguments = [] , expression = Node { start = { row = 2, column = 3 }, end = { row = 2, column = 62 } } - (Application - [ Node { start = { row = 2, column = 3 }, end = { row = 2, column = 18 } } (FunctionOrValue [] "beginnerProgram") - , Node { start = { row = 2, column = 19 }, end = { row = 2, column = 62 } } - (RecordExpr + (FunctionCall + (Node { start = { row = 2, column = 3 }, end = { row = 2, column = 18 } } (FunctionOrValue [] "beginnerProgram")) + (Node { start = { row = 2, column = 19 }, end = { row = 2, column = 62 } } + (Expression.Record [ Node { start = { row = 2, column = 21 }, end = { row = 2, column = 30 } } ( Node { start = { row = 2, column = 21 }, end = { row = 2, column = 26 } } "model" - , Node { start = { row = 2, column = 29 }, end = { row = 2, column = 30 } } (Integer 0) + , Node { start = { row = 2, column = 29 }, end = { row = 2, column = 30 } } (IntegerLiteral 0) ) , Node { start = { row = 2, column = 32 }, end = { row = 2, column = 43 } } ( Node { start = { row = 2, column = 32 }, end = { row = 2, column = 36 } } "view" @@ -280,7 +280,8 @@ foo = bar""" ) ] ) - ] + ) + [] ) } } @@ -304,28 +305,27 @@ foo = bar""" Node { start = { row = 1, column = 1 }, end = { row = 7, column = 16 } } { name = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 7 } } "update" , arguments = - [ Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } (VarPattern "msg") - , Node { start = { row = 1, column = 12 }, end = { row = 1, column = 17 } } (VarPattern "model") + [ Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } (VarPattern_ "msg") + , Node { start = { row = 1, column = 12 }, end = { row = 1, column = 17 } } (VarPattern_ "model") ] , expression = Node { start = { row = 2, column = 3 }, end = { row = 7, column = 16 } } - (CaseExpression + (Case { expression = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 11 } } (FunctionOrValue [] "msg") - , cases = - [ ( Node { start = { row = 3, column = 5 }, end = { row = 3, column = 14 } } (NamedPattern { moduleName = [], name = "Increment" } []) - , Node { start = { row = 4, column = 7 }, end = { row = 4, column = 16 } } - (OperatorApplication "+" - Left - (Node { start = { row = 4, column = 7 }, end = { row = 4, column = 12 } } (FunctionOrValue [] "model")) - (Node { start = { row = 4, column = 15 }, end = { row = 4, column = 16 } } (Integer 1)) - ) - ) - , ( Node { start = { row = 6, column = 5 }, end = { row = 6, column = 14 } } (NamedPattern { moduleName = [], name = "Decrement" } []) + , firstCase = + ( Node { start = { row = 3, column = 5 }, end = { row = 3, column = 14 } } (NamedPattern { moduleName = [], name = "Increment" } []) + , Node { start = { row = 4, column = 7 }, end = { row = 4, column = 16 } } + (Operation "+" + (Node { start = { row = 4, column = 7 }, end = { row = 4, column = 12 } } (FunctionOrValue [] "model")) + (Node { start = { row = 4, column = 15 }, end = { row = 4, column = 16 } } (IntegerLiteral 1)) + ) + ) + , restOfCases = + [ ( Node { start = { row = 6, column = 5 }, end = { row = 6, column = 14 } } (NamedPattern { moduleName = [], name = "Decrement" } []) , Node { start = { row = 7, column = 7 }, end = { row = 7, column = 16 } } - (OperatorApplication "-" - Left + (Operation "-" (Node { start = { row = 7, column = 7 }, end = { row = 7, column = 12 } } (FunctionOrValue [] "model")) - (Node { start = { row = 7, column = 15 }, end = { row = 7, column = 16 } } (Integer 1)) + (Node { start = { row = 7, column = 15 }, end = { row = 7, column = 16 } } (IntegerLiteral 1)) ) ) ] @@ -341,28 +341,32 @@ foo = bar""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 51 } } (PortDeclaration - { name = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 19 } } "parseResponse" - , typeAnnotation = - Node { start = { row = 1, column = 22 }, end = { row = 1, column = 51 } } - (FunctionTypeAnnotation - (Node { start = { row = 1, column = 22 }, end = { row = 1, column = 40 } } - (Tupled - [ Node { start = { row = 1, column = 24 }, end = { row = 1, column = 30 } } (Typed (Node { start = { row = 1, column = 24 }, end = { row = 1, column = 30 } } ( [], "String" )) []) - , Node { start = { row = 1, column = 32 }, end = { row = 1, column = 38 } } - (Typed - (Node { start = { row = 1, column = 32 }, end = { row = 1, column = 38 } } ( [], "String" )) - [] + { documentation = Nothing + , signature = + Node { start = { row = 1, column = 6 }, end = { row = 1, column = 51 } } <| + { name = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 19 } } "parseResponse" + , typeAnnotation = + Node { start = { row = 1, column = 22 }, end = { row = 1, column = 51 } } + (FunctionTypeAnnotation + (Node { start = { row = 1, column = 22 }, end = { row = 1, column = 40 } } + (Tuple + [ Node { start = { row = 1, column = 24 }, end = { row = 1, column = 30 } } (Type (Node { start = { row = 1, column = 24 }, end = { row = 1, column = 30 } } ( [], "String" )) []) + , Node { start = { row = 1, column = 32 }, end = { row = 1, column = 38 } } + (Type + (Node { start = { row = 1, column = 32 }, end = { row = 1, column = 38 } } ( [], "String" )) + [] + ) + ] ) - ] - ) - ) - (Node { start = { row = 1, column = 44 }, end = { row = 1, column = 51 } } - (Typed - (Node { start = { row = 1, column = 44 }, end = { row = 1, column = 47 } } ( [], "Cmd" )) - [ Node { start = { row = 1, column = 48 }, end = { row = 1, column = 51 } } (GenericType "msg") ] + ) + (Node { start = { row = 1, column = 44 }, end = { row = 1, column = 51 } } + (Type + (Node { start = { row = 1, column = 44 }, end = { row = 1, column = 47 } } ( [], "Cmd" )) + [ Node { start = { row = 1, column = 48 }, end = { row = 1, column = 51 } } (Var "msg") ] + ) + ) ) - ) - ) + } } ) ) @@ -372,25 +376,29 @@ foo = bar""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 39 } } (PortDeclaration - { name = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 12 } } "scroll" - , typeAnnotation = - Node { start = { row = 1, column = 15 }, end = { row = 1, column = 39 } } - (FunctionTypeAnnotation - (Node { start = { row = 1, column = 15 }, end = { row = 1, column = 28 } } + { documentation = Nothing + , signature = + Node { start = { row = 1, column = 6 }, end = { row = 1, column = 39 } } <| + { name = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 12 } } "scroll" + , typeAnnotation = + Node { start = { row = 1, column = 15 }, end = { row = 1, column = 39 } } (FunctionTypeAnnotation - (Node { start = { row = 1, column = 16 }, end = { row = 1, column = 20 } } - (Typed (Node { start = { row = 1, column = 16 }, end = { row = 1, column = 20 } } ( [], "Move" )) []) + (Node { start = { row = 1, column = 15 }, end = { row = 1, column = 28 } } + (FunctionTypeAnnotation + (Node { start = { row = 1, column = 16 }, end = { row = 1, column = 20 } } + (Type (Node { start = { row = 1, column = 16 }, end = { row = 1, column = 20 } } ( [], "Move" )) []) + ) + (Node { start = { row = 1, column = 24 }, end = { row = 1, column = 27 } } (Var "msg")) + ) + ) + (Node { start = { row = 1, column = 32 }, end = { row = 1, column = 39 } } + (Type (Node { start = { row = 1, column = 32 }, end = { row = 1, column = 35 } } ( [], "Sub" )) + [ Node { start = { row = 1, column = 36 }, end = { row = 1, column = 39 } } (Var "msg") + ] + ) ) - (Node { start = { row = 1, column = 24 }, end = { row = 1, column = 27 } } (GenericType "msg")) - ) - ) - (Node { start = { row = 1, column = 32 }, end = { row = 1, column = 39 } } - (Typed (Node { start = { row = 1, column = 32 }, end = { row = 1, column = 35 } } ( [], "Sub" )) - [ Node { start = { row = 1, column = 36 }, end = { row = 1, column = 39 } } (GenericType "msg") - ] ) - ) - ) + } } ) ) @@ -413,10 +421,10 @@ foo = bar""" , arguments = [] , expression = Node { start = { row = 2, column = 3 }, end = { row = 2, column = 23 } } - (Application - [ Node { start = { row = 2, column = 3 }, end = { row = 2, column = 7 } } (FunctionOrValue [] "text") - , Node { start = { row = 2, column = 8 }, end = { row = 2, column = 23 } } (Literal "Hello, World!") - ] + (FunctionCall + (Node { start = { row = 2, column = 3 }, end = { row = 2, column = 7 } } (FunctionOrValue [] "text")) + (Node { start = { row = 2, column = 8 }, end = { row = 2, column = 23 } } (StringLiteral SingleQuote "Hello, World!")) + [] ) } } @@ -437,10 +445,10 @@ foo = bar""" , arguments = [] , expression = Node { start = { row = 2, column = 3 }, end = { row = 2, column = 23 } } - (Application - [ Node { start = { row = 2, column = 3 }, end = { row = 2, column = 7 } } (FunctionOrValue [] "text") - , Node { start = { row = 2, column = 8 }, end = { row = 2, column = 23 } } (Literal "Hello, World!") - ] + (FunctionCall + (Node { start = { row = 2, column = 3 }, end = { row = 2, column = 7 } } (FunctionOrValue [] "text")) + (Node { start = { row = 2, column = 8 }, end = { row = 2, column = 23 } } (StringLiteral SingleQuote "Hello, World!")) + [] ) } } @@ -477,31 +485,28 @@ foo = bar""" , declaration = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 83 } } { name = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 12 } } "updateState" - , arguments = [ Node { start = { row = 1, column = 13 }, end = { row = 1, column = 19 } } (VarPattern "update"), Node { start = { row = 1, column = 20 }, end = { row = 1, column = 28 } } (VarPattern "sendPort") ] + , arguments = [ Node { start = { row = 1, column = 13 }, end = { row = 1, column = 19 } } (VarPattern_ "update"), Node { start = { row = 1, column = 20 }, end = { row = 1, column = 28 } } (VarPattern_ "sendPort") ] , expression = Node { start = { row = 1, column = 31 }, end = { row = 1, column = 83 } } - (OperatorApplication "<|" - Right + (Operation "<|" (Node { start = { row = 1, column = 31 }, end = { row = 1, column = 36 } } (FunctionOrValue [] "curry")) (Node { start = { row = 1, column = 40 }, end = { row = 1, column = 83 } } - (OperatorApplication ">>" - Right + (Operation ">>" (Node { start = { row = 1, column = 40 }, end = { row = 1, column = 56 } } - (ParenthesizedExpression - (Node { start = { row = 1, column = 41 }, end = { row = 1, column = 55 } } - (Application - [ Node { start = { row = 1, column = 41 }, end = { row = 1, column = 48 } } (FunctionOrValue [] "uncurry") - , Node { start = { row = 1, column = 49 }, end = { row = 1, column = 55 } } (FunctionOrValue [] "update") - ] + (TupleExpression + [ Node { start = { row = 1, column = 41 }, end = { row = 1, column = 55 } } + (FunctionCall + (Node { start = { row = 1, column = 41 }, end = { row = 1, column = 48 } } (FunctionOrValue [] "uncurry")) + (Node { start = { row = 1, column = 49 }, end = { row = 1, column = 55 } } (FunctionOrValue [] "update")) + [] ) - ) + ] ) ) (Node { start = { row = 1, column = 60 }, end = { row = 1, column = 83 } } - (Application - [ Node { start = { row = 1, column = 60 }, end = { row = 1, column = 74 } } (FunctionOrValue [] "batchStateCmds") - , Node { start = { row = 1, column = 75 }, end = { row = 1, column = 83 } } (FunctionOrValue [] "sendPort") - ] + (FunctionCall (Node { start = { row = 1, column = 60 }, end = { row = 1, column = 74 } } (FunctionOrValue [] "batchStateCmds")) + (Node { start = { row = 1, column = 75 }, end = { row = 1, column = 83 } } (FunctionOrValue [] "sendPort")) + [] ) ) ) @@ -529,28 +534,29 @@ foo = bar""" Node { start = { row = 1, column = 1 }, end = { row = 7, column = 16 } } { name = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 7 } } "update" , arguments = - [ Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } (VarPattern "msg") - , Node { start = { row = 1, column = 12 }, end = { row = 1, column = 17 } } (VarPattern "model") + [ Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } (VarPattern_ "msg") + , Node { start = { row = 1, column = 12 }, end = { row = 1, column = 17 } } (VarPattern_ "model") ] , expression = Node { start = { row = 2, column = 3 }, end = { row = 7, column = 16 } } - (CaseExpression + (Case { expression = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 11 } } (FunctionOrValue [] "msg") - , cases = - [ ( Node { start = { row = 3, column = 5 }, end = { row = 3, column = 14 } } (NamedPattern { moduleName = [], name = "Increment" } []) - , Node { start = { row = 4, column = 7 }, end = { row = 4, column = 16 } } - (OperatorApplication "+" - Left - (Node { start = { row = 4, column = 7 }, end = { row = 4, column = 12 } } (FunctionOrValue [] "model")) - (Node { start = { row = 4, column = 15 }, end = { row = 4, column = 16 } } (Integer 1)) - ) - ) - , ( Node { start = { row = 6, column = 5 }, end = { row = 6, column = 14 } } (NamedPattern { moduleName = [], name = "Decrement" } []) + , firstCase = + ( Node { start = { row = 3, column = 5 }, end = { row = 3, column = 14 } } + (NamedPattern { moduleName = [], name = "Increment" } []) + , Node { start = { row = 4, column = 7 }, end = { row = 4, column = 16 } } + (Operation "+" + (Node { start = { row = 4, column = 7 }, end = { row = 4, column = 12 } } (FunctionOrValue [] "model")) + (Node { start = { row = 4, column = 15 }, end = { row = 4, column = 16 } } (IntegerLiteral 1)) + ) + ) + , restOfCases = + [ ( Node { start = { row = 6, column = 5 }, end = { row = 6, column = 14 } } + (NamedPattern { moduleName = [], name = "Decrement" } []) , Node { start = { row = 7, column = 7 }, end = { row = 7, column = 16 } } - (OperatorApplication "-" - Left + (Operation "-" (Node { start = { row = 7, column = 7 }, end = { row = 7, column = 12 } } (FunctionOrValue [] "model")) - (Node { start = { row = 7, column = 15 }, end = { row = 7, column = 16 } } (Integer 1)) + (Node { start = { row = 7, column = 15 }, end = { row = 7, column = 16 } } (IntegerLiteral 1)) ) ) ] @@ -573,15 +579,15 @@ update msg model = Just (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 15 } } { name = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 7 } } "update" - , typeAnnotation = Node { start = { row = 1, column = 10 }, end = { row = 1, column = 15 } } (Typed (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 15 } } ( [], "Model" )) []) + , typeAnnotation = Node { start = { row = 1, column = 10 }, end = { row = 1, column = 15 } } (Type (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 15 } } ( [], "Model" )) []) } ) , declaration = Node { start = { row = 2, column = 1 }, end = { row = 3, column = 8 } } { name = Node { start = { row = 2, column = 1 }, end = { row = 2, column = 7 } } "update" , arguments = - [ Node { start = { row = 2, column = 8 }, end = { row = 2, column = 11 } } (VarPattern "msg") - , Node { start = { row = 2, column = 12 }, end = { row = 2, column = 17 } } (VarPattern "model") + [ Node { start = { row = 2, column = 8 }, end = { row = 2, column = 11 } } (VarPattern_ "msg") + , Node { start = { row = 2, column = 12 }, end = { row = 2, column = 17 } } (VarPattern_ "model") ] , expression = Node { start = { row = 3, column = 5 }, end = { row = 3, column = 8 } } (FunctionOrValue [] "msg") } @@ -599,11 +605,11 @@ update msg model = , generics = [] , typeAnnotation = Node { start = { row = 1, column = 18 }, end = { row = 1, column = 34 } } - (Record + (TypeAnnotation.Record [ Node { start = { row = 1, column = 19 }, end = { row = 1, column = 32 } } ( Node { start = { row = 1, column = 19 }, end = { row = 1, column = 24 } } "color" , Node { start = { row = 1, column = 26 }, end = { row = 1, column = 32 } } - (Typed (Node { start = { row = 1, column = 26 }, end = { row = 1, column = 32 } } ( [], "String" )) []) + (Type (Node { start = { row = 1, column = 26 }, end = { row = 1, column = 32 } } ( [], "String" )) []) ) ] ) @@ -622,11 +628,11 @@ type alias Foo = {color: String }""" , generics = [] , typeAnnotation = Node { start = { row = 2, column = 18 }, end = { row = 2, column = 34 } } - (Record + (TypeAnnotation.Record [ Node { start = { row = 2, column = 19 }, end = { row = 2, column = 32 } } ( Node { start = { row = 2, column = 19 }, end = { row = 2, column = 24 } } "color" , Node { start = { row = 2, column = 26 }, end = { row = 2, column = 32 } } - (Typed (Node { start = { row = 2, column = 26 }, end = { row = 2, column = 32 } } ( [], "String" )) []) + (Type (Node { start = { row = 2, column = 26 }, end = { row = 2, column = 32 } } ( [], "String" )) []) ) ] ) @@ -644,18 +650,18 @@ type alias Foo = {color: String }""" , generics = [] , typeAnnotation = Node { start = { row = 1, column = 16 }, end = { row = 1, column = 32 } } - (Record + (TypeAnnotation.Record [ Node { start = { row = 1, column = 17 }, end = { row = 1, column = 30 } } ( Node { start = { row = 1, column = 17 }, end = { row = 1, column = 22 } } "color" , Node { start = { row = 1, column = 24 }, end = { row = 1, column = 30 } } - (Typed (Node { start = { row = 1, column = 24 }, end = { row = 1, column = 30 } } ( [], "String" )) []) + (Type (Node { start = { row = 1, column = 24 }, end = { row = 1, column = 30 } } ( [], "String" )) []) ) ] ) } ) ) - , test "type alias with GenericType " <| + , test "type alias with Var " <| \() -> "type alias Foo a = {some : a }" |> expectAst @@ -666,10 +672,10 @@ type alias Foo = {color: String }""" , generics = [ Node { start = { row = 1, column = 16 }, end = { row = 1, column = 17 } } "a" ] , typeAnnotation = Node { start = { row = 1, column = 20 }, end = { row = 1, column = 31 } } - (Record + (TypeAnnotation.Record [ Node { start = { row = 1, column = 21 }, end = { row = 1, column = 29 } } ( Node { start = { row = 1, column = 21 }, end = { row = 1, column = 25 } } "some" - , Node { start = { row = 1, column = 28 }, end = { row = 1, column = 29 } } (GenericType "a") + , Node { start = { row = 1, column = 28 }, end = { row = 1, column = 29 } } (Var "a") ) ] ) @@ -685,15 +691,16 @@ type alias Foo = {color: String }""" { documentation = Nothing , name = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 11 } } "Color" , generics = [] - , constructors = - [ Node { start = { row = 1, column = 14 }, end = { row = 1, column = 25 } } + , firstConstructor = + Node { start = { row = 1, column = 14 }, end = { row = 1, column = 25 } } { name = Node { start = { row = 1, column = 14 }, end = { row = 1, column = 18 } } "Blue" , arguments = [ Node { start = { row = 1, column = 19 }, end = { row = 1, column = 25 } } - (Typed (Node { start = { row = 1, column = 19 }, end = { row = 1, column = 25 } } ( [], "String" )) []) + (Type (Node { start = { row = 1, column = 19 }, end = { row = 1, column = 25 } } ( [], "String" )) []) ] } - , Node { start = { row = 1, column = 28 }, end = { row = 1, column = 31 } } + , restOfConstructors = + [ Node { start = { row = 1, column = 28 }, end = { row = 1, column = 31 } } { name = Node { start = { row = 1, column = 28 }, end = { row = 1, column = 31 } } "Red" , arguments = [] } @@ -715,18 +722,19 @@ type Color = Blue String | Red | Green""" { documentation = Just (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 19 } } "{-| Classic RGB -}") , name = Node { start = { row = 2, column = 6 }, end = { row = 2, column = 11 } } "Color" , generics = [] - , constructors = - [ Node + , firstConstructor = + Node { start = { row = 2, column = 14 } , end = { row = 2, column = 25 } } { name = Node { start = { row = 2, column = 14 }, end = { row = 2, column = 18 } } "Blue" , arguments = [ Node { start = { row = 2, column = 19 }, end = { row = 2, column = 25 } } - (Typed (Node { start = { row = 2, column = 19 }, end = { row = 2, column = 25 } } ( [], "String" )) []) + (Type (Node { start = { row = 2, column = 19 }, end = { row = 2, column = 25 } } ( [], "String" )) []) ] } - , Node { start = { row = 2, column = 28 }, end = { row = 2, column = 31 } } + , restOfConstructors = + [ Node { start = { row = 2, column = 28 }, end = { row = 2, column = 31 } } { name = Node { start = { row = 2, column = 28 }, end = { row = 2, column = 31 } } "Red" , arguments = [] } @@ -747,16 +755,16 @@ type Color = Blue String | Red | Green""" { documentation = Nothing , name = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } "D" , generics = [] - , constructors = - [ Node { start = { row = 1, column = 10 }, end = { row = 1, column = 15 } } + , firstConstructor = + Node { start = { row = 1, column = 10 }, end = { row = 1, column = 15 } } { name = Node { start = { row = 1, column = 10 }, end = { row = 1, column = 11 } } "C" , arguments = - [ Node { start = { row = 1, column = 12 }, end = { row = 1, column = 13 } } (GenericType "a") + [ Node { start = { row = 1, column = 12 }, end = { row = 1, column = 13 } } (Var "a") , Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } - (Typed (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } ( [], "B" )) []) + (Type (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } ( [], "B" )) []) ] } - ] + , restOfConstructors = [] } ) ) @@ -769,16 +777,16 @@ type Color = Blue String | Red | Green""" { documentation = Nothing , name = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } "D" , generics = [] - , constructors = - [ Node { start = { row = 1, column = 10 }, end = { row = 1, column = 15 } } + , firstConstructor = + Node { start = { row = 1, column = 10 }, end = { row = 1, column = 15 } } { name = Node { start = { row = 1, column = 10 }, end = { row = 1, column = 11 } } "C" , arguments = [ Node { start = { row = 1, column = 12 }, end = { row = 1, column = 13 } } - (Typed (Node { start = { row = 1, column = 12 }, end = { row = 1, column = 13 } } ( [], "B" )) []) - , Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } (GenericType "a") + (Type (Node { start = { row = 1, column = 12 }, end = { row = 1, column = 13 } } ( [], "B" )) []) + , Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } (Var "a") ] } - ] + , restOfConstructors = [] } ) ) @@ -786,7 +794,7 @@ type Color = Blue String | Red | Green""" \() -> "type D = C B\na" |> expectInvalid - , test "type with GenericType" <| + , test "type with Var" <| \() -> "type Maybe a = Just a | Nothing" |> expectAst @@ -795,12 +803,13 @@ type Color = Blue String | Red | Green""" { documentation = Nothing , name = Node { start = { row = 1, column = 6 }, end = { row = 1, column = 11 } } "Maybe" , generics = [ Node { start = { row = 1, column = 12 }, end = { row = 1, column = 13 } } "a" ] - , constructors = - [ Node { start = { row = 1, column = 16 }, end = { row = 1, column = 22 } } + , firstConstructor = + Node { start = { row = 1, column = 16 }, end = { row = 1, column = 22 } } { name = Node { start = { row = 1, column = 16 }, end = { row = 1, column = 20 } } "Just" - , arguments = [ Node { start = { row = 1, column = 21 }, end = { row = 1, column = 22 } } (GenericType "a") ] + , arguments = [ Node { start = { row = 1, column = 21 }, end = { row = 1, column = 22 } } (Var "a") ] } - , Node { start = { row = 1, column = 25 }, end = { row = 1, column = 32 } } + , restOfConstructors = + [ Node { start = { row = 1, column = 25 }, end = { row = 1, column = 32 } } { name = Node { start = { row = 1, column = 25 }, end = { row = 1, column = 32 } } "Nothing" , arguments = [] } diff --git a/tests/Elm/Parser/ExposeTests.elm b/tests/Elm/Parser/ExposeTests.elm index b3d9925df..f0f8e8d40 100644 --- a/tests/Elm/Parser/ExposeTests.elm +++ b/tests/Elm/Parser/ExposeTests.elm @@ -51,8 +51,8 @@ all = "exposing (Model,Msg(..),Info(..),init,(::))" |> expectAst (Explicit - [ Node { start = { row = 1, column = 11 }, end = { row = 1, column = 16 } } (TypeOrAliasExpose "Model") - , Node { start = { row = 1, column = 17 }, end = { row = 1, column = 24 } } + (Node { start = { row = 1, column = 11 }, end = { row = 1, column = 16 } } (TypeOrAliasExpose "Model")) + [ Node { start = { row = 1, column = 17 }, end = { row = 1, column = 24 } } (TypeExpose { name = "Msg" , open = Just { start = { row = 1, column = 20 }, end = { row = 1, column = 24 } } @@ -72,9 +72,8 @@ all = \() -> "exposing (Model, Msg, Info (..) ,init,(::) )" |> expectAst - (Explicit - [ Node { start = { row = 1, column = 11 }, end = { row = 1, column = 16 } } (TypeOrAliasExpose "Model") - , Node { start = { row = 1, column = 18 }, end = { row = 1, column = 21 } } (TypeOrAliasExpose "Msg") + (Explicit (Node { start = { row = 1, column = 11 }, end = { row = 1, column = 16 } } (TypeOrAliasExpose "Model")) + [ Node { start = { row = 1, column = 18 }, end = { row = 1, column = 21 } } (TypeOrAliasExpose "Msg") , Node { start = { row = 1, column = 23 }, end = { row = 1, column = 34 } } (TypeExpose { name = "Info" @@ -95,9 +94,8 @@ all = (::) )""" |> expectAst - (Explicit - [ Node { start = { row = 2, column = 7 }, end = { row = 2, column = 8 } } (TypeOrAliasExpose "A") - , Node { start = { row = 3, column = 7 }, end = { row = 3, column = 12 } } + (Explicit (Node { start = { row = 2, column = 7 }, end = { row = 2, column = 8 } } (TypeOrAliasExpose "A")) + [ Node { start = { row = 3, column = 7 }, end = { row = 3, column = 12 } } (TypeExpose { name = "B" , open = Just { start = { row = 3, column = 8 }, end = { row = 3, column = 12 } } @@ -118,10 +116,7 @@ all = "exposing (foo\n --bar\n )" |> expectAstWithComments { ast = - Explicit - [ Node { start = { row = 1, column = 11 }, end = { row = 1, column = 14 } } - (FunctionExpose "foo") - ] + Explicit (Node { start = { row = 1, column = 11 }, end = { row = 1, column = 14 } } (FunctionExpose "foo")) [] , comments = [ Node { start = { row = 2, column = 2 }, end = { row = 2, column = 7 } } "--bar" ] } ] diff --git a/tests/Elm/Parser/ExpressionTests.elm b/tests/Elm/Parser/ExpressionTests.elm index dda2dae7d..bf207f9fe 100644 --- a/tests/Elm/Parser/ExpressionTests.elm +++ b/tests/Elm/Parser/ExpressionTests.elm @@ -2,10 +2,10 @@ module Elm.Parser.ExpressionTests exposing (all) import Elm.Parser.Expression exposing (expression) import Elm.Parser.ParserWithCommentsTestUtil as ParserWithCommentsUtil +import Elm.Syntax.DestructurePattern exposing (DestructurePattern(..)) import Elm.Syntax.Expression exposing (Expression(..)) -import Elm.Syntax.Infix as Infix exposing (InfixDirection(..)) import Elm.Syntax.Node exposing (Node(..)) -import Elm.Syntax.Pattern exposing (..) +import Elm.Syntax.StringLiteralType exposing (StringLiteralType(..)) import Expect import Test exposing (Test, describe, test) @@ -17,14 +17,14 @@ all = \() -> "" |> expectInvalid - , test "Integer literal" <| + , test "IntegerLiteral literal" <| \() -> "101" - |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } (Integer 101)) + |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } (IntegerLiteral 101)) , test "String literal" <| \() -> "\"Bar\"" - |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 6 } } (Literal "Bar")) + |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 6 } } (StringLiteral SingleQuote "Bar")) , test "character literal" <| \() -> "'c'" @@ -34,9 +34,9 @@ all = "(1,2)" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 6 } } - (TupledExpression - [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 3 } } (Integer 1) - , Node { start = { row = 1, column = 4 }, end = { row = 1, column = 5 } } (Integer 2) + (TupleExpression + [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 3 } } (IntegerLiteral 1) + , Node { start = { row = 1, column = 4 }, end = { row = 1, column = 5 } } (IntegerLiteral 2) ] ) ) @@ -45,16 +45,16 @@ all = "( 1 , 2 )" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 12 } } - (TupledExpression - [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (Integer 1) - , Node { start = { row = 1, column = 9 }, end = { row = 1, column = 10 } } (Integer 2) + (TupleExpression + [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (IntegerLiteral 1) + , Node { start = { row = 1, column = 9 }, end = { row = 1, column = 10 } } (IntegerLiteral 2) ] ) ) , test "String literal multiline" <| \() -> "\"\"\"Bar foo \n a\"\"\"" - |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 2, column = 6 } } (Literal "Bar foo \n a")) + |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 2, column = 6 } } (StringLiteral TripleQuote "Bar foo \n a")) , test "Regression test for multiline strings with backslashes" <| \() -> "\"\"\"\\{\\}\"\"\"" @@ -62,11 +62,11 @@ all = , test "Regression test 2 for multiline strings with backslashes" <| \() -> "\"\"\"\\\\{\\\\}\"\"\"" - |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 13 } } (Literal "\\{\\}")) + |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 13 } } (StringLiteral TripleQuote "\\{\\}")) , test "Regression test 3 for multiline strings with backslashes" <| \() -> "\"\"\"\\\\a-blablabla-\\\\b\"\"\"" - |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 24 } } (Literal "\\a-blablabla-\\b")) + |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 24 } } (StringLiteral TripleQuote "\\a-blablabla-\\b")) , test "Type expression for upper case" <| \() -> "Bar" @@ -84,8 +84,8 @@ all = "(bar)" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 6 } } - (ParenthesizedExpression - (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 5 } } (FunctionOrValue [] "bar")) + (TupleExpression + [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 5 } } (FunctionOrValue [] "bar") ] ) ) , test "parenthesized expression starting with a negation" <| @@ -93,16 +93,15 @@ all = "(-1 * sign)" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 12 } } - (ParenthesizedExpression - (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 11 } } - (OperatorApplication "*" - Left + (TupleExpression + [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 11 } } + (Operation "*" (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 4 } } - (Negation (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (Integer 1))) + (Negation (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (IntegerLiteral 1))) ) (Node { start = { row = 1, column = 7 }, end = { row = 1, column = 11 } } (FunctionOrValue [] "sign")) ) - ) + ] ) ) , test "application expression" <| @@ -110,53 +109,53 @@ all = "List.concat []" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 15 } } <| - Application - [ Node { start = { row = 1, column = 1 }, end = { row = 1, column = 12 } } <| FunctionOrValue [ "List" ] "concat" - , Node { start = { row = 1, column = 13 }, end = { row = 1, column = 15 } } <| ListExpr [] - ] + FunctionCall + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 12 } } <| FunctionOrValue [ "List" ] "concat") + (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 15 } } (ListLiteral [])) + [] ) , test "application expression with operator" <| \() -> "model + 1" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 10 } } <| - OperatorApplication "+" - Infix.Left + Operation "+" (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 6 } } <| FunctionOrValue [] "model") - (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 10 } } <| Integer 1) + (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 10 } } <| IntegerLiteral 1) ) , test "application expression 2" <| \() -> "(\"\", always (List.concat [ [ fileName ], [] ]))" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 48 } } <| - TupledExpression - [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 4 } } <| Literal "" + TupleExpression + [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 4 } } <| StringLiteral SingleQuote "" , Node { start = { row = 1, column = 6 }, end = { row = 1, column = 47 } } <| - Application - [ Node { start = { row = 1, column = 6 }, end = { row = 1, column = 12 } } <| FunctionOrValue [] "always" - , Node { start = { row = 1, column = 13 }, end = { row = 1, column = 47 } } <| - ParenthesizedExpression - (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 46 } } <| - Application - [ Node { start = { row = 1, column = 14 }, end = { row = 1, column = 25 } } <| - FunctionOrValue [ "List" ] "concat" - , Node { start = { row = 1, column = 26 }, end = { row = 1, column = 46 } } <| - ListExpr + FunctionCall + (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 12 } } <| FunctionOrValue [] "always") + (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 47 } } <| + TupleExpression + [ Node { start = { row = 1, column = 14 }, end = { row = 1, column = 46 } } <| + FunctionCall + (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 25 } } (FunctionOrValue [ "List" ] "concat")) + (Node { start = { row = 1, column = 26 }, end = { row = 1, column = 46 } } <| + ListLiteral [ Node { start = { row = 1, column = 28 }, end = { row = 1, column = 40 } } <| - ListExpr + ListLiteral [ Node { start = { row = 1, column = 30 }, end = { row = 1, column = 38 } } <| FunctionOrValue [] "fileName" ] , Node { start = { row = 1, column = 42 }, end = { row = 1, column = 44 } } <| - ListExpr [] + ListLiteral [] ] - ] - ) - ] + ) + [] + ] + ) + [] ] ) - , test "expressionNotApplication simple" <| + , test "expressionNotFunctionCall simple" <| \() -> "foo" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } (FunctionOrValue [] "foo")) @@ -165,10 +164,10 @@ all = "Task.succeed ()" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 16 } } - (Application - [ Node { start = { row = 1, column = 1 }, end = { row = 1, column = 13 } } (FunctionOrValue [ "Task" ] "succeed") - , Node { start = { row = 1, column = 14 }, end = { row = 1, column = 16 } } UnitExpr - ] + (FunctionCall + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 13 } } (FunctionOrValue [ "Task" ] "succeed")) + (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 16 } } (TupleExpression [])) + [] ) ) , test "Function call" <| @@ -176,10 +175,10 @@ all = "foo bar" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 8 } } - (Application - [ Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } (FunctionOrValue [] "foo") - , Node { start = { row = 1, column = 5 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "bar") - ] + (FunctionCall + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } (FunctionOrValue [] "foo")) + (Node { start = { row = 1, column = 5 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "bar")) + [] ) ) , test "Function call with argument badly indented" <| @@ -191,7 +190,7 @@ all = "if True then foo else bar" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 26 } } - (IfBlock + (If (Node { start = { row = 1, column = 4 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "True")) (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 17 } } (FunctionOrValue [] "foo")) (Node { start = { row = 1, column = 23 }, end = { row = 1, column = 26 } } (FunctionOrValue [] "bar")) @@ -202,10 +201,10 @@ all = "if True then if False then foo else baz else bar" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 49 } } - (IfBlock + (If (Node { start = { row = 1, column = 4 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "True")) (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 40 } } - (IfBlock + (If (Node { start = { row = 1, column = 17 }, end = { row = 1, column = 22 } } (FunctionOrValue [] "False")) (Node { start = { row = 1, column = 28 }, end = { row = 1, column = 31 } } (FunctionOrValue [] "foo")) (Node { start = { row = 1, column = 37 }, end = { row = 1, column = 40 } } (FunctionOrValue [] "baz")) @@ -219,10 +218,10 @@ all = "{ model = 0, view = view, update = update }" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 44 } } - (RecordExpr + (Record [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 12 } } ( Node { start = { row = 1, column = 3 }, end = { row = 1, column = 8 } } "model" - , Node { start = { row = 1, column = 11 }, end = { row = 1, column = 12 } } (Integer 0) + , Node { start = { row = 1, column = 11 }, end = { row = 1, column = 12 } } (IntegerLiteral 0) ) , Node { start = { row = 1, column = 14 }, end = { row = 1, column = 25 } } ( Node { start = { row = 1, column = 14 }, end = { row = 1, column = 18 } } "view" @@ -241,14 +240,14 @@ all = |> expectAstWithComments { ast = Node { start = { row = 1, column = 1 }, end = { row = 2, column = 13 } } - (RecordExpr + (Record [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 10 } } ( Node { start = { row = 1, column = 3 }, end = { row = 1, column = 6 } } "foo" - , Node { start = { row = 1, column = 9 }, end = { row = 1, column = 10 } } (Integer 1) + , Node { start = { row = 1, column = 9 }, end = { row = 1, column = 10 } } (IntegerLiteral 1) ) , Node { start = { row = 2, column = 4 }, end = { row = 2, column = 12 } } ( Node { start = { row = 2, column = 4 }, end = { row = 2, column = 7 } } "baz" - , Node { start = { row = 2, column = 10 }, end = { row = 2, column = 11 } } (Integer 2) + , Node { start = { row = 2, column = 10 }, end = { row = 2, column = 11 } } (IntegerLiteral 2) ) ] ) @@ -259,18 +258,18 @@ all = "[ class \"a\", text \"Foo\"]" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } - (ListExpr + (ListLiteral [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 12 } } - (Application - [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "class") - , Node { start = { row = 1, column = 9 }, end = { row = 1, column = 12 } } (Literal "a") - ] + (FunctionCall + (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "class")) + (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 12 } } (StringLiteral SingleQuote "a")) + [] ) , Node { start = { row = 1, column = 14 }, end = { row = 1, column = 24 } } - (Application - [ Node { start = { row = 1, column = 14 }, end = { row = 1, column = 18 } } (FunctionOrValue [] "text") - , Node { start = { row = 1, column = 19 }, end = { row = 1, column = 24 } } (Literal "Foo") - ] + (FunctionCall + (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 18 } } (FunctionOrValue [] "text")) + (Node { start = { row = 1, column = 19 }, end = { row = 1, column = 24 } } (StringLiteral SingleQuote "Foo")) + [] ) ] ) @@ -281,8 +280,8 @@ all = |> expectAstWithComments { ast = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 15 } } - (ListExpr - [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (Integer 1) + (ListLiteral + [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (IntegerLiteral 1) ] ) , comments = [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 13 } } "{- Foo-}" ] @@ -291,7 +290,7 @@ all = \() -> "[{- Foo -}]" |> expectAstWithComments - { ast = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 12 } } (ListExpr []) + { ast = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 12 } } (ListLiteral []) , comments = [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 11 } } "{- Foo -}" ] } , test "qualified expression" <| @@ -343,13 +342,14 @@ all = "{ model | count = 1, loading = True }" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 38 } } - (RecordUpdateExpression + (RecordUpdate (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 8 } } "model") - [ Node { start = { row = 1, column = 11 }, end = { row = 1, column = 20 } } + (Node { start = { row = 1, column = 11 }, end = { row = 1, column = 20 } } ( Node { start = { row = 1, column = 11 }, end = { row = 1, column = 16 } } "count" - , Node { start = { row = 1, column = 19 }, end = { row = 1, column = 20 } } (Integer 1) + , Node { start = { row = 1, column = 19 }, end = { row = 1, column = 20 } } (IntegerLiteral 1) ) - , Node { start = { row = 1, column = 22 }, end = { row = 1, column = 37 } } + ) + [ Node { start = { row = 1, column = 22 }, end = { row = 1, column = 37 } } ( Node { start = { row = 1, column = 22 }, end = { row = 1, column = 29 } } "loading" , Node { start = { row = 1, column = 32 }, end = { row = 1, column = 36 } } (FunctionOrValue [] "True") ) @@ -361,13 +361,14 @@ all = "{model| count = 1, loading = True }" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 36 } } - (RecordUpdateExpression + (RecordUpdate (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 7 } } "model") - [ Node { start = { row = 1, column = 9 }, end = { row = 1, column = 18 } } + (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 18 } } ( Node { start = { row = 1, column = 9 }, end = { row = 1, column = 14 } } "count" - , Node { start = { row = 1, column = 17 }, end = { row = 1, column = 18 } } (Integer 1) + , Node { start = { row = 1, column = 17 }, end = { row = 1, column = 18 } } (IntegerLiteral 1) ) - , Node { start = { row = 1, column = 20 }, end = { row = 1, column = 35 } } + ) + [ Node { start = { row = 1, column = 20 }, end = { row = 1, column = 35 } } ( Node { start = { row = 1, column = 20 }, end = { row = 1, column = 27 } } "loading" , Node { start = { row = 1, column = 30 }, end = { row = 1, column = 34 } } (FunctionOrValue [] "True") ) @@ -379,10 +380,10 @@ all = "List.map .name people" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 22 } } - (Application - [ Node { start = { row = 1, column = 1 }, end = { row = 1, column = 9 } } (FunctionOrValue [ "List" ] "map") - , Node { start = { row = 1, column = 10 }, end = { row = 1, column = 15 } } (RecordAccessFunction ".name") - , Node { start = { row = 1, column = 16 }, end = { row = 1, column = 22 } } (FunctionOrValue [] "people") + (FunctionCall + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 9 } } (FunctionOrValue [ "List" ] "map")) + (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 15 } } (RecordAccessFunction "name")) + [ Node { start = { row = 1, column = 16 }, end = { row = 1, column = 22 } } (FunctionOrValue [] "people") ] ) ) @@ -391,14 +392,14 @@ all = "(.spaceEvenly Internal.Style.classes)" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 38 } } - (ParenthesizedExpression - (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 37 } } - (Application - [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 14 } } (RecordAccessFunction ".spaceEvenly") - , Node { start = { row = 1, column = 15 }, end = { row = 1, column = 37 } } (FunctionOrValue [ "Internal", "Style" ] "classes") - ] + (TupleExpression + [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 37 } } + (FunctionCall + (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 14 } } (RecordAccessFunction "spaceEvenly")) + (Node { start = { row = 1, column = 15 }, end = { row = 1, column = 37 } } (FunctionOrValue [ "Internal", "Style" ] "classes")) + [] ) - ) + ] ) ) , test "positive integer should be invalid" <| @@ -414,10 +415,10 @@ all = "(::) x" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 7 } } - (Application - [ Node { start = { row = 1, column = 1 }, end = { row = 1, column = 5 } } (PrefixOperator "::") - , Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } (FunctionOrValue [] "x") - ] + (FunctionCall + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 5 } } (PrefixOperator "::")) + (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } (FunctionOrValue [] "x")) + [] ) ) , test "subtraction without spaces" <| @@ -425,10 +426,9 @@ all = "2-1" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } - (OperatorApplication "-" - Left - (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 2 } } (Integer 2)) - (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (Integer 1)) + (Operation "-" + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 2 } } (IntegerLiteral 2)) + (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (IntegerLiteral 1)) ) ) , test "negated expression for value" <| @@ -443,11 +443,12 @@ all = "toFloat -5" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 11 } } - (Application - [ Node { start = { row = 1, column = 1 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "toFloat") - , Node { start = { row = 1, column = 9 }, end = { row = 1, column = 11 } } - (Negation (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 11 } } (Integer 5))) - ] + (FunctionCall + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "toFloat")) + (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 11 } } + (Negation (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 11 } } (IntegerLiteral 5))) + ) + [] ) ) , test "negated expression for parenthesized" <| @@ -457,14 +458,13 @@ all = (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 9 } } (Negation (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 9 } } - (ParenthesizedExpression - (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 8 } } - (OperatorApplication "-" - Left + (TupleExpression + [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 8 } } + (Operation "-" (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (FunctionOrValue [] "x")) (Node { start = { row = 1, column = 7 }, end = { row = 1, column = 8 } } (FunctionOrValue [] "y")) ) - ) + ] ) ) ) @@ -474,29 +474,25 @@ all = "-1 + -10 * -100^2 == -100001" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 29 } } - (OperatorApplication "==" - Non + (Operation "==" (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 18 } } - (OperatorApplication "+" - Left + (Operation "+" (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 3 } } - (Negation (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 3 } } (Integer 1))) + (Negation (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 3 } } (IntegerLiteral 1))) ) (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 18 } } - (OperatorApplication "*" - Left + (Operation "*" (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 9 } } - (Negation (Node { start = { row = 1, column = 7 }, end = { row = 1, column = 9 } } (Integer 10))) + (Negation (Node { start = { row = 1, column = 7 }, end = { row = 1, column = 9 } } (IntegerLiteral 10))) ) (Node { start = { row = 1, column = 12 }, end = { row = 1, column = 18 } } - (OperatorApplication "^" - Right + (Operation "^" (Node { start = { row = 1, column = 12 }, end = { row = 1, column = 16 } } (Negation - (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 16 } } (Integer 100)) + (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 16 } } (IntegerLiteral 100)) ) ) - (Node { start = { row = 1, column = 17 }, end = { row = 1, column = 18 } } (Integer 2)) + (Node { start = { row = 1, column = 17 }, end = { row = 1, column = 18 } } (IntegerLiteral 2)) ) ) ) @@ -504,7 +500,7 @@ all = ) ) (Node { start = { row = 1, column = 22 }, end = { row = 1, column = 29 } } - (Negation (Node { start = { row = 1, column = 23 }, end = { row = 1, column = 29 } } (Integer 100001))) + (Negation (Node { start = { row = 1, column = 23 }, end = { row = 1, column = 29 } } (IntegerLiteral 100001))) ) ) ) @@ -514,16 +510,14 @@ all = |> expectAst (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 11 } } - (OperatorApplication "-" - Left + (Operation "-" (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 7 } } - (OperatorApplication "+" - Left - (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 3 } } (Integer 1)) - (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } (Integer 2)) + (Operation "+" + (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 3 } } (IntegerLiteral 1)) + (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } (IntegerLiteral 2)) ) ) - (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 11 } } (Integer 3)) + (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 11 } } (IntegerLiteral 3)) ) ) , test "pipe operation" <| @@ -531,8 +525,7 @@ all = "a |> b" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 7 } } - (OperatorApplication "|>" - Left + (Operation "|>" (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 2 } } (FunctionOrValue [] "a")) (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 7 } } (FunctionOrValue [] "b")) ) @@ -542,30 +535,27 @@ all = "chompWhile (\\c -> c == ' ' || c == '\\n' || c == '\\r')" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 54 } } - (Application - [ Node { start = { row = 1, column = 1 }, end = { row = 1, column = 11 } } (FunctionOrValue [] "chompWhile") - , Node { start = { row = 1, column = 12 }, end = { row = 1, column = 54 } } - (ParenthesizedExpression - (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 53 } } + (FunctionCall + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 11 } } (FunctionOrValue [] "chompWhile")) + (Node { start = { row = 1, column = 12 }, end = { row = 1, column = 54 } } + (TupleExpression + [ Node { start = { row = 1, column = 13 }, end = { row = 1, column = 53 } } (LambdaExpression - { args = [ Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } (VarPattern "c") ] + { firstArg = Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } (VarPattern_ "c") + , restOfArgs = [] , expression = Node { start = { row = 1, column = 19 }, end = { row = 1, column = 53 } } - (OperatorApplication "||" - Right + (Operation "||" (Node { start = { row = 1, column = 19 }, end = { row = 1, column = 27 } } - (OperatorApplication "==" - Non + (Operation "==" (Node { start = { row = 1, column = 19 }, end = { row = 1, column = 20 } } (FunctionOrValue [] "c")) (Node { start = { row = 1, column = 24 }, end = { row = 1, column = 27 } } (CharLiteral ' ')) ) ) (Node { start = { row = 1, column = 31 }, end = { row = 1, column = 53 } } - (OperatorApplication "||" - Right + (Operation "||" (Node { start = { row = 1, column = 31 }, end = { row = 1, column = 40 } } - (OperatorApplication "==" - Non + (Operation "==" (Node { start = { row = 1, column = 31 }, end = { row = 1, column = 32 } } (FunctionOrValue [] "c")) (Node { start = { row = 1, column = 36 }, end = { row = 1, column = 40 } } (CharLiteral '\n') @@ -573,8 +563,7 @@ all = ) ) (Node { start = { row = 1, column = 44 }, end = { row = 1, column = 53 } } - (OperatorApplication "==" - Non + (Operation "==" (Node { start = { row = 1, column = 44 }, end = { row = 1, column = 45 } } (FunctionOrValue [] "c")) (Node { start = { row = 1, column = 49 }, end = { row = 1, column = 53 } } (CharLiteral '\u{000D}') @@ -586,9 +575,10 @@ all = ) } ) - ) + ] ) - ] + ) + [] ) ) , test "application should be lower-priority than field access" <| @@ -596,28 +586,30 @@ all = "foo { d | b = f x y }.b" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 24 } } - (Application - [ Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } (FunctionOrValue [] "foo") - , Node { start = { row = 1, column = 5 }, end = { row = 1, column = 24 } } + (FunctionCall + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } (FunctionOrValue [] "foo")) + (Node { start = { row = 1, column = 5 }, end = { row = 1, column = 24 } } (RecordAccess (Node { start = { row = 1, column = 5 }, end = { row = 1, column = 22 } } - (RecordUpdateExpression (Node { start = { row = 1, column = 7 }, end = { row = 1, column = 8 } } "d") - [ Node { start = { row = 1, column = 11 }, end = { row = 1, column = 21 } } + (RecordUpdate (Node { start = { row = 1, column = 7 }, end = { row = 1, column = 8 } } "d") + (Node { start = { row = 1, column = 11 }, end = { row = 1, column = 21 } } ( Node { start = { row = 1, column = 11 }, end = { row = 1, column = 12 } } "b" , Node { start = { row = 1, column = 15 }, end = { row = 1, column = 20 } } - (Application - [ Node { start = { row = 1, column = 15 }, end = { row = 1, column = 16 } } (FunctionOrValue [] "f") - , Node { start = { row = 1, column = 17 }, end = { row = 1, column = 18 } } (FunctionOrValue [] "x") - , Node { start = { row = 1, column = 19 }, end = { row = 1, column = 20 } } (FunctionOrValue [] "y") + (FunctionCall + (Node { start = { row = 1, column = 15 }, end = { row = 1, column = 16 } } (FunctionOrValue [] "f")) + (Node { start = { row = 1, column = 17 }, end = { row = 1, column = 18 } } (FunctionOrValue [] "x")) + [ Node { start = { row = 1, column = 19 }, end = { row = 1, column = 20 } } (FunctionOrValue [] "y") ] ) ) - ] + ) + [] ) ) (Node { start = { row = 1, column = 23 }, end = { row = 1, column = 24 } } "b") ) - ] + ) + [] ) ) , test "should not consider a negative number parameter as the start of a new application" <| @@ -625,15 +617,16 @@ all = "Random.list -1 generator" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } - (Application - [ Node { start = { row = 1, column = 1 }, end = { row = 1, column = 12 } } (FunctionOrValue [ "Random" ] "list") - , Node { start = { row = 1, column = 13 }, end = { row = 1, column = 15 } } + (FunctionCall + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 12 } } (FunctionOrValue [ "Random" ] "list")) + (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 15 } } (Negation (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } - (Integer 1) + (IntegerLiteral 1) ) ) - , Node { start = { row = 1, column = 16 }, end = { row = 1, column = 25 } } (FunctionOrValue [] "generator") + ) + [ Node { start = { row = 1, column = 16 }, end = { row = 1, column = 25 } } (FunctionOrValue [] "generator") ] ) ) @@ -642,18 +635,17 @@ all = "1 + -{x = 10}.x" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 16 } } - (OperatorApplication "+" - Left - (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 2 } } (Integer 1)) + (Operation "+" + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 2 } } (IntegerLiteral 1)) (Node { start = { row = 1, column = 5 }, end = { row = 1, column = 16 } } (Negation (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 16 } } (RecordAccess (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 14 } } - (RecordExpr + (Record [ Node { start = { row = 1, column = 7 }, end = { row = 1, column = 13 } } ( Node { start = { row = 1, column = 7 }, end = { row = 1, column = 8 } } "x" - , Node { start = { row = 1, column = 11 }, end = { row = 1, column = 13 } } (Integer 10) + , Node { start = { row = 1, column = 11 }, end = { row = 1, column = 13 } } (IntegerLiteral 10) ) ] ) diff --git a/tests/Elm/Parser/FileTests.elm b/tests/Elm/Parser/FileTests.elm index ad92b15c2..29e18b947 100644 --- a/tests/Elm/Parser/FileTests.elm +++ b/tests/Elm/Parser/FileTests.elm @@ -1,21 +1,17 @@ module Elm.Parser.FileTests exposing (all) -import Elm.Internal.RawFile as InternalRawFile import Elm.Parser import Elm.Parser.File as Parser import Elm.Parser.Samples as Samples -import Elm.RawFile as RawFile exposing (RawFile) import Elm.Syntax.Declaration exposing (Declaration(..)) +import Elm.Syntax.DestructurePattern exposing (DestructurePattern(..)) import Elm.Syntax.Exposing exposing (Exposing(..)) import Elm.Syntax.Expression exposing (Expression(..), LetDeclaration(..)) -import Elm.Syntax.Infix exposing (InfixDirection(..)) import Elm.Syntax.Module exposing (Module(..)) import Elm.Syntax.Node exposing (Node(..)) import Elm.Syntax.Pattern exposing (Pattern(..)) import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation(..)) import Expect -import Json.Decode -import Json.Encode import ParserFast import Test exposing (..) @@ -52,27 +48,6 @@ all = -- Parser.parse "module Foo exposing (..) \nx = \n x + _" -- |> Expect.equal (Err [ "Could not continue parsing on location (2,6)" ]) -- ] - , describe "FileTests - serialisation" - (Samples.allSamples - |> List.map - (\( n, s ) -> - test ("sample " ++ String.fromInt n) <| - \() -> - let - parsed : Maybe RawFile - parsed = - ParserFast.run Parser.file s - |> Result.toMaybe - |> Maybe.map InternalRawFile.Raw - - roundTrip : Maybe RawFile - roundTrip = - parsed - |> Maybe.andThen (RawFile.encode >> Json.Encode.encode 0 >> Json.Decode.decodeString RawFile.decoder >> Result.toMaybe) - in - Expect.equal parsed roundTrip - ) - ) , test "Comments ordering" <| \() -> let @@ -100,7 +75,7 @@ f = {- 6 -} """ in - Elm.Parser.parseToFile input + Elm.Parser.parse input |> Result.map .comments |> Expect.equal (Ok @@ -133,7 +108,7 @@ b = 1 """ - |> Elm.Parser.parseToFile + |> Elm.Parser.parse |> Result.map .comments |> Expect.equal (Ok @@ -159,7 +134,7 @@ caseWhitespace f = case f of """ - |> Elm.Parser.parseToFile + |> Elm.Parser.parse |> Expect.equal (Ok { moduleDefinition = @@ -180,17 +155,18 @@ caseWhitespace f = case f of , declaration = Node { start = { row = 4, column = 1 }, end = { row = 11, column = 11 } } { name = Node { start = { row = 4, column = 1 }, end = { row = 4, column = 15 } } "caseWhitespace" - , arguments = [ Node { start = { row = 4, column = 16 }, end = { row = 4, column = 17 } } (VarPattern "f") ] + , arguments = [ Node { start = { row = 4, column = 16 }, end = { row = 4, column = 17 } } (VarPattern_ "f") ] , expression = Node { start = { row = 4, column = 20 }, end = { row = 11, column = 11 } } - (CaseExpression + (Case { expression = Node { start = { row = 4, column = 25 }, end = { row = 4, column = 26 } } (FunctionOrValue [] "f") - , cases = - [ ( Node { start = { row = 5, column = 3 }, end = { row = 5, column = 7 } } (NamedPattern { moduleName = [], name = "True" } []) - , Node { start = { row = 6, column = 5 }, end = { row = 6, column = 6 } } (Integer 1) - ) - , ( Node { start = { row = 7, column = 3 }, end = { row = 7, column = 8 } } (NamedPattern { moduleName = [], name = "False" } []) - , Node { start = { row = 11, column = 10 }, end = { row = 11, column = 11 } } (Integer 2) + , firstCase = + ( Node { start = { row = 5, column = 3 }, end = { row = 5, column = 7 } } (NamedPattern { moduleName = [], name = "True" } []) + , Node { start = { row = 6, column = 5 }, end = { row = 6, column = 6 } } (IntegerLiteral 1) + ) + , restOfCases = + [ ( Node { start = { row = 7, column = 3 }, end = { row = 7, column = 8 } } (NamedPattern { moduleName = [], name = "False" } []) + , Node { start = { row = 11, column = 10 }, end = { row = 11, column = 11 } } (IntegerLiteral 2) ) ] } @@ -217,7 +193,7 @@ lambdaWhitespace = \\ a b -> a --some comment """ - |> Elm.Parser.parseToFile + |> Elm.Parser.parse |> Expect.equal (Ok { moduleDefinition = @@ -242,14 +218,12 @@ lambdaWhitespace = \\ a b -> a , expression = Node { start = { row = 4, column = 22 }, end = { row = 8, column = 6 } } (LambdaExpression - { args = - [ Node { start = { row = 4, column = 24 }, end = { row = 4, column = 25 } } (VarPattern "a") - , Node { start = { row = 4, column = 26 }, end = { row = 4, column = 27 } } (VarPattern "b") - ] + { firstArg = Node { start = { row = 4, column = 24 }, end = { row = 4, column = 25 } } (VarPattern_ "a") + , restOfArgs = + [ Node { start = { row = 4, column = 26 }, end = { row = 4, column = 27 } } (VarPattern_ "b") ] , expression = Node { start = { row = 4, column = 34 }, end = { row = 8, column = 6 } } - (OperatorApplication "+" - Left + (Operation "+" (Node { start = { row = 4, column = 34 }, end = { row = 4, column = 35 } } (FunctionOrValue [] "a")) (Node { start = { row = 8, column = 5 }, end = { row = 8, column = 6 } } (FunctionOrValue [] "b")) ) @@ -277,7 +251,7 @@ letWhitespace = let --some comment """ - |> Elm.Parser.parseToFile + |> Elm.Parser.parse |> Expect.equal (Ok { moduleDefinition = @@ -301,7 +275,7 @@ letWhitespace = let , arguments = [] , expression = Node { start = { row = 4, column = 17 }, end = { row = 8, column = 3 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 5, column = 19 }, end = { row = 5, column = 26 } } (LetFunction @@ -311,7 +285,7 @@ letWhitespace = let Node { start = { row = 5, column = 19 }, end = { row = 5, column = 26 } } { name = Node { start = { row = 5, column = 19 }, end = { row = 5, column = 20 } } "b" , arguments = [] - , expression = Node { start = { row = 5, column = 25 }, end = { row = 5, column = 26 } } (Integer 1) + , expression = Node { start = { row = 5, column = 25 }, end = { row = 5, column = 26 } } (IntegerLiteral 1) } } ) @@ -338,7 +312,7 @@ import Dict type Configuration = Configuration """ - |> Elm.Parser.parseToFile + |> Elm.Parser.parse |> Expect.equal (Ok { moduleDefinition = @@ -363,12 +337,12 @@ type Configuration { documentation = Just (Node { start = { row = 6, column = 1 }, end = { row = 7, column = 3 } } "{-| Config goes here\n-}") , name = Node { start = { row = 8, column = 6 }, end = { row = 8, column = 19 } } "Configuration" , generics = [] - , constructors = - [ Node { start = { row = 9, column = 7 }, end = { row = 9, column = 20 } } + , firstConstructor = + Node { start = { row = 9, column = 7 }, end = { row = 9, column = 20 } } { name = Node { start = { row = 9, column = 7 }, end = { row = 9, column = 20 } } "Configuration" , arguments = [] } - ] + , restOfConstructors = [] } ) ] @@ -385,7 +359,7 @@ module Foo exposing (..) type Configuration = Configuration """ - |> Elm.Parser.parseToFile + |> Elm.Parser.parse |> Expect.equal (Ok { moduleDefinition = @@ -404,19 +378,19 @@ type Configuration { documentation = Nothing , name = Node { start = { row = 6, column = 6 }, end = { row = 6, column = 19 } } "Configuration" , generics = [] - , constructors = - [ Node { start = { row = 7, column = 7 }, end = { row = 7, column = 20 } } + , firstConstructor = + Node { start = { row = 7, column = 7 }, end = { row = 7, column = 20 } } { name = Node { start = { row = 7, column = 7 }, end = { row = 7, column = 20 } } "Configuration" , arguments = [] } - ] + , restOfConstructors = [] } ) ] , comments = [ Node { start = { row = 4, column = 1 }, end = { row = 5, column = 3 } } "{-| actually module doc\n-}" ] } ) - , test "documentation on a port declaration is treated as a normal comment" <| + , test "documentation on a port declaration" <| \() -> """ port module Foo exposing (..) @@ -427,7 +401,7 @@ import String -} port sendResponse : String -> Cmd msg """ - |> Elm.Parser.parseToFile + |> Elm.Parser.parse |> Expect.equal (Ok { moduleDefinition = @@ -447,25 +421,29 @@ port sendResponse : String -> Cmd msg } ] , declarations = - [ Node { start = { row = 8, column = 1 }, end = { row = 8, column = 38 } } + [ Node { start = { row = 6, column = 1 }, end = { row = 8, column = 38 } } (PortDeclaration - { name = Node { start = { row = 8, column = 6 }, end = { row = 8, column = 18 } } "sendResponse" - , typeAnnotation = - Node { start = { row = 8, column = 21 }, end = { row = 8, column = 38 } } - (FunctionTypeAnnotation - (Node { start = { row = 8, column = 21 }, end = { row = 8, column = 27 } } - (Typed (Node { start = { row = 8, column = 21 }, end = { row = 8, column = 27 } } ( [], "String" )) []) - ) - (Node { start = { row = 8, column = 31 }, end = { row = 8, column = 38 } } - (Typed (Node { start = { row = 8, column = 31 }, end = { row = 8, column = 34 } } ( [], "Cmd" )) - [ Node { start = { row = 8, column = 35 }, end = { row = 8, column = 38 } } (GenericType "msg") ] + { documentation = Just (Node { start = { row = 6, column = 1 }, end = { row = 7, column = 3 } } "{-| foo\n-}") + , signature = + Node { start = { row = 8, column = 6 }, end = { row = 8, column = 38 } } + { name = Node { start = { row = 8, column = 6 }, end = { row = 8, column = 18 } } "sendResponse" + , typeAnnotation = + Node { start = { row = 8, column = 21 }, end = { row = 8, column = 38 } } + (FunctionTypeAnnotation + (Node { start = { row = 8, column = 21 }, end = { row = 8, column = 27 } } + (Type (Node { start = { row = 8, column = 21 }, end = { row = 8, column = 27 } } ( [], "String" )) []) + ) + (Node { start = { row = 8, column = 31 }, end = { row = 8, column = 38 } } + (Type (Node { start = { row = 8, column = 31 }, end = { row = 8, column = 34 } } ( [], "Cmd" )) + [ Node { start = { row = 8, column = 35 }, end = { row = 8, column = 38 } } (Var "msg") ] + ) + ) ) - ) - ) + } } ) ] - , comments = [ Node { start = { row = 6, column = 1 }, end = { row = 7, column = 3 } } "{-| foo\n-}" ] + , comments = [] } ) ] diff --git a/tests/Elm/Parser/GlslTests.elm b/tests/Elm/Parser/GlslTests.elm index 83ec2c69e..1a4bd179b 100644 --- a/tests/Elm/Parser/GlslTests.elm +++ b/tests/Elm/Parser/GlslTests.elm @@ -16,7 +16,7 @@ all = "[glsl| precision mediump float; |]" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 37 } } - (GLSLExpression " precision mediump float; ") + (GLSL " precision mediump float; ") ) ] diff --git a/tests/Elm/Parser/ImportsTests.elm b/tests/Elm/Parser/ImportsTests.elm index 34f4f5e5b..4095a029c 100644 --- a/tests/Elm/Parser/ImportsTests.elm +++ b/tests/Elm/Parser/ImportsTests.elm @@ -23,7 +23,8 @@ all = Just (Node { start = { row = 1, column = 12 }, end = { row = 1, column = 37 } } (Explicit - [ Node { start = { row = 1, column = 22 }, end = { row = 1, column = 27 } } (TypeOrAliasExpose "Model"), Node { start = { row = 1, column = 29 }, end = { row = 1, column = 36 } } (TypeExpose { name = "Msg", open = Just { start = { row = 1, column = 32 }, end = { row = 1, column = 36 } } }) ] + (Node { start = { row = 1, column = 22 }, end = { row = 1, column = 27 } } (TypeOrAliasExpose "Model")) + [ Node { start = { row = 1, column = 29 }, end = { row = 1, column = 36 } } (TypeExpose { name = "Msg", open = Just { start = { row = 1, column = 32 }, end = { row = 1, column = 36 } } }) ] ) ) } @@ -38,9 +39,7 @@ all = , exposingList = Just (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 28 } } - (Explicit - [ Node { start = { row = 1, column = 23 }, end = { row = 1, column = 27 } } (FunctionExpose "text") ] - ) + (Explicit (Node { start = { row = 1, column = 23 }, end = { row = 1, column = 27 } } (FunctionExpose "text")) []) ) } ) diff --git a/tests/Elm/Parser/LambdaExpressionTests.elm b/tests/Elm/Parser/LambdaExpressionTests.elm index 3ef82799f..b66a210f4 100644 --- a/tests/Elm/Parser/LambdaExpressionTests.elm +++ b/tests/Elm/Parser/LambdaExpressionTests.elm @@ -2,10 +2,9 @@ module Elm.Parser.LambdaExpressionTests exposing (all) import Elm.Parser.Expression exposing (expression) import Elm.Parser.ParserWithCommentsTestUtil as ParserWithCommentsUtil +import Elm.Syntax.DestructurePattern exposing (DestructurePattern(..)) import Elm.Syntax.Expression exposing (..) -import Elm.Syntax.Infix exposing (InfixDirection(..)) import Elm.Syntax.Node exposing (Node(..)) -import Elm.Syntax.Pattern exposing (..) import Expect import Test exposing (..) @@ -19,7 +18,8 @@ all = |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 11 } } (LambdaExpression - { args = [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 4 } } UnitPattern ] + { firstArg = Node { start = { row = 1, column = 2 }, end = { row = 1, column = 4 } } UnitPattern_ + , restOfArgs = [] , expression = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } (FunctionOrValue [] "foo") } ) @@ -30,7 +30,8 @@ all = |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 14 } } (LambdaExpression - { args = [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 7 } } (RecordPattern [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 6 } } "foo" ]) ] + { firstArg = Node { start = { row = 1, column = 2 }, end = { row = 1, column = 7 } } (RecordPattern_ [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 6 } } "foo" ]) + , restOfArgs = [] , expression = Node { start = { row = 1, column = 11 }, end = { row = 1, column = 14 } } (FunctionOrValue [] "foo") } ) @@ -41,7 +42,8 @@ all = |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 11 } } (LambdaExpression - { args = [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 4 } } (RecordPattern []) ] + { firstArg = Node { start = { row = 1, column = 2 }, end = { row = 1, column = 4 } } (RecordPattern_ []) + , restOfArgs = [] , expression = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } (FunctionOrValue [] "foo") } ) @@ -52,14 +54,13 @@ all = |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 14 } } (LambdaExpression - { args = - [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 3 } } (VarPattern "a") - , Node { start = { row = 1, column = 4 }, end = { row = 1, column = 5 } } (VarPattern "b") + { firstArg = Node { start = { row = 1, column = 2 }, end = { row = 1, column = 3 } } (VarPattern_ "a") + , restOfArgs = + [ Node { start = { row = 1, column = 4 }, end = { row = 1, column = 5 } } (VarPattern_ "b") ] , expression = Node { start = { row = 1, column = 9 }, end = { row = 1, column = 14 } } - (OperatorApplication "+" - Left + (Operation "+" (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 10 } } (FunctionOrValue [] "a")) (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 14 } } (FunctionOrValue [] "b")) ) @@ -72,18 +73,17 @@ all = |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 16 } } (LambdaExpression - { args = - [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 7 } } - (TuplePattern - [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (VarPattern "a") - , Node { start = { row = 1, column = 5 }, end = { row = 1, column = 6 } } (VarPattern "b") + { firstArg = + Node { start = { row = 1, column = 2 }, end = { row = 1, column = 7 } } + (TuplePattern_ + [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 4 } } (VarPattern_ "a") + , Node { start = { row = 1, column = 5 }, end = { row = 1, column = 6 } } (VarPattern_ "b") ] ) - ] + , restOfArgs = [] , expression = Node { start = { row = 1, column = 11 }, end = { row = 1, column = 16 } } - (OperatorApplication "+" - Left + (Operation "+" (Node { start = { row = 1, column = 11 }, end = { row = 1, column = 12 } } (FunctionOrValue [] "a")) (Node { start = { row = 1, column = 15 }, end = { row = 1, column = 16 } } (FunctionOrValue [] "b")) ) diff --git a/tests/Elm/Parser/LetExpressionTests.elm b/tests/Elm/Parser/LetExpressionTests.elm index 80e88bb60..ba3323cc2 100644 --- a/tests/Elm/Parser/LetExpressionTests.elm +++ b/tests/Elm/Parser/LetExpressionTests.elm @@ -2,9 +2,9 @@ module Elm.Parser.LetExpressionTests exposing (all) import Elm.Parser.Expression exposing (expression) import Elm.Parser.ParserWithCommentsTestUtil as ParserWithCommentsUtil -import Elm.Syntax.Expression exposing (..) +import Elm.Syntax.DestructurePattern exposing (DestructurePattern(..)) +import Elm.Syntax.Expression as Expression exposing (..) import Elm.Syntax.Node exposing (Node(..)) -import Elm.Syntax.Pattern exposing (..) import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation(..)) import Expect import Test exposing (..) @@ -12,7 +12,7 @@ import Test exposing (..) all : Test all = - describe "LetExpressionTests" + describe "LetTests" [ test "let expression with multiple declarations" <| \() -> """let @@ -21,7 +21,7 @@ all = john n = n in 1""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 4, column = 18 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 2, column = 3 }, end = { row = 2, column = 12 } } (LetFunction @@ -42,13 +42,13 @@ all = , declaration = Node { start = { row = 4, column = 3 }, end = { row = 4, column = 13 } } { name = Node { start = { row = 4, column = 3 }, end = { row = 4, column = 7 } } "john" - , arguments = [ Node { start = { row = 4, column = 8 }, end = { row = 4, column = 9 } } (VarPattern "n") ] + , arguments = [ Node { start = { row = 4, column = 8 }, end = { row = 4, column = 9 } } (VarPattern_ "n") ] , expression = Node { start = { row = 4, column = 12 }, end = { row = 4, column = 13 } } (FunctionOrValue [] "n") } } ) ] - , expression = Node { start = { row = 4, column = 17 }, end = { row = 4, column = 18 } } (Integer 1) + , expression = Node { start = { row = 4, column = 17 }, end = { row = 4, column = 18 } } (IntegerLiteral 1) } ) ) @@ -60,7 +60,7 @@ all = bar""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 4, column = 6 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 2, column = 3 }, end = { row = 2, column = 10 } } (LetFunction @@ -70,7 +70,7 @@ all = Node { start = { row = 2, column = 3 }, end = { row = 2, column = 10 } } { name = Node { start = { row = 2, column = 3 }, end = { row = 2, column = 6 } } "bar" , arguments = [] - , expression = Node { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } } (Integer 1) + , expression = Node { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } } (IntegerLiteral 1) } } ) @@ -102,7 +102,7 @@ all = bar""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 4, column = 7 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 2, column = 3 }, end = { row = 2, column = 10 } } (LetFunction @@ -112,7 +112,7 @@ all = Node { start = { row = 2, column = 3 }, end = { row = 2, column = 10 } } { name = Node { start = { row = 2, column = 3 }, end = { row = 2, column = 6 } } "bar" , arguments = [] - , expression = Node { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } } (Integer 1) + , expression = Node { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } } (IntegerLiteral 1) } } ) @@ -130,7 +130,7 @@ all = bar""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 5, column = 6 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 2, column = 5 }, end = { row = 3, column = 12 } } (LetFunction @@ -141,14 +141,14 @@ all = { name = Node { start = { row = 2, column = 5 }, end = { row = 2, column = 8 } } "bar" , typeAnnotation = Node { start = { row = 2, column = 11 }, end = { row = 2, column = 14 } } - (Typed (Node { start = { row = 2, column = 11 }, end = { row = 2, column = 14 } } ( [], "Int" )) []) + (Type (Node { start = { row = 2, column = 11 }, end = { row = 2, column = 14 } } ( [], "Int" )) []) } ) , declaration = Node { start = { row = 3, column = 5 }, end = { row = 3, column = 12 } } { name = Node { start = { row = 3, column = 5 }, end = { row = 3, column = 8 } } "bar" , arguments = [] - , expression = Node { start = { row = 3, column = 11 }, end = { row = 3, column = 12 } } (Integer 1) + , expression = Node { start = { row = 3, column = 11 }, end = { row = 3, column = 12 } } (IntegerLiteral 1) } } ) @@ -168,7 +168,7 @@ all = bar""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 7, column = 6 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 2, column = 5 }, end = { row = 5, column = 12 } } (LetFunction @@ -177,14 +177,14 @@ all = Just (Node { start = { row = 2, column = 5 }, end = { row = 2, column = 14 } } { name = Node { start = { row = 2, column = 5 }, end = { row = 2, column = 8 } } "bar" - , typeAnnotation = Node { start = { row = 2, column = 11 }, end = { row = 2, column = 14 } } (Typed (Node { start = { row = 2, column = 11 }, end = { row = 2, column = 14 } } ( [], "Int" )) []) + , typeAnnotation = Node { start = { row = 2, column = 11 }, end = { row = 2, column = 14 } } (Type (Node { start = { row = 2, column = 11 }, end = { row = 2, column = 14 } } ( [], "Int" )) []) } ) , declaration = Node { start = { row = 5, column = 5 }, end = { row = 5, column = 12 } } { name = Node { start = { row = 5, column = 5 }, end = { row = 5, column = 8 } } "bar" , arguments = [] - , expression = Node { start = { row = 5, column = 11 }, end = { row = 5, column = 12 } } (Integer 1) + , expression = Node { start = { row = 5, column = 11 }, end = { row = 5, column = 12 } } (IntegerLiteral 1) } } ) @@ -224,7 +224,7 @@ all = bar""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 4, column = 12 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 2, column = 11 }, end = { row = 2, column = 24 } } (LetFunction @@ -234,10 +234,10 @@ all = Node { start = { row = 2, column = 11 }, end = { row = 2, column = 24 } } { name = Node { start = { row = 2, column = 11 }, end = { row = 2, column = 14 } } "bar" , arguments = - [ Node { start = { row = 2, column = 15 }, end = { row = 2, column = 18 } } (NamedPattern { moduleName = [], name = "Bar" } []) - , Node { start = { row = 2, column = 19 }, end = { row = 2, column = 20 } } (VarPattern "m") + [ Node { start = { row = 2, column = 15 }, end = { row = 2, column = 18 } } (NamedPattern_ { moduleName = [], name = "Bar" } []) + , Node { start = { row = 2, column = 19 }, end = { row = 2, column = 20 } } (VarPattern_ "m") ] - , expression = Node { start = { row = 2, column = 23 }, end = { row = 2, column = 24 } } (Integer 1) + , expression = Node { start = { row = 2, column = 23 }, end = { row = 2, column = 24 } } (IntegerLiteral 1) } } ) @@ -302,23 +302,23 @@ all = 1""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 7, column = 6 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 2, column = 5 }, end = { row = 2, column = 10 } } - (LetDestructuring (Node { start = { row = 2, column = 5 }, end = { row = 2, column = 6 } } AllPattern) (Node { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } } (FunctionOrValue [] "b"))) + (LetDestructuring (Node { start = { row = 2, column = 5 }, end = { row = 2, column = 6 } } AllPattern_) (Node { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } } (FunctionOrValue [] "b"))) , Node { start = { row = 3, column = 5 }, end = { row = 3, column = 12 } } (LetDestructuring (Node { start = { row = 3, column = 5 }, end = { row = 3, column = 8 } } - (RecordPattern [ Node { start = { row = 3, column = 6 }, end = { row = 3, column = 7 } } "a" ]) + (RecordPattern_ [ Node { start = { row = 3, column = 6 }, end = { row = 3, column = 7 } } "a" ]) ) (Node { start = { row = 3, column = 11 }, end = { row = 3, column = 12 } } (FunctionOrValue [] "b")) ) , Node { start = { row = 4, column = 5 }, end = { row = 4, column = 15 } } (LetDestructuring (Node { start = { row = 4, column = 5 }, end = { row = 4, column = 11 } } - (TuplePattern - [ Node { start = { row = 4, column = 6 }, end = { row = 4, column = 7 } } (VarPattern "c") - , Node { start = { row = 4, column = 9 }, end = { row = 4, column = 10 } } (VarPattern "d") + (TuplePattern_ + [ Node { start = { row = 4, column = 6 }, end = { row = 4, column = 7 } } (VarPattern_ "c") + , Node { start = { row = 4, column = 9 }, end = { row = 4, column = 10 } } (VarPattern_ "d") ] ) ) @@ -327,14 +327,20 @@ all = , Node { start = { row = 5, column = 5 }, end = { row = 5, column = 19 } } (LetDestructuring (Node { start = { row = 5, column = 5 }, end = { row = 5, column = 15 } } - (ParenthesizedPattern - (Node { start = { row = 5, column = 6 }, end = { row = 5, column = 14 } } (NamedPattern { moduleName = [], name = "Node" } [ Node { start = { row = 5, column = 11 }, end = { row = 5, column = 12 } } AllPattern, Node { start = { row = 5, column = 13 }, end = { row = 5, column = 14 } } (VarPattern "f") ])) + (ParenthesizedPattern_ + (Node { start = { row = 5, column = 6 }, end = { row = 5, column = 14 } } + (NamedPattern_ { moduleName = [], name = "Node" } + [ Node { start = { row = 5, column = 11 }, end = { row = 5, column = 12 } } AllPattern_ + , Node { start = { row = 5, column = 13 }, end = { row = 5, column = 14 } } (VarPattern_ "f") + ] + ) + ) ) ) (Node { start = { row = 5, column = 18 }, end = { row = 5, column = 19 } } (FunctionOrValue [] "g")) ) ] - , expression = Node { start = { row = 7, column = 5 }, end = { row = 7, column = 6 } } (Integer 1) + , expression = Node { start = { row = 7, column = 5 }, end = { row = 7, column = 6 } } (IntegerLiteral 1) } ) ) @@ -343,7 +349,7 @@ all = "let indent = String.length s in indent" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 39 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 29 } } (LetFunction @@ -355,10 +361,10 @@ all = , arguments = [] , expression = Node { start = { row = 1, column = 14 }, end = { row = 1, column = 29 } } - (Application - [ Node { start = { row = 1, column = 14 }, end = { row = 1, column = 27 } } (FunctionOrValue [ "String" ] "length") - , Node { start = { row = 1, column = 28 }, end = { row = 1, column = 29 } } (FunctionOrValue [] "s") - ] + (FunctionCall + (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 27 } } (FunctionOrValue [ "String" ] "length")) + (Node { start = { row = 1, column = 28 }, end = { row = 1, column = 29 } } (FunctionOrValue [] "s")) + [] ) } } @@ -375,7 +381,7 @@ all = in[]""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 3, column = 9 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 2, column = 9 }, end = { row = 2, column = 14 } } (LetFunction @@ -385,12 +391,12 @@ all = Node { start = { row = 2, column = 9 }, end = { row = 2, column = 14 } } { name = Node { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } } "a" , arguments = [] - , expression = Node { start = { row = 2, column = 13 }, end = { row = 2, column = 14 } } (Integer 1) + , expression = Node { start = { row = 2, column = 13 }, end = { row = 2, column = 14 } } (IntegerLiteral 1) } } ) ] - , expression = Node { start = { row = 3, column = 7 }, end = { row = 3, column = 9 } } (ListExpr []) + , expression = Node { start = { row = 3, column = 7 }, end = { row = 3, column = 9 } } (ListLiteral []) } ) ) @@ -401,7 +407,7 @@ all = in{}""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 3, column = 9 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 2, column = 9 }, end = { row = 2, column = 14 } } (LetFunction @@ -411,12 +417,12 @@ all = Node { start = { row = 2, column = 9 }, end = { row = 2, column = 14 } } { name = Node { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } } "a" , arguments = [] - , expression = Node { start = { row = 2, column = 13 }, end = { row = 2, column = 14 } } (Integer 1) + , expression = Node { start = { row = 2, column = 13 }, end = { row = 2, column = 14 } } (IntegerLiteral 1) } } ) ] - , expression = Node { start = { row = 3, column = 7 }, end = { row = 3, column = 9 } } (RecordExpr []) + , expression = Node { start = { row = 3, column = 7 }, end = { row = 3, column = 9 } } (Expression.Record []) } ) ) @@ -427,7 +433,7 @@ all = in\\_ -> 1""" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 3, column = 14 } } - (LetExpression + (Let { declarations = [ Node { start = { row = 2, column = 9 }, end = { row = 2, column = 14 } } (LetFunction @@ -437,14 +443,19 @@ all = Node { start = { row = 2, column = 9 }, end = { row = 2, column = 14 } } { name = Node { start = { row = 2, column = 9 }, end = { row = 2, column = 10 } } "a" , arguments = [] - , expression = Node { start = { row = 2, column = 13 }, end = { row = 2, column = 14 } } (Integer 1) + , expression = Node { start = { row = 2, column = 13 }, end = { row = 2, column = 14 } } (IntegerLiteral 1) } } ) ] , expression = Node { start = { row = 3, column = 7 }, end = { row = 3, column = 14 } } - (LambdaExpression { args = [ Node { start = { row = 3, column = 8 }, end = { row = 3, column = 9 } } AllPattern ], expression = Node { start = { row = 3, column = 13 }, end = { row = 3, column = 14 } } (Integer 1) }) + (LambdaExpression + { firstArg = Node { start = { row = 3, column = 8 }, end = { row = 3, column = 9 } } AllPattern_ + , restOfArgs = [] + , expression = Node { start = { row = 3, column = 13 }, end = { row = 3, column = 14 } } (IntegerLiteral 1) + } + ) } ) ) diff --git a/tests/Elm/Parser/ModuleTests.elm b/tests/Elm/Parser/ModuleTests.elm index b4513b220..f5b208641 100644 --- a/tests/Elm/Parser/ModuleTests.elm +++ b/tests/Elm/Parser/ModuleTests.elm @@ -4,12 +4,11 @@ import Elm.Parser.File as File import Elm.Parser.Modules as Parser import Elm.Parser.ParserWithCommentsTestUtil as ParserWithCommentsUtil exposing (..) import Elm.Syntax.Declaration exposing (Declaration(..)) +import Elm.Syntax.DestructurePattern exposing (DestructurePattern(..)) import Elm.Syntax.Exposing exposing (..) import Elm.Syntax.Expression exposing (Expression(..)) -import Elm.Syntax.Infix exposing (InfixDirection(..)) import Elm.Syntax.Module exposing (..) import Elm.Syntax.Node exposing (Node(..)) -import Elm.Syntax.Pattern exposing (Pattern(..)) import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation(..)) import Expect import ParserFast @@ -28,8 +27,8 @@ all = , exposingList = Node { start = { row = 1, column = 12 }, end = { row = 1, column = 26 } } (Explicit - [ Node { start = { row = 1, column = 22 }, end = { row = 1, column = 25 } } (TypeOrAliasExpose "Bar") - ] + (Node { start = { row = 1, column = 22 }, end = { row = 1, column = 25 } } (TypeOrAliasExpose "Bar")) + [] ) } ) @@ -41,9 +40,7 @@ all = { moduleName = Node { start = { row = 1, column = 13 }, end = { row = 1, column = 16 } } [ "Foo" ] , exposingList = Node { start = { row = 1, column = 17 }, end = { row = 1, column = 31 } } - (Explicit - [ Node { start = { row = 1, column = 27 }, end = { row = 1, column = 30 } } (TypeOrAliasExpose "Bar") ] - ) + (Explicit (Node { start = { row = 1, column = 27 }, end = { row = 1, column = 30 } } (TypeOrAliasExpose "Bar")) []) } ) , test "port moduleDefinition with spacing" <| @@ -54,9 +51,7 @@ all = { moduleName = Node { start = { row = 1, column = 13 }, end = { row = 1, column = 16 } } [ "Foo" ] , exposingList = Node { start = { row = 1, column = 17 }, end = { row = 1, column = 33 } } - (Explicit - [ Node { start = { row = 1, column = 28 }, end = { row = 1, column = 31 } } (TypeOrAliasExpose "Bar") ] - ) + (Explicit (Node { start = { row = 1, column = 28 }, end = { row = 1, column = 31 } } (TypeOrAliasExpose "Bar")) []) } ) , test "effect moduleDefinition" <| @@ -65,7 +60,9 @@ all = |> expectAst (EffectModule { moduleName = Node { start = { row = 1, column = 15 }, end = { row = 1, column = 18 } } [ "Foo" ] - , exposingList = Node { start = { row = 1, column = 66 }, end = { row = 1, column = 80 } } (Explicit [ Node { start = { row = 1, column = 76 }, end = { row = 1, column = 79 } } (TypeOrAliasExpose "Bar") ]) + , exposingList = + Node { start = { row = 1, column = 66 }, end = { row = 1, column = 80 } } + (Explicit (Node { start = { row = 1, column = 76 }, end = { row = 1, column = 79 } } (TypeOrAliasExpose "Bar")) []) , command = Just (Node { start = { row = 1, column = 36 }, end = { row = 1, column = 41 } } "MyCmd") , subscription = Just (Node { start = { row = 1, column = 58 }, end = { row = 1, column = 63 } } "MySub") } @@ -159,11 +156,11 @@ b = 3 , arguments = [] , expression = Node { start = { row = 4, column = 5 }, end = { row = 7, column = 10 } } - (IfBlock + (If (Node { start = { row = 4, column = 8 }, end = { row = 4, column = 12 } } (FunctionOrValue [] "cond") ) - (Node { start = { row = 5, column = 9 }, end = { row = 5, column = 10 } } (Integer 1)) + (Node { start = { row = 5, column = 9 }, end = { row = 5, column = 10 } } (IntegerLiteral 1)) (Node { start = { row = 7 @@ -171,7 +168,7 @@ b = 3 } , end = { row = 7, column = 10 } } - (Integer 2) + (IntegerLiteral 2) ) ) } @@ -191,7 +188,7 @@ b = 3 } { name = Node { start = { row = 13, column = 1 }, end = { row = 13, column = 2 } } "b" , arguments = [] - , expression = Node { start = { row = 13, column = 5 }, end = { row = 13, column = 6 } } (Integer 3) + , expression = Node { start = { row = 13, column = 5 }, end = { row = 13, column = 6 } } (IntegerLiteral 3) } } ) @@ -247,7 +244,7 @@ b = 3 { start = { row = 4, column = 5 } , end = { row = 4, column = 6 } } - (Integer 2) + (IntegerLiteral 2) } } ) @@ -267,7 +264,7 @@ b = 3 { start = { row = 10, column = 5 } , end = { row = 10, column = 6 } } - (Integer 3) + (IntegerLiteral 3) } } ) @@ -317,7 +314,7 @@ a = 1 Node { start = { row = 5, column = 1 }, end = { row = 5, column = 6 } } { name = Node { start = { row = 5, column = 1 }, end = { row = 5, column = 2 } } "a" , arguments = [] - , expression = Node { start = { row = 5, column = 5 }, end = { row = 5, column = 6 } } (Integer 1) + , expression = Node { start = { row = 5, column = 5 }, end = { row = 5, column = 6 } } (IntegerLiteral 1) } } ) @@ -352,12 +349,13 @@ b = 2 { documentation = Nothing , name = Node { start = { row = 2, column = 6 }, end = { row = 2, column = 7 } } "A" , generics = [] - , constructors = - [ Node { start = { row = 2, column = 10 }, end = { row = 2, column = 11 } } + , firstConstructor = + Node { start = { row = 2, column = 10 }, end = { row = 2, column = 11 } } { name = Node { start = { row = 2, column = 10 }, end = { row = 2, column = 11 } } "B" , arguments = [] } - , Node { start = { row = 2, column = 14 }, end = { row = 2, column = 15 } } + , restOfConstructors = + [ Node { start = { row = 2, column = 14 }, end = { row = 2, column = 15 } } { name = Node { start = { row = 2, column = 14 }, end = { row = 2, column = 15 } } "C" , arguments = [] } @@ -372,7 +370,7 @@ b = 2 Node { start = { row = 3, column = 1 }, end = { row = 3, column = 6 } } { name = Node { start = { row = 3, column = 1 }, end = { row = 3, column = 2 } } "a" , arguments = [] - , expression = Node { start = { row = 3, column = 5 }, end = { row = 3, column = 6 } } (Integer 1) + , expression = Node { start = { row = 3, column = 5 }, end = { row = 3, column = 6 } } (IntegerLiteral 1) } } ) @@ -383,7 +381,7 @@ b = 2 , generics = [] , typeAnnotation = Node { start = { row = 4, column = 16 }, end = { row = 4, column = 17 } } - (Typed (Node { start = { row = 4, column = 16 }, end = { row = 4, column = 17 } } ( [], "A" )) []) + (Type (Node { start = { row = 4, column = 16 }, end = { row = 4, column = 17 } } ( [], "A" )) []) } ) , Node { start = { row = 5, column = 1 }, end = { row = 6, column = 6 } } @@ -393,14 +391,14 @@ b = 2 Just (Node { start = { row = 5, column = 1 }, end = { row = 5, column = 8 } } { name = Node { start = { row = 5, column = 1 }, end = { row = 5, column = 2 } } "b" - , typeAnnotation = Node { start = { row = 5, column = 5 }, end = { row = 5, column = 8 } } (Typed (Node { start = { row = 5, column = 5 }, end = { row = 5, column = 8 } } ( [], "Int" )) []) + , typeAnnotation = Node { start = { row = 5, column = 5 }, end = { row = 5, column = 8 } } (Type (Node { start = { row = 5, column = 5 }, end = { row = 5, column = 8 } } ( [], "Int" )) []) } ) , declaration = Node { start = { row = 6, column = 1 }, end = { row = 6, column = 6 } } { name = Node { start = { row = 6, column = 1 }, end = { row = 6, column = 2 } } "b" , arguments = [] - , expression = Node { start = { row = 6, column = 5 }, end = { row = 6, column = 6 } } (Integer 2) + , expression = Node { start = { row = 6, column = 5 }, end = { row = 6, column = 6 } } (IntegerLiteral 2) } } ) @@ -437,7 +435,19 @@ fun2 n = File.file |> Expect.equal (Just - { moduleDefinition = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 31 } } (NormalModule { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 9 } } [ "A" ], exposingList = Node { start = { row = 1, column = 10 }, end = { row = 1, column = 31 } } (Explicit [ Node { start = { row = 1, column = 20 }, end = { row = 1, column = 24 } } (FunctionExpose "fun1"), Node { start = { row = 1, column = 26 }, end = { row = 1, column = 30 } } (FunctionExpose "fun2") ]) }) + { moduleDefinition = + Node { start = { row = 1, column = 1 }, end = { row = 1, column = 31 } } + (NormalModule + { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 9 } } [ "A" ] + , exposingList = + Node { start = { row = 1, column = 10 }, end = { row = 1, column = 31 } } + (Explicit + (Node { start = { row = 1, column = 20 }, end = { row = 1, column = 24 } } (FunctionExpose "fun1")) + [ Node { start = { row = 1, column = 26 }, end = { row = 1, column = 30 } } (FunctionExpose "fun2") + ] + ) + } + ) , imports = [] , declarations = [ Node { start = { row = 3, column = 1 }, end = { row = 5, column = 11 } } @@ -447,23 +457,22 @@ fun2 n = , declaration = Node { start = { row = 3, column = 1 }, end = { row = 5, column = 11 } } { name = Node { start = { row = 3, column = 1 }, end = { row = 3, column = 5 } } "fun1" - , arguments = [ Node { start = { row = 3, column = 6 }, end = { row = 3, column = 7 } } (VarPattern "n") ] + , arguments = [ Node { start = { row = 3, column = 6 }, end = { row = 3, column = 7 } } (VarPattern_ "n") ] , expression = Node { start = { row = 4, column = 3 }, end = { row = 5, column = 11 } } - (OperatorApplication "+" - Left + (Operation "+" (Node { start = { row = 4, column = 3 }, end = { row = 4, column = 9 } } - (Application - [ Node { start = { row = 4, column = 3 }, end = { row = 4, column = 7 } } (FunctionOrValue [] "fun2") - , Node { start = { row = 4, column = 8 }, end = { row = 4, column = 9 } } (FunctionOrValue [] "n") - ] + (FunctionCall + (Node { start = { row = 4, column = 3 }, end = { row = 4, column = 7 } } (FunctionOrValue [] "fun2")) + (Node { start = { row = 4, column = 8 }, end = { row = 4, column = 9 } } (FunctionOrValue [] "n")) + [] ) ) (Node { start = { row = 5, column = 5 }, end = { row = 5, column = 11 } } - (Application - [ Node { start = { row = 5, column = 5 }, end = { row = 5, column = 9 } } (FunctionOrValue [] "fun2") - , Node { start = { row = 5, column = 10 }, end = { row = 5, column = 11 } } (FunctionOrValue [] "n") - ] + (FunctionCall + (Node { start = { row = 5, column = 5 }, end = { row = 5, column = 9 } } (FunctionOrValue [] "fun2")) + (Node { start = { row = 5, column = 10 }, end = { row = 5, column = 11 } } (FunctionOrValue [] "n")) + [] ) ) ) @@ -477,13 +486,13 @@ fun2 n = , declaration = Node { start = { row = 7, column = 1 }, end = { row = 8, column = 9 } } { name = Node { start = { row = 7, column = 1 }, end = { row = 7, column = 5 } } "fun2" - , arguments = [ Node { start = { row = 7, column = 6 }, end = { row = 7, column = 7 } } (VarPattern "n") ] + , arguments = [ Node { start = { row = 7, column = 6 }, end = { row = 7, column = 7 } } (VarPattern_ "n") ] , expression = Node { start = { row = 8, column = 3 }, end = { row = 8, column = 9 } } - (Application - [ Node { start = { row = 8, column = 3 }, end = { row = 8, column = 7 } } (FunctionOrValue [] "fun1") - , Node { start = { row = 8, column = 8 }, end = { row = 8, column = 9 } } (FunctionOrValue [] "n") - ] + (FunctionCall + (Node { start = { row = 8, column = 3 }, end = { row = 8, column = 7 } } (FunctionOrValue [] "fun1")) + (Node { start = { row = 8, column = 8 }, end = { row = 8, column = 9 } } (FunctionOrValue [] "n")) + [] ) } } diff --git a/tests/Elm/Parser/PatternTests.elm b/tests/Elm/Parser/PatternTests.elm index cdb5857ab..321f2d275 100644 --- a/tests/Elm/Parser/PatternTests.elm +++ b/tests/Elm/Parser/PatternTests.elm @@ -4,6 +4,7 @@ import Elm.Parser.ParserWithCommentsTestUtil as ParserWithCommentsUtil exposing import Elm.Parser.Patterns as Parser import Elm.Syntax.Node exposing (Node(..)) import Elm.Syntax.Pattern exposing (..) +import Elm.Syntax.StringLiteralType exposing (StringLiteralType(..)) import Expect import Test exposing (..) @@ -28,7 +29,11 @@ all = , test "String" <| \() -> "\"Foo\"" - |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 6 } } (StringPattern "Foo")) + |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 6 } } (StringPattern SingleQuote "Foo")) + , test "Multi-line string" <| + \() -> + "\"\"\"Foo\"\"\"" + |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 10 } } (StringPattern TripleQuote "Foo")) , test "Char" <| \() -> "'f'" diff --git a/tests/Elm/Parser/Samples.elm b/tests/Elm/Parser/Samples.elm index d58bf598e..24adbce57 100644 --- a/tests/Elm/Parser/Samples.elm +++ b/tests/Elm/Parser/Samples.elm @@ -208,7 +208,7 @@ module B exposing (x) c = Maybe.map (\\( p, v, s ) -> - OperatorApplication + Operation ) diff --git a/tests/Elm/Parser/TokenTests.elm b/tests/Elm/Parser/TokenTests.elm index fbd70c37f..f55f779c4 100644 --- a/tests/Elm/Parser/TokenTests.elm +++ b/tests/Elm/Parser/TokenTests.elm @@ -2,6 +2,7 @@ module Elm.Parser.TokenTests exposing (all) import Elm.Parser.TestUtil exposing (..) import Elm.Parser.Tokens as Parser +import Elm.Syntax.StringLiteralType exposing (StringLiteralType(..)) import Expect import Test exposing (..) @@ -82,11 +83,11 @@ all = , test "multiline string" <| \() -> parse "\"\"\"Bar foo \n a\"\"\"" Parser.singleOrTripleQuotedStringLiteral - |> Expect.equal (Just "Bar foo \n a") + |> Expect.equal (Just ( TripleQuote, "Bar foo \n a" )) , test "multiline string escape" <| \() -> parse """\"\"\" \\\"\"\" \"\"\"""" Parser.singleOrTripleQuotedStringLiteral - |> Expect.equal (Just """ \"\"\" """) + |> Expect.equal (Just ( TripleQuote, """ \"\"\" """ )) , test "character escaped" <| \() -> parse "'\\''" Parser.characterLiteral @@ -106,11 +107,11 @@ all = , test "string escaped 3" <| \() -> parse "\"\\\"\"" Parser.singleOrTripleQuotedStringLiteral - |> Expect.equal (Just "\"") + |> Expect.equal (Just ( SingleQuote, "\"" )) , test "string escaped" <| \() -> parse "\"foo\\\\\"" Parser.singleOrTripleQuotedStringLiteral - |> Expect.equal (Just "foo\\") + |> Expect.equal (Just ( SingleQuote, "foo\\" )) , test "character escaped 3" <| \() -> parse "'\\n'" Parser.characterLiteral diff --git a/tests/Elm/Parser/TypeAnnotationTests.elm b/tests/Elm/Parser/TypeAnnotationTests.elm index b62dac582..6a1fe9e3f 100644 --- a/tests/Elm/Parser/TypeAnnotationTests.elm +++ b/tests/Elm/Parser/TypeAnnotationTests.elm @@ -17,7 +17,7 @@ all = \() -> "()" |> expectAst - (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 3 } } Unit) + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 3 } } (Tuple [])) , test "unitTypeReference with spaces" <| \() -> "( )" @@ -27,9 +27,9 @@ all = "( (), ())" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 10 } } - (Tupled - [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 5 } } Unit - , Node { start = { row = 1, column = 7 }, end = { row = 1, column = 9 } } Unit + (Tuple + [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 5 } } (Tuple []) + , Node { start = { row = 1, column = 7 }, end = { row = 1, column = 9 } } (Tuple []) ] ) ) @@ -38,18 +38,18 @@ all = -- TODO This feels incorrect, there should be a Parenthesized type for this "( () )" |> expectAst - (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 7 } } Unit) + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 7 } } (Tuple [])) , test "tupledTypeReference 3" <| \() -> "( () , Maybe m )" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 17 } } - (Tupled - [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 5 } } Unit + (Tuple + [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 5 } } (Tuple []) , Node { start = { row = 1, column = 8 }, end = { row = 1, column = 15 } } - (Typed + (Type (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 13 } } ( [], "Maybe" )) - [ Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } (GenericType "m") ] + [ Node { start = { row = 1, column = 14 }, end = { row = 1, column = 15 } } (Var "m") ] ) ] ) @@ -59,14 +59,14 @@ all = "Foo.Bar" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 8 } } - (Typed (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 8 } } ( [ "Foo" ], "Bar" )) []) + (Type (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 8 } } ( [ "Foo" ], "Bar" )) []) ) , test "typeAnnotationNoFn" <| \() -> "Bar" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } - (Typed (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Bar" )) []) + (Type (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Bar" )) []) ) , test "types with and without spacing should parse to the same" <| \() -> @@ -77,11 +77,11 @@ all = "Foo () a Bar" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 13 } } - (Typed (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Foo" )) - [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 7 } } Unit - , Node { start = { row = 1, column = 8 }, end = { row = 1, column = 9 } } (GenericType "a") + (Type (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Foo" )) + [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 7 } } (Tuple []) + , Node { start = { row = 1, column = 8 }, end = { row = 1, column = 9 } } (Var "a") , Node { start = { row = 1, column = 10 }, end = { row = 1, column = 13 } } - (Typed (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 13 } } ( [], "Bar" )) []) + (Type (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 13 } } ( [], "Bar" )) []) ] ) ) @@ -90,11 +90,11 @@ all = "Foo () a Bar" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 13 } } - (Typed (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Foo" )) - [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 7 } } Unit - , Node { start = { row = 1, column = 8 }, end = { row = 1, column = 9 } } (GenericType "a") + (Type (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Foo" )) + [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 7 } } (Tuple []) + , Node { start = { row = 1, column = 8 }, end = { row = 1, column = 9 } } (Var "a") , Node { start = { row = 1, column = 10 }, end = { row = 1, column = 13 } } - (Typed (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 13 } } ( [], "Bar" )) []) + (Type (Node { start = { row = 1, column = 10 }, end = { row = 1, column = 13 } } ( [], "Bar" )) []) ] ) ) @@ -112,7 +112,7 @@ all = [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 15 } } ( Node { start = { row = 1, column = 2 }, end = { row = 1, column = 7 } } "color" , Node { start = { row = 1, column = 9 }, end = { row = 1, column = 15 } } - (Typed (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 15 } } ( [], "String" )) []) + (Type (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 15 } } ( [], "String" )) []) ) ] ) @@ -127,12 +127,12 @@ all = [ Node { start = { row = 1, column = 10 }, end = { row = 1, column = 25 } } ( Node { start = { row = 1, column = 10 }, end = { row = 1, column = 18 } } "position" , Node { start = { row = 1, column = 21 }, end = { row = 1, column = 25 } } - (Typed (Node { start = { row = 1, column = 21 }, end = { row = 1, column = 25 } } ( [], "Vec2" )) []) + (Type (Node { start = { row = 1, column = 21 }, end = { row = 1, column = 25 } } ( [], "Vec2" )) []) ) , Node { start = { row = 1, column = 27 }, end = { row = 1, column = 42 } } ( Node { start = { row = 1, column = 27 }, end = { row = 1, column = 34 } } "texture" , Node { start = { row = 1, column = 37 }, end = { row = 1, column = 41 } } - (Typed (Node { start = { row = 1, column = 37 }, end = { row = 1, column = 41 } } ( [], "Vec2" )) []) + (Type (Node { start = { row = 1, column = 37 }, end = { row = 1, column = 41 } } ( [], "Vec2" )) []) ) ] ) @@ -155,17 +155,17 @@ all = [ Node { start = { row = 1, column = 10 }, end = { row = 1, column = 17 } } ( Node { start = { row = 1, column = 10 }, end = { row = 1, column = 11 } } "r" , Node { start = { row = 1, column = 14 }, end = { row = 1, column = 17 } } - (Typed (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 17 } } ( [], "Int" )) []) + (Type (Node { start = { row = 1, column = 14 }, end = { row = 1, column = 17 } } ( [], "Int" )) []) ) , Node { start = { row = 1, column = 19 }, end = { row = 1, column = 25 } } ( Node { start = { row = 1, column = 19 }, end = { row = 1, column = 20 } } "g" , Node { start = { row = 1, column = 22 }, end = { row = 1, column = 25 } } - (Typed (Node { start = { row = 1, column = 22 }, end = { row = 1, column = 25 } } ( [], "Int" )) []) + (Type (Node { start = { row = 1, column = 22 }, end = { row = 1, column = 25 } } ( [], "Int" )) []) ) , Node { start = { row = 1, column = 27 }, end = { row = 1, column = 34 } } ( Node { start = { row = 1, column = 27 }, end = { row = 1, column = 28 } } "b" , Node { start = { row = 1, column = 30 }, end = { row = 1, column = 33 } } - (Typed (Node { start = { row = 1, column = 30 }, end = { row = 1, column = 33 } } ( [], "Int" )) []) + (Type (Node { start = { row = 1, column = 30 }, end = { row = 1, column = 33 } } ( [], "Int" )) []) ) ] ) @@ -182,17 +182,17 @@ all = [ Node { start = { row = 1, column = 3 }, end = { row = 1, column = 12 } } ( Node { start = { row = 1, column = 3 }, end = { row = 1, column = 6 } } "foo" , Node { start = { row = 1, column = 9 }, end = { row = 1, column = 12 } } <| - Typed (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 12 } } ( [], "Int" )) [] + Type (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 12 } } ( [], "Int" )) [] ) , Node { start = { row = 1, column = 14 }, end = { row = 1, column = 23 } } ( Node { start = { row = 1, column = 14 }, end = { row = 1, column = 17 } } "bar" , Node { start = { row = 1, column = 20 }, end = { row = 1, column = 23 } } <| - Typed (Node { start = { row = 1, column = 20 }, end = { row = 1, column = 23 } } ( [], "Int" )) [] + Type (Node { start = { row = 1, column = 20 }, end = { row = 1, column = 23 } } ( [], "Int" )) [] ) , Node { start = { row = 1, column = 25 }, end = { row = 1, column = 35 } } ( Node { start = { row = 1, column = 25 }, end = { row = 1, column = 28 } } "baz" , Node { start = { row = 1, column = 31 }, end = { row = 1, column = 34 } } <| - Typed (Node { start = { row = 1, column = 31 }, end = { row = 1, column = 34 } } ( [], "Int" )) [] + Type (Node { start = { row = 1, column = 31 }, end = { row = 1, column = 34 } } ( [], "Int" )) [] ) ] ) @@ -204,7 +204,7 @@ all = (Record [ Node { start = { row = 1, column = 2 }, end = { row = 1, column = 10 } } ( Node { start = { row = 1, column = 2 }, end = { row = 1, column = 7 } } "color" - , Node { start = { row = 1, column = 9 }, end = { row = 1, column = 10 } } (GenericType "s") + , Node { start = { row = 1, column = 9 }, end = { row = 1, column = 10 } } (Var "s") ) ] ) @@ -216,10 +216,10 @@ all = (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 11 } } (FunctionTypeAnnotation (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } - (Typed (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Foo" )) []) + (Type (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Foo" )) []) ) (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } - (Typed (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } ( [], "Bar" )) []) + (Type (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } ( [], "Bar" )) []) ) ) ) @@ -230,14 +230,14 @@ all = (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 18 } } (FunctionTypeAnnotation (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } - (Typed (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Foo" )) []) + (Type (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Foo" )) []) ) (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 18 } } (FunctionTypeAnnotation (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } - (Typed (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } ( [], "Bar" )) []) + (Type (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } ( [], "Bar" )) []) ) - (Node { start = { row = 1, column = 15 }, end = { row = 1, column = 18 } } (GenericType "baz")) + (Node { start = { row = 1, column = 15 }, end = { row = 1, column = 18 } } (Var "baz")) ) ) ) @@ -248,11 +248,11 @@ all = |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 20 } } (FunctionTypeAnnotation - (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 5 } } (GenericType "cMsg")) + (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 5 } } (Var "cMsg")) (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 20 } } (FunctionTypeAnnotation - (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 15 } } (GenericType "cModel")) - (Node { start = { row = 1, column = 19 }, end = { row = 1, column = 20 } } (GenericType "a")) + (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 15 } } (Var "cModel")) + (Node { start = { row = 1, column = 19 }, end = { row = 1, column = 20 } } (Var "a")) ) ) ) @@ -264,21 +264,21 @@ all = (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 33 } } (FunctionTypeAnnotation (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } - (Typed (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Msg" )) []) + (Type (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 4 } } ( [], "Msg" )) []) ) (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 33 } } (FunctionTypeAnnotation (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 13 } } - (Typed (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 13 } } ( [], "Model" )) []) + (Type (Node { start = { row = 1, column = 8 }, end = { row = 1, column = 13 } } ( [], "Model" )) []) ) (Node { start = { row = 1, column = 17 }, end = { row = 1, column = 33 } } - (Tupled + (Tuple [ Node { start = { row = 1, column = 18 }, end = { row = 1, column = 23 } } - (Typed (Node { start = { row = 1, column = 18 }, end = { row = 1, column = 23 } } ( [], "Model" )) []) + (Type (Node { start = { row = 1, column = 18 }, end = { row = 1, column = 23 } } ( [], "Model" )) []) , Node { start = { row = 1, column = 25 }, end = { row = 1, column = 32 } } - (Typed (Node { start = { row = 1, column = 25 }, end = { row = 1, column = 28 } } ( [], "Cmd" )) + (Type (Node { start = { row = 1, column = 25 }, end = { row = 1, column = 28 } } ( [], "Cmd" )) [ Node { start = { row = 1, column = 29 }, end = { row = 1, column = 32 } } - (Typed (Node { start = { row = 1, column = 29 }, end = { row = 1, column = 32 } } ( [], "Msg" )) []) + (Type (Node { start = { row = 1, column = 29 }, end = { row = 1, column = 32 } } ( [], "Msg" )) []) ] ) ] @@ -296,15 +296,15 @@ all = (FunctionTypeAnnotation (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 24 } } (FunctionTypeAnnotation - (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 7 } } (GenericType "cMsg")) + (Node { start = { row = 1, column = 3 }, end = { row = 1, column = 7 } } (Var "cMsg")) (Node { start = { row = 1, column = 11 }, end = { row = 1, column = 22 } } - (FunctionTypeAnnotation (Node { start = { row = 1, column = 11 }, end = { row = 1, column = 17 } } (GenericType "cModel")) - (Node { start = { row = 1, column = 21 }, end = { row = 1, column = 22 } } (GenericType "a")) + (FunctionTypeAnnotation (Node { start = { row = 1, column = 11 }, end = { row = 1, column = 17 } } (Var "cModel")) + (Node { start = { row = 1, column = 21 }, end = { row = 1, column = 22 } } (Var "a")) ) ) ) ) - (Node { start = { row = 1, column = 28 }, end = { row = 1, column = 29 } } (GenericType "b")) + (Node { start = { row = 1, column = 28 }, end = { row = 1, column = 29 } } (Var "b")) ) ) , test "type with params" <| @@ -314,13 +314,13 @@ all = (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 13 } } (FunctionTypeAnnotation (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 5 } } - (Typed + (Type (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 5 } } ( [], "Foo" )) [] ) ) (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 12 } } - (Typed + (Type (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 12 } } ( [], "Bar" )) [] ) @@ -336,20 +336,20 @@ all = (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 13 } } (FunctionTypeAnnotation (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 5 } } - (Typed + (Type (Node { start = { row = 1, column = 2 }, end = { row = 1, column = 5 } } ( [], "Foo" )) [] ) ) (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 12 } } - (Typed + (Type (Node { start = { row = 1, column = 9 }, end = { row = 1, column = 12 } } ( [], "Bar" )) [] ) ) ) ) - (Node { start = { row = 1, column = 17 }, end = { row = 1, column = 20 } } (GenericType "baz")) + (Node { start = { row = 1, column = 17 }, end = { row = 1, column = 20 } } (Var "baz")) ) ) , test "parseTypeWith wrong indent" <| @@ -361,9 +361,9 @@ all = "Maybe\n a" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 2, column = 3 } } - (Typed + (Type (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 6 } } ( [], "Maybe" )) - [ Node { start = { row = 2, column = 2 }, end = { row = 2, column = 3 } } (GenericType "a") ] + [ Node { start = { row = 2, column = 2 }, end = { row = 2, column = 3 } } (Var "a") ] ) ) , test "issue #5 - no spaces between type and generic with parens" <| @@ -371,10 +371,10 @@ all = "List(String)" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 13 } } - (Typed + (Type (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 5 } } ( [], "List" )) [ Node { start = { row = 1, column = 5 }, end = { row = 1, column = 13 } } - (Typed + (Type (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 12 } } ( [], "String" )) [] ) @@ -386,15 +386,15 @@ all = "Dict String Int" |> expectAst (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 16 } } - (Typed + (Type (Node { start = { row = 1, column = 1 }, end = { row = 1, column = 5 } } ( [], "Dict" )) [ Node { start = { row = 1, column = 6 }, end = { row = 1, column = 12 } } - (Typed + (Type (Node { start = { row = 1, column = 6 }, end = { row = 1, column = 12 } } ( [], "String" )) [] ) , Node { start = { row = 1, column = 13 }, end = { row = 1, column = 16 } } - (Typed (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 16 } } ( [], "Int" )) []) + (Type (Node { start = { row = 1, column = 13 }, end = { row = 1, column = 16 } } ( [], "Int" )) []) ] ) ) diff --git a/tests/Elm/ProcessingTests.elm b/tests/Elm/ProcessingTests.elm deleted file mode 100644 index 4577a68c4..000000000 --- a/tests/Elm/ProcessingTests.elm +++ /dev/null @@ -1,924 +0,0 @@ -module Elm.ProcessingTests exposing (suite, suiteDeprecated) - -import Elm.Parser as Parser -import Elm.Processing as Processing -import Elm.Syntax.Declaration exposing (..) -import Elm.Syntax.Exposing exposing (..) -import Elm.Syntax.Expression exposing (..) -import Elm.Syntax.File exposing (..) -import Elm.Syntax.Infix exposing (..) -import Elm.Syntax.Module exposing (..) -import Elm.Syntax.Node exposing (Node(..)) -import Elm.Syntax.Pattern exposing (Pattern(..)) -import Elm.Syntax.TypeAnnotation exposing (..) -import Expect -import Parser -import Test exposing (..) - - - --- TODO Elm.Processing doesn't exiat anymore, move these tests somewhere else - - -suite : Test -suite = - describe "Elm.Processing" - (List.map - (\( name, input, output ) -> - test name <| - \() -> - Parser.parseToFile (String.trim input) - |> Expect.equal (Ok output) - ) - testCases - ) - - -{-| Using the deprecated `Elm.Parser.parse`. --} -suiteDeprecated : Test -suiteDeprecated = - describe "Elm.Processing.parse" - (List.map - (\( name, input, output ) -> - test name <| - \() -> - case context of - Ok context_ -> - Parser.parse (String.trim input) - |> Result.map (Processing.process context_) - |> Expect.equal (Ok output) - - Err _ -> - Expect.fail "Failed to generate context." - ) - testCases - ) - - -testCases : List ( String, String, File ) -testCases = - [ functionWithDocs - , functionWithDocsAndSignature - , functionWithSingleLineCommentAsDoc - , fileWithMultipleComments - , functionWithMultiLineCommentAsDoc - , postProcessInfixOperators - , postProcessInfixOperators2 - , postProcessInfixOperators3 - , postProcessInfixOperatorsInNegation - , postProcessInfixOperatorsInRecordAccess - , postProcessInfixOperatorsRegressionTest - , postProcessInfixOperatorsAssociativityTest - , typeAliasWithDocumentation - , typeWithDocumentation - , maxCallStackSizeFailure - ] - - -context : Result (List Parser.DeadEnd) Processing.ProcessContext -context = - """ -module Basics exposing ((+), (-), (*), (/), (//), (^), (==), (/=), (<), (>), (<=), (>=), (&&), (||), (++), (<|), (|>), (<<), (>>)) - - -infix right 0 (<|) = apL -infix left 0 (|>) = apR -infix right 2 (||) = or -infix right 3 (&&) = and -infix non 4 (==) = eq -infix non 4 (/=) = neq -infix non 4 (<) = lt -infix non 4 (>) = gt -infix non 4 (<=) = le -infix non 4 (>=) = ge -infix right 5 (++) = append -infix left 6 (+) = add -infix left 6 (-) = sub -infix left 7 (*) = mul -infix left 7 (/) = fdiv -infix left 7 (//) = idiv -infix right 8 (^) = pow -infix left 9 (<<) = composeL -infix right 9 (>>) = composeR""" - |> String.trim - |> Parser.parse - |> Result.map (\a -> Processing.addFile a Processing.init) - - -functionWithDocs : ( String, String, File ) -functionWithDocs = - ( "functionWithDocs" - , """ -module Bar exposing (..) - -import String - -{-| The docs --} -bar = 1 -""" |> String.replace "\u{000D}" "" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } <| - NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ] - , exposingList = - Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } <| - All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } } - } - , imports = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 14 } } - { moduleName = Node { start = { row = 3, column = 8 }, end = { row = 3, column = 14 } } [ "String" ] - , moduleAlias = Nothing - , exposingList = Nothing - } - ] - , declarations = - [ Node { start = { row = 5, column = 1 }, end = { row = 7, column = 8 } } <| - FunctionDeclaration - { documentation = Just (Node { start = { row = 5, column = 1 }, end = { row = 6, column = 3 } } "{-| The docs\n-}") - , signature = Nothing - , declaration = - Node { start = { row = 7, column = 1 }, end = { row = 7, column = 8 } } - { name = Node { start = { row = 7, column = 1 }, end = { row = 7, column = 4 } } "bar" - , arguments = [] - , expression = Node { start = { row = 7, column = 7 }, end = { row = 7, column = 8 } } <| Integer 1 - } - } - ] - , comments = [] - } - ) - - -functionWithDocsAndSignature : ( String, String, File ) -functionWithDocsAndSignature = - ( "functionWithDocsAndSignature" - , """ -module Bar exposing (..) - -import String - -{-| The docs --} -bar : Int -bar = 1 -""" |> String.replace "\u{000D}" "" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } <| - NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ] - , exposingList = Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } <| All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } } - } - , imports = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 14 } } - { moduleName = Node { start = { row = 3, column = 8 }, end = { row = 3, column = 14 } } [ "String" ] - , moduleAlias = Nothing - , exposingList = Nothing - } - ] - , declarations = - [ Node { start = { row = 5, column = 1 }, end = { row = 8, column = 8 } } <| - FunctionDeclaration - { documentation = - Just <| Node { start = { row = 5, column = 1 }, end = { row = 6, column = 3 } } "{-| The docs\n-}" - , signature = - Just - (Node { start = { row = 7, column = 1 }, end = { row = 7, column = 10 } } <| - { name = Node { start = { row = 7, column = 1 }, end = { row = 7, column = 4 } } "bar" - , typeAnnotation = - Node { start = { row = 7, column = 7 }, end = { row = 7, column = 10 } } <| - Typed (Node { start = { row = 7, column = 7 }, end = { row = 7, column = 10 } } ( [], "Int" )) [] - } - ) - , declaration = - Node { start = { row = 8, column = 1 }, end = { row = 8, column = 8 } } - { name = Node { start = { row = 8, column = 1 }, end = { row = 8, column = 4 } } "bar" - , arguments = [] - , expression = Node { start = { row = 8, column = 7 }, end = { row = 8, column = 8 } } <| Integer 1 - } - } - ] - , comments = [] - } - ) - - -functionWithSingleLineCommentAsDoc : ( String, String, File ) -functionWithSingleLineCommentAsDoc = - ( "functionWithSingleLineCommentAsDoc" - , """ -module Bar exposing (..) - ---The Doc -bar = 1 -""" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } <| - NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ] - , exposingList = Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } <| All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } } - } - , imports = [] - , declarations = - [ Node { start = { row = 4, column = 1 }, end = { row = 4, column = 8 } } <| - FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 4, column = 1 }, end = { row = 4, column = 8 } } - { name = Node { start = { row = 4, column = 1 }, end = { row = 4, column = 4 } } "bar" - , arguments = [] - , expression = Node { start = { row = 4, column = 7 }, end = { row = 4, column = 8 } } <| Integer 1 - } - } - ] - , comments = [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 10 } } "--The Doc" ] - } - ) - - -fileWithMultipleComments : ( String, String, File ) -fileWithMultipleComments = - ( "fileWithMultipleComments" - , """ --- comment 1 -module Bar exposing (..) - --- comment 2 -bar = {- comment 3 -} 1 -- comment 4 - -- comment 5 -""" - , { moduleDefinition = - Node { start = { row = 2, column = 1 }, end = { row = 2, column = 25 } } <| - NormalModule - { moduleName = Node { start = { row = 2, column = 8 }, end = { row = 2, column = 11 } } [ "Bar" ] - , exposingList = Node { start = { row = 2, column = 12 }, end = { row = 2, column = 25 } } <| All { start = { row = 2, column = 22 }, end = { row = 2, column = 24 } } - } - , imports = [] - , declarations = - [ Node { start = { row = 5, column = 1 }, end = { row = 5, column = 24 } } - (FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 5, column = 1 }, end = { row = 5, column = 24 } } - { name = Node { start = { row = 5, column = 1 }, end = { row = 5, column = 4 } } "bar" - , arguments = [] - , expression = Node { start = { row = 5, column = 23 }, end = { row = 5, column = 24 } } (Integer 1) - } - } - ) - ] - , comments = - [ Node { start = { row = 1, column = 1 }, end = { row = 1, column = 13 } } "-- comment 1" - , Node { start = { row = 4, column = 1 }, end = { row = 4, column = 13 } } "-- comment 2" - , Node { start = { row = 5, column = 7 }, end = { row = 5, column = 22 } } "{- comment 3 -}" - , Node { start = { row = 5, column = 25 }, end = { row = 5, column = 37 } } "-- comment 4" - , Node { start = { row = 6, column = 2 }, end = { row = 6, column = 14 } } "-- comment 5" - ] - } - ) - - -functionWithMultiLineCommentAsDoc : ( String, String, File ) -functionWithMultiLineCommentAsDoc = - ( "functionWithMultiLineCommentAsDoc" - , """ -module Bar exposing (..) - -{- The Doc -} -bar = 1 -""" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } <| - NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ] - , exposingList = - Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } <| - All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } } - } - , imports = [] - , declarations = - [ Node { start = { row = 4, column = 1 }, end = { row = 4, column = 8 } } <| - FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 4, column = 1 }, end = { row = 4, column = 8 } } - { name = Node { start = { row = 4, column = 1 }, end = { row = 4, column = 4 } } "bar" - , arguments = [] - , expression = Node { start = { row = 4, column = 7 }, end = { row = 4, column = 8 } } <| Integer 1 - } - } - ] - , comments = [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 14 } } "{- The Doc -}" ] - } - ) - - -typeAliasWithDocumentation : ( String, String, File ) -typeAliasWithDocumentation = - ( "typeAliasWithDocumentation" - , """ -module Bar exposing (..) - -import String - -{-| The Doc -} -type alias Foo - = { name : String } -""" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } <| - NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ] - , exposingList = Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } <| All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } } - } - , imports = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 14 } } - { moduleName = Node { start = { row = 3, column = 8 }, end = { row = 3, column = 14 } } [ "String" ] - , moduleAlias = Nothing - , exposingList = Nothing - } - ] - , declarations = - [ Node { start = { row = 5, column = 1 }, end = { row = 7, column = 23 } } <| - AliasDeclaration - { documentation = - Just <| - Node { start = { row = 5, column = 1 }, end = { row = 5, column = 15 } } "{-| The Doc -}" - , name = Node { start = { row = 6, column = 12 }, end = { row = 6, column = 15 } } "Foo" - , generics = [] - , typeAnnotation = - Node { start = { row = 7, column = 6 }, end = { row = 7, column = 23 } } <| - Record - [ Node { start = { row = 7, column = 8 }, end = { row = 7, column = 21 } } - ( Node { start = { row = 7, column = 8 }, end = { row = 7, column = 12 } } "name" - , Node { start = { row = 7, column = 15 }, end = { row = 7, column = 21 } } <| - Typed (Node { start = { row = 7, column = 15 }, end = { row = 7, column = 21 } } ( [], "String" )) [] - ) - ] - } - ] - , comments = [] - } - ) - - -maxCallStackSizeFailure : ( String, String, File ) -maxCallStackSizeFailure = - ( "maxCallStackSizeFailure" - , """module Simplify.AstHelpers exposing (log) - - -log : Int -> Int -log a = - Debug.log "ok" a -""" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 42 } } - (NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 27 } } [ "Simplify", "AstHelpers" ] - , exposingList = - Node { start = { row = 1, column = 28 }, end = { row = 1, column = 42 } } - (Explicit - [ Node { start = { row = 1, column = 38 }, end = { row = 1, column = 41 } } (FunctionExpose "log") - ] - ) - } - ) - , imports = [] - , declarations = - [ Node { start = { row = 4, column = 1 }, end = { row = 6, column = 21 } } - (FunctionDeclaration - { documentation = Nothing - , signature = Just (Node { start = { row = 4, column = 1 }, end = { row = 4, column = 17 } } { name = Node { start = { row = 4, column = 1 }, end = { row = 4, column = 4 } } "log", typeAnnotation = Node { start = { row = 4, column = 7 }, end = { row = 4, column = 17 } } (FunctionTypeAnnotation (Node { start = { row = 4, column = 7 }, end = { row = 4, column = 10 } } (Typed (Node { start = { row = 4, column = 7 }, end = { row = 4, column = 10 } } ( [], "Int" )) [])) (Node { start = { row = 4, column = 14 }, end = { row = 4, column = 17 } } (Typed (Node { start = { row = 4, column = 14 }, end = { row = 4, column = 17 } } ( [], "Int" )) []))) }) - , declaration = - Node { start = { row = 5, column = 1 }, end = { row = 6, column = 21 } } - { name = Node { start = { row = 5, column = 1 }, end = { row = 5, column = 4 } } "log" - , arguments = [ Node { start = { row = 5, column = 5 }, end = { row = 5, column = 6 } } (VarPattern "a") ] - , expression = - Node { start = { row = 6, column = 5 }, end = { row = 6, column = 21 } } - (Application - [ Node { start = { row = 6, column = 5 }, end = { row = 6, column = 14 } } (FunctionOrValue [ "Debug" ] "log") - , Node { start = { row = 6, column = 15 }, end = { row = 6, column = 19 } } (Literal "ok") - , Node { start = { row = 6, column = 20 }, end = { row = 6, column = 21 } } (FunctionOrValue [] "a") - ] - ) - } - } - ) - ] - , comments = [] - } - ) - - -typeWithDocumentation : ( String, String, File ) -typeWithDocumentation = - ( "typeWithDocumentation" - , """ -module Bar exposing (..) - -import String - -{-| The Doc -} -type Foo - = Red - | Blue -""" - , { moduleDefinition = Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } (NormalModule { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ], exposingList = Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } (All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } }) }) - , imports = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 14 } } - { moduleName = Node { start = { row = 3, column = 8 }, end = { row = 3, column = 14 } } [ "String" ] - , moduleAlias = Nothing - , exposingList = Nothing - } - ] - , declarations = - [ Node { start = { row = 5, column = 1 }, end = { row = 8, column = 10 } } - (CustomTypeDeclaration - { documentation = - Just - (Node { start = { row = 5, column = 1 }, end = { row = 5, column = 15 } } - "{-| The Doc -}" - ) - , name = Node { start = { row = 6, column = 6 }, end = { row = 6, column = 9 } } "Foo" - , generics = [] - , constructors = - [ Node { start = { row = 7, column = 6 }, end = { row = 7, column = 9 } } - { name = Node { start = { row = 7, column = 6 }, end = { row = 7, column = 9 } } "Red", arguments = [] } - , Node { start = { row = 8, column = 6 }, end = { row = 8, column = 10 } } - { name = Node { start = { row = 8, column = 6 }, end = { row = 8, column = 10 } } "Blue", arguments = [] } - ] - } - ) - ] - , comments = [] - } - ) - - -postProcessInfixOperators : ( String, String, File ) -postProcessInfixOperators = - ( "postProcessInfixOperators" - , """ -module Bar exposing (..) - -bar = (x + 1) * (2 * y) -""" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } <| - NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ] - , exposingList = Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } <| All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } } - } - , imports = [] - , declarations = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 24 } } <| - FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 3, column = 1 }, end = { row = 3, column = 24 } } - { name = Node { start = { row = 3, column = 1 }, end = { row = 3, column = 4 } } "bar" - , arguments = [] - , expression = - Node { start = { row = 3, column = 7 }, end = { row = 3, column = 24 } } <| - OperatorApplication "*" - Left - (Node { start = { row = 3, column = 7 }, end = { row = 3, column = 14 } } <| - ParenthesizedExpression - (Node { start = { row = 3, column = 8 }, end = { row = 3, column = 13 } } <| - OperatorApplication "+" - Left - (Node { start = { row = 3, column = 8 }, end = { row = 3, column = 9 } } <| - FunctionOrValue [] "x" - ) - (Node { start = { row = 3, column = 12 }, end = { row = 3, column = 13 } } <| Integer 1) - ) - ) - (Node { start = { row = 3, column = 17 }, end = { row = 3, column = 24 } } <| - ParenthesizedExpression - (Node { start = { row = 3, column = 18 }, end = { row = 3, column = 23 } } <| - OperatorApplication "*" - Left - (Node { start = { row = 3, column = 18 }, end = { row = 3, column = 19 } } <| Integer 2) - (Node { start = { row = 3, column = 22 }, end = { row = 3, column = 23 } } <| FunctionOrValue [] "y") - ) - ) - } - } - ] - , comments = [] - } - ) - - -postProcessInfixOperators2 : ( String, String, File ) -postProcessInfixOperators2 = - ( "postProcessInfixOperators2" - , """ -module Bar exposing (..) - -bar = x + 1 * 2 -""" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } <| - NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ] - , exposingList = Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } <| All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } } - } - , imports = [] - , declarations = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 16 } } <| - FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 3, column = 1 }, end = { row = 3, column = 16 } } - { name = Node { start = { row = 3, column = 1 }, end = { row = 3, column = 4 } } "bar" - , arguments = [] - , expression = - Node { start = { row = 3, column = 7 }, end = { row = 3, column = 16 } } <| - OperatorApplication "+" - Left - (Node { start = { row = 3, column = 7 }, end = { row = 3, column = 8 } } <| - FunctionOrValue [] "x" - ) - (Node { start = { row = 3, column = 11 }, end = { row = 3, column = 16 } } <| - OperatorApplication "*" - Left - (Node { start = { row = 3, column = 11 }, end = { row = 3, column = 12 } } <| Integer 1) - (Node { start = { row = 3, column = 15 }, end = { row = 3, column = 16 } } <| Integer 2) - ) - } - } - ] - , comments = [] - } - ) - - -postProcessInfixOperators3 : ( String, String, File ) -postProcessInfixOperators3 = - ( "postProcessInfixOperators3" - , """ -module Bar exposing (..) - -bar = x * 1 + 2 -""" - , { moduleDefinition = - Node - { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } - <| - NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ] - , exposingList = Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } <| All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } } - } - , imports = [] - , declarations = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 16 } } <| - FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 3, column = 1 }, end = { row = 3, column = 16 } } - { name = Node { start = { row = 3, column = 1 }, end = { row = 3, column = 4 } } "bar" - , arguments = [] - , expression = - Node { start = { row = 3, column = 7 }, end = { row = 3, column = 16 } } <| - OperatorApplication "+" - Left - (Node { start = { row = 3, column = 7 }, end = { row = 3, column = 12 } } <| - OperatorApplication "*" - Left - (Node { start = { row = 3, column = 7 }, end = { row = 3, column = 8 } } <| FunctionOrValue [] "x") - (Node { start = { row = 3, column = 11 }, end = { row = 3, column = 12 } } <| Integer 1) - ) - (Node { start = { row = 3, column = 15 }, end = { row = 3, column = 16 } } <| Integer 2) - } - } - ] - , comments = [] - } - ) - - -postProcessInfixOperatorsInNegation : ( String, String, File ) -postProcessInfixOperatorsInNegation = - ( "postProcessInfixOperatorsInNegation" - , """ -module Bar exposing (..) - -bar = -(1 * 2) -""" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } <| - NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ] - , exposingList = Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } <| All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } } - } - , imports = [] - , declarations = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 15 } } - (FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 3, column = 1 }, end = { row = 3, column = 15 } } - { name = Node { start = { row = 3, column = 1 }, end = { row = 3, column = 4 } } "bar" - , arguments = [] - , expression = - Node { start = { row = 3, column = 7 }, end = { row = 3, column = 15 } } - (Negation - (Node { start = { row = 3, column = 8 }, end = { row = 3, column = 15 } } - (ParenthesizedExpression - (Node { start = { row = 3, column = 9 }, end = { row = 3, column = 14 } } - (OperatorApplication - "*" - Left - (Node { start = { row = 3, column = 9 }, end = { row = 3, column = 10 } } (Integer 1)) - (Node { start = { row = 3, column = 13 }, end = { row = 3, column = 14 } } (Integer 2)) - ) - ) - ) - ) - ) - } - } - ) - ] - , comments = [] - } - ) - - -postProcessInfixOperatorsInRecordAccess : ( String, String, File ) -postProcessInfixOperatorsInRecordAccess = - ( "postProcessInfixOperatorsInRecordAccess" - , """ -module Bar exposing (..) - -bar = (1 * 2).x -""" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 25 } } <| - NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 11 } } [ "Bar" ] - , exposingList = Node { start = { row = 1, column = 12 }, end = { row = 1, column = 25 } } <| All { start = { row = 1, column = 22 }, end = { row = 1, column = 24 } } - } - , imports = [] - , declarations = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 16 } } - (FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 3, column = 1 }, end = { row = 3, column = 16 } } - { name = Node { start = { row = 3, column = 1 }, end = { row = 3, column = 4 } } "bar" - , arguments = [] - , expression = - Node { start = { row = 3, column = 7 }, end = { row = 3, column = 16 } } - (RecordAccess - (Node { start = { row = 3, column = 7 }, end = { row = 3, column = 14 } } - (ParenthesizedExpression - (Node { start = { row = 3, column = 8 }, end = { row = 3, column = 13 } } - (OperatorApplication - "*" - Left - (Node { start = { row = 3, column = 8 }, end = { row = 3, column = 9 } } (Integer 1)) - (Node { start = { row = 3, column = 12 }, end = { row = 3, column = 13 } } (Integer 2)) - ) - ) - ) - ) - (Node { start = { row = 3, column = 15 }, end = { row = 3, column = 16 } } "x") - ) - } - } - ) - ] - , comments = [] - } - ) - - -{-| Check to make sure this issue is fixed --} -postProcessInfixOperatorsRegressionTest : ( String, String, File ) -postProcessInfixOperatorsRegressionTest = - ( "postProcessInfixOperatorsRegressionTest" - , """ -module A exposing (..) - -bool1 = True && True || True -bool2 = True || True && True - -numeric1 = 1 ^ 2 * 3 + 4 -numeric2 = 1 + 2 * 3 ^ 4 -""" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 23 } } - (NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 9 } } [ "A" ] - , exposingList = - Node { start = { row = 1, column = 10 }, end = { row = 1, column = 23 } } - (All { start = { row = 1, column = 20 }, end = { row = 1, column = 22 } }) - } - ) - , imports = [] - , declarations = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 29 } } - (FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 3, column = 1 }, end = { row = 3, column = 29 } } - { name = Node { start = { row = 3, column = 1 }, end = { row = 3, column = 6 } } "bool1" - , arguments = [] - , expression = - Node { start = { row = 3, column = 9 }, end = { row = 3, column = 29 } } - (OperatorApplication "||" - Right - (Node { start = { row = 3, column = 9 }, end = { row = 3, column = 21 } } - (OperatorApplication "&&" - Right - (Node { start = { row = 3, column = 9 }, end = { row = 3, column = 13 } } (FunctionOrValue [] "True")) - (Node { start = { row = 3, column = 17 }, end = { row = 3, column = 21 } } (FunctionOrValue [] "True")) - ) - ) - (Node { start = { row = 3, column = 25 }, end = { row = 3, column = 29 } } (FunctionOrValue [] "True")) - ) - } - } - ) - , Node { start = { row = 4, column = 1 }, end = { row = 4, column = 29 } } - (FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 4, column = 1 }, end = { row = 4, column = 29 } } - { name = Node { start = { row = 4, column = 1 }, end = { row = 4, column = 6 } } "bool2" - , arguments = [] - , expression = - Node { start = { row = 4, column = 9 }, end = { row = 4, column = 29 } } - (OperatorApplication "||" - Right - (Node { start = { row = 4, column = 9 }, end = { row = 4, column = 13 } } (FunctionOrValue [] "True")) - (Node { start = { row = 4, column = 17 }, end = { row = 4, column = 29 } } - (OperatorApplication "&&" - Right - (Node { start = { row = 4, column = 17 }, end = { row = 4, column = 21 } } (FunctionOrValue [] "True")) - (Node { start = { row = 4, column = 25 }, end = { row = 4, column = 29 } } (FunctionOrValue [] "True")) - ) - ) - ) - } - } - ) - , Node { start = { row = 6, column = 1 }, end = { row = 6, column = 25 } } - (FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 6, column = 1 }, end = { row = 6, column = 25 } } - { name = Node { start = { row = 6, column = 1 }, end = { row = 6, column = 9 } } "numeric1" - , arguments = [] - , expression = - Node { start = { row = 6, column = 12 }, end = { row = 6, column = 25 } } - (OperatorApplication "+" - Left - (Node { start = { row = 6, column = 12 }, end = { row = 6, column = 21 } } - (OperatorApplication "*" - Left - (Node { start = { row = 6, column = 12 }, end = { row = 6, column = 17 } } - (OperatorApplication "^" - Right - (Node { start = { row = 6, column = 12 }, end = { row = 6, column = 13 } } (Integer 1)) - (Node { start = { row = 6, column = 16 }, end = { row = 6, column = 17 } } (Integer 2)) - ) - ) - (Node { start = { row = 6, column = 20 }, end = { row = 6, column = 21 } } (Integer 3)) - ) - ) - (Node - { start = { row = 6, column = 24 }, end = { row = 6, column = 25 } } - (Integer 4) - ) - ) - } - } - ) - , Node { start = { row = 7, column = 1 }, end = { row = 7, column = 25 } } - (FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 7, column = 1 }, end = { row = 7, column = 25 } } - { name = Node { start = { row = 7, column = 1 }, end = { row = 7, column = 9 } } "numeric2" - , arguments = [] - , expression = - Node { start = { row = 7, column = 12 }, end = { row = 7, column = 25 } } - (OperatorApplication "+" - Left - (Node { start = { row = 7, column = 12 }, end = { row = 7, column = 13 } } (Integer 1)) - (Node { start = { row = 7, column = 16 }, end = { row = 7, column = 25 } } - (OperatorApplication "*" - Left - (Node { start = { row = 7, column = 16 }, end = { row = 7, column = 17 } } (Integer 2)) - (Node { start = { row = 7, column = 20 }, end = { row = 7, column = 25 } } - (OperatorApplication "^" - Right - (Node { start = { row = 7, column = 20 }, end = { row = 7, column = 21 } } (Integer 3)) - (Node { start = { row = 7, column = 24 }, end = { row = 7, column = 25 } } (Integer 4)) - ) - ) - ) - ) - ) - } - } - ) - ] - , comments = [] - } - ) - - -{-| Check to make sure this issue is fixed --} -postProcessInfixOperatorsAssociativityTest : ( String, String, File ) -postProcessInfixOperatorsAssociativityTest = - ( "postProcessInfixOperatorsAssociativityTest" - , """ -module A exposing (..) - -numeric1 = 1 + 2 - 3 - -pipeline1 = 1 |> 2 |> 3 -""" - , { moduleDefinition = - Node { start = { row = 1, column = 1 }, end = { row = 1, column = 23 } } - (NormalModule - { moduleName = Node { start = { row = 1, column = 8 }, end = { row = 1, column = 9 } } [ "A" ] - , exposingList = - Node { start = { row = 1, column = 10 }, end = { row = 1, column = 23 } } - (All { start = { row = 1, column = 20 }, end = { row = 1, column = 22 } }) - } - ) - , imports = [] - , declarations = - [ Node { start = { row = 3, column = 1 }, end = { row = 3, column = 21 } } - (FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 3, column = 1 }, end = { row = 3, column = 21 } } - { name = Node { start = { row = 3, column = 1 }, end = { row = 3, column = 9 } } "numeric1" - , arguments = [] - , expression = - Node { start = { row = 3, column = 12 }, end = { row = 3, column = 21 } } - (OperatorApplication "-" - Left - (Node { start = { row = 3, column = 12 }, end = { row = 3, column = 17 } } - (OperatorApplication "+" - Left - (Node { start = { row = 3, column = 12 }, end = { row = 3, column = 13 } } (Integer 1)) - (Node { start = { row = 3, column = 16 }, end = { row = 3, column = 17 } } (Integer 2)) - ) - ) - (Node { start = { row = 3, column = 20 }, end = { row = 3, column = 21 } } (Integer 3)) - ) - } - } - ) - , Node { start = { row = 5, column = 1 }, end = { row = 5, column = 24 } } - (FunctionDeclaration - { documentation = Nothing - , signature = Nothing - , declaration = - Node { start = { row = 5, column = 1 }, end = { row = 5, column = 24 } } - { name = Node { start = { row = 5, column = 1 }, end = { row = 5, column = 10 } } "pipeline1" - , arguments = [] - , expression = - Node { start = { row = 5, column = 13 }, end = { row = 5, column = 24 } } - (OperatorApplication "|>" - Left - (Node { start = { row = 5, column = 13 }, end = { row = 5, column = 19 } } - (OperatorApplication "|>" - Left - (Node { start = { row = 5, column = 13 }, end = { row = 5, column = 14 } } (Integer 1)) - (Node { start = { row = 5, column = 18 }, end = { row = 5, column = 19 } } (Integer 2)) - ) - ) - (Node { start = { row = 5, column = 23 }, end = { row = 5, column = 24 } } (Integer 3)) - ) - } - } - ) - ] - , comments = [] - } - ) diff --git a/tests/Elm/Syntax/ExposingTests.elm b/tests/Elm/Syntax/ExposingTests.elm deleted file mode 100644 index 9d3a7ed89..000000000 --- a/tests/Elm/Syntax/ExposingTests.elm +++ /dev/null @@ -1,32 +0,0 @@ -module Elm.Syntax.ExposingTests exposing (suite) - -import Elm.Syntax.Exposing as Exposing exposing (..) -import Elm.Syntax.Node as Node -import Expect -import Json.Decode exposing (Decoder, Value) -import Test exposing (..) - - -symmetric : a -> (a -> Value) -> Decoder a -> Expect.Expectation -symmetric v enc dec = - Expect.equal (Json.Decode.decodeValue dec (enc v)) (Ok v) - - -suite : Test -suite = - describe "Elm.Syntax.Exposing" - [ describe "serialization" - [ test "exposing (beginnerProgram, div, button, text)" <| - \() -> - symmetric - (Explicit - [ Node.empty (FunctionExpose "beginnerProgram") - , Node.empty (FunctionExpose "div") - , Node.empty (FunctionExpose "button") - , Node.empty (FunctionExpose "text") - ] - ) - Exposing.encode - Exposing.decoder - ] - ] diff --git a/tests/Elm/Syntax/ImportTests.elm b/tests/Elm/Syntax/ImportTests.elm deleted file mode 100644 index 77264356d..000000000 --- a/tests/Elm/Syntax/ImportTests.elm +++ /dev/null @@ -1,44 +0,0 @@ -module Elm.Syntax.ImportTests exposing (suite) - -import Elm.Syntax.Exposing exposing (..) -import Elm.Syntax.Import as Import exposing (Import) -import Elm.Syntax.Node as Node -import Elm.Syntax.Range exposing (empty) -import Expect -import Json.Decode exposing (Decoder, Value) -import Test exposing (..) - - -symmetric : a -> (a -> Value) -> Decoder a -> Expect.Expectation -symmetric v enc dec = - Expect.equal (Json.Decode.decodeValue dec (enc v)) (Ok v) - - -suite : Test -suite = - describe "Elm.Syntax.Import" - [ describe "serialization" - [ test "case 1" <| - \() -> - symmetric (Import (Node.empty [ "A", "B" ]) (Just <| Node.empty [ "C" ]) (Just <| Node.empty <| All empty)) - Import.encode - Import.decoder - , test "import Html exposing (beginnerProgram, div, button, text)" <| - \() -> - symmetric - (Import (Node.empty [ "Html" ]) - Nothing - (Just <| - Node.empty <| - Explicit - [ Node.empty (FunctionExpose "beginnerProgram") - , Node.empty (FunctionExpose "div") - , Node.empty (FunctionExpose "button") - , Node.empty (FunctionExpose "text") - ] - ) - ) - Import.encode - Import.decoder - ] - ] diff --git a/tests/Elm/WriterTests.elm b/tests/Elm/WriterTests.elm deleted file mode 100644 index 6259b9deb..000000000 --- a/tests/Elm/WriterTests.elm +++ /dev/null @@ -1,468 +0,0 @@ -module Elm.WriterTests exposing (suite) - -import Elm.Parser.Expression exposing (expression) -import Elm.Parser.ParserWithCommentsTestUtil exposing (parse) -import Elm.Syntax.Declaration exposing (..) -import Elm.Syntax.Exposing exposing (..) -import Elm.Syntax.Expression exposing (..) -import Elm.Syntax.Module exposing (..) -import Elm.Syntax.Node exposing (Node(..)) -import Elm.Syntax.Pattern exposing (..) -import Elm.Syntax.Range exposing (empty) -import Elm.Syntax.Type exposing (..) -import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation(..)) -import Elm.Writer as Writer -import Expect -import Test exposing (..) - - -suite : Test -suite = - describe "Writer" - [ test "write file exposing all" <| - \() -> - { moduleDefinition = - Node empty <| - NormalModule - { moduleName = Node empty <| [ "A" ] - , exposingList = Node empty <| All empty - } - , imports = - [ Node empty - { moduleName = Node empty <| [ "B" ] - , moduleAlias = Nothing - , exposingList = Nothing - } - , Node empty - { moduleName = Node empty <| [ "C" ] - , moduleAlias = Just (Node empty [ "D" ]) - , exposingList = Just (Node empty <| All empty) - } - ] - , declarations = [] - , comments = [] - } - |> Writer.writeFile - |> Writer.write - |> Expect.equal - ("module A exposing (..)\n" - ++ "import B " - ++ "\n" - ++ "import C as D exposing (..)\n" - ) - , describe "Expression" - [ test "write simple expression" <| - \() -> - (Node empty <| - Application - [ Node empty <| FunctionOrValue [] "abc" - , Node empty <| UnitExpr - ] - ) - |> Writer.writeExpression - |> Writer.write - |> Expect.equal "abc ()" - , test "write qualified expression" <| - \() -> - (Node empty <| FunctionOrValue [ "Foo", "Bar" ] "baz") - |> Writer.writeExpression - |> Writer.write - |> Expect.equal "Foo.Bar.baz" - , test "Expression.RecordAccessFunction should be parsed then written idempotently" <| - \() -> - let - input : String - input = - "(.spaceEvenly Internal.Style.classes)" - in - parse input expression - |> Maybe.map Writer.writeExpression - |> Maybe.map Writer.write - |> Expect.equal - (Just input) - , test "regression test for Expression.RecordAccessFunction being written without leading period" <| - \() -> - (Node empty <| - Application - [ Node empty <| FunctionOrValue [ "List" ] "map" - , Node empty <| RecordAccessFunction "name" - , Node empty <| FunctionOrValue [] "people" - ] - ) - |> Writer.writeExpression - |> Writer.write - |> Expect.equal "List.map .name people" - ] - , describe "Pattern" - [ test "write string pattern" <| - \() -> - StringPattern "test" - |> Node empty - |> Writer.writePattern - |> Writer.write - |> Expect.equal "\"test\"" - , test "write string pattern containing \"" <| - \() -> - StringPattern "test\"" - |> Node empty - |> Writer.writePattern - |> Writer.write - |> Expect.equal "\"test\\\"\"" - ] - , describe "TypeAnnotation" - [ test "write simple type" <| - \() -> - Typed (Node empty <| ( [], "String" )) [] - |> Node empty - |> Writer.writeTypeAnnotation - |> Writer.write - |> Expect.equal "String" - , test "write qualified type" <| - \() -> - (Node empty <| - Typed - (Node empty <| ( [ "Json", "Decode" ], "Decoder" )) - [ Node empty <| GenericType "a" ] - ) - |> Writer.writeTypeAnnotation - |> Writer.write - |> Expect.equal "Json.Decode.Decoder a" - , test "write type arguments that require parentheses" <| - \() -> - (Node empty <| - Typed (Node empty ( [], "List" )) - [ Node empty <| - Typed (Node empty ( [], "Dict" )) - [ Node empty <| Typed (Node empty ( [], "String" )) [] - , Node empty <| Typed (Node empty ( [], "Int" )) [] - ] - ] - ) - |> Writer.writeTypeAnnotation - |> Writer.write - |> Expect.equal "List (Dict String Int)" - , test "write type arguments that are functions" <| - \() -> - (Node empty <| - FunctionTypeAnnotation - (Node empty <| - FunctionTypeAnnotation - (Node empty <| GenericType "a") - (Node empty <| GenericType "b") - ) - (Node empty <| Typed (Node empty ( [], "Int" )) []) - ) - |> Writer.writeTypeAnnotation - |> Writer.write - |> Expect.equal "(a -> b) -> Int" - ] - , describe "Declaration" - [ test "write type declaration > simple constructors" <| - \() -> - (Node empty <| - CustomTypeDeclaration - (Type - Nothing - (Node empty "Sample") - [] - [ Node empty <| ValueConstructor (Node empty "Foo") [] - , Node empty <| ValueConstructor (Node empty "Bar") [] - ] - ) - ) - |> Writer.writeDeclaration - |> Writer.write - |> Expect.equal - ("type Sample \n" - ++ " = Foo | Bar" - ) - , test "write type declaration > constructors with arguments" <| - \() -> - let - listT : TypeAnnotation - listT = - Typed (Node empty ( [], "List" )) - [ Node empty <| - Typed (Node empty ( [], "String" )) [] - ] - - stringT : TypeAnnotation - stringT = - Typed (Node empty ( [], "String" )) [] - in - (Node empty <| - CustomTypeDeclaration - (Type - Nothing - (Node empty "Sample") - [] - [ Node empty <| ValueConstructor (Node empty "Foo") [ Node empty listT, Node empty stringT ] - , Node empty <| ValueConstructor (Node empty "Bar") [] - ] - ) - ) - |> Writer.writeDeclaration - |> Writer.write - |> Expect.equal - ("type Sample \n" - ++ " = Foo (List String) String | Bar" - ) - , test "write type declaration > constructors with functions as arguments" <| - \() -> - let - funcT : TypeAnnotation - funcT = - FunctionTypeAnnotation - (Node empty <| Typed (Node empty ( [], "String" )) []) - (Node empty <| Typed (Node empty ( [], "Int" )) []) - - stringT : TypeAnnotation - stringT = - Typed (Node empty ( [], "String" )) [] - in - (Node empty <| - CustomTypeDeclaration - (Type - Nothing - (Node empty "Sample") - [] - [ Node empty <| - ValueConstructor (Node empty "Foo") - [ Node empty funcT - , Node empty stringT - ] - , Node empty <| ValueConstructor (Node empty "Bar") [] - ] - ) - ) - |> Writer.writeDeclaration - |> Writer.write - |> Expect.equal - ("type Sample \n" - ++ " = Foo (String -> Int) String | Bar" - ) - , test "write function with case expression using the right indentations" <| - \() -> - let - body : Expression - body = - CaseExpression - (CaseBlock (Node empty <| FunctionOrValue [] "someCase") - [ ( Node empty <| IntPattern 1, Node empty <| FunctionOrValue [] "doSomething" ) - , ( Node empty <| IntPattern 2, Node empty <| FunctionOrValue [] "doSomethingElse" ) - ] - ) - - function : Declaration - function = - FunctionDeclaration - (Function Nothing - Nothing - (Node empty <| - FunctionImplementation - (Node empty <| "functionName") - [] - (Node empty body) - ) - ) - in - Node empty function - |> Writer.writeDeclaration - |> Writer.write - |> Expect.equal - ("\n\nfunctionName =\n" - ++ " \n" - ++ " case someCase of\n" - ++ " 1 ->\n" - ++ " doSomething\n" - ++ " 2 ->\n" - ++ " doSomethingElse\n" - ++ " " - ) - , test "regression test for incorrect indentation in case expression" <| - \() -> - let - body : Expression - body = - LambdaExpression - { args = [ Node empty (VarPattern "myArgument") ] - , expression = - Node empty <| - CaseExpression - (CaseBlock (Node empty <| FunctionOrValue [] "someCase") - [ ( Node empty <| IntPattern 1, Node empty <| FunctionOrValue [] "doSomething" ) - , ( Node empty <| IntPattern 2, Node empty <| FunctionOrValue [] "doSomethingElse" ) - ] - ) - } - - function : Declaration - function = - FunctionDeclaration - (Function Nothing - Nothing - (Node empty <| - FunctionImplementation - (Node empty <| "functionName") - [] - (Node empty body) - ) - ) - in - Node empty function - |> Writer.writeDeclaration - |> Writer.write - |> Expect.equal - ("\n\nfunctionName =\n" - ++ " \\myArgument -> \n" - ++ " case someCase of\n" - ++ " 1 ->\n" - ++ " doSomething\n" - ++ " 2 ->\n" - ++ " doSomethingElse\n" - ++ " " - ) - , test "regression test for incorrect parenthesis placement in case expression" <| - \() -> - let - body : Expression - body = - ParenthesizedExpression - (Node empty <| - LambdaExpression - { args = [ Node empty (VarPattern "myArgument") ] - , expression = - Node empty <| - CaseExpression - (CaseBlock (Node empty <| FunctionOrValue [] "someCase") - [ ( Node empty <| IntPattern 1, Node empty <| FunctionOrValue [] "doSomething" ) - , ( Node empty <| IntPattern 2, Node empty <| FunctionOrValue [] "doSomethingElse" ) - ] - ) - } - ) - - function : Declaration - function = - FunctionDeclaration - (Function Nothing - Nothing - (Node empty <| - FunctionImplementation - (Node empty <| "functionName") - [] - (Node empty body) - ) - ) - in - Node empty function - |> Writer.writeDeclaration - |> Writer.write - |> Expect.equal - ("\n\nfunctionName =\n" - ++ " (\\myArgument -> \n" - ++ " case someCase of\n" - ++ " 1 ->\n" - ++ " doSomething\n" - ++ " 2 ->\n" - ++ " doSomethingElse\n" - ++ " )" - ) - , test "regression test for char literals not being escaped" <| - \() -> - ListExpr - [ Node empty (CharLiteral '\\') - , Node empty (CharLiteral '"') - , Node empty (CharLiteral '\'') - , Node empty (CharLiteral '\t') - , Node empty (CharLiteral '→') - , Node empty (CharLiteral '\u{00A0}') - ] - |> Node empty - |> Writer.writeExpression - |> Writer.write - |> Expect.equal "['\\\\', '\"', '\\'', '\\\t', '→', '\u{00A0}']" - , test "regression test for char pattern not being escaped" <| - \() -> - ListPattern - [ Node empty (CharPattern '\\') - , Node empty (CharPattern '"') - , Node empty (CharPattern '\'') - , Node empty (CharPattern '\t') - , Node empty (CharPattern '→') - , Node empty (CharPattern '\u{00A0}') - ] - |> Node empty - |> Writer.writePattern - |> Writer.write - |> Expect.equal "['\\\\', '\"', '\\'', '\\\t', '→', '\u{00A0}']" - , test "nested case expressions" <| - \() -> - let - body : Node Expression -> Node Expression - body nested = - Node empty <| - CaseExpression - (CaseBlock (Node empty <| FunctionOrValue [] "someCase") - [ ( Node empty <| IntPattern 1, nested ) - , ( Node empty <| IntPattern 2, Node empty <| FunctionOrValue [] "doSomethingElse" ) - ] - ) - - function : Declaration - function = - FunctionDeclaration - (Function Nothing - Nothing - (Node empty <| - FunctionImplementation - (Node empty <| "functionName") - [] - (Node empty - (ParenthesizedExpression - (Node empty <| - LambdaExpression - { args = [ Node empty (VarPattern "myArgument") ] - , expression = - body (body (Node empty UnitExpr)) - } - ) - ) - ) - ) - ) - in - Node empty function - |> Writer.writeDeclaration - |> Writer.write - |> Expect.equal - ("\n\nfunctionName =\n" - ++ " (\\myArgument -> \n" - ++ " case someCase of\n" - ++ " 1 ->\n" - ++ " \n" - ++ " case someCase of\n" - ++ " 1 ->\n" - ++ " ()\n" - ++ " 2 ->\n" - ++ " doSomethingElse\n" - ++ " \n" - ++ " 2 ->\n" - ++ " doSomethingElse\n" - ++ " )" - ) - ] - , describe "Tuple" - [ test "write tuple" <| - \() -> - (Node empty <| - TupledExpression - [ Node empty <| Integer 1 - , Node empty <| Integer 2 - ] - ) - |> Writer.writeExpression - |> Writer.write - |> Expect.equal "(1, 2)" - ] - ]