Permalink
Browse files

Working in comment processing.

  • Loading branch information...
1 parent 9b48077 commit 42faafb4f5abb4bf5b026be060b2ae996b4901ee @alanz alanz committed Feb 29, 2012
Showing with 17 additions and 2 deletions.
  1. +2 −0 TODO.txt
  2. +15 −2 src/Language/JavaScript/Pretty/Printer.hs
View
2 TODO.txt
@@ -33,5 +33,7 @@ manipulation.
14. Look at http://jsshaper.org/
+15. Store number of rows/cols in a comment, to speed output
+
EOF
View
17 src/Language/JavaScript/Pretty/Printer.hs
@@ -6,8 +6,8 @@ module Language.JavaScript.Pretty.Printer (
import Data.Char
import Data.List
import Data.Monoid (Monoid, mappend, mempty, mconcat)
--- import Text.Jasmine.Parse
import Language.JavaScript.Parser
+import Language.JavaScript.Parser.Token
import qualified Blaze.ByteString.Builder as BB
import qualified Blaze.ByteString.Builder.Char.Utf8 as BS
import qualified Data.ByteString.Lazy as LB
@@ -40,13 +40,25 @@ punctuate :: a -> [a] -> [a]
punctuate p xs = intersperse p xs
-- ---------------------------------------------------------------------
+-- Utility (boilerplate) functions
bp :: (Int,Int) -> TokenPosn -> ((Int,Int) -> ((Int,Int),BB.Builder)) -> ((Int,Int),BB.Builder)
bp (r,c) p f = ((r'',c''),bb <> bb')
where
((r',c'),bb) = skipTo (r,c) p
((r'',c''),bb') = f (r',c')
+
+bpc (r,c) p cs f = ((r'',c''),bb <> bb')
+ where
+ ((r', c'), bb) = foldl' (\((rc,cc),bb) (pc,comment) -> bp (rc,cc) pc (rComment comment)) ((r,c),mempty) cs
+ ((r'',c''),bb') = bp (r',c') p f
+
+rComment NoComment (r,c) = ((r,c),mempty)
+rComment (CommentA p s) (r,c) = ((r',c'),text s)
+ where
+ (r',c') = (r,c) -- TODO: advance as per actual comment
+
bprJS
:: (Int, Int) -> TokenPosn -> [JSNode] -> ((Int, Int), BB.Builder)
bprJS (r,c) p xs = bp (r,c) p (\(r,c) -> rJS (r,c) xs)
@@ -55,6 +67,7 @@ bpText
:: (Int, Int) -> TokenPosn -> [Char] -> ((Int, Int), BB.Builder)
bpText (r,c) p s = bp (r,c) p (\(r,c) -> ((r,c + (length s)),text s))
+bpcText (r,c) p cs s = bpc (r,c) p cs (\(r,c) -> ((r,c + (length s)),text s))
-- ---------------------------------------------------------------------
@@ -83,7 +96,7 @@ rn (r,c) (NS (JSDecimal i) p cs) = bpText (r,c) p i
rn (r,c) (NS (JSLiteral l) p cs) = bpText (r,c) p l
-rn (r,c) (NS (JSUnary l) p cs = bpText (r,c) p l
+rn (r,c) (NS (JSUnary l) p cs) = bpText (r,c) p l
{-

0 comments on commit 42faafb

Please sign in to comment.