summaryrefslogtreecommitdiff
path: root/src/GF/JavaScript/PrintJS.hs
diff options
context:
space:
mode:
authorbringert <bringert@cs.chalmers.se>2007-01-21 17:00:15 +0000
committerbringert <bringert@cs.chalmers.se>2007-01-21 17:00:15 +0000
commit71111e0a7c23c3b6b8bf99915f5e1479141cc27b (patch)
treef1d4e4cc16d851a0b184c7970fbca57873e6e187 /src/GF/JavaScript/PrintJS.hs
parent169659c096a38f8fc0cf48e3054efbdf30a96c8c (diff)
Nicer spacing and indentation in JavaScript pretty-printer.
Diffstat (limited to 'src/GF/JavaScript/PrintJS.hs')
-rw-r--r--src/GF/JavaScript/PrintJS.hs25
1 files changed, 11 insertions, 14 deletions
diff --git a/src/GF/JavaScript/PrintJS.hs b/src/GF/JavaScript/PrintJS.hs
index 402e20c9d..f901a62df 100644
--- a/src/GF/JavaScript/PrintJS.hs
+++ b/src/GF/JavaScript/PrintJS.hs
@@ -18,22 +18,19 @@ doc = (:)
render :: Doc -> String
render d = rend 0 (map ($ "") $ d []) "" where
rend i ss = case ss of
- "[" :ts -> showChar '[' . rend i ts
- "(" :ts -> showChar '(' . rend i ts
- "{" :ts -> showChar '{' . new (i+1) . rend (i+1) ts
- "}" : ";":ts -> new (i-1) . space "}" . showChar ';' . new (i-1) . rend (i-1) ts
- "}" :ts -> new (i-1) . showChar '}' . new (i-1) . rend (i-1) ts
- ";" :ts -> showChar ';' . new i . rend i ts
- t : "," :ts -> showString t . space "," . rend i ts
- t : "." :ts -> showString t . showString "." . rend i ts
- t : ")" :ts -> showString t . showChar ')' . rend i ts
- t : "]" :ts -> showString t . showChar ']' . rend i ts
- t : "[" :ts -> showString t . showChar '[' . rend i ts
- t :ts -> space t . rend i ts
- _ -> id
+ t:ts | not (spaceAfter t) -> showString t . rend i ts
+ t:ts@(t':_) | not (spaceBefore t') -> showString t . rend i ts
+ t:ts -> space t . rend i ts
+ [] -> id
new i = showChar '\n' . replicateS (2*i) (showChar ' ') . dropWhile isSpace
space t = showString t . (\s -> if null s then "" else (' ':s))
+spaceAfter :: String -> Bool
+spaceAfter = (`notElem` [".","(","["])
+
+spaceBefore :: String -> Bool
+spaceBefore = (`notElem` [",",".",":",";","(",")","[","]","{","}"])
+
parenth :: Doc -> Doc
parenth ss = doc (showChar '(') . ss . doc (showChar ')')
@@ -100,7 +97,7 @@ instance Print Element where
prtList es = case es of
[] -> (concatD [])
- x:xs -> (concatD [prt 0 x , prt 0 xs])
+ x:xs -> (concatD [prt 0 x , doc (showString "\n"), prt 0 xs]) -- HACKED!
instance Print Stmt where
prt i e = case e of