packages feed

rtk-0.11: test/golden/debug-test/DebugTestQQ.hs

-- Generated by RTK from grammar 'DebugTest'. Do not edit by hand.
{-# LANGUAGE TemplateHaskell #-}
module DebugTestQQ
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 DebugTestLexer
import DebugTestParser

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 [ ("program","Program"),("assignment","Assignment"),("block","Block"),("expression","Expression"),("factor","Factor"),("ifStatement","IfStatement"),("statement","Statement"),("term","Term"),("unusedRule1","UnusedRule1"),("unusedRule2","UnusedRule2"),("whileLoop","WhileLoop")]

-- 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

quoteDebugTestExp :: Data.Data a => String -> (Program -> a) -> String -> TH.ExpQ
quoteDebugTestExp dummy func s = do
  s1 <- either fail return (replaceAllPatterns s)
  ast <- case scanTokens (dummy ++ " " ++ s1 ++ " " ++ dummy) >>= parseDebugTest of
           Left err -> fail (rtkRenderError err)
           Right a -> return a
  let expr = func ast
  dataToExpQ (const Nothing `Generics.extQ` antiStatementExp `Generics.extQ` antiAssignmentExp `Generics.extQ` antiExpressionExp `Generics.extQ` antiTermExp `Generics.extQ` antiFactorExp `Generics.extQ` antiIfStatementExp `Generics.extQ` antiWhileLoopExp `Generics.extQ` antiBlockExp `Generics.extQ` antiUnusedRule1Exp) expr
quoteDebugTestPat :: Data.Data a => String -> (Program -> a) -> String -> TH.PatQ
quoteDebugTestPat dummy func s = do
  s1 <- either fail return (replaceAllPatterns s)
  ast <- case scanTokens (dummy ++ " " ++ s1 ++ " " ++ dummy) >>= parseDebugTest of
           Left err -> fail (rtkRenderError err)
           Right a -> return a
  let expr = func ast
  dataToPatQ (const Nothing `Generics.extQ` rtkPosWildPat `Generics.extQ` antiStatementPat `Generics.extQ` antiAssignmentPat `Generics.extQ` antiExpressionPat `Generics.extQ` antiTermPat `Generics.extQ` antiFactorPat `Generics.extQ` antiIfStatementPat `Generics.extQ` antiWhileLoopPat `Generics.extQ` antiBlockPat `Generics.extQ` antiUnusedRule1Pat) expr

antiUnusedRule1Exp :: UnusedRule1 -> Maybe (TH.Q TH.Exp )
antiUnusedRule1Exp ( Anti_UnusedRule1 v) = Just $ TH.varE (TH.mkName v)
antiUnusedRule1Exp _ = Nothing


antiBlockExp :: Block -> Maybe (TH.Q TH.Exp )
antiBlockExp ( Anti_Block v) = Just $ TH.varE (TH.mkName v)
antiBlockExp _ = Nothing


antiWhileLoopExp :: WhileLoop -> Maybe (TH.Q TH.Exp )
antiWhileLoopExp ( Anti_WhileLoop v) = Just $ TH.varE (TH.mkName v)
antiWhileLoopExp _ = Nothing


antiIfStatementExp :: IfStatement -> Maybe (TH.Q TH.Exp )
antiIfStatementExp ( Anti_IfStatement v) = Just $ TH.varE (TH.mkName v)
antiIfStatementExp _ = Nothing


antiFactorExp :: Factor -> Maybe (TH.Q TH.Exp )
antiFactorExp ( Anti_Factor v) = Just $ TH.varE (TH.mkName v)
antiFactorExp _ = Nothing


antiTermExp :: Term -> Maybe (TH.Q TH.Exp )
antiTermExp ( Anti_Term v) = Just $ TH.varE (TH.mkName v)
antiTermExp _ = Nothing


antiExpressionExp :: Expression -> Maybe (TH.Q TH.Exp )
antiExpressionExp ( Anti_Expression v) = Just $ TH.varE (TH.mkName v)
antiExpressionExp _ = Nothing


antiAssignmentExp :: Assignment -> Maybe (TH.Q TH.Exp )
antiAssignmentExp ( Anti_Assignment v) = Just $ TH.varE (TH.mkName v)
antiAssignmentExp _ = Nothing


antiStatementExp :: [ Statement ] -> Maybe (TH.Q TH.Exp)
antiStatementExp ((Anti_Statement v):rest) =
 let restExp =   dataToExpQ (const Nothing `Generics.extQ` antiStatementExp `Generics.extQ` antiAssignmentExp `Generics.extQ` antiExpressionExp `Generics.extQ` antiTermExp `Generics.extQ` antiFactorExp `Generics.extQ` antiIfStatementExp `Generics.extQ` antiWhileLoopExp `Generics.extQ` antiBlockExp `Generics.extQ` antiUnusedRule1Exp) rest
     lvar = TH.varE $ TH.mkName v
   in Just [| $lvar ++ $restExp |]
antiStatementExp _ = Nothing



antiUnusedRule1Pat :: UnusedRule1 -> Maybe (TH.Q TH.Pat )
antiUnusedRule1Pat ( Anti_UnusedRule1 v) = Just $ TH.varP (TH.mkName v)
antiUnusedRule1Pat _ = Nothing


antiBlockPat :: Block -> Maybe (TH.Q TH.Pat )
antiBlockPat ( Anti_Block v) = Just $ TH.varP (TH.mkName v)
antiBlockPat _ = Nothing


antiWhileLoopPat :: WhileLoop -> Maybe (TH.Q TH.Pat )
antiWhileLoopPat ( Anti_WhileLoop v) = Just $ TH.varP (TH.mkName v)
antiWhileLoopPat _ = Nothing


antiIfStatementPat :: IfStatement -> Maybe (TH.Q TH.Pat )
antiIfStatementPat ( Anti_IfStatement v) = Just $ TH.varP (TH.mkName v)
antiIfStatementPat _ = Nothing


antiFactorPat :: Factor -> Maybe (TH.Q TH.Pat )
antiFactorPat ( Anti_Factor v) = Just $ TH.varP (TH.mkName v)
antiFactorPat _ = Nothing


antiTermPat :: Term -> Maybe (TH.Q TH.Pat )
antiTermPat ( Anti_Term v) = Just $ TH.varP (TH.mkName v)
antiTermPat _ = Nothing


antiExpressionPat :: Expression -> Maybe (TH.Q TH.Pat )
antiExpressionPat ( Anti_Expression v) = Just $ TH.varP (TH.mkName v)
antiExpressionPat _ = Nothing


antiAssignmentPat :: Assignment -> Maybe (TH.Q TH.Pat )
antiAssignmentPat ( Anti_Assignment v) = Just $ TH.varP (TH.mkName v)
antiAssignmentPat _ = Nothing


antiStatementPat :: [ Statement ] -> Maybe (TH.Q TH.Pat)
antiStatementPat [Anti_Statement v] = Just $ TH.varP (TH.mkName v)
antiStatementPat _ = Nothing



-- This grammar's quasi-quoters work in expression and pattern contexts only
quoteDebugTestType _ = fail "this quasi-quoter cannot be used in a type context"
quoteDebugTestDecs _ = fail "this quasi-quoter cannot be used in a declaration context"