egison / egison-pattern-src

Manipulating Egison patterns: abstract syntax, parser, and pretty-printer
https://hackage.haskell.org/package/egison-pattern-src
BSD 3-Clause "New" or "Revised" License
5 stars 0 forks source link

add non-greedy parsers #19

Closed coord-e closed 4 years ago

coord-e commented 4 years ago

parseExpr is currently a greedy parser, that consumes the input until the end. Non-greedy parser, that only consumes the input until Expr is fully parsed, would be useful especially when we want to parse match clauses such as pat -> body outside of this package.

coord-e commented 4 years ago

Some points to consider:

coord-e commented 4 years ago
class Source s => HasParser mode s a | mode -> s where
  parse :: MonadError (Errors s) m => mode -> s -> m a
  parseNonGreedy :: MonadError (Errors s) m => mode -> s -> m (a, s)
  parse = ...

instance HasParser (ParseMode n v e s) s (ExprL n v e) where
  parseNonGreedy mode content = ...

instance HasParser mode s (Cofree (Base x) a) => HasParser mode s x where
  parseNonGreedy mode = first unAnnotate . parseNonGreedy mode

-- egison-pattern-src-haskell-mode
data ParseMode
  = ParseMode { haskellMode :: Haskell.ParseMode
              , fixities :: Maybe [ParseFixity]
              }

instance HasParser ParseMode String ExprL where
  parseNonGreedy = parseNonGreedy . makeHaskellMode