egison / egison-pattern-src

Manipulating Egison patterns: abstract syntax, parser, and pretty-printer

Home Page:https://hackage.haskell.org/package/egison-pattern-src

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add non-greedy parsers

coord-e opened this issue · comments

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.

Some points to consider:

  • how can we deal with the matrix problem of parser configuration? (with location, greedy or not, with fixities, etc.)
  • is it appropriate to include parsers for match clauses here?
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