rtk-0.11: test/golden/p/PQQ.hs
-- Generated by RTK from grammar 'P'. Do not edit by hand.
{-# LANGUAGE TemplateHaskell #-}
module PQQ
where
import qualified Data.Char as C
import qualified Data.Map as M
import Data.List
import Data.Maybe
import qualified Data.Generics as Generics
import qualified Data.Data as Data
import qualified Language.Haskell.TH as TH
import Language.Haskell.TH.Quote
import PLexer
import PParser
qqShortcuts :: M.Map String String
-- A $name metavariable is rewritten to $Type:name using the qqShortcuts
-- table below. The rewrite is purely textual, so it would also fire inside
-- the quoted language's own string literals: write $$name there to escape
-- it and get the literal text $name. Each '$$' pair directly before a
-- metavariable stands for one literal '$' (so $$$x is a literal '$'
-- followed by the metavariable $x). A '$' not followed by an identifier is
-- never rewritten and needs no escape, and an explicit $Type:name antiquote
-- (the character after the name is ':') passes through untouched.
-- A plain character scan rather than a regex: metavariables are recognized
-- regardless of what follows the name - including a newline or the end of
-- the quote, which the previous regex's terminator class missed.
replaceAllPatterns :: String -> Either String String
replaceAllPatterns [] = Right []
replaceAllPatterns s@('$' : _) =
let (dollars, rest) = span (== '$') s
in case rest of
(c1 : _) | C.isAlpha c1 || c1 == '_' ->
let (varName, post) = span (\ ch -> C.isAlphaNum ch || ch == '_') rest
escCount = length dollars - 1
keptPre = replicate (div escCount 2) '$'
in case post of
(':' : _) -> ((dollars ++ varName) ++) <$> replaceAllPatterns post
_ | odd escCount -> ((keptPre ++ '$' : varName) ++) <$> replaceAllPatterns post
_ -> case catMaybes $ map (\ prefix -> M.lookup prefix qqShortcuts) $ reverse $ inits varName of
[] -> Left $ unlines
[ "Unknown metavariable $" ++ varName ++ " in quasi-quote:"
, "no prefix of '" ++ varName ++ "' is a known shortcut. Known shortcuts:"
, " " ++ intercalate ", " (M.keys qqShortcuts)
, "To include the literal text $" ++ varName ++ " in the quoted code"
, "(e.g. inside a string literal), escape it as $$" ++ varName ++ "." ]
(rule : _) -> ((keptPre ++ '$' : rule ++ ":" ++ varName) ++) <$> replaceAllPatterns post
_ -> (dollars ++) <$> replaceAllPatterns rest
replaceAllPatterns (c : rest) = (c :) <$> replaceAllPatterns rest
-- The generated lexer and parser encode error positions as "LINE:COL:message"
-- so structured-diagnostic callers can split them; render them back
-- human-readably for quasi-quote compile errors. Positions refer to the quote
-- body (padded with a start token in front).
rtkRenderError :: String -> String
rtkRenderError err =
case span (/= ':') err of
(l, ':' : rest1) | [(line, "")] <- (reads l :: [(Int, String)]) ->
case span (/= ':') rest1 of
(c, ':' : msg) | [(col, "")] <- (reads c :: [(Int, String)]) ->
"line " ++ show line ++ ", column " ++ show col ++ ": " ++ msg
_ -> err
_ -> err
qqShortcuts = M.fromList [ ("p","P"),("e","E"),("id","Id"),("op1","Op1"),("op2","Op2")]
-- A quasi-quote pattern must match an AST parsed from anywhere in a source
-- file, while the pattern itself was parsed from the quote body - so every
-- RtkPos position field becomes a wildcard in generated patterns.
-- (Expressions need no special case: the compile-time position they embed
-- is equality-transparent.)
rtkPosWildPat :: RtkPos -> Maybe (TH.Q TH.Pat)
rtkPosWildPat _ = Just TH.wildP
quotePExp :: Data.Data a => String -> (P -> a) -> String -> TH.ExpQ
quotePExp dummy func s = do
s1 <- either fail return (replaceAllPatterns s)
ast <- case scanTokens (dummy ++ " " ++ s1 ++ " " ++ dummy) >>= parseP of
Left err -> fail (rtkRenderError err)
Right a -> return a
let expr = func ast
dataToExpQ (const Nothing `Generics.extQ` antiPExp `Generics.extQ` antiEExp `Generics.extQ` antiOp1Exp `Generics.extQ` antiOp2Exp `Generics.extQ` antiIdExp) expr
quotePPat :: Data.Data a => String -> (P -> a) -> String -> TH.PatQ
quotePPat dummy func s = do
s1 <- either fail return (replaceAllPatterns s)
ast <- case scanTokens (dummy ++ " " ++ s1 ++ " " ++ dummy) >>= parseP of
Left err -> fail (rtkRenderError err)
Right a -> return a
let expr = func ast
dataToPatQ (const Nothing `Generics.extQ` rtkPosWildPat `Generics.extQ` antiPPat `Generics.extQ` antiEPat `Generics.extQ` antiOp1Pat `Generics.extQ` antiOp2Pat `Generics.extQ` antiIdPat) expr
antiIdExp :: Id -> Maybe (TH.Q TH.Exp )
antiIdExp ( Anti_Id v) = Just $ TH.varE (TH.mkName v)
antiIdExp _ = Nothing
antiOp2Exp :: Op2 -> Maybe (TH.Q TH.Exp )
antiOp2Exp ( Anti_Op2 v) = Just $ TH.varE (TH.mkName v)
antiOp2Exp _ = Nothing
antiOp1Exp :: Op1 -> Maybe (TH.Q TH.Exp )
antiOp1Exp ( Anti_Op1 v) = Just $ TH.varE (TH.mkName v)
antiOp1Exp _ = Nothing
antiEExp :: E -> Maybe (TH.Q TH.Exp )
antiEExp ( Anti_E v) = Just $ TH.varE (TH.mkName v)
antiEExp _ = Nothing
antiPExp :: P -> Maybe (TH.Q TH.Exp )
antiPExp ( Anti_P v) = Just $ TH.varE (TH.mkName v)
antiPExp _ = Nothing
antiIdPat :: Id -> Maybe (TH.Q TH.Pat )
antiIdPat ( Anti_Id v) = Just $ TH.varP (TH.mkName v)
antiIdPat _ = Nothing
antiOp2Pat :: Op2 -> Maybe (TH.Q TH.Pat )
antiOp2Pat ( Anti_Op2 v) = Just $ TH.varP (TH.mkName v)
antiOp2Pat _ = Nothing
antiOp1Pat :: Op1 -> Maybe (TH.Q TH.Pat )
antiOp1Pat ( Anti_Op1 v) = Just $ TH.varP (TH.mkName v)
antiOp1Pat _ = Nothing
antiEPat :: E -> Maybe (TH.Q TH.Pat )
antiEPat ( Anti_E v) = Just $ TH.varP (TH.mkName v)
antiEPat _ = Nothing
antiPPat :: P -> Maybe (TH.Q TH.Pat )
antiPPat ( Anti_P v) = Just $ TH.varP (TH.mkName v)
antiPPat _ = Nothing
-- This grammar's quasi-quoters work in expression and pattern contexts only
quotePType _ = fail "this quasi-quoter cannot be used in a type context"
quotePDecs _ = fail "this quasi-quoter cannot be used in a declaration context"
getP ( Ctr__P__0 _ s) = s
p :: QuasiQuoter
p = QuasiQuoter (quotePExp "tok_P_dummy_4" getP ) (quotePPat "tok_P_dummy_4" getP ) quotePType quotePDecs
getE ( Ctr__P__1 _ s) = s
e :: QuasiQuoter
e = QuasiQuoter (quotePExp "tok_E_dummy_3" getE ) (quotePPat "tok_E_dummy_3" getE ) quotePType quotePDecs
getId ( Ctr__P__2 _ s) = s
id :: QuasiQuoter
id = QuasiQuoter (quotePExp "tok_Id_dummy_2" getId ) (quotePPat "tok_Id_dummy_2" getId ) quotePType quotePDecs
getOp1 ( Ctr__P__3 _ s) = s
op1 :: QuasiQuoter
op1 = QuasiQuoter (quotePExp "tok_Op1_dummy_1" getOp1 ) (quotePPat "tok_Op1_dummy_1" getOp1 ) quotePType quotePDecs
getOp2 ( Ctr__P__4 _ s) = s
op2 :: QuasiQuoter
op2 = QuasiQuoter (quotePExp "tok_Op2_dummy_0" getOp2 ) (quotePPat "tok_Op2_dummy_0" getOp2 ) quotePType quotePDecs