Skip to content

Commit

Permalink
Merge pull request #743 from emmabastas/new-parser-2021-refactor
Browse files Browse the repository at this point in the history
Parsec API refactor
  • Loading branch information
avh4 committed Aug 11, 2021
2 parents f9cd7ff + 8d325a8 commit f98fd08
Show file tree
Hide file tree
Showing 30 changed files with 836 additions and 1,007 deletions.
10 changes: 1 addition & 9 deletions elm-format-lib/elm-format-lib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,12 @@ common common-options
Parse.Literal
Parse.Module
Parse.Parse
Parse.ParsecAdapter
Parse.Pattern
Parse.Primitives
Parse.State
Parse.Type
Parse.Whitespace
Text.Parsec
Text.Parsec.Char
Text.Parsec.Combinator
Text.Parsec.Error
Text.Parsec.Indent
Text.Parsec.Pos
Text.Parsec.Prim
Text.ParserCombinators.Parsec.Char
Text.ParserCombinators.Parsec.Combinator

build-depends:
aeson >= 1.5.5.1 && < 1.6,
Expand Down
2 changes: 1 addition & 1 deletion elm-format-lib/src/Parse/Binop.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE DataKinds #-}
module Parse.Binop (binops) where

import Text.Parsec ((<|>), choice, try)
import Parse.ParsecAdapter ((<|>), choice, try)

import AST.V0_16
import AST.Structure (FixAST)
Expand Down
2 changes: 1 addition & 1 deletion elm-format-lib/src/Parse/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Parse.Common
) where

import AST.V0_16
import Text.Parsec
import Parse.ParsecAdapter
import Parse.Helpers
import Parse.Whitespace
import Parse.IParser
Expand Down
2 changes: 1 addition & 1 deletion elm-format-lib/src/Parse/Declaration.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE DataKinds #-}
module Parse.Declaration where

import Text.Parsec ( (<|>), (<?>), choice, digit, optionMaybe, string, try )
import Parse.ParsecAdapter ( (<|>), (<?>), choice, digit, optionMaybe, string, try )

import AST.Structure
import qualified Data.Indexed as I
Expand Down
3 changes: 1 addition & 2 deletions elm-format-lib/src/Parse/Expression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ module Parse.Expression (term, typeAnnotation, definition, expr) where
import Data.Coapplicative
import qualified Data.Indexed as I
import Data.Maybe (fromMaybe)
import Text.Parsec hiding (newline, spaces)
import Text.Parsec.Indent (block, withPos, checkIndent)

import Parse.ParsecAdapter hiding (newline, spaces)
import qualified Parse.Binop as Binop
import Parse.Helpers
import Parse.Common
Expand Down
20 changes: 13 additions & 7 deletions elm-format-lib/src/Parse/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import Prelude hiding (until)
import Control.Monad (guard)
import qualified Data.Indexed as I
import Data.Map.Strict hiding (foldl)
import Text.Parsec hiding (newline, spaces, State)
import Text.Parsec.Indent (indented, runIndent)
import Parse.ParsecAdapter hiding (newline, spaces, State)

import AST.V0_16
import qualified AST.Helpers as Help
Expand All @@ -17,6 +16,7 @@ import qualified Parse.State as State
import Parse.Comments
import Parse.IParser
import Parse.Whitespace
import qualified Parse.Primitives as EP
import qualified Reporting.Annotation as A
import qualified Reporting.Error.Syntax as Syntax
import qualified Reporting.Region as R
Expand Down Expand Up @@ -512,12 +512,18 @@ commentedKeyword elmVersion word parser =

-- ODD COMBINATORS

-- Behaves the same as `Parse.ParsecAdapter.fail` except that the consumed
-- continuation is called instead of the empty continuation.
failure :: String -> IParser String
failure msg = do
inp <- getInput
setInput ('x':inp)
_ <- anyToken
fail msg
failure msg =
EP.Parser $ \s _ _ cerr _ ->
let
(EP.Parser p) = fail msg
in
-- This looks really unsound, but `p` which was created with `fail` will
-- only ever call the empty error continuation (which in this case
-- re-routes to the consumed error continuation)
p s undefined undefined undefined cerr


until :: IParser a -> IParser b -> IParser b
Expand Down
5 changes: 3 additions & 2 deletions elm-format-lib/src/Parse/IParser.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Parse.IParser where

import Text.Parsec.Prim (Parser)
import Parse.Primitives (Parser)
import Parse.ParsecAdapter (ParseError)


type IParser a = Parser a
type IParser a = Parser ParseError a
3 changes: 1 addition & 2 deletions elm-format-lib/src/Parse/Literal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ module Parse.Literal (literal) where

import Prelude hiding (exponent)
import Data.Char (digitToInt, isSpace)
import Text.Parsec ((<|>), (<?>), digit, hexDigit, lookAhead, many1, option, string, try, char, notFollowedBy, choice, anyChar, satisfy, manyTill, many, between, skipMany, skipMany1)
import Text.Parsec.Char (octDigit, space, upper)
import Parse.ParsecAdapter
import Parse.Helpers (processAs, escaped, expecting, sandwich, betwixt)
import Parse.IParser

Expand Down
2 changes: 1 addition & 1 deletion elm-format-lib/src/Parse/Module.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Parse.Module (moduleDecl, elmModule, topLevel, import') where
import qualified Control.Applicative
import Data.Map.Strict ( Map, empty, insert, insertWith )
import Elm.Utils ((|>))
import Text.Parsec ( char, letter, string, choice, eof, option, optionMaybe, (<?>), (<|>), many, try )
import Parse.ParsecAdapter ( char, letter, string, choice, eof, option, optionMaybe, (<?>), (<|>), many, try )
import Parse.Helpers
import qualified Parse.Declaration as Decl
import AST.Listing (Listing(..), mergeCommentedMap, mergeListing)
Expand Down
4 changes: 2 additions & 2 deletions elm-format-lib/src/Parse/Parse.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{-# LANGUAGE DataKinds #-}
module Parse.Parse (parse, parseModule, parseDeclarations, parseExpressions) where

import qualified Text.Parsec.Error as Parsec
import Parse.ParsecAdapter (eof)
import qualified Parse.ParsecAdapter as Parsec

import AST.V0_16
import AST.Module (Module)
Expand All @@ -17,7 +18,6 @@ import qualified Reporting.Region as R
import qualified Reporting.Error.Syntax as Error
import qualified Reporting.Result as Result
import Parse.IParser
import Text.Parsec (eof)


parseModule :: ElmVersion -> String -> Result.Result () Error.Error (Module [UppercaseIdentifier] (ASTNS Located [UppercaseIdentifier] 'TopLevelNK))
Expand Down
Loading

0 comments on commit f98fd08

Please sign in to comment.