<commit_before>module Language.JavaScript.Pretty.Printer (
  -- * Printing
  renderJS
  , renderToString
  ) where

--import Control.Applicative
import Control.Monad (MonadPlus(..), ap)
import Data.Char
import Data.List
import Data.Monoid (Monoid, mappend, mempty, mconcat)
import Language.JavaScript.Parser.AST
import Language.JavaScript.Parser.Parser
import Language.JavaScript.Parser.SrcLocation
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

-- ---------------------------------------------------------------------

data Foo = Foo (Int,Int) BB.Builder
--data Foo a = Foom (Int,Int) a

-- ---------------------------------------------------------------------
-- Pretty printer stuff via blaze-builder

(<>) :: BB.Builder -> BB.Builder -> BB.Builder
(<>) a b = mappend a b

(<+>) :: BB.Builder -> BB.Builder -> BB.Builder
(<+>) a b = mconcat [a, (text " "), b]

--() ((Int, Int), BB.Builder) -> ((Int, Int), BB.Builder) -> ((Int, Int), BB.Builder)
--() a b =

hcat :: (Monoid a) => [a] -> a
hcat xs = mconcat xs

empty :: BB.Builder
empty = mempty

text :: String -> BB.Builder
text s = BS.fromString s

char :: Char -> BB.Builder
char c = BS.fromChar c

comma :: BB.Builder
comma = BS.fromChar ','

punctuate :: a -> [a] -> [a]
punctuate p xs = intersperse p xs

-- ---------------------------------------------------------------------

(<!>) :: Foo -> Foo -> Foo
(<!>) (Foo _ bb1) (Foo (r,c) bb2) = Foo (r,c) (mappend bb1 bb2)

-- ---------------------------------------------------------------------

renderJS :: JSNode -> BB.Builder
renderJS node = bb
  where
    Foo _ bb = rn node (Foo (1,1) empty)

-- Take in the current
-- rn :: (Int, Int) -> JSNode -> ((Int, Int), BB.Builder)
rn :: JSNode -> Foo -> Foo

rn (NS (JSSourceElementsTop xs) p cs) foo = rJS cs p xs foo
rn (NS (JSSourceElements    xs) p cs) foo = rJS cs p xs foo
rn (NS (JSExpression xs) p cs) foo        = rJS cs p xs foo
rn (NS (JSIdentifier s) p cs) foo         = rcs cs p s  foo
rn (NS (JSOperator n) p cs) foo           = rJS cs p [n] foo
rn (NS (JSDecimal i) p cs) foo            = rcs cs p i foo
rn (NS (JSLiteral l) p cs) foo            = rcs cs p l foo
rn (NS (JSUnary l n) p cs) foo            = rJS cs p [n] foo
rn (NS (JSHexInteger i) p cs) foo         = rcs cs p i foo
rn (NS (JSStringLiteral s l) p cs) foo    = rcs cs p ((s:l)++[s]) foo
rn (NS (JSRegEx s) p cs) foo              = rcs cs p s foo

rn (NS (JSArrayLiteral lb xs rb) p []) foo  = (rJS [] p [rb] (rJS  [] p xs (rJS [] p [lb] foo)))

rn (NS (JSElision xs) p cs) foo             = rJS [] p xs foo
rn (NS (JSStatementBlock lb x rb) p []) foo = (rJS [] p [rb] (rJS [] p [x] (rJS [] p [lb] foo)))
rn (NS (JSStatementList xs) p cs) foo       = rJS cs p xs foo
rn (NS (JSLabelled l c v) p cs) foo           = (rJS [] p [v] (rJS cs p [c] (rJS [] p [l] foo)))

rn (NS (JSObjectLiteral lb xs rb) p []) foo     = (rJS [] p [rb] (rJS  [] p xs (rJS [] p [lb] foo)))
rn (NS (JSPropertyNameandValue n colon vs) p []) foo  = (rJS [] p vs (rJS [] p [colon] (rJS [] p [n] foo)))

-- rn (NS (JSPropertyAccessor s n ps b) p cs) foo = (rcs [] p s (rcs [] p " " (rJS [] p [n] (rcs [] p ")" (rJS [] p ps (rcs [] p "(" (rJS [] p [b] foo)))))))

rn (NS (JSPropertyAccessor s n lb1 ps rb1 lb2 b rb2) p []) foo =
  (rJS [] p [rb2]
  (rJS [] p [b]
  (rJS [] p [lb2]
  (rJS [] p [rb1]
  (rJS [] p ps
  (rJS [] p [lb1]
  (rJS [] p [n]
  (rJS [] p [s] foo))))))))

-- NOTE: the braces are attached to the surrounding production
rn (NS (JSFunctionBody xs) p []) foo = (rJS [] p xs foo)


-- rn (JSReturn [])                 = (text "return")
-- rn (JSReturn [(NS (JSLiteral ";") _ _)])    = (text "return;")
-- rn (JSReturn xs)                 = (text "return") <> (if (spaceNeeded xs) then (text " ") else (empty)) <> (rJS xs)

rn (NS (JSReturn xs) p []) foo = (rJS [] p xs foo)

-- Debug helper
rn what foo = rs (show what) foo

-- ---------------------------------------------------------------------
-- Helper functions
<commit_msg>Back to all tests passing. :)<commit_after>module Language.JavaScript.Pretty.Printer (
  -- * Printing
  renderJS
  , renderToString
  ) where

--import Control.Applicative
import Control.Monad (MonadPlus(..), ap)
import Data.Char
import Data.List
import Data.Monoid (Monoid, mappend, mempty, mconcat)
import Language.JavaScript.Parser.AST
import Language.JavaScript.Parser.Parser
import Language.JavaScript.Parser.SrcLocation
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

-- ---------------------------------------------------------------------

data Foo = Foo (Int,Int) BB.Builder
--data Foo a = Foom (Int,Int) a

-- ---------------------------------------------------------------------
-- Pretty printer stuff via blaze-builder

(<>) :: BB.Builder -> BB.Builder -> BB.Builder
(<>) a b = mappend a b

(<+>) :: BB.Builder -> BB.Builder -> BB.Builder
(<+>) a b = mconcat [a, (text " "), b]

--() ((Int, Int), BB.Builder) -> ((Int, Int), BB.Builder) -> ((Int, Int), BB.Builder)
--() a b =

hcat :: (Monoid a) => [a] -> a
hcat xs = mconcat xs

empty :: BB.Builder
empty = mempty

text :: String -> BB.Builder
text s = BS.fromString s

char :: Char -> BB.Builder
char c = BS.fromChar c

comma :: BB.Builder
comma = BS.fromChar ','

punctuate :: a -> [a] -> [a]
punctuate p xs = intersperse p xs

-- ---------------------------------------------------------------------

(<!>) :: Foo -> Foo -> Foo
(<!>) (Foo _ bb1) (Foo (r,c) bb2) = Foo (r,c) (mappend bb1 bb2)

-- ---------------------------------------------------------------------

renderJS :: JSNode -> BB.Builder
renderJS node = bb
  where
    Foo _ bb = rn node (Foo (1,1) empty)

-- Take in the current
-- rn :: (Int, Int) -> JSNode -> ((Int, Int), BB.Builder)
rn :: JSNode -> Foo -> Foo

rn (NS (JSSourceElementsTop xs) p cs) foo = rJS cs p xs foo
rn (NS (JSSourceElements    xs) p cs) foo = rJS cs p xs foo
rn (NS (JSExpression xs) p cs) foo        = rJS cs p xs foo
rn (NS (JSIdentifier s) p cs) foo         = rcs cs p s  foo
rn (NS (JSOperator n) p cs) foo           = rJS cs p [n] foo
rn (NS (JSDecimal i) p cs) foo            = rcs cs p i foo
rn (NS (JSLiteral l) p cs) foo            = rcs cs p l foo
rn (NS (JSUnary l n) p cs) foo            = rJS cs p [n] foo
rn (NS (JSHexInteger i) p cs) foo         = rcs cs p i foo
rn (NS (JSStringLiteral s l) p cs) foo    = rcs cs p ((s:l)++[s]) foo
rn (NS (JSRegEx s) p cs) foo              = rcs cs p s foo

rn (NS (JSArrayLiteral lb xs rb) p []) foo  = (rJS [] p [rb] (rJS  [] p xs (rJS [] p [lb] foo)))

rn (NS (JSElision xs) p cs) foo             = rJS [] p xs foo
rn (NS (JSStatementBlock lb x rb) p []) foo = (rJS [] p [rb] (rJS [] p [x] (rJS [] p [lb] foo)))
rn (NS (JSStatementList xs) p cs) foo       = rJS cs p xs foo
rn (NS (JSLabelled l c v) p cs) foo           = (rJS [] p [v] (rJS cs p [c] (rJS [] p [l] foo)))

rn (NS (JSObjectLiteral lb xs rb) p []) foo     = (rJS [] p [rb] (rJS  [] p xs (rJS [] p [lb] foo)))
rn (NS (JSPropertyNameandValue n colon vs) p []) foo  = (rJS [] p vs (rJS [] p [colon] (rJS [] p [n] foo)))

-- rn (NS (JSPropertyAccessor s n ps b) p cs) foo = (rcs [] p s (rcs [] p " " (rJS [] p [n] (rcs [] p ")" (rJS [] p ps (rcs [] p "(" (rJS [] p [b] foo)))))))

rn (NS (JSPropertyAccessor s n lb1 ps rb1 lb2 b rb2) p []) foo =
  (rJS [] p [rb2]
  (rJS [] p [b]
  (rJS [] p [lb2]
  (rJS [] p [rb1]
  (rJS [] p ps
  (rJS [] p [lb1]
  (rJS [] p [n]
  (rJS [] p [s] foo))))))))

-- NOTE: the braces are attached to the surrounding production
rn (NS (JSFunctionBody xs) p []) foo = (rJS [] p xs foo)


-- rn (JSReturn [])                 = (text "return")
-- rn (JSReturn [(NS (JSLiteral ";") _ _)])    = (text "return;")
-- rn (JSReturn xs)                 = (text "return") <> (if (spaceNeeded xs) then (text " ") else (empty)) <> (rJS xs)

rn (NS (JSReturn xs) p []) foo = (rJS [] p xs foo)

rn (NS (JSExpressionParen lb e rb) p []) foo   = (rJS [] p [rb] (rJS  [] p [e] (rJS [] p [lb] foo)))
rn (NS (JSExpressionTernary cond h v1 c v2) p []) foo  =  (rJS [] p v2
                                                           (rJS [] p [c]
                                                            (rJS [] p v1
                                                             (rJS [] p [h]
                                                              (rJS [] p cond foo)))))

rn (NS (JSExpressionBinary s lhs op rhs) p []) foo   = (rJS [] p rhs (rJS  [] p [op] (rJS [] p lhs foo)))

-- Debug helper
rn what foo = rs (show what) foo

-- ---------------------------------------------------------------------
-- Helper functions

{'fraction_non_alphanumeric': 0.2356550392000909, 'fraction_numerical': 0.003863197363935916, 'mean_word_length': 1.836609732516919, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6027145', 'n_tokens_mistral': 3179, 'n_tokens_neox': 3084, 'n_words': 1172}