@@ -148,6 +148,7 @@ collect (c@(PClauses _ o _ _) : ds)
148148 getfc (PClauses fc _ _ _) = fc
149149
150150collect (PParams f ns ps : ds) = PParams f ns (collect ps) : collect ds
151+collect (PNamespace ns ps : ds) = PNamespace ns (collect ps) : collect ds
151152collect (PClass f s cs n ps ds : ds') = PClass f s cs n ps (collect ds) : collect ds'
152153collect (PInstance f s cs n ps t ds : ds')
153154 = PInstance f s cs n ps t (collect ds) : collect ds'
@@ -168,6 +169,7 @@ pDecl syn
168169 return [d']
169170 <|> pUsing syn
170171 <|> pParams syn
172+ <|> pNamespace syn
171173 <|> pClass syn
172174 <|> pInstance syn
173175 <|> pDirective
@@ -237,7 +239,8 @@ pSynSym = try (do lchar '['; n <- pName; lchar ']'
237239 return (Symbol sym)
238240
239241pFunDecl' :: SyntaxInfo -> IParser PDecl
240−pFunDecl' syn = try (do n <- pfName;
242+pFunDecl' syn = try (do n_in <- pfName
243+ let n = expandNS syn n_in
241244 ty <- pTSig syn
242245 fc <- pfc
243246 lchar ';'
@@ -270,6 +273,20 @@ pParams syn =
270273 fc <- pfc
271274 return [PParams fc ns (concat ds)]
272275
276+pNamespace :: SyntaxInfo -> IParser [PDecl]
277+pNamespace syn =
278+ do reserved "namespace";
279+ n <- identifier
280+ lchar '{'
281+ ds <- many1 (pDecl syn { syn_namespace = n : syn_namespace syn })
282+ lchar '}'
283+ return [PNamespace n (concat ds)]
284+
285+expandNS :: SyntaxInfo -> Name -> Name
286+expandNS syn n = case syn_namespace syn of
287+ [] -> n
288+ xs -> NS n xs
289+
273290--------- Fixity ---------
274291
275292pFixity :: IParser PDecl
@@ -295,7 +312,7 @@ pClass :: SyntaxInfo -> IParser [PDecl]
295312pClass syn = do reserved "class"
296313 fc <- pfc
297314 cons <- pConstList syn
298− n <- pName
315+ n_in <- pName; let n = expandNS syn n_in
299316 cs <- many1 carg
300317 reserved "where"; lchar '{'
301318 ds <- many1 $ pFunDecl syn;
@@ -607,14 +624,16 @@ prefix name f = Prefix (do { reservedOp name; fc <- pfc;
607624
608625pData :: SyntaxInfo -> IParser PDecl
609626pData syn = try (do reserved "data"; fc <- pfc
610− tyn <- pfName; ty <- pTSig syn
627+ tyn_in <- pfName; ty <- pTSig syn
628+ let tyn = expandNS syn tyn_in
611629 reserved "where"
612630-- ty' <- implicit syn tyn ty
613631 cons <- sepBy (pConstructor syn) (lchar '|')
614632 lchar ';'
615633 return $ PData syn fc (PDatadecl tyn ty cons))
616634 <|> do reserved "data"; fc <- pfc
617− tyn <- pfName; args <- many pName
635+ tyn_in <- pfName; args <- many pName
636+ let tyn = expandNS syn tyn_in
618637 lchar '='
619638 cons <- sepBy1 (pSimpleCon syn) (lchar '|')
620639 lchar ';'
@@ -636,14 +655,16 @@ bindArgs (x:xs) t = PPi expl (MN 0 "t") x (bindArgs xs t)
636655
637656pConstructor :: SyntaxInfo -> IParser (Name, PTerm, FC)
638657pConstructor syn
639− = do cn <- pfName; fc <- pfc
658+ = do cn_in <- pfName; fc <- pfc
659+ let cn = expandNS syn cn_in
640660 ty <- pTSig syn
641661-- ty' <- implicit syn cn ty
642662 return (cn, ty, fc)
643663
644664pSimpleCon :: SyntaxInfo -> IParser (Name, [PTerm], FC)
645665pSimpleCon syn
646− = do cn <- pfName
666+ = do cn_in <- pfName
667+ let cn = expandNS syn cn_in
647668 fc <- pfc
648669 args <- many (pSimpleExpr syn)
649670 return (cn, args, fc)
@@ -668,7 +689,7 @@ pRHS syn n = do lchar '='; pExpr syn
668689
669690pClause :: SyntaxInfo -> IParser PClause
670691pClause syn
671− = try (do n <- pfName
692+ = try (do n_in <- pfName; let n = expandNS syn n_in
672693 cargs <- many (pConstraintArg syn)
673694 iargs <- many (pImplicitArg syn)
674695 fc <- pfc
@@ -677,11 +698,13 @@ pClause syn
677698 rhs <- pRHS syn n
678699 ist <- getState
679700 let ctxt = tt_ctxt ist
680− (wheres, nmap) <- choice [pWhereblock n syn, do lchar ';'
681− return ([], [])]
701+ let wsyn = syn { syn_namespace = [] }
702+ (wheres, nmap) <- choice [pWhereblock n wsyn,
703+ do lchar ';'
704+ return ([], [])]
682705 return $ PClause n (PApp fc (PRef fc n)
683706 (iargs ++ cargs ++ map pexp args)) wargs rhs wheres)
684− <|> try (do n <- pfName
707+ <|> try (do n_in <- pfName; let n = expandNS syn n_in
685708 cargs <- many (pConstraintArg syn)
686709 iargs <- many (pImplicitArg syn)
687710 fc <- pfc
@@ -698,19 +721,21 @@ pClause syn
698721
699722 <|> do l <- pArgExpr syn
700723 op <- operator
701− let n = UN op
724+ let n = expandNS syn (UN op)
702725 r <- pArgExpr syn
703726 fc <- pfc
704727 wargs <- many (pWExpr syn)
705728 rhs <- pRHS syn n
706− (wheres, nmap) <- choice [pWhereblock n syn, do lchar ';'
707− return ([], [])]
729+ let wsyn = syn { syn_namespace = [] }
730+ (wheres, nmap) <- choice [pWhereblock n wsyn,
731+ do lchar ';'
732+ return ([], [])]
708733 return $ PClause n (PApp fc (PRef fc n) [pexp l,pexp r])
709734 wargs rhs wheres
710735
711736 <|> do l <- pArgExpr syn
712737 op <- operator
713− let n = UN op
738+ let n = expandNS syn (UN op)
714739 r <- pArgExpr syn
715740 fc <- pfc
716741 wargs <- many (pWExpr syn)