rtk-0.11: test/golden/java-simple/JavaSimpleQQ.hs
-- Generated by RTK from grammar 'JavaSimple'. Do not edit by hand.
{-# LANGUAGE TemplateHaskell #-}
module JavaSimpleQQ
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 JavaSimpleLexer
import JavaSimpleParser
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 [ ("javaSimple","JavaSimple"),("classDeclaration","ClassDeclaration"),("compilationUnit","CompilationUnit"),("compoundName","CompoundName"),("field","Field"),("fieldList","FieldList"),("package","Package"),("type","Type")]
-- 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
quoteJavaSimpleExp :: Data.Data a => String -> (JavaSimple -> a) -> String -> TH.ExpQ
quoteJavaSimpleExp dummy func s = do
s1 <- either fail return (replaceAllPatterns s)
ast <- case scanTokens (dummy ++ " " ++ s1 ++ " " ++ dummy) >>= parseJavaSimple of
Left err -> fail (rtkRenderError err)
Right a -> return a
let expr = func ast
dataToExpQ (const Nothing `Generics.extQ` antiJavaSimpleExp `Generics.extQ` antiCompilationUnitExp `Generics.extQ` antiPackageExp `Generics.extQ` antiClassDeclarationExp `Generics.extQ` antiFieldExp `Generics.extQ` antiTypeExp `Generics.extQ` antiCompoundNameExp) expr
quoteJavaSimplePat :: Data.Data a => String -> (JavaSimple -> a) -> String -> TH.PatQ
quoteJavaSimplePat dummy func s = do
s1 <- either fail return (replaceAllPatterns s)
ast <- case scanTokens (dummy ++ " " ++ s1 ++ " " ++ dummy) >>= parseJavaSimple of
Left err -> fail (rtkRenderError err)
Right a -> return a
let expr = func ast
dataToPatQ (const Nothing `Generics.extQ` rtkPosWildPat `Generics.extQ` antiJavaSimplePat `Generics.extQ` antiCompilationUnitPat `Generics.extQ` antiPackagePat `Generics.extQ` antiClassDeclarationPat `Generics.extQ` antiFieldPat `Generics.extQ` antiTypePat `Generics.extQ` antiCompoundNamePat) expr
antiCompoundNameExp :: CompoundName -> Maybe (TH.Q TH.Exp )
antiCompoundNameExp ( Anti_CompoundName v) = Just $ TH.varE (TH.mkName v)
antiCompoundNameExp _ = Nothing
antiTypeExp :: Type -> Maybe (TH.Q TH.Exp )
antiTypeExp ( Anti_Type v) = Just $ TH.varE (TH.mkName v)
antiTypeExp _ = Nothing
antiFieldExp :: [ Field ] -> Maybe (TH.Q TH.Exp)
antiFieldExp ((Anti_Field v):rest) =
let restExp = dataToExpQ (const Nothing `Generics.extQ` antiJavaSimpleExp `Generics.extQ` antiCompilationUnitExp `Generics.extQ` antiPackageExp `Generics.extQ` antiClassDeclarationExp `Generics.extQ` antiFieldExp `Generics.extQ` antiTypeExp `Generics.extQ` antiCompoundNameExp) rest
lvar = TH.varE $ TH.mkName v
in Just [| $lvar ++ $restExp |]
antiFieldExp _ = Nothing
antiClassDeclarationExp :: ClassDeclaration -> Maybe (TH.Q TH.Exp )
antiClassDeclarationExp ( Anti_ClassDeclaration v) = Just $ TH.varE (TH.mkName v)
antiClassDeclarationExp _ = Nothing
antiPackageExp :: Package -> Maybe (TH.Q TH.Exp )
antiPackageExp ( Anti_Package v) = Just $ TH.varE (TH.mkName v)
antiPackageExp _ = Nothing
antiCompilationUnitExp :: CompilationUnit -> Maybe (TH.Q TH.Exp )
antiCompilationUnitExp ( Anti_CompilationUnit v) = Just $ TH.varE (TH.mkName v)
antiCompilationUnitExp _ = Nothing
antiJavaSimpleExp :: JavaSimple -> Maybe (TH.Q TH.Exp )
antiJavaSimpleExp ( Anti_JavaSimple v) = Just $ TH.varE (TH.mkName v)
antiJavaSimpleExp _ = Nothing
antiCompoundNamePat :: CompoundName -> Maybe (TH.Q TH.Pat )
antiCompoundNamePat ( Anti_CompoundName v) = Just $ TH.varP (TH.mkName v)
antiCompoundNamePat _ = Nothing
antiTypePat :: Type -> Maybe (TH.Q TH.Pat )
antiTypePat ( Anti_Type v) = Just $ TH.varP (TH.mkName v)
antiTypePat _ = Nothing
antiFieldPat :: [ Field ] -> Maybe (TH.Q TH.Pat)
antiFieldPat [Anti_Field v] = Just $ TH.varP (TH.mkName v)
antiFieldPat _ = Nothing
antiClassDeclarationPat :: ClassDeclaration -> Maybe (TH.Q TH.Pat )
antiClassDeclarationPat ( Anti_ClassDeclaration v) = Just $ TH.varP (TH.mkName v)
antiClassDeclarationPat _ = Nothing
antiPackagePat :: Package -> Maybe (TH.Q TH.Pat )
antiPackagePat ( Anti_Package v) = Just $ TH.varP (TH.mkName v)
antiPackagePat _ = Nothing
antiCompilationUnitPat :: CompilationUnit -> Maybe (TH.Q TH.Pat )
antiCompilationUnitPat ( Anti_CompilationUnit v) = Just $ TH.varP (TH.mkName v)
antiCompilationUnitPat _ = Nothing
antiJavaSimplePat :: JavaSimple -> Maybe (TH.Q TH.Pat )
antiJavaSimplePat ( Anti_JavaSimple v) = Just $ TH.varP (TH.mkName v)
antiJavaSimplePat _ = Nothing
-- This grammar's quasi-quoters work in expression and pattern contexts only
quoteJavaSimpleType _ = fail "this quasi-quoter cannot be used in a type context"
quoteJavaSimpleDecs _ = fail "this quasi-quoter cannot be used in a declaration context"
getJavaSimple ( Ctr__JavaSimple__0 _ s) = s
javaSimple :: QuasiQuoter
javaSimple = QuasiQuoter (quoteJavaSimpleExp "tok_JavaSimple_dummy_11" getJavaSimple ) (quoteJavaSimplePat "tok_JavaSimple_dummy_11" getJavaSimple ) quoteJavaSimpleType quoteJavaSimpleDecs
getClassDeclaration ( Ctr__JavaSimple__1 _ s) = s
classDeclaration :: QuasiQuoter
classDeclaration = QuasiQuoter (quoteJavaSimpleExp "tok_ClassDeclaration_dummy_10" getClassDeclaration ) (quoteJavaSimplePat "tok_ClassDeclaration_dummy_10" getClassDeclaration ) quoteJavaSimpleType quoteJavaSimpleDecs
getCompilationUnit ( Ctr__JavaSimple__2 _ s) = s
compilationUnit :: QuasiQuoter
compilationUnit = QuasiQuoter (quoteJavaSimpleExp "tok_CompilationUnit_dummy_9" getCompilationUnit ) (quoteJavaSimplePat "tok_CompilationUnit_dummy_9" getCompilationUnit ) quoteJavaSimpleType quoteJavaSimpleDecs
getCompoundName ( Ctr__JavaSimple__3 _ s) = s
compoundName :: QuasiQuoter
compoundName = QuasiQuoter (quoteJavaSimpleExp "tok_CompoundName_dummy_8" getCompoundName ) (quoteJavaSimplePat "tok_CompoundName_dummy_8" getCompoundName ) quoteJavaSimpleType quoteJavaSimpleDecs
getField ( Ctr__JavaSimple__4 _ s) = s
field :: QuasiQuoter
field = QuasiQuoter (quoteJavaSimpleExp "tok_Field_dummy_7" getField ) (quoteJavaSimplePat "tok_Field_dummy_7" getField ) quoteJavaSimpleType quoteJavaSimpleDecs
getFieldList ( Ctr__JavaSimple__5 _ s) = s
fieldList :: QuasiQuoter
fieldList = QuasiQuoter (quoteJavaSimpleExp "tok_FieldList_dummy_6" getFieldList ) (quoteJavaSimplePat "tok_FieldList_dummy_6" getFieldList ) quoteJavaSimpleType quoteJavaSimpleDecs
getPackage ( Ctr__JavaSimple__6 _ s) = s
package :: QuasiQuoter
package = QuasiQuoter (quoteJavaSimpleExp "tok_Package_dummy_5" getPackage ) (quoteJavaSimplePat "tok_Package_dummy_5" getPackage ) quoteJavaSimpleType quoteJavaSimpleDecs
getType ( Ctr__JavaSimple__7 _ s) = s
__type :: QuasiQuoter
__type = QuasiQuoter (quoteJavaSimpleExp "tok_Type_dummy_4" getType ) (quoteJavaSimplePat "tok_Type_dummy_4" getType ) quoteJavaSimpleType quoteJavaSimpleDecs