Skip to content
  • Vladislav Zavialov's avatar
    Pattern/expression ambiguity resolution · 52fc2719
    Vladislav Zavialov authored and Ömer Sinan Ağacan's avatar Ömer Sinan Ağacan committed
    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.
    52fc2719