git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1619 from purescript/1090

Fix #1090, allow accessors in operator sections

commitf1c330f6a5eb374676219a94f960b19ad1370b3f authorPhil Freeman <paf31@cantab.net> dateMon 9 Nov 2015, 10:20 (11 years ago) parent6bedebe7b4222c
2 files changed+19−9
examples/passing/OperatorSections.purs+6 src/Language/PureScript/Parser/Declarations.hs+13−9
examples/passing/OperatorSections.purs+6
@@ -8,4 +8,10 @@ main = do
88 assert $ (2.0 /) 4.0 == 0.5
99 assert $ (`const` 1.0) 2.0 == 2.0
1010 assert $ (1.0 `const`) 2.0 == 1.0
11+ let foo = { x: 2.0 }
12+ assert $ (/ foo.x) 4.0 == 2.0
13+ assert $ (foo.x /) 4.0 == 0.5
14+ let (//) x y = x.x / y.x
15+ assert $ (// foo { x = 4.0 }) { x: 4.0 } == 1.0
16+ assert $ (foo { x = 4.0 } //) { x: 4.0 } == 1.0
1117 Control.Monad.Eff.Console.log "Done!"
src/Language/PureScript/Parser/Declarations.hs+13−9
@@ -414,8 +414,8 @@ parseInfixExpr = P.between tick tick parseValue
414414parseOperatorSection :: TokenParser Expr
415415parseOperatorSection = parens $ left <|> right
416416 where
417− right = OperatorSection <$> parseInfixExpr <* indented <*> (Right <$> parseValueAtom)
418− left = flip OperatorSection <$> (Left <$> parseValueAtom) <* indented <*> parseInfixExpr
417+ right = OperatorSection <$> parseInfixExpr <* indented <*> (Right <$> indexersAndAccessors)
418+ left = flip OperatorSection <$> (Left <$> indexersAndAccessors) <* indented <*> parseInfixExpr
419419
420420parsePropertyUpdate :: TokenParser (String, Maybe Expr)
421421parsePropertyUpdate = do
@@ -449,21 +449,25 @@ parseDoNotationElement = P.choice
449449parseObjectGetter :: TokenParser Expr
450450parseObjectGetter = ObjectGetter <$> (underscore *> C.indented *> dot *> C.indented *> (lname <|> stringLiteral))
451451
452+-- | Expressions including indexers and record updates
453+indexersAndAccessors :: TokenParser Expr
454+indexersAndAccessors = C.buildPostfixParser postfixTable parseValueAtom
455+ where
456+ postfixTable = [ parseAccessor
457+ , P.try . parseUpdaterBody . Just ]
458+
452459-- |
453460-- Parse a value
454461--
455462parseValue :: TokenParser Expr
456463parseValue = withSourceSpan PositionedValue
457464 (P.buildExpressionParser operators
458− . C.buildPostfixParser postfixTable2
465+ . C.buildPostfixParser postfixTable
459466 $ indexersAndAccessors) P.<?> "expression"
460467 where
461− indexersAndAccessors = C.buildPostfixParser postfixTable1 parseValueAtom
462− postfixTable1 = [ parseAccessor
463− , P.try . parseUpdaterBody . Just ]
464− postfixTable2 = [ \v -> P.try (flip App <$> (C.indented *> indexersAndAccessors)) <*> pure v
465− , \v -> flip (TypedValue True) <$> (P.try (C.indented *> doubleColon) *> parsePolyType) <*> pure v
466− ]
468+ postfixTable = [ \v -> P.try (flip App <$> (C.indented *> indexersAndAccessors)) <*> pure v
469+ , \v -> flip (TypedValue True) <$> (P.try (C.indented *> doubleColon) *> parsePolyType) <*> pure v
470+ ]
467471 operators = [ [ P.Prefix (P.try (C.indented *> symbol' "-") >> return UnaryMinus)
468472 ]
469473 , [ P.Infix (P.try (C.indented *> parseInfixExpr P.<?> "infix expression") >>= \ident ->