lue-bird/elm-syntax-format/1.1.1

NOTE: the latest version is 1.1.20

ElmSyntaxParserLenient

Like Elm.Parser but able to parse badly indented code and similar somewhat incorrect syntax, similar to elm-format.

This is not a fault-tolerant parser! So if you write something it can't recognize in a file, the whole thing will fail.

Also, precise ranges of some parts in in the parsed result are not reliable. Though they will still be correct when viewed relative to each other and will tell you how many lines they span. This means ElmSyntaxPrint can pick this up and format it in a way compatible with the compiler or Elm.Parser.

Some additional lenient parsing:

  • f | g → f |> g

  • a != b → a /= b

  • merges consecutive , in record, list or explicit exposing

  • removes extra , before first record field, list element or expose

  • removes remove extra | before first variant declaration

  • corrects expression record field name-value separators

    { name : value } or { name value }

    → { name = value }

  • corrects type record field name-value separators

    { name = value } or { name value }

    → { name : value }

  • expands expression record field punning

    { field } → { field = field }

  • corrects -> to = in an expression declaration and let expression declaration

    function parameters -> result

    → function parameters = result

  • allows omitting the name before the type in an expression declaration or let expression declaration

    : Type
    function parameters =
        result

    →

    function : Type
    function parameters =
        result
  • allows matching everything before

    3 |> String.toInt case
        Nothing ->
            0
    
    
        Just n ->
            n

    →

    case 3 |> String.toInt of
        Nothing ->
            0
    
    
        Just n ->
            n
  • moves import statements anywhere at the top level to the import section

  • corrects exposing (...) → (..)

type alias Parser a =
Parser a

Can turn a String into syntax or Nothing. See ElmSyntaxParserLenient.run

(This is not related to elm/parser. Open an issue if you need a way to covert to that)

run : Parser a -> String -> Maybe a

Turn a given source String into Just the parsed syntax or Nothing if any unrecognizable part is found.

module_ : Parser File

That's all you'll need most of the time.

Sometimes it's useful to parse only some part of the syntax, to, say, display only an expression in an article or reparse only the touched declarations on save.

type alias Comments =
Maybe (RopeFilled (Node String))

A bag of comment nodes. Each comment string contains the {-, -} or --.

Access with commentsToList

commentsToList : Comments -> List (Node String)

Extract a list of comment nodes from parse result Comments

Parser
{ comments : Comments
, syntax : Node TopLevelExpose
}
Parser
{ comments : Comments
, syntax : Exposing
}

Parser for an Elm.Syntax.Exposing.Exposing (the stuff after exposing in an import or module)

Parser
{ comments : Comments
, syntax : Node Module
}

Parser for an Elm.Syntax.Module.Module (confusingly, that's their name for only the module X exposing (Y) lines)

Parser
{ comments : Comments
, syntax : Node Import
}
Parser
{ comments : Comments
, syntax : List (Node Declaration)
}

Parser for a list of Elm.Syntax.Declaration.Declarations and comments in between

Parser
{ comments : Comments
, syntax : Node Declaration
}
Parser
{ comments : Comments
, syntax : Node TypeAnnotation
}
Parser
{ comments : Comments
, syntax : Node Pattern
}
Parser
{ comments : Comments
, syntax : Node Expression
}

whitespace

multiLineComment : Parser (Node String)

Parser for a {-...-} comment, also verifying that it itself isn't a documentation comment

singleLineComment : Parser (Node String)

Parser for a --... comment

whitespaceAndComments : Parser Comments

Parser for the space between syntax tokens which can contain spaces, linebreaks, multiLineComments and singleLineComments

low-level

moduleName : Parser (Node ModuleName)
nameLowercase : Parser String

Parser for a name used for record field names and unqualified function/value references

nameUppercase : Parser String

Parser for a name used for type names, variant names, record type alias constructor function names and module names

type RopeFilled a
= RopeLeaf a ()
| RopeBranch2 (RopeFilled a) (RopeFilled a)

Constantly appending lists of comments when combining parse can get expensive, so we summarize everything in this temporary structure and only convert to a list when we're done.

Inspired by miniBill/elm-rope