module Language.JavaScript.Parser.Parser (
parse
, readJs
, parseFile
, parseFileUtf8
, parseUsing
, showStripped
, showStrippedMaybe
) where
import Language.JavaScript.Parser.ParseError
import Language.JavaScript.Parser.Grammar5
import Language.JavaScript.Parser.Lexer
import qualified Language.JavaScript.Parser.AST as AST
import System.IO
parse :: String
-> String
-> Either String AST.JSNode
parse input _srcName = runAlex input parseProgram
readJs :: String -> AST.JSNode
readJs input = do
case (parse input "src") of
Left msg -> error (show msg)
Right p -> p
parseFile :: FilePath -> IO AST.JSNode
parseFile filename =
do
x <- readFile (filename)
return $ readJs x
parseFileUtf8 :: FilePath -> IO AST.JSNode
parseFileUtf8 filename =
do
h <- openFile filename ReadMode
hSetEncoding h utf8
x <- hGetContents h
return $ readJs x
showStripped :: AST.JSNode -> String
showStripped ast = AST.showStripped ast
showStrippedMaybe :: Show a => Either a AST.JSNode -> String
showStrippedMaybe maybeAst = do
case maybeAst of
Left msg -> "Left (" ++ show msg ++ ")"
Right p -> "Right (" ++ AST.showStripped p ++ ")"
parseUsing ::
Alex AST.JSNode
-> String
-> String
-> Either String AST.JSNode
parseUsing p input _srcName = runAlex input p