aihc-parser 6.0.0.0 → 7.0.0.0
raw patch · 20 files changed
+152/−20 lines, 20 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Aihc.Parser.Internal.Testing: TkKeywordStatic :: LexTokenKind
+ Aihc.Parser.Syntax: EStatic :: Expr -> Expr
+ Aihc.Parser.Token: TkKeywordStatic :: LexTokenKind
Files
- CHANGELOG.md +14/−0
- aihc-parser.cabal +1/−1
- src/Aihc/Parser/Internal/CheckPattern.hs +1/−0
- src/Aihc/Parser/Internal/Common.hs +1/−0
- src/Aihc/Parser/Internal/Expr.hs +24/−1
- src/Aihc/Parser/Lex.hs +1/−0
- src/Aihc/Parser/Lex/Types.hs +1/−0
- src/Aihc/Parser/Parens.hs +51/−18
- src/Aihc/Parser/Pretty.hs +2/−0
- src/Aihc/Parser/Shorthand.hs +2/−0
- src/Aihc/Parser/Syntax.hs +2/−0
- test/Test/Fixtures/golden/expr/static-pointers-application.yaml +6/−0
- test/Test/Fixtures/golden/expr/static-pointers-argument.yaml +6/−0
- test/Test/Fixtures/lexer/core/static-identifier-without-extension.yaml +7/−0
- test/Test/Fixtures/lexer/core/static-keyword-with-extension.yaml +8/−0
- test/Test/Fixtures/oracle/StaticPointers/static-forms.hs +14/−0
- test/Test/Fixtures/oracle/StaticPointers/static-identifier-without-extension.hs +7/−0
- test/Test/Properties/Arb/Expr.hs +2/−0
- test/Test/Properties/Arb/Utils.hs +1/−0
- test/Test/Properties/NoExceptions.hs +1/−0
CHANGELOG.md view
@@ -6,6 +6,20 @@ ## [Unreleased] +## [7.0.0.0] - 2026-11-27++### Added++- The parser accepts the `static e` form when the `StaticPointers`+ extension is on. The new `EStatic` constructor of `Expr` holds the+ operand. A function that matches on all `Expr` constructors must add a+ case for `EStatic`.+- With `StaticPointers` on, `static` is a keyword. The lexer gives the new+ `TkKeywordStatic` token. Without the extension, `static` is an ordinary+ identifier.+- As in GHC, `static` takes one atomic operand at the start of an+ application. Thus `static f x` is `(static f) x`.+ ## [6.0.0.0] - 2026-11-12 ### Added
aihc-parser.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.8 name: aihc-parser-version: 6.0.0.0+version: 7.0.0.0 build-type: Simple license: Unlicense license-file: LICENSE
src/Aihc/Parser/Internal/CheckPattern.hs view
@@ -149,6 +149,7 @@ ETHTypeNameQuote {} -> Left "unexpected Template Haskell type name quote in pattern" ETHTypedSplice {} -> Left "unexpected typed Template Haskell splice in pattern" EProc {} -> Left "unexpected proc expression in pattern"+ EStatic {} -> Left "unexpected static expression in pattern" where checkPattern' = checkPatternWith nPlusK
src/Aihc/Parser/Internal/Common.hs view
@@ -383,6 +383,7 @@ TkKeywordThen -> "keyword 'then'" TkKeywordElse -> "keyword 'else'" TkKeywordProc -> "keyword 'proc'"+ TkKeywordStatic -> "keyword 'static'" TkKeywordPattern -> "keyword 'pattern'" TkKeywordRec -> "keyword 'rec'" TkKeywordBy -> "keyword 'by'"
src/Aihc/Parser/Internal/Expr.hs view
@@ -407,6 +407,13 @@ -- | Report core: -- -- > fexp -> [fexp] aexp+--+-- The StaticPointers extension adds one more form at the start of an+-- application, as in GHC:+--+-- > fexp -> 'static' aexp+--+-- Thus @static f x@ is @(static f) x@. appExprParser :: TokParser Expr appExprParser = appExprParserWith atomOrRecordExprParser @@ -418,7 +425,7 @@ let !startInput = MP.stateInput startState !startOffset = MP.stateOffset startState !inputStart = MP.pstateSourcePos (MP.statePosState startState)- first <- atomParser+ first <- headParser rest <- MP.many appArg case rest of [] -> pure first@@ -429,6 +436,22 @@ appSpan = consumedSpan inputStart startInput startOffset endInput endOffset pure (EAnn (mkSpanAnnotation appSpan) (foldl applyArg first rest)) where+ -- The lexer gives 'TkKeywordStatic' only when StaticPointers is on.+ headParser :: TokParser Expr+ headParser = do+ tok <- peekToken+ case lexTokenKind tok of+ TkKeywordStatic -> staticExprParser+ _ -> atomParser++ -- GHC does not check BlockArguments for the operand of @static@. Thus a+ -- block form such as @static do ...@ is always a valid operand.+ staticExprParser :: TokParser Expr+ staticExprParser = withSpanAnn (EAnn . mkSpanAnnotation) $ do+ expectedTok TkKeywordStatic+ tok <- peekToken+ EStatic <$> fromMaybe appExprArgParser (lexpBlockParserForToken NormalExprAtom (lexTokenKind tok))+ appArg :: TokParser (Either Type Expr) appArg = (Left <$> typeAppArg) <|> (Right <$> appExprArgParser)
src/Aihc/Parser/Lex.hs view
@@ -1124,6 +1124,7 @@ 6 -> case txt of "forall" -> Just TkKeywordForall "import" -> Just TkKeywordImport+ "static" | memberExtension StaticPointers exts -> Just TkKeywordStatic "infixl" -> Just TkKeywordInfixl "infixr" -> Just TkKeywordInfixr "module" -> Just TkKeywordModule
src/Aihc/Parser/Lex/Types.hs view
@@ -86,6 +86,7 @@ | TkKeywordUnderscore | -- Extension-conditional keywords TkKeywordProc+ | TkKeywordStatic | TkKeywordRec | TkKeywordMdo | TkKeywordPattern
src/Aihc/Parser/Parens.hs view
@@ -32,7 +32,7 @@ where import Aihc.Parser.Syntax-import Data.Bifunctor (bimap)+import Data.Bifunctor (bimap, first) import Data.Char (isHexDigit) import Data.Maybe (isJust, isNothing) import Data.Text qualified as T@@ -109,6 +109,8 @@ EProc {} -> True EApp _ arg | isBlockExpr arg -> isOpenEnded arg EApp _ arg -> isGreedyExpr arg+ EStatic arg | isBlockExpr arg -> isOpenEnded arg+ EStatic arg -> isGreedyExpr arg _ -> False -- | Check if an expression is "open-ended" - its rightmost component can@@ -125,6 +127,7 @@ ENegate inner -> isOpenEnded inner EInfix _ _ rhs -> isOpenEnded rhs EApp _ arg | isBlockExpr arg -> isOpenEnded arg+ EStatic arg | isBlockExpr arg -> isOpenEnded arg _ -> False -- | Does the pretty-printed form of an expression end with @:: Type@?@@ -141,6 +144,7 @@ ENegate inner -> endsWithTypeSig inner EInfix _ _ rhs -> endsWithTypeSig rhs EApp _ arg -> endsWithTypeSig arg+ EStatic arg -> endsWithTypeSig arg EIf _ _ no -> endsWithTypeSig no _ -> False @@ -545,6 +549,7 @@ GuardArrow -> typeSigNeedsParens ty GuardEquals -> False EApp _ arg | isBlockExpr arg -> guardExprNeedsParensWith typeSigNeedsParens arrow arg+ EStatic arg | isBlockExpr arg -> guardExprNeedsParensWith typeSigNeedsParens arrow arg expr -> case arrow of GuardArrow -> trailingTypeSigNeedsParens typeSigNeedsParens expr@@ -559,6 +564,7 @@ ELambdaPats _ body -> trailingTypeSigNeedsParens typeSigNeedsParens body EInfix _ _ rhs -> trailingTypeSigNeedsParens typeSigNeedsParens rhs EApp _ arg -> trailingTypeSigNeedsParens typeSigNeedsParens arg+ EStatic arg -> trailingTypeSigNeedsParens typeSigNeedsParens arg EIf _ _ no -> trailingTypeSigNeedsParens typeSigNeedsParens no _ -> False @@ -1196,6 +1202,10 @@ EUnboxedSum altIdx arity inner -> EUnboxedSum altIdx arity (addDelimitedExprParens inner) EProc pat body -> wrapExpr (prec > 0) (EProc (addArrowBndrPatternParens pat) (addCmdParensIn CtxCmdTop body))+ -- GHC parses @static@ at the application level (@fexp -> 'static' aexp@).+ -- Thus @static f x@ is @(static f) x@, and the operand obeys the rules of+ -- the first argument of an application.+ EStatic {} -> addAppsChainPrec prec expr EPragma pragma inner -> -- EPragma is transparent w.r.t. precedence: wrapping decisions are made -- by the outer context via needsExprParens, not by a self-prec check.@@ -1211,22 +1221,41 @@ addAppsChainPrec :: Int -> Expr -> Expr addAppsChainPrec prec expr = let (root, args) = flattenApps expr- root' = addExprParensIn CtxAppFun root- nArgs = length args- args' =- [ let isLast = i == nArgs - 1- canStayBare = isBlockExpr a && not (endsWithTypeSig a) && (isLast || not (isOpenEnded a))- ctx- | canStayBare = CtxAppArgNoParens- | isLast = CtxAppArg- | isGreedyExpr a = CtxAppArgGreedy- | otherwise = CtxAppArg- in addExprParensIn ctx a- | (i, a) <- Prelude.zip [0 :: Int ..] args- ]- rebuilt = foldl EApp root' args'+ rebuilt = withStaticRoot root args (addExprParensIn CtxAppFun) addArgs in wrapExpr (prec > 2) rebuilt+ where+ addArgs args =+ let nArgs = length args+ in [ let isLast = i == nArgs - 1+ canStayBare = isBlockExpr a && not (endsWithTypeSig a) && (isLast || not (isOpenEnded a))+ ctx+ | canStayBare = CtxAppArgNoParens+ | isLast = CtxAppArg+ | isGreedyExpr a = CtxAppArgGreedy+ | otherwise = CtxAppArg+ in addExprParensIn ctx a+ | (i, a) <- Prelude.zip [0 :: Int ..] args+ ] +-- | Rebuild an application chain from its root and its arguments.+--+-- A @static e@ root is like an application: its operand @e@ is the first+-- argument of the chain. Thus @static e@ and the arguments that follow it get+-- the same parentheses as the arguments of a function.+withStaticRoot :: Expr -> [Expr] -> (Expr -> Expr) -> ([Expr] -> [Expr]) -> Expr+withStaticRoot root args addRoot addArgs =+ case splitStaticRoot root of+ Just (rebuildRoot, body) ->+ case addArgs (body : args) of+ body' : args' -> foldl EApp (rebuildRoot body') args'+ [] -> foldl EApp (rebuildRoot body) args+ Nothing -> foldl EApp (addRoot root) (addArgs args)+ where+ splitStaticRoot = \case+ EAnn ann sub -> first (EAnn ann .) <$> splitStaticRoot sub+ EStatic body -> Just (EStatic, body)+ _ -> Nothing+ addSpliceBodyParens :: Expr -> Expr addSpliceBodyParens body = case body of@@ -1413,6 +1442,7 @@ EParen {} -> False EInfix _ _ rhs -> needsCompTransformInfixRhsParens rhs EApp _ arg -> needsCompTransformParens arg+ EStatic arg -> needsCompTransformParens arg ETypeApp fn _ -> needsCompTransformParens fn ENegate inner -> needsCompTransformParens inner EPragma _ inner -> needsCompTransformParens inner@@ -1472,6 +1502,7 @@ EProc {} -> True ENegate inner -> absorbsFollowingInfix inner EApp _ arg | isBlockExpr arg -> absorbsFollowingInfix arg+ EStatic arg | isBlockExpr arg -> absorbsFollowingInfix arg EInfix _ _ rhs -> absorbsFollowingInfix rhs _ -> False @@ -1846,6 +1877,7 @@ EAnn ann sub -> EAnn ann (addViewExprBodyParens sub) EInfix {} -> addViewExprInfixParens e EApp {} -> addViewExprAppChainParens e+ EStatic {} -> addViewExprAppChainParens e _ -> addExprParens e addViewExprInfixParens e =@@ -1874,9 +1906,8 @@ addViewExprAppChainParens e = let (root, args) = flattenApps e- root' = addExprParensIn CtxAppFun root- args' = [addExprParensIn (viewExprAppArgCtx isLast arg) arg | (isLast, arg) <- markLast args]- in foldl EApp root' args'+ addArgs = map (\(isLast, arg) -> addExprParensIn (viewExprAppArgCtx isLast arg) arg) . markLast+ in withStaticRoot root args (addExprParensIn CtxAppFun) addArgs viewExprAppArgCtx isLast arg | viewExprAppArgCanStayBare isLast arg = CtxAppArgNoParens@@ -2181,6 +2212,7 @@ ENegate inner -> endsWithCmdLayoutTail inner EPragma _ inner -> endsWithCmdLayoutTail inner EApp _ arg -> endsWithCmdLayoutBlock arg+ EStatic arg -> endsWithCmdLayoutBlock arg _ -> False endsWithCmdLayoutBlock :: Expr -> Bool@@ -2190,6 +2222,7 @@ ELambdaCase {} -> True ELambdaCases {} -> True EApp _ arg -> endsWithCmdLayoutBlock arg+ EStatic arg -> endsWithCmdLayoutBlock arg _ -> False addCmdArrAppRhsParens :: CmdCtx -> Expr -> Expr
src/Aihc/Parser/Pretty.hs view
@@ -1362,6 +1362,7 @@ in hsep ["(#", prettyBarSeparated slots, "#)"] EProc pat body -> "proc" <+> prettyPattern pat <+> "->" <+> prettyCmd body+ EStatic body -> "static" <+> prettyExpr body EPragma pragma inner -> prettyPragma pragma <+> prettyExpr inner EAnn _ sub -> prettyExpr sub@@ -1400,6 +1401,7 @@ ELambdaPats _ body -> endsWithLayoutBlock body EIf _ _ no -> endsWithLayoutBlock no EApp _ arg -> endsWithLayoutBlock arg+ EStatic arg -> endsWithLayoutBlock arg EInfix _ _ rhs -> endsWithLayoutBlock rhs ENegate inner -> endsWithLayoutBlock inner EProc {} -> True
src/Aihc/Parser/Shorthand.hs view
@@ -947,6 +947,7 @@ ETypeApp inner ty -> "ETypeApp" <+> parens (docExpr inner) <+> parens (docType ty) EApp f x -> "EApp" <+> parens (docExpr f) <+> parens (docExpr x) EProc pat body -> "EProc" <+> parens (docPattern pat) <+> parens (docCmd body)+ EStatic body -> "EStatic" <+> parens (docExpr body) EPragma pragma inner -> "EPragma" <+> parens (docPragma pragma) <+> parens (docExpr inner) EAnn _ sub -> docExpr sub @@ -1086,6 +1087,7 @@ TkKeywordWhere -> "TkKeywordWhere" TkKeywordUnderscore -> "TkKeywordUnderscore" TkKeywordProc -> "TkKeywordProc"+ TkKeywordStatic -> "TkKeywordStatic" TkKeywordPattern -> "TkKeywordPattern" TkKeywordRec -> "TkKeywordRec" TkKeywordMdo -> "TkKeywordMdo"
src/Aihc/Parser/Syntax.hs view
@@ -2292,6 +2292,8 @@ -- | @proc pat -> cmd@ EProc Pattern Cmd+ | -- | @static e@ (StaticPointers extension)+ EStatic Expr | -- | @{-# SCC foo #-} expr@ EPragma Pragma Expr deriving (Data, Eq, Show, Generic, NFData)
+ test/Test/Fixtures/golden/expr/static-pointers-application.yaml view
@@ -0,0 +1,6 @@+extensions: [StaticPointers]+input: |+ static f x+ast: |-+ EApp (EStatic (EVar "f")) (EVar "x")+status: pass
+ test/Test/Fixtures/golden/expr/static-pointers-argument.yaml view
@@ -0,0 +1,6 @@+extensions: [StaticPointers]+input: |+ g (static (f . h)) <$$> y+ast: |-+ EInfix (EApp (EVar "g") (EParen (EStatic (EParen (EInfix (EVar "f") "." (EVar "h")))))) "<$$>" (EVar "y")+status: pass
+ test/Test/Fixtures/lexer/core/static-identifier-without-extension.yaml view
@@ -0,0 +1,7 @@+extensions: []+input: "static f"+tokens:+ - 'TkVarId "static"'+ - 'TkVarId "f"'+ - 'TkEOF'+status: pass
+ test/Test/Fixtures/lexer/core/static-keyword-with-extension.yaml view
@@ -0,0 +1,8 @@+extensions:+ - StaticPointers+input: "static f"+tokens:+ - 'TkKeywordStatic'+ - 'TkVarId "f"'+ - 'TkEOF'+status: pass
+ test/Test/Fixtures/oracle/StaticPointers/static-forms.hs view
@@ -0,0 +1,14 @@+{- ORACLE_TEST pass -}+{-# LANGUAGE StaticPointers #-}+module StaticForms where++import GHC.StaticPtr++ref1 = static 'a'+ref2 = static inc+ref3 = static (inc 1)+ref4 = static (rootRoles . root) <$$> trusted+ref5 = deRefStaticPtr (static inc) 1+ref6 = static inc `seq` ()+ref7 = static Just @Int+ref8 = fmap (static f <$$>) xs
+ test/Test/Fixtures/oracle/StaticPointers/static-identifier-without-extension.hs view
@@ -0,0 +1,7 @@+{- ORACLE_TEST pass -}+module StaticIdentifierWithoutExtension where++static :: Int -> Int+static x = x + 1++y = static 1
test/Test/Properties/Arb/Expr.hs view
@@ -77,6 +77,7 @@ EParen . ETypeSyntax TypeSyntaxExplicitNamespace <$> genTypeSyntaxType, EParen <$> genExpr, EProc <$> genPattern <*> genCmdWith,+ EStatic <$> genExpr, EParen <$> (EPragma <$> genSccPragma <*> genPragmaExpr), -- OverloadedRecordDot EGetField <$> genExpr <*> genRecordFieldName,@@ -601,6 +602,7 @@ EProc pat body -> [EProc pat' body | pat' <- shrinkPattern pat] <> [EProc pat body' | body' <- shrinkCmd body]+ EStatic body -> body : [EStatic body' | body' <- shrinkExpr body] EAnn _ sub -> shrinkExpr sub shrinkCmd :: Cmd -> [Cmd]
test/Test/Properties/Arb/Utils.hs view
@@ -65,6 +65,7 @@ QuasiQuotes, RecursiveDo, RequiredTypeArguments,+ StaticPointers, TemplateHaskell, TransformListComp, TupleSections,
test/Test/Properties/NoExceptions.hs view
@@ -160,6 +160,7 @@ pure TkKeywordType, pure TkKeywordUnderscore, pure TkKeywordProc,+ pure TkKeywordStatic, pure TkKeywordRec, pure TkKeywordMdo, pure TkKeywordPattern,