Template Haskell: Is there a function (or special syntax) that parses a String and returns Q Exp? -


i trying learn bit of template haskell , quasi quotation, , looking function takes string , parses q exp, type is:

string -> q exp 

tried searching hoogle, results saw had lifting string literals q exp, , closest found language.haskell.th.dyn quite want, single variable.

are there other options? e.g. special syntax? i'm in process of familiarizing myself [||] , $(), maybe there purpose too?

an example of how imagine work:

runq (parse "(1+)") == infixe (just (lite (integerl 1))) (vare ghc.num.+) nothing 

also, aware of this

runq [| (1+) |] == infixe (just (lite (integerl 1))) (vare ghc.num.+) nothing 

but wont work variable strings because -- understandably -- string inside taken literal.

runq [| "(1+)" |] == lite (stringl "(1+)") 

edit (2015-07-25): i've started using haskell-src-meta, , seems work far. take quite bit of time cabal install (about 10 minutes on machine). shame, package rather small, , if install quick. knows of solution has smaller dependencies?

as has said haskell-src-meta provides

parsepat :: string -> either string pat parseexp :: string -> either string exp parsetype :: string -> either string type parsedecs :: string -> either string [dec] 

where pat, exp, type, , dec same language.haskell.th.syntax.


why doesn't ghc expose own parser?

it does. fire ghci ghci -package ghc (ghc hidden package default) , can import parser. has functions parse string preliminary asts (whose data declarations in hssyn) patterns, expressions, types, , declarations.

ok, why there not exist library uses parser , converts output ast template-haskell (the 1 in language.haskell.th.syntax)?

looking inside hssyn, obvious ast isn't quite same 1 in language.haskell.th.syntax. open both hsexpr , exp , side side you'll see latter filled types posttc id <some-other-type> , postrn id <some-other-type>. ast passed parser renamer type checker, these bits , pieces filled in. example, don't know fixities of operators until type-checking!

in order make functions want, need run more parser (at least renamer , type checker too, maybe more). imagine that: every time want parse small expression "1 + 2" you'll still have type check bunch of imports. then, converting language.haskell.th.syntax wouldn't walk in park: ghc has variety of peculiarities own special global way of storing names , identifiers.

hmmm... ghc quasi-quotes?

that's cool part! unlike exp, hsexpr has hssplice representing splices. @ types first 2 constructors:

hstypedsplice :: id -> lhsexpr id -> hssplice id.   -- things [|| 1 + 2 ||] hsuntypedsplice :: id -> lhsexpr id -> hssplice id  -- things [| 1 + 2 |] 

notice aren't storing string, storing ast already! splices parsed @ same time rest of ast. , rest of ast, splices passed along renamer, type checker, etc. missing information filled in.

so fundamentally impossible use ghc's parser

probably not. extricating rest of ghc may quite difficult. if use ghc's parser have run type-checker , renamer, may more elegant , simple use standalone parser haskell-src-exts (which haskell-src-meta depends on) able in 1 pass (fixities, example, 1 of things have give ahead of time parser).


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -