Pattern/expression ambiguity resolution
authorVladislav Zavialov <vlad.z.4096@gmail.com>
Tue, 23 Apr 2019 18:21:33 +0000 (21:21 +0300)
committerÖmer Sinan Ağacan <omeragacan@gmail.com>
Fri, 3 May 2019 18:54:50 +0000 (21:54 +0300)
commit52fc2719b93ab39be3e52eba531ee173b9134183
tree2ee2a341d5cc747707765ecf8695795a4ca0eb4b
parent8f929388c4b79b82a6e7772720d785f3cbc1f3c1
Pattern/expression ambiguity resolution

This patch removes 'EWildPat', 'EAsPat', 'EViewPat', and 'ELazyPat'
from 'HsExpr' by using the ambiguity resolution system introduced
earlier for the command/expression ambiguity.

Problem: there are places in the grammar where we do not know whether we
are parsing an expression or a pattern, for example:

do { Con a b <- x } -- 'Con a b' is a pattern
do { Con a b }      -- 'Con a b' is an expression

Until we encounter binding syntax (<-) we don't know whether to parse
'Con a b' as an expression or a pattern.

The old solution was to parse as HsExpr always, and rejig later:

checkPattern :: LHsExpr GhcPs -> P (LPat GhcPs)

This meant polluting 'HsExpr' with pattern-related constructors. In
other words, limitations of the parser were affecting the AST, and all
other code (the renamer, the typechecker) had to deal with these extra
constructors.

We fix this abstraction leak by parsing into an overloaded
representation:

class DisambECP b where ...
newtype ECP = ECP { runECP_PV :: forall b. DisambECP b => PV (Located b) }

See Note [Ambiguous syntactic categories] for details.

Now the intricacies of parsing have no effect on the hsSyn AST when it
comes to the expression/pattern ambiguity.
48 files changed:
compiler/deSugar/DsExpr.hs
compiler/hieFile/HieAst.hs
compiler/hsSyn/HsExpr.hs
compiler/hsSyn/HsExtension.hs
compiler/parser/Lexer.x
compiler/parser/Parser.y
compiler/parser/RdrHsSyn.hs
compiler/rename/RnExpr.hs
compiler/typecheck/TcRnTypes.hs
testsuite/tests/parser/should_fail/InfixAppPatErr.stderr
testsuite/tests/parser/should_fail/T984.stderr
testsuite/tests/parser/should_fail/all.T
testsuite/tests/parser/should_fail/cmdFail001.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail001.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail002.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail002.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail003.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail003.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail004.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail004.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail005.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail005.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail006.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail006.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail007.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail007.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail008.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail008.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail009.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/cmdFail009.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail001.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail001.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail002.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail002.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail003.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail003.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail004.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail004.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail005.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail005.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail006.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail006.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail007.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail007.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail008.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail008.stderr [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail009.hs [new file with mode: 0644]
testsuite/tests/parser/should_fail/patFail009.stderr [new file with mode: 0644]