git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix postfix operator precedence
commitcb44ac240f08e8e1622c7ea818291aa801ce9b62 authorPhil Freeman <paf31@cantab.net> dateSun 20 Oct 2013, 17:58 (13 years ago) parent5cf8fbf
5 files changed+16−14
examples/Objects.ps+2−2 examples/Operators.ps+1−1 examples/TypeSynonyms.ps+4−4 src/PureScript/Parser/Common.hs+3 src/PureScript/Parser/Values.hs+6−7
examples/Objects.ps+2−2
@@ -4,6 +4,6 @@ append = \o -> { foo: o.foo, bar: 1 }
44
55apTest = append({foo : "Foo", baz: "Baz"})
66
7−f = (\a -> (a.b).c)({ b: { c: 1, d: "Hello" }, e: "World" })
7+f = (\a -> a.b.c)({ b: { c: 1, d: "Hello" }, e: "World" })
88
9−g = (\a -> (a.f)({ x: 1, y: "y" }))({ f: \o -> o.x + 1 })
9+g = (\a -> a.f({ x: 1, y: "y" }))({ f: \o -> o.x + 1 })
examples/Operators.ps+1−1
@@ -1,6 +1,6 @@
11test1 = \x, y, z -> x * y + z(x)(y)
22
3−test2 = (\x -> (x.foo)(false))({ foo : \_ -> 1 })
3+test2 = (\x -> x.foo(false))({ foo : \_ -> 1 })
44
55test3 = (\x, y -> x)(1 + 2 * (1 + 2), true && (false || false))
66
examples/TypeSynonyms.ps+4−4
@@ -5,9 +5,9 @@ type Lens a b =
55
66composeLenses :: forall a b c. (Lens a b) -> (Lens b c) -> Lens a c
77composeLenses = \l1 -> \l2 ->
8− { get: \a -> (l2.get)((l1.get)(a))
8+ { get: \a -> l2.get(l1.get(a))
99 , set: \a -> \c -> do
10− var b = (l1.get)(a)
11− var b1 = (l2.set)(b)(c)
12− return (l1.set)(a)(b1)
10+ var b = l1.get(a)
11+ var b1 = l2.set(b)(c)
12+ return l1.set(a)(b1)
1313 }
src/PureScript/Parser/Common.hs+3
@@ -138,6 +138,9 @@ fold first more combine = do
138138 bs <- P.many more
139139 return $ foldl combine a bs
140140
141+buildPostfixParser :: P.Stream s m t => [P.ParsecT s u m (a -> a)] -> P.ParsecT s u m a -> P.ParsecT s u m a
142+buildPostfixParser f x = fold x (P.choice (map P.try f)) (flip ($))
143+
141144parseIdent :: P.Parsec String u Ident
142145parseIdent = (Ident <$> identifier) <|> (Op <$> parens operator)
143146
src/PureScript/Parser/Values.hs+6−7
@@ -95,14 +95,13 @@ parseValueAtom = C.indented *> P.choice (map P.try
9595 , C.parens parseValue ])
9696
9797parseValue :: P.Parsec String P.Column Value
98−parseValue = buildExpressionParser operators $ C.fold (C.lexeme typedValue) (C.lexeme funArgs) App
98+parseValue = buildExpressionParser operators . C.buildPostfixParser postfixTable $ parseValueAtom
9999 where
100− typedValue = C.augment parseValueAtom parseTypeAnnotation TypedValue
101− funArgs = C.indented *> C.parens (parseValue `P.sepBy` (C.indented *> C.comma))
102− parseTypeAnnotation = P.try $ C.lexeme (C.indented *> P.string "::") *> parsePolyType
103− operators = [ [ Postfix $ Accessor <$> (C.indented *> C.dot *> C.indented *> C.identifier)
104− , Postfix $ Indexer <$> (C.indented *> C.squares parseValue) ]
105− , [ Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "!") >> return (Unary Not)
100+ postfixTable = [ Accessor <$> (C.indented *> C.dot *> C.indented *> C.identifier)
101+ , Indexer <$> (C.indented *> C.squares parseValue)
102+ , flip App <$> (C.indented *> C.parens (parseValue `P.sepBy` (C.indented *> C.comma)))
103+ , flip TypedValue <$> (P.try $ C.lexeme (C.indented *> P.string "::") *> parsePolyType) ]
104+ operators = [ [ Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "!") >> return (Unary Not)
106105 , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "~") >> return (Unary BitwiseNot)
107106 , Prefix $ C.lexeme (P.try $ C.indented *> C.reservedOp "-") >> return (Unary Negate) ]
108107 , [ Infix (C.lexeme (P.try (C.indented *> C.parseIdentInfix) >>= \ident -> return $ \t1 t2 -> App (App (Var ident) [t1]) [t2])) AssocRight ]