module Language.ECMAScript3.Syntax (JavaScript(..)
,unJavaScript
,Statement(..)
,isIterationStmt
,CaseClause(..)
,CatchClause(..)
,ForInit(..)
,ForInInit(..)
,VarDecl(..)
,Expression(..)
,InfixOp(..)
,AssignOp(..)
,Id(..)
,unId
,PrefixOp(..)
,Prop(..)
,UnaryAssignOp(..)
,LValue (..)
,SourcePos
) where
import Text.Parsec.Pos(initialPos,SourcePos)
import Data.Data (Data)
import Data.Typeable (Typeable)
import Data.Foldable (Foldable)
import Data.Traversable (Traversable)
import Data.Default.Class
data JavaScript a
= Script a [Statement a]
deriving (Show,Data,Typeable,Eq,Ord,Functor,Foldable,Traversable)
instance Default a => Default (JavaScript a) where
def = Script def []
unJavaScript :: JavaScript a -> [Statement a]
unJavaScript (Script _ stmts) = stmts
instance Default SourcePos where
def = initialPos ""
data Id a = Id a String
deriving (Show,Eq,Ord,Data,Typeable,Functor,Foldable,Traversable)
unId :: Id a -> String
unId (Id _ s) = s
data InfixOp = OpLT
| OpLEq
| OpGT
| OpGEq
| OpIn
| OpInstanceof
| OpEq
| OpNEq
| OpStrictEq
| OpStrictNEq
| OpLAnd
| OpLOr
| OpMul
| OpDiv
| OpMod
| OpSub
| OpLShift
| OpSpRShift
| OpZfRShift
| OpBAnd
| OpBXor
| OpBOr
| OpAdd
deriving (Show,Data,Typeable,Eq,Ord,Enum)
data AssignOp = OpAssign
| OpAssignAdd
| OpAssignSub
| OpAssignMul
| OpAssignDiv
| OpAssignMod
| OpAssignLShift
| OpAssignSpRShift
| OpAssignZfRShift
| OpAssignBAnd
| OpAssignBXor
| OpAssignBOr
deriving (Show,Data,Typeable,Eq,Ord)
data UnaryAssignOp = PrefixInc
| PrefixDec
| PostfixInc
| PostfixDec
deriving (Show, Data, Typeable, Eq, Ord)
data PrefixOp = PrefixLNot
| PrefixBNot
| PrefixPlus
| PrefixMinus
| PrefixTypeof
| PrefixVoid
| PrefixDelete
deriving (Show,Data,Typeable,Eq,Ord)
data Prop a = PropId a (Id a)
| PropString a String
| PropNum a Integer
deriving (Show,Data,Typeable,Eq,Ord,Functor,Foldable,Traversable)
data LValue a
= LVar a String
| LDot a (Expression a) String
| LBracket a (Expression a) (Expression a)
deriving (Show, Eq, Ord, Data, Typeable, Functor,Foldable,Traversable)
data Expression a
= StringLit a String
| RegexpLit a String Bool Bool
| NumLit a Double
| IntLit a Int
| BoolLit a Bool
| NullLit a
| ArrayLit a [Expression a]
| ObjectLit a [(Prop a, Expression a)]
| ThisRef a
| VarRef a (Id a)
| DotRef a (Expression a) (Id a)
| BracketRef a (Expression a) (Expression a)
| NewExpr a (Expression a) [Expression a]
| PrefixExpr a PrefixOp (Expression a)
| UnaryAssignExpr a UnaryAssignOp (LValue a)
| InfixExpr a InfixOp (Expression a) (Expression a)
| CondExpr a (Expression a) (Expression a) (Expression a)
| AssignExpr a AssignOp (LValue a) (Expression a)
| ListExpr a [Expression a]
| CallExpr a (Expression a) [Expression a]
| FuncExpr a (Maybe (Id a)) [Id a] [Statement a]
deriving (Show,Data,Typeable,Eq,Ord,Functor,Foldable,Traversable)
data CaseClause a = CaseClause a (Expression a) [Statement a]
| CaseDefault a [Statement a]
deriving (Show,Data,Typeable,Eq,Ord,Functor,Foldable,Traversable)
data CatchClause a = CatchClause a (Id a) (Statement a)
deriving (Show,Data,Typeable,Eq,Ord,Functor,Foldable,Traversable)
data VarDecl a = VarDecl a (Id a) (Maybe (Expression a))
deriving (Show,Data,Typeable,Eq,Ord,Functor,Foldable,Traversable)
data ForInit a = NoInit
| VarInit [VarDecl a]
| ExprInit (Expression a)
deriving (Show,Data,Typeable,Eq,Ord,Functor,Foldable,Traversable)
data ForInInit a = ForInVar (Id a)
| ForInLVal (LValue a)
deriving (Show,Data,Typeable,Eq,Ord,Functor,Foldable,Traversable)
data Statement a
= BlockStmt a [Statement a]
| EmptyStmt a
| ExprStmt a (Expression a)
| IfStmt a (Expression a) (Statement a) (Statement a)
| IfSingleStmt a (Expression a) (Statement a)
| SwitchStmt a (Expression a) [CaseClause a]
| WhileStmt a (Expression a) (Statement a)
| DoWhileStmt a (Statement a) (Expression a)
| BreakStmt a (Maybe (Id a))
| ContinueStmt a (Maybe (Id a))
| LabelledStmt a (Id a) (Statement a)
| ForInStmt a (ForInInit a) (Expression a) (Statement a)
| ForStmt a (ForInit a)
(Maybe (Expression a))
(Maybe (Expression a))
(Statement a)
| TryStmt a (Statement a) (Maybe (CatchClause a))
(Maybe (Statement a))
| ThrowStmt a (Expression a)
| ReturnStmt a (Maybe (Expression a))
| WithStmt a (Expression a) (Statement a)
| VarDeclStmt a [VarDecl a]
| FunctionStmt a (Id a) [Id a] [Statement a]
deriving (Show,Data,Typeable,Eq,Ord,Functor,Foldable,Traversable)
isIterationStmt :: Statement a -> Bool
isIterationStmt s = case s of
WhileStmt {} -> True
DoWhileStmt {} -> True
ForStmt {} -> True
ForInStmt {} -> True
_ -> False