jmacro 0.4.6 → 0.5
raw patch · 4 files changed
+43/−12 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Language.Javascript.JMacro: renderPrefixJs :: (JsToDoc a, JMacro a) => String -> a -> Doc
Files
- Language/Javascript/JMacro.hs +3/−3
- Language/Javascript/JMacro/Base.hs +29/−4
- Language/Javascript/JMacro/QQ.hs +10/−4
- jmacro.cabal +1/−1
Language/Javascript/JMacro.hs view
@@ -46,15 +46,15 @@ As is the above. -> renderJs [$jmacroE|foo (x,y)|]]+> renderJs [$jmacroE|foo (x,y)|] While the above is an error. (i.e. standard javascript function application cannot seperate the leading parenthesis of the argument from the function being applied) -> \x -> [$jmacroE|foo `(x)`|]]+> \x -> [$jmacroE|foo `(x)`|] The above is a haskell expression that provides a function that takes an x, and yields an expression representing the application of foo to the value of x as transformed to a Javascript expression. -> [$jmacroE|\x ->`(foo x)`|]]+> [$jmacroE|\x ->`(foo x)`|] Meanwhile, the above lambda is in Javascript, and brings the variable into scope both in javascript and in the enclosed antiquotes. The expression is a Javascript function that takes an x, and yields an expression produced by the application of the Haskell function foo as applied to the identifier x (which is of type JExpr -- i.e. a Javascript expression).
Language/Javascript/JMacro/Base.hs view
@@ -21,7 +21,7 @@ -- * Hygienic transformation withHygiene, scopify, -- * Display/Output- renderJs, JsToDoc(..),+ renderJs, renderPrefixJs, JsToDoc(..), -- * Ad-hoc data marshalling ToJExpr(..), -- * Literals@@ -41,19 +41,18 @@ import Control.Monad.Identity import Data.Function-import Data.Char (toLower)+import Data.Char (toLower,isControl) import Data.Maybe (fromMaybe) import qualified Data.Set as S import qualified Data.Map as M import Data.Generics import Data.Monoid +import Numeric(showHex) import Safe import Text.JSON import Text.PrettyPrint.HughesPJ as PP -import Web.Encodings- import Language.Javascript.JMacro.Types {--------------------------------------------------------------------@@ -432,6 +431,10 @@ renderJs :: (JsToDoc a, JMacro a) => a -> Doc renderJs = jsToDoc . jsSaturate Nothing +-- | Render a syntax tree as a pretty-printable document, using a given prefix to all generated names. Use this with distinct prefixes to ensure distinct generated names between independent calls to render(Prefix)Js.+renderPrefixJs :: (JsToDoc a, JMacro a) => String -> a -> Doc+renderPrefixJs pfx = jsToDoc . jsSaturate (Just $ "jmId_"++pfx)+ braceNest :: Doc -> Doc braceNest x = char '{' $$ nest 2 x $$ char '}' @@ -729,4 +732,26 @@ toJExpr (JSString s) = ValExpr $ JStr $ fromJSString s toJExpr (JSArray vs) = ValExpr $ JList $ map toJExpr vs toJExpr (JSObject obj) = ValExpr $ JHash $ M.fromList $ map (second toJExpr) $ fromJSObject obj++-------------------------++-- Taken from json package by Sigbjorn Finne.+encodeJson = concatMap encodeJsonChar++encodeJsonChar :: Char -> String+encodeJsonChar '/' = "\\/"+encodeJsonChar '\b' = "\\b"+encodeJsonChar '\f' = "\\f"+encodeJsonChar '\n' = "\\n"+encodeJsonChar '\r' = "\\r"+encodeJsonChar '\t' = "\\t"+encodeJsonChar '"' = "\\\""+encodeJsonChar '\\' = "\\\\"+encodeJsonChar c+ | not $ isControl c = [c]+ | c < '\x10' = '\\' : 'u' : '0' : '0' : '0' : hexxs+ | c < '\x100' = '\\' : 'u' : '0' : '0' : hexxs+ | c < '\x1000' = '\\' : 'u' : '0' : hexxs+ where hexxs = showHex (fromEnum c) "" -- FIXME+encodeJsonChar c = [c]
Language/Javascript/JMacro/QQ.hs view
@@ -323,9 +323,15 @@ statblock :: JMParser [JStat] statblock = concat <$> (sepEndBy1 (whiteSpace >> statement) (semi <|> return "")) +statblock0 :: JMParser [JStat]+statblock0 = try statblock <|> (whiteSpace >> return [])+ l2s :: [JStat] -> JStat l2s xs = BlockStat xs +statementOrEmpty :: JMParser [JStat]+statementOrEmpty = try emptyStat <|> statement+ where emptyStat = braces (whiteSpace >> return []) -- return either an expression or a statement statement :: JMParser [JStat]@@ -386,18 +392,18 @@ ifStat = do reserved "if" p <- parens expr- b <- l2s <$> statement+ b <- l2s <$> statementOrEmpty isElse <- (lookAhead (reserved "else") >> return True) <|> return False if isElse then do reserved "else"- return . IfStat p b . l2s <$> statement+ return . IfStat p b . l2s <$> statementOrEmpty else return $ [IfStat p b nullStat] whileStat = reserved "while" >> liftM2 (\e b -> [WhileStat e (l2s b)])- (parens expr) statement+ (parens expr) statementOrEmpty switchStat = do reserved "switch"@@ -406,7 +412,7 @@ return $ [SwitchStat e l (l2s d)] caseStat =- reserved "case" >> liftM2 (,) expr (char ':' >> l2s <$> statblock)+ reserved "case" >> liftM2 (,) expr (char ':' >> l2s <$> statblock0) tryStat = do reserved "try"
jmacro.cabal view
@@ -1,5 +1,5 @@ name: jmacro-version: 0.4.6+version: 0.5 synopsis: QuasiQuotation library for programmatic generation of Javascript code. description: Javascript syntax, functional syntax, hygienic names, compile-time guarantees of syntactic correctness, limited typechecking. category: Language