git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Whitespace sensitive syntax for blocks
commitdb7dadffcd0e9c221fc661885edf282d43f54a2d authorPhil Freeman <paf31@cantab.net> dateMon 14 Oct 2013, 22:44 (13 years ago) parentd6ab2e7
8 files changed+29−30
examples/Arrays.ps+6−8 examples/List.ps+6−9 examples/Match.ps+1−1 examples/Maybe.ps+2−3 examples/Patterns.ps+3−4 src/PureScript/Parser/Common.hs+7−1 src/PureScript/Parser/Declarations.hs+1−1 src/PureScript/Parser/Values.hs+3−3
examples/Arrays.ps+6−8
@@ -1,16 +1,14 @@
11test1 = \arr -> arr[0] + arr[1]
22
33test2 = \arr -> case arr of
4− { [x, y] -> x + y
5− ; [x] -> x
6− ; [] -> 0
7− ; [x, y : _] -> x + y
8− }
4+ [x, y] -> x + y
5+ [x] -> x
6+ [] -> 0
7+ [x, y : _] -> x + y
98
109data Tree = One Number | Some [Tree]
1110
1211test3 = \tree, sum -> case tree of
13− { One n -> n
14− ; Some [n1, n2 : rest] -> test3(n1, sum) * 10 + test3(n2, sum) * 5 + sum(rest)
15− }
12+ One n -> n
13+ Some [n1, n2 : rest] -> test3(n1, sum) * 10 + test3(n2, sum) * 5 + sum(rest)
1614
examples/List.ps+6−9
@@ -6,19 +6,16 @@ data List a
66 }
77
88null = \l -> case l of
9− { Nil -> true
10− ; _ -> false
11− }
9+ Nil -> true
10+ _ -> false
1211
1312concat = \l1 -> \l2 -> case l1 of
14− { Nil -> l2
15− ; Cons x -> Cons(
13+ Nil -> l2
14+ Cons x -> Cons(
1615 { head: x.head
1716 , tail: concat(x.tail)(l2)
1817 })
19− }
2018
2119flatten = \xss -> case xss of
22− { Nil -> Nil
23− ; Cons cons -> concat(cons.head)(flatten(cons.tail))
24− }
20+ Nil -> Nil
21+ Cons cons -> concat(cons.head)(flatten(cons.tail))
examples/Match.ps+1−1
@@ -1,3 +1,3 @@
11data Foo a = Foo
22
3−foo = \f -> case f of { Foo -> "foo" }
3+foo = \f -> case f of Foo -> "foo"
examples/Maybe.ps+2−3
@@ -1,9 +1,8 @@
11data Maybe a = Nothing | Just a
22
33maybe = \b -> \f -> \m -> case m of
4− { Nothing -> b
5− ; Just a -> f(a)
6− }
4+ Nothing -> b
5+ Just a -> f(a)
76
87fmap = \m -> \f -> maybe(Nothing)(\a -> Just(f(a)))(m)
98
examples/Patterns.ps+3−4
@@ -1,5 +1,4 @@
11test = \x -> case x of
2− { { str = "Foo", bool = true } -> true
3− ; { str = "Bar", bool = b } -> b
4− ; _ -> false
5− }
2+ { str = "Foo", bool = true } -> true
3+ { str = "Bar", bool = b } -> b
4+ _ -> false
src/PureScript/Parser/Common.hs+7−1
@@ -59,10 +59,16 @@ opLetter = P.oneOf ":#$%&*+./<=>?@^|"
5959whiteSpace :: I.IndentParser String u ()
6060whiteSpace = P.skipMany $ P.choice
6161 [ P.oneOf " \t" >> return ()
62− , P.try $ P.skipMany1 (P.oneOf "\r\n") >> P.skipMany1 (P.oneOf " \t") >> I.indented
62+ , P.try $ P.skipMany1 (P.oneOf " \t\r\n") >> I.indented
6363 , P.try lineComment
6464 , P.try blockComment ]
6565
66+unindentedBlock :: I.IndentParser String u a -> I.IndentParser String u [a]
67+unindentedBlock p = I.block $ I.withPos (p <* P.skipMany (P.oneOf "\r\n"))
68+
69+indentedBlock :: I.IndentParser String u a -> I.IndentParser String u [a]
70+indentedBlock p = I.block $ I.withPos (p <* P.skipMany (P.oneOf " \t\r\n"))
71+
6672lineComment :: I.IndentParser String u ()
6773lineComment = do
6874 P.string "--"
src/PureScript/Parser/Declarations.hs+1−1
@@ -65,7 +65,7 @@ parseDeclaration = P.choice $ map P.try
6565 , parseExternDeclaration ]
6666
6767parseManyDeclarations :: I.IndentParser String () [Declaration]
68−parseManyDeclarations = I.block $ I.withPos (parseDeclaration <* P.skipMany (P.oneOf "\r\n"))
68+parseManyDeclarations = I.withPos $ unindentedBlock parseDeclaration
6969
7070parseDeclarations :: String -> Either P.ParseError [Declaration]
7171parseDeclarations = flip evalState (P.initialPos "Declarations") . P.runPT (parseManyDeclarations <* P.eof) () "Declarations"
src/PureScript/Parser/Values.hs+3−3
@@ -73,13 +73,13 @@ parseConstructor = Constructor <$> C.properName
7373
7474parseCase :: I.IndentParser String () Value
7575parseCase = Case <$> P.between (C.reserved "case") (C.reserved "of") parseValue
76− <*> C.braces (C.semiSep parseCaseAlternative)
76+ <*> I.withPos (C.indentedBlock parseCaseAlternative)
7777
7878parseCaseAlternative :: I.IndentParser String () (Binder, Value)
7979parseCaseAlternative = (,) <$> (parseBinder <* C.lexeme (P.string "->")) <*> parseValue
8080
8181parseBlock :: I.IndentParser String () Value
82−parseBlock = Block <$> (C.braces $ P.many (parseStatement True))
82+parseBlock = Block <$> (C.braces $ I.withPos $ C.indentedBlock $ parseStatement True)
8383
8484parseValueAtom :: I.IndentParser String () Value
8585parseValueAtom = P.choice $ map P.try
@@ -147,7 +147,7 @@ parseAssignment requireSemi = do
147147 return $ Assignment tgt value
148148
149149parseManyStatements :: I.IndentParser String () [Statement]
150−parseManyStatements = C.braces $ P.many (parseStatement True)
150+parseManyStatements = C.braces $ I.withPos $ C.indentedBlock $ parseStatement True
151151
152152parseWhile :: I.IndentParser String () Statement
153153parseWhile = While <$> (C.reserved "while" *> C.parens parseValue)