Skip to content
This repository was archived by the owner on Oct 5, 2024. It is now read-only.

Parse comments #106

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Elm/Compiler.elm
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ import Elm.Data.Type.Concrete exposing (ConcreteType)
import Elm.Data.TypeAnnotation exposing (TypeAnnotation)
import Elm.Data.VarName exposing (VarName)
import OurExtras.Dict as Dict
import Parser.Advanced as P
import Result.Extra as Result
import Stage.Desugar
import Stage.Desugar.Boilerplate
Expand All @@ -151,6 +150,7 @@ import Stage.InferTypes.Boilerplate
import Stage.InferTypes.SubstitutionMap exposing (SubstitutionMap)
import Stage.Optimize
import Stage.Optimize.Boilerplate
import Stage.Parse.AdvancedWithState as P
import Stage.Parse.Parser


Expand All @@ -162,16 +162,17 @@ import Stage.Parse.Parser
what ParseContext or ParseProblem means. Don't expose it.
-}
type alias Parser a =
P.Parser ParseContext ParseProblem a
P.Parser ParseContext ParseProblem Stage.Parse.Parser.State a


{-| A helper for a common pattern with our Elm parsers. Don't expose it.
-}
parse : Parser a -> FileContents -> Result Error a
parse parser sourceCode =
Result.mapError
(\errorList -> ParseError (ParseProblem ( errorList, sourceCode )))
(P.run parser sourceCode)
P.run parser { comments = [] } sourceCode
|> Result.map Tuple.second
|> Result.mapError
(\errorList -> ParseError (ParseProblem ( errorList, sourceCode )))


{-| Parse a single expression like
Expand Down
63 changes: 62 additions & 1 deletion src/Elm/Compiler/Error.elm
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type ParseContext
| InTuple
| InTuple3
| InRecord
| InRecordBinding
| InFile FilePath
| InCase
| InPattern
Expand Down Expand Up @@ -102,7 +103,8 @@ type ParseContext
and the [location info](Elm.Data.Located) this should give you enough info about what's wrong.
-}
type ParseProblem
= ExpectingPortKeyword -- `>port< module ...`
= TooMuchIndentation String
| ExpectingPortKeyword -- `>port< module ...`
| ExpectingEffectKeyword -- `>effect< module ...`
| ExpectingModuleKeyword -- `>module< Foo.Bar exposing (..)`
| ExpectingModuleName -- `module >Foo.Bar< exposing (..)`
Expand Down Expand Up @@ -151,6 +153,8 @@ type ParseProblem
| ExpectingTrue
| ExpectingFalse
| ExpectingLet
| ExpectingLetIndentation
| ExpectingLetBindingIndentation
| ExpectingIn
| ExpectingUnit
| ExpectingColon
Expand Down Expand Up @@ -404,6 +408,18 @@ fullVarName { qualifiedness, name } =
parseProblemToString : ParseProblem -> String
parseProblemToString problem =
case problem of
TooMuchIndentation str ->
{- TODO
Too Much Indentation
Line 1, Column 2
This `module` should not have any spaces before it:

1| module Main
^
Delete the spaces before `module` until there are none left!
-}
"TooMuchIndentation " ++ str

ExpectingPortKeyword ->
"ExpectingPortKeyword"

Expand Down Expand Up @@ -551,6 +567,51 @@ parseProblemToString problem =
ExpectingLet ->
"ExpectingLet"

ExpectingLetIndentation ->
{- TODO
Unfinished Let
Line 20, Column 6
I was partway through parsing a `let` expression, but I got stuck here:

20| let
^
I was expecting a value to be defined here.

Note: Here is an example with a valid `let` expression for reference:

viewPerson person =
let
fullName =
person.firstName ++ " " ++ person.lastName
in
div [] [ text fullName ]

Here we defined a `viewPerson` function that turns a person into some HTML. We
use a `let` expression to define the `fullName` we want to show. Notice the
indentation! The `fullName` is indented more than the `let` keyword, and the
actual value of `fullName` is indented a bit more than that. That is important!
-}
"ExpectingLetIndentation"

ExpectingLetBindingIndentation ->
{- TODO
ERRORS
Unexpected Equals
Line 22, Column 7
I was not expecting to see this equals sign:

22| y = 2 in { count = 0 }
^
Maybe you want == instead? To check if two values are equal?

Note: I may be getting confused by your indentation. I think I am still parsing
the `x` definition. Is this supposed to be part of a definition after that? If
so, the problem may be a bit before the equals sign. I need all definitions to
be indented exactly the same amount, so the problem may be that this new
definition has too many spaces in front of it.
-}
"ExpectingLetBindingIndentation"

ExpectingIn ->
"ExpectingIn"

Expand Down
16 changes: 15 additions & 1 deletion src/Elm/Data/Module.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Elm.Data.Module exposing
( Module, ModuleType(..)
, map
, unalias, exposes, imports, findModuleOfVar
, Comment, CommentKind(..)
)

{-| Module information (corresponds to a single .elm file).
Expand All @@ -20,6 +21,7 @@ import Elm.Data.Declaration as Declaration exposing (Declaration)
import Elm.Data.Exposing exposing (ExposedItem(..), Exposing(..))
import Elm.Data.FilePath exposing (FilePath)
import Elm.Data.Import exposing (Import)
import Elm.Data.Located exposing (Located)
import Elm.Data.ModuleName exposing (ModuleName)
import Elm.Data.Qualifiedness exposing (PossiblyQualified(..), Qualified(..))
import Elm.Data.VarName exposing (VarName)
Expand All @@ -29,14 +31,14 @@ import Result.Extra

{-| -}
type alias Module expr annotation qualifiedness =
-- TODO comments? doc comments?
{ -- TODO somewhere check that dependencies' exposing lists contain only what's in that module's exposing list
imports : Dict ModuleName Import
, name : ModuleName
, filePath : FilePath
, declarations : Dict VarName (Declaration expr annotation qualifiedness)
, type_ : ModuleType
, exposing_ : Exposing
, comments : List Comment
}


Expand All @@ -58,6 +60,17 @@ type ModuleType
| EffectModule


type alias Comment =
{ content : Located String
, kind : CommentKind
}


type CommentKind
= SingleLine
| MultiLine


{-| Does this module import this module name?
(This doesn't take the `as ...` part of the import line into consideration.)
-}
Expand Down Expand Up @@ -144,6 +157,7 @@ map fnExpr fnAnnotation fnQualifiedness module_ =
module_.declarations
, type_ = module_.type_
, exposing_ = module_.exposing_
, comments = module_.comments
}


Expand Down
1 change: 1 addition & 0 deletions src/Stage/Desugar/Boilerplate.elm
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ moduleOfNewType old newDecls =
, filePath = old.filePath
, type_ = old.type_
, exposing_ = old.exposing_
, comments = old.comments

-- all that code because of this:
, declarations = newDecls
Expand Down
1 change: 1 addition & 0 deletions src/Stage/InferTypes/Boilerplate.elm
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ moduleOfNewType old newDecls =
, filePath = old.filePath
, type_ = old.type_
, exposing_ = old.exposing_
, comments = old.comments

-- all that code because of this:
, declarations = newDecls
Expand Down
6 changes: 4 additions & 2 deletions src/Stage/Parse.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import Elm.Compiler.Error exposing (Error(..), ParseError(..))
import Elm.Data.FileContents exposing (FileContents)
import Elm.Data.FilePath exposing (FilePath)
import Elm.Data.Module exposing (Module)
import Elm.Data.ModuleName as ModuleName exposing (ModuleName)
import Elm.Data.Qualifiedness exposing (PossiblyQualified)
import Elm.Data.TypeAnnotation exposing (TypeAnnotation)
import Parser.Advanced as P
import Stage.Parse.AdvancedWithState as P
import Stage.Parse.Parser as Parser


Expand All @@ -18,6 +19,7 @@ parse :
{ filePath : FilePath, fileContents : FileContents }
-> Result Error (Module Frontend.LocatedExpr TypeAnnotation PossiblyQualified)
parse { filePath, fileContents } =
P.run (Parser.module_ filePath) fileContents
P.run (Parser.module_ filePath) { comments = [] } fileContents
|> Result.map Tuple.second
|> Result.mapError
(\errorList -> ParseError (ParseProblem ( errorList, fileContents )))
Loading