Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

#3, Add support for namespaces.

Add support for the namespace and the use statement, and allow the namespace separator in identifiers.
  • Loading branch information...
commit b651f7103cf22ceb03e1de7731fe01d1626ebeff 1 parent fe512ef
@arvidj authored
View
5 src/Lang/Php/Ast/Lex.hs
@@ -80,7 +80,7 @@ lineParser :: Parser String
lineParser = liftM2 (++) (many $ satisfy (/= '\n')) ((:[]) <$> newline)
identStartChars :: String
-identStartChars = ['a'..'z'] ++ ['A'..'Z'] ++ ['_']
+identStartChars = ['\\'] ++ ['a'..'z'] ++ ['A'..'Z'] ++ ['_']
identEndChars :: String
identEndChars = identStartChars ++ ['0'..'9']
@@ -293,6 +293,7 @@ tokSwitch = "switch"
tokThrow = "throw"
tokTry = "try"
tokUnset = "unset"
+tokUse = "use"
tokVar = "var"
tokWhile = "while"
tokXorWd = "xor"
@@ -356,6 +357,7 @@ reservedWords = Set.fromList [
tokThrow,
tokTry,
tokUnset,
+ tokUse,
tokVar,
tokWhile,
tokXorWd]
@@ -423,6 +425,7 @@ tokSwitchP = identCI tokSwitch
tokThrowP = identCI tokThrow
tokTryP = identCI tokTry
tokUnsetP = identCI tokUnset
+tokUseP = identCI tokUse
tokVarP = identCI tokVar
tokWhileP = identCI tokWhile
tokXorWdP = identCI tokXorWd
View
20 src/Lang/Php/Ast/StmtParse.hs
@@ -30,6 +30,7 @@ instance Unparse Stmt where
intercalate tokComma (map unparse a) ++ unparse end
StmtIf a -> unparse a
StmtInterface a -> unparse a
+ StmtNamespace n end -> tokNamespace ++ unparse n ++ unparse end
StmtNothing end -> unparse end
StmtReturn rMb w end -> tokReturn ++ unparse rMb ++ unparse w ++
unparse end
@@ -41,6 +42,7 @@ instance Unparse Stmt where
StmtUnset (WSCap w1 a w2) end -> tokUnset ++ unparse w1 ++ tokLParen ++
intercalate tokComma (map unparse a) ++ tokRParen ++ unparse w2 ++
unparse end
+ StmtUse n end -> tokUse ++ unparse n ++ unparse end
StmtWhile a -> unparse a
instance Unparse StmtEnd where
@@ -153,6 +155,12 @@ instance Unparse IfaceStmt where
unparse (IfaceConst vars) = cStmtConstUnparser vars
unparse (IfaceFunc a) = unparse a
+instance Unparse Namespace where
+ unparse (Namespace n) = n
+
+instance Unparse Use where
+ unparse (Use n) = n
+
instance Unparse VarMbVal where
unparse (VarMbVal var exprMb) = unparse var ++ maybe []
(\ (w, expr) -> w2With tokEquals w ++ unparse expr) exprMb
@@ -200,6 +208,8 @@ simpleStmtParser =
liftM2 (uncurry StmtExpr) parse parse <|>
StmtFuncDef <$> parse <|>
liftM2 StmtGlobal (tokGlobalP >> sepBy1 parse tokCommaP) parse <|>
+ liftM2 StmtNamespace (tokNamespaceP >> parse) parse <|>
+ liftM2 StmtUse (tokUseP >> parse) parse <|>
StmtInterface <$> parse <|>
StmtNothing <$> parse <|>
liftM3 StmtReturn (tokReturnP >> parse) (optionMaybe parse) parse <|>
@@ -405,6 +415,16 @@ instance Parse Interface where
((tokExtendsP >> sepBy1 parse tokCommaP) <|> return [])
parse
+instance Parse Namespace where
+ parse = do
+ n <- identifierParser
+ return $ Namespace n
+
+instance Parse Use where
+ parse = do
+ n <- identifierParser
+ return $ Use n
+
instance Parse IfaceStmt where
parse =
classConstParser IfaceConst <|>
View
10 src/Lang/Php/Ast/StmtTypes.hs
@@ -25,6 +25,7 @@ data Stmt =
StmtGlobal [WSCap Var] StmtEnd |
StmtIf If |
StmtInterface Interface |
+ StmtNamespace (WSCap Namespace) StmtEnd |
StmtNothing StmtEnd |
StmtReturn WS (Maybe (Expr, WS)) StmtEnd |
-- this list must have at least one element.. should i make a type for that?
@@ -33,6 +34,7 @@ data Stmt =
StmtThrow (WSCap Expr) StmtEnd |
StmtTry (WSCap (Block Stmt)) (IC.Intercal Catch WS) |
StmtUnset (WSCap [WSCap LRVal]) StmtEnd |
+ StmtUse (WSCap Use) StmtEnd |
StmtWhile While
deriving (Eq, Show, Typeable, Data)
@@ -41,6 +43,12 @@ data Stmt =
data Block a = Block (IC.Intercal WS a)
deriving (Eq, Show, Typeable, Data)
+data Namespace = Namespace String
+ deriving (Eq, Show, Typeable, Data)
+
+data Use = Use String
+ deriving (Eq, Show, Typeable, Data)
+
data Func = Func {
funcWS :: WS,
funcRef :: Maybe WS,
@@ -181,10 +189,12 @@ $(derive makeBinary ''If)
$(derive makeBinary ''IfaceStmt)
$(derive makeBinary ''IfBlock)
$(derive makeBinary ''Interface)
+$(derive makeBinary ''Namespace)
$(derive makeBinary ''Stmt)
$(derive makeBinary ''StmtEnd)
$(derive makeBinary ''Switch)
$(derive makeBinary ''TopLevel)
+$(derive makeBinary ''Use)
$(derive makeBinary ''VarMbVal)
$(derive makeBinary ''VarEqVal)
$(derive makeBinary ''While)
Please sign in to comment.
Something went wrong with that request. Please try again.