{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TemplateHaskell, TypeOperators #-}
{-|
Module      : Text.Gigaparsec.Token.Patterns
Description : Template Haskell generators to help with patterns
License     : BSD-3-Clause
Maintainer  : Jamie Willis, Gigaparsec Maintainers
Stability   : experimental

This module is currently experimental, and may have bugs depending on the version
of Haskell, or the extensions enabled. Please report any issues to the maintainers.

@since 0.2.2.0
-}
module Text.Gigaparsec.Token.Patterns (overloadedStrings) where

import Text.Gigaparsec (Parsec)
import Text.Gigaparsec.Token.Lexer (lexeme, sym)

import Data.String (IsString(fromString))
import Language.Haskell.TH.Syntax (Q, Dec, Exp)

{-|
When given a quoted reference to a 'Text.Gigaparsec.Token.Lexer.Lexer', for example
@[|lexer|]@, this function will synthesise an `IsString` instance that will
allow string literals to serve as @Parsec ()@. These literals will parse symbols
in the language associated with the lexer, followed by consuming valid whitespace.

@since 0.2.2.0
-}
overloadedStrings :: Q Exp   -- ^ the quoted 'Text.Gigaparsec.Token.Lexer.Lexer'
                  -> Q [Dec] -- ^ a synthesised `IsString` instance.
overloadedStrings :: Q Exp -> Q [Dec]
overloadedStrings Q Exp
qlexer = [d|
    instance u ~ () => IsString (Parsec u) where
      fromString = sym (lexeme $Q Exp
qlexer) -- TODO: one day, $qlexer.lexeme.sym
  |]