Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions language-ecmascript.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ Library
Hs-Source-Dirs:
src
Build-Depends:
base >= 4 && < 4.19,
base >= 4 && < 4.23,
mtl >= 1 && < 3,
parsec > 3 && < 3.2.0,
ansi-wl-pprint >= 0.6 && < 1,
ansi-wl-pprint >= 0.6 && < 2,
containers == 0.*,
uniplate >= 1.6 && <1.7,
data-default-class >= 0.0.1 && < 0.2,
QuickCheck >= 2.5 && < 3,
template-haskell >= 2.7 && < 3,
Diff == 0.4.*,
Diff < 1.1.0,
charset >= 0.3
ghc-options:
-fwarn-incomplete-patterns
Expand Down
45 changes: 22 additions & 23 deletions src/Language/ECMAScript3/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import Language.ECMAScript3.Syntax.Annotations
import Data.Default.Class
import Text.Parsec hiding (parse)
import Text.Parsec.Expr
import Control.Monad(liftM,liftM2)
import Control.Monad.Trans (MonadIO,liftIO)
import Numeric(readDec,readOct,readHex, readFloat)
import Data.Char
Expand Down Expand Up @@ -98,7 +97,7 @@ withFreshLabelStack p = do oldState <- getState

identifier :: Stream s Identity Char => Parser s (Id SourcePos)
identifier =
liftM2 Id getPosition Lexer.identifier
liftA2 Id getPosition Lexer.identifier

--{{{ Statements

Expand Down Expand Up @@ -176,7 +175,7 @@ parseContinueStmt = do
pos' <- getPosition
-- Ensure that the identifier is on the same line as 'continue.'
id <- if sourceLine pos == sourceLine pos'
then liftM Just identifier <|> return Nothing
then fmap Just identifier <|> return Nothing
else return Nothing
optional semi
return $ ContinueStmt pos id
Expand All @@ -188,7 +187,7 @@ parseBreakStmt = do
pos' <- getPosition
-- Ensure that the identifier is on the same line as 'break.'
id <- if sourceLine pos == sourceLine pos'
then liftM Just identifier <|> return Nothing
then fmap Just identifier <|> return Nothing
else return Nothing
optional semi
return $ BreakStmt pos id
Expand Down Expand Up @@ -228,8 +227,8 @@ parseExpressionStmt = do

parseForInStmt:: Stream s Identity Char => StatementParser s
parseForInStmt =
let parseInit = (reserved "var" >> liftM ForInVar identifier)
<|> liftM ForInLVal lvalue
let parseInit = (reserved "var" >> fmap ForInVar identifier)
<|> fmap ForInLVal lvalue
in do pos <- getPosition
-- Lookahead, so that we don't clash with parseForStmt
(init,expr) <- try $ do reserved "for"
Expand All @@ -242,8 +241,8 @@ parseForInStmt =

parseForStmt:: Stream s Identity Char => StatementParser s
parseForStmt =
let parseInit = (reserved "var" >> liftM VarInit (parseVarDecl `sepBy` comma))
<|> liftM ExprInit parseListExpr
let parseInit = (reserved "var" >> fmap VarInit (parseVarDecl `sepBy` comma))
<|> fmap ExprInit parseListExpr
<|> return NoInit
in do pos <- getPosition
reserved "for"
Expand Down Expand Up @@ -304,7 +303,7 @@ parseVarDecl :: Stream s Identity Char => Parser s (VarDecl SourcePos)
parseVarDecl = do
pos <- getPosition
id <- identifier
init <- (reservedOp "=" >> liftM Just assignExpr) <|> return Nothing
init <- (reservedOp "=" >> fmap Just assignExpr) <|> return Nothing
return (VarDecl pos id init)

parseVarDeclStmt:: Stream s Identity Char => StatementParser s
Expand Down Expand Up @@ -380,10 +379,10 @@ parseBoolLit = do
parseTrueLit <|> parseFalseLit

parseVarRef:: Stream s Identity Char => ExpressionParser s
parseVarRef = liftM2 VarRef getPosition identifier
parseVarRef = liftA2 VarRef getPosition identifier

parseArrayLit:: Stream s Identity Char => ExpressionParser s
parseArrayLit = liftM2 ArrayLit getPosition (squares (assignExpr `sepEndBy` comma))
parseArrayLit = liftA2 ArrayLit getPosition (squares (assignExpr `sepEndBy` comma))

parseFuncExpr :: Stream s Identity Char => ExpressionParser s
parseFuncExpr = do
Expand Down Expand Up @@ -420,7 +419,7 @@ parseAsciiHexChar = do
parseUnicodeHexChar :: Stream s Identity Char => Parser s Char
parseUnicodeHexChar = do
char 'u'
liftM (chr.fst.head.readHex)
fmap (chr.fst.head.readHex)
(sequence [hexDigit,hexDigit,hexDigit,hexDigit])

isWhitespace ch = ch `elem` " \t"
Expand All @@ -440,7 +439,7 @@ parseStringLit' endWith =
if c == '\r' || c == '\n'
then return (c:dropWhile isWhitespace cs)
else return (c:cs)) <|>
liftM2 (:) anyChar (parseStringLit' endWith)
liftA2 (:) anyChar (parseStringLit' endWith)

parseStringLit:: Stream s Identity Char => ExpressionParser s
parseStringLit = do
Expand Down Expand Up @@ -471,7 +470,7 @@ parseRegexpLit = do
ch <- anyChar -- TODO: too lenient
rest <- parseRe
return ('\\':ch:rest)) <|>
liftM2 (:) anyChar parseRe
liftA2 (:) anyChar parseRe
pos <- getPosition
char '/'
notFollowedBy $ char '/'
Expand All @@ -486,9 +485,9 @@ parseObjectLit =
-- Parses a string, identifier or integer as the property name. I
-- apologize for the abstruse style, but it really does make the code
-- much shorter.
name <- liftM (\(StringLit p s) -> PropString p s) parseStringLit
<|> liftM2 PropId getPosition identifier
<|> liftM2 PropNum getPosition (parseNumber >>= toInt)
name <- fmap (\(StringLit p s) -> PropString p s) parseStringLit
<|> liftA2 PropId getPosition identifier
<|> liftA2 PropNum getPosition (parseNumber >>= toInt)
colon
val <- assignExpr
return (name,val)
Expand Down Expand Up @@ -679,16 +678,16 @@ unaryAssignExpr = do
p <- getPosition
let prefixInc = do
reservedOp "++"
liftM (UnaryAssignExpr p PrefixInc) lvalue
fmap (UnaryAssignExpr p PrefixInc) lvalue
let prefixDec = do
reservedOp "--"
liftM (UnaryAssignExpr p PrefixDec) lvalue
fmap (UnaryAssignExpr p PrefixDec) lvalue
let postfixInc e = do
reservedOp "++"
liftM (UnaryAssignExpr p PostfixInc) (asLValue p e)
fmap (UnaryAssignExpr p PostfixInc) (asLValue p e)
let postfixDec e = do
reservedOp "--"
liftM (UnaryAssignExpr p PostfixDec) (asLValue p e)
fmap (UnaryAssignExpr p PostfixDec) (asLValue p e)
let other = do
e <- parseSimpleExpr Nothing
postfixInc e <|> postfixDec e <|> return e
Expand Down Expand Up @@ -748,12 +747,12 @@ parseListExpr :: Stream s Identity Char => ExpressionParser s
parseListExpr = assignExpr `sepBy1` comma >>= \exprs ->
case exprs of
[expr] -> return expr
es -> liftM2 ListExpr getPosition (return es)
es -> liftA2 ListExpr getPosition (return es)

parseScript:: Stream s Identity Char => Parser s (JavaScript SourcePos)
parseScript = do
whiteSpace
liftM2 Script getPosition (parseStatement `sepBy` whiteSpace)
liftA2 Script getPosition (parseStatement `sepBy` whiteSpace)

-- | A parser that parses an ECMAScript program.
program :: Stream s Identity Char => Parser s (JavaScript SourcePos)
Expand Down
12 changes: 6 additions & 6 deletions src/Language/ECMAScript3/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,12 @@ checkStmtM stmt = case stmt of
DoWhileStmt _ s _ -> iterCommon s
ForStmt _ _ _ _ s -> iterCommon s
ForInStmt _ _ _ s -> iterCommon s
SwitchStmt _ _ cs -> pushEnclosing EnclosingSwitch $ liftM and $ mapM checkCaseM cs
BlockStmt _ ss -> pushEnclosing EnclosingOther $ liftM and $ mapM checkStmtM ss
IfStmt _ _ t e -> liftM2 (&&) (checkStmtM t) (checkStmtM e)
SwitchStmt _ _ cs -> pushEnclosing EnclosingSwitch $ fmap and $ mapM checkCaseM cs
BlockStmt _ ss -> pushEnclosing EnclosingOther $ fmap and $ mapM checkStmtM ss
IfStmt _ _ t e -> liftA2 (&&) (checkStmtM t) (checkStmtM e)
IfSingleStmt _ _ t -> checkStmtM t
TryStmt _ body mcatch mfinally -> liftM2 (&&) (checkStmtM body) $
liftM2 (&&) (maybe (return True) checkCatchM mcatch)
TryStmt _ body mcatch mfinally -> liftA2 (&&) (checkStmtM body) $
liftA2 (&&) (maybe (return True) checkCatchM mcatch)
(maybe (return True) checkStmtM mfinally)
WithStmt _ _ body -> checkStmtM body
_ -> return True
Expand All @@ -347,7 +347,7 @@ pushLabel l = bracketState (first (unId l:))
checkCaseM c = let ss = case c of
CaseClause _ _ body -> body
CaseDefault _ body -> body
in liftM and $ mapM checkStmtM ss
in fmap and $ mapM checkStmtM ss

checkCatchM (CatchClause _ _ body) = checkStmtM body

Expand Down
6 changes: 3 additions & 3 deletions src/Language/ECMAScript3/Syntax/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ instance (Data a) => Fixable (JavaScript a) where
:: Statement a -> Gen (Statement a))
>=>transformBiM (return . fixLValue
:: LValue a -> Gen (LValue a))
>=>(\(Script a ss)-> liftM (Script a) $ fixBreakContinue ss)
>=>(\(Script a ss)-> fmap (Script a) $ fixBreakContinue ss)

instance (Data a) => Fixable (Expression a) where
fixUp = (fixUpFunExpr . transformBi (identifierFixup :: Id a -> Id a))
Expand Down Expand Up @@ -269,7 +269,7 @@ fixLValue lv = case lv of

fixUpFunExpr :: (Data a) => Expression a -> Gen (Expression a)
fixUpFunExpr e = case e of
FuncExpr a mid params body -> liftM (FuncExpr a mid params) $ fixBreakContinue body
FuncExpr a mid params body -> fmap (FuncExpr a mid params) $ fixBreakContinue body
_ -> return e

fixUpListExpr :: (Data a) => Expression a -> Gen (Expression a)
Expand All @@ -279,7 +279,7 @@ fixUpListExpr e = case e of

fixUpFunStmt :: (Data a) => Statement a -> Gen (Statement a)
fixUpFunStmt s = case s of
FunctionStmt a id params body -> liftM (FunctionStmt a id params) $ fixBreakContinue body
FunctionStmt a id params body -> fmap (FunctionStmt a id params) $ fixBreakContinue body
_ -> return s

identifierFixup :: Id a -> Id a
Expand Down