git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Whitespace sensitive syntax for blocks
db7dadfPhil Freeman
examples/Arrays.ps+6−8
@@ -1,16 +1,14 @@
1test1 = \arr -> arr[0] + arr[1]
1test1 = \arr -> arr[0] + arr[1]
2
2
3test2 = \arr -> case arr of
3test2 = \arr -> case arr of
4− { [x, y] -> x + y
4+ [x, y] -> x + y
5− ; [x] -> x
5+ [x] -> x
6− ; [] -> 0
6+ [] -> 0
7− ; [x, y : _] -> x + y
7+ [x, y : _] -> x + y
8− }
9
8
10data Tree = One Number | Some [Tree]
9data Tree = One Number | Some [Tree]
11
10
12test3 = \tree, sum -> case tree of
11test3 = \tree, sum -> case tree of
13− { One n -> n
12+ One n -> n
14− ; Some [n1, n2 : rest] -> test3(n1, sum) * 10 + test3(n2, sum) * 5 + sum(rest)
13+ Some [n1, n2 : rest] -> test3(n1, sum) * 10 + test3(n2, sum) * 5 + sum(rest)
15− }
16
14
examples/List.ps+6−9
@@ -6,19 +6,16 @@ data List a
6 }
6 }
7
7
8null = \l -> case l of
8null = \l -> case l of
9− { Nil -> true
9+ Nil -> true
10− ; _ -> false
10+ _ -> false
11− }
12
11
13concat = \l1 -> \l2 -> case l1 of
12concat = \l1 -> \l2 -> case l1 of
14− { Nil -> l2
13+ Nil -> l2
15− ; Cons x -> Cons(
14+ Cons x -> Cons(
16 { head: x.head
15 { head: x.head
17 , tail: concat(x.tail)(l2)
16 , tail: concat(x.tail)(l2)
18 })
17 })
19− }
20
18
21flatten = \xss -> case xss of
19flatten = \xss -> case xss of
22− { Nil -> Nil
20+ Nil -> Nil
23− ; Cons cons -> concat(cons.head)(flatten(cons.tail))
21+ Cons cons -> concat(cons.head)(flatten(cons.tail))
24− }
examples/Match.ps+1−1
@@ -1,3 +1,3 @@
1data Foo a = Foo
1data Foo a = Foo
2
2
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 @@
1data Maybe a = Nothing | Just a
1data Maybe a = Nothing | Just a
2
2
3maybe = \b -> \f -> \m -> case m of
3maybe = \b -> \f -> \m -> case m of
4− { Nothing -> b
4+ Nothing -> b
5− ; Just a -> f(a)
5+ Just a -> f(a)
6− }
7
6
8fmap = \m -> \f -> maybe(Nothing)(\a -> Just(f(a)))(m)
7fmap = \m -> \f -> maybe(Nothing)(\a -> Just(f(a)))(m)
9
8
examples/Patterns.ps+3−4
@@ -1,5 +1,4 @@
1test = \x -> case x of
1test = \x -> case x of
2− { { str = "Foo", bool = true } -> true
2+ { str = "Foo", bool = true } -> true
3− ; { str = "Bar", bool = b } -> b
3+ { str = "Bar", bool = b } -> b
4− ; _ -> false
4+ _ -> false
5− }
src/PureScript/Parser/Common.hs+7−1
@@ -59,10 +59,16 @@ opLetter = P.oneOf ":#$%&*+./<=>?@^|"
59whiteSpace :: I.IndentParser String u ()
59whiteSpace :: I.IndentParser String u ()
60whiteSpace = P.skipMany $ P.choice
60whiteSpace = P.skipMany $ P.choice
61 [ P.oneOf " \t" >> return ()
61 [ 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
63 , P.try lineComment
63 , P.try lineComment
64 , P.try blockComment ]
64 , P.try blockComment ]
65
65
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+
66lineComment :: I.IndentParser String u ()
72lineComment :: I.IndentParser String u ()
67lineComment = do
73lineComment = do
68 P.string "--"
74 P.string "--"
src/PureScript/Parser/Declarations.hs+1−1
@@ -65,7 +65,7 @@ parseDeclaration = P.choice $ map P.try
65 , parseExternDeclaration ]
65 , parseExternDeclaration ]
66
66
67parseManyDeclarations :: I.IndentParser String () [Declaration]
67parseManyDeclarations :: I.IndentParser String () [Declaration]
68−parseManyDeclarations = I.block $ I.withPos (parseDeclaration <* P.skipMany (P.oneOf "\r\n"))
68+parseManyDeclarations = I.withPos $ unindentedBlock parseDeclaration
69
69
70parseDeclarations :: String -> Either P.ParseError [Declaration]
70parseDeclarations :: String -> Either P.ParseError [Declaration]
71parseDeclarations = flip evalState (P.initialPos "Declarations") . P.runPT (parseManyDeclarations <* P.eof) () "Declarations"
71parseDeclarations = 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
73
73
74parseCase :: I.IndentParser String () Value
74parseCase :: I.IndentParser String () Value
75parseCase = Case <$> P.between (C.reserved "case") (C.reserved "of") parseValue
75parseCase = Case <$> P.between (C.reserved "case") (C.reserved "of") parseValue
76− <*> C.braces (C.semiSep parseCaseAlternative)
76+ <*> I.withPos (C.indentedBlock parseCaseAlternative)
77
77
78parseCaseAlternative :: I.IndentParser String () (Binder, Value)
78parseCaseAlternative :: I.IndentParser String () (Binder, Value)
79parseCaseAlternative = (,) <$> (parseBinder <* C.lexeme (P.string "->")) <*> parseValue
79parseCaseAlternative = (,) <$> (parseBinder <* C.lexeme (P.string "->")) <*> parseValue
80
80
81parseBlock :: I.IndentParser String () Value
81parseBlock :: I.IndentParser String () Value
82−parseBlock = Block <$> (C.braces $ P.many (parseStatement True))
82+parseBlock = Block <$> (C.braces $ I.withPos $ C.indentedBlock $ parseStatement True)
83
83
84parseValueAtom :: I.IndentParser String () Value
84parseValueAtom :: I.IndentParser String () Value
85parseValueAtom = P.choice $ map P.try
85parseValueAtom = P.choice $ map P.try
@@ -147,7 +147,7 @@ parseAssignment requireSemi = do
147 return $ Assignment tgt value
147 return $ Assignment tgt value
148
148
149parseManyStatements :: I.IndentParser String () [Statement]
149parseManyStatements :: I.IndentParser String () [Statement]
150−parseManyStatements = C.braces $ P.many (parseStatement True)
150+parseManyStatements = C.braces $ I.withPos $ C.indentedBlock $ parseStatement True
151
151
152parseWhile :: I.IndentParser String () Statement
152parseWhile :: I.IndentParser String () Statement
153parseWhile = While <$> (C.reserved "while" *> C.parens parseValue)
153parseWhile = While <$> (C.reserved "while" *> C.parens parseValue)