diff --git a/rzk/grammar/Syntax.cf b/rzk/grammar/Syntax.cf index 14dbd2518..bd27d7155 100644 --- a/rzk/grammar/Syntax.cf +++ b/rzk/grammar/Syntax.cf @@ -60,8 +60,10 @@ SomeSectionName. SectionName ::= VarIdent ; PatternUnit. Pattern ::= "unit" ; PatternVar. Pattern ::= VarIdent ; PatternPair. Pattern ::= "(" Pattern "," Pattern ")" ; -PatternTuple. Pattern ::= "(" Pattern "," Pattern "," [Pattern] ")" ; +PatternTuple. Pattern ::= "(" Pattern "," Pattern "," [Pattern1] ")" ; separator nonempty Pattern "" ; +_. Pattern1 ::= Pattern ; +separator nonempty Pattern1 "," ; -- Parameter introduction (for lambda abstractions) ParamPattern. Param ::= Pattern ; diff --git a/rzk/src/Language/Rzk/Syntax/Doc.txt b/rzk/src/Language/Rzk/Syntax/Doc.txt index f8adc1639..4713fc59e 100644 --- a/rzk/src/Language/Rzk/Syntax/Doc.txt +++ b/rzk/src/Language/Rzk/Syntax/Doc.txt @@ -97,9 +97,12 @@ All other symbols are terminals. | //Pattern// | -> | ``unit`` | | **|** | //VarIdent// | | **|** | ``(`` //Pattern// ``,`` //Pattern// ``)`` - | | **|** | ``(`` //Pattern// ``,`` //Pattern// ``,`` //[Pattern]// ``)`` + | | **|** | ``(`` //Pattern// ``,`` //Pattern// ``,`` //[Pattern1]// ``)`` | //[Pattern]// | -> | //Pattern// | | **|** | //Pattern// //[Pattern]// + | //Pattern1// | -> | //Pattern// + | //[Pattern1]// | -> | //Pattern1// + | | **|** | //Pattern1// ``,`` //[Pattern1]// | //Param// | -> | //Pattern// | | **|** | ``(`` //[Pattern]// ``:`` //Term// ``)`` | | **|** | ``(`` //[Pattern]// ``:`` //Term// ``|`` //Term// ``)`` diff --git a/rzk/src/Language/Rzk/Syntax/Par.y b/rzk/src/Language/Rzk/Syntax/Par.y index 9089984c0..6d33bfc7e 100644 --- a/rzk/src/Language/Rzk/Syntax/Par.y +++ b/rzk/src/Language/Rzk/Syntax/Par.y @@ -20,6 +20,8 @@ module Language.Rzk.Syntax.Par , pSectionName , pPattern , pListPattern + , pPattern1 + , pListPattern1 , pParam , pListParam , pParamDecl @@ -57,6 +59,8 @@ import Language.Rzk.Syntax.Lex %name pSectionName_internal SectionName %name pPattern_internal Pattern %name pListPattern_internal ListPattern +%name pPattern1_internal Pattern1 +%name pListPattern1_internal ListPattern1 %name pParam_internal Param %name pListParam_internal ListParam %name pParamDecl_internal ParamDecl @@ -234,13 +238,21 @@ Pattern : 'unit' { (uncurry Language.Rzk.Syntax.Abs.BNFC'Position (tokenLineCol $1), Language.Rzk.Syntax.Abs.PatternUnit (uncurry Language.Rzk.Syntax.Abs.BNFC'Position (tokenLineCol $1))) } | VarIdent { (fst $1, Language.Rzk.Syntax.Abs.PatternVar (fst $1) (snd $1)) } | '(' Pattern ',' Pattern ')' { (uncurry Language.Rzk.Syntax.Abs.BNFC'Position (tokenLineCol $1), Language.Rzk.Syntax.Abs.PatternPair (uncurry Language.Rzk.Syntax.Abs.BNFC'Position (tokenLineCol $1)) (snd $2) (snd $4)) } - | '(' Pattern ',' Pattern ',' ListPattern ')' { (uncurry Language.Rzk.Syntax.Abs.BNFC'Position (tokenLineCol $1), Language.Rzk.Syntax.Abs.PatternTuple (uncurry Language.Rzk.Syntax.Abs.BNFC'Position (tokenLineCol $1)) (snd $2) (snd $4) (snd $6)) } + | '(' Pattern ',' Pattern ',' ListPattern1 ')' { (uncurry Language.Rzk.Syntax.Abs.BNFC'Position (tokenLineCol $1), Language.Rzk.Syntax.Abs.PatternTuple (uncurry Language.Rzk.Syntax.Abs.BNFC'Position (tokenLineCol $1)) (snd $2) (snd $4) (snd $6)) } ListPattern :: { (Language.Rzk.Syntax.Abs.BNFC'Position, [Language.Rzk.Syntax.Abs.Pattern]) } ListPattern : Pattern { (fst $1, (:[]) (snd $1)) } | Pattern ListPattern { (fst $1, (:) (snd $1) (snd $2)) } +Pattern1 :: { (Language.Rzk.Syntax.Abs.BNFC'Position, Language.Rzk.Syntax.Abs.Pattern) } +Pattern1 : Pattern { (fst $1, (snd $1)) } + +ListPattern1 :: { (Language.Rzk.Syntax.Abs.BNFC'Position, [Language.Rzk.Syntax.Abs.Pattern]) } +ListPattern1 + : Pattern1 { (fst $1, (:[]) (snd $1)) } + | Pattern1 ',' ListPattern1 { (fst $1, (:) (snd $1) (snd $3)) } + Param :: { (Language.Rzk.Syntax.Abs.BNFC'Position, Language.Rzk.Syntax.Abs.Param) } Param : Pattern { (fst $1, Language.Rzk.Syntax.Abs.ParamPattern (fst $1) (snd $1)) } @@ -430,6 +442,12 @@ pPattern = fmap snd . pPattern_internal pListPattern :: [Token] -> Err [Language.Rzk.Syntax.Abs.Pattern] pListPattern = fmap snd . pListPattern_internal +pPattern1 :: [Token] -> Err Language.Rzk.Syntax.Abs.Pattern +pPattern1 = fmap snd . pPattern1_internal + +pListPattern1 :: [Token] -> Err [Language.Rzk.Syntax.Abs.Pattern] +pListPattern1 = fmap snd . pListPattern1_internal + pParam :: [Token] -> Err Language.Rzk.Syntax.Abs.Param pParam = fmap snd . pParam_internal diff --git a/rzk/src/Language/Rzk/Syntax/Print.hs b/rzk/src/Language/Rzk/Syntax/Print.hs index ae973872f..0d27b1e21 100644 --- a/rzk/src/Language/Rzk/Syntax/Print.hs +++ b/rzk/src/Language/Rzk/Syntax/Print.hs @@ -198,10 +198,12 @@ instance Print (Language.Rzk.Syntax.Abs.Pattern' a) where Language.Rzk.Syntax.Abs.PatternUnit _ -> prPrec i 0 (concatD [doc (showString "unit")]) Language.Rzk.Syntax.Abs.PatternVar _ varident -> prPrec i 0 (concatD [prt 0 varident]) Language.Rzk.Syntax.Abs.PatternPair _ pattern_1 pattern_2 -> prPrec i 0 (concatD [doc (showString "("), prt 0 pattern_1, doc (showString ","), prt 0 pattern_2, doc (showString ")")]) - Language.Rzk.Syntax.Abs.PatternTuple _ pattern_1 pattern_2 patterns -> prPrec i 0 (concatD [doc (showString "("), prt 0 pattern_1, doc (showString ","), prt 0 pattern_2, doc (showString ","), prt 0 patterns, doc (showString ")")]) + Language.Rzk.Syntax.Abs.PatternTuple _ pattern_1 pattern_2 patterns -> prPrec i 0 (concatD [doc (showString "("), prt 0 pattern_1, doc (showString ","), prt 0 pattern_2, doc (showString ","), prt 1 patterns, doc (showString ")")]) instance Print [Language.Rzk.Syntax.Abs.Pattern' a] where prt _ [] = concatD [] + prt 1 [x] = concatD [prt 1 x] + prt 1 (x:xs) = concatD [prt 1 x, doc (showString ","), prt 1 xs] prt _ [x] = concatD [prt 0 x] prt _ (x:xs) = concatD [prt 0 x, prt 0 xs]