Nathan Ringo: 1 Adds import modifiers. 5 files changed, 52 insertions(+), 9 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~remexre/public-inbox/patches/25862/mbox | git am -3Learn more about email & git
--- bootstrap/Language/Sylvan/CST.hs | 16 +++++++++++++--- bootstrap/Language/Sylvan/CST/Pretty.hs | 16 ++++++++++++++-- bootstrap/Language/Sylvan/Parser.y | 13 +++++++++++-- bootstrap/Language/Sylvan/Parser/Lexer.x | 14 +++++++++++++- src/sylvan/driver/main.syl | 2 +- 5 files changed, 52 insertions(+), 9 deletions(-) diff --git a/bootstrap/Language/Sylvan/CST.hs b/bootstrap/Language/Sylvan/CST.hs index d35311c..cf16140 100644 --- a/bootstrap/Language/Sylvan/CST.hs @@ -10,7 +10,7 @@ import Data.ByteString (ByteString) import Data.ByteString qualified as BS import Data.ByteString.UTF8 qualified as BS import Data.Foldable (Foldable (..)) -import Data.Sequence (Seq) +import Data.Sequence (Seq (..)) data File = MkFile { file'moduleName :: ModuleName, @@ -20,11 +20,21 @@ data File = MkFile deriving (Show) data Import = MkImport - { import'moduleName :: ModuleName - -- TODO + { import'moduleName :: ModuleName, + import'modifiers :: Seq ImportModifier } deriving (Show) +addImportModifier :: Import -> ImportModifier -> Import +addImportModifier (MkImport moduleName modifiers) modifier = + MkImport moduleName (modifiers :|> modifier) + +data ImportModifier + = Using (Seq ByteString) + | Hiding (Seq ByteString) + | Renaming (Seq (ByteString, ByteString)) + deriving (Show) + data Decl where D'ForeignGlobal :: ByteString -> GenericDeclArgs -> Type -> ForeignItems -> Decl D'ForeignType :: ByteString -> GenericDeclArgs -> Kind -> ForeignItems -> Decl diff --git a/bootstrap/Language/Sylvan/CST/Pretty.hs b/bootstrap/Language/Sylvan/CST/Pretty.hs index 8d5dbf8..f43abeb 100644 --- a/bootstrap/Language/Sylvan/CST/Pretty.hs @@ -13,7 +13,7 @@ import Data.Foldable (Foldable (..)) import Data.Sequence (Seq (..)) import Data.Sequence qualified as Seq import Language.Sylvan.CST -import Prettyprinter (Doc, Pretty (..), angles, annotate, braces, colon, comma, dot, dquotes, emptyDoc, equals, group, hardline, hsep, line, line', nest, parens, punctuate, semi, softline, softline', (<+>)) +import Prettyprinter (Doc, Pretty (..), angles, annotate, braces, colon, comma, dot, dquotes, emptyDoc, equals, group, hardline, hsep, line, line', nest, parens, punctuate, semi, softline, softline', space, (<+>)) import Prettyprinter.Render.Terminal (AnsiStyle, Color (..), bold, color, colorDull) -- Nonterminals @@ -30,7 +30,19 @@ prettyFile (MkFile moduleName imports decls) = moduleLine <> importLines <> fold declLines = toList (prettyDecl <$> decls) prettyImport :: Import -> Doc AnsiStyle -prettyImport (MkImport moduleName) = kwd "import" <+> prettyModuleName moduleName <> semi <> hardline +prettyImport (MkImport moduleName modifiers) = + kwd "import" <+> prettyModuleName moduleName <> nest 2 (foldMap prettyImportModifier modifiers) + <> semi + <> hardline + +prettyImportModifier :: ImportModifier -> Doc AnsiStyle +prettyImportModifier (Using names) = + space <> kwd "using" <+> parens (manyComma (pretty <<< BS.toString) names) +prettyImportModifier (Hiding names) = + space <> kwd "hiding" <+> parens (manyComma (pretty <<< BS.toString) names) +prettyImportModifier (Renaming mappings) = + space <> kwd "renaming" + <+> parens (manyComma (pair (pretty <<< BS.toString) (kwd " to ") (pretty <<< BS.toString)) mappings) prettyDecl :: Decl -> Doc AnsiStyle prettyDecl (D'ForeignGlobal name genericArgs ty foreignItems) = diff --git a/bootstrap/Language/Sylvan/Parser.y b/bootstrap/Language/Sylvan/Parser.y index 1b97632..62e67ae 100644 --- a/bootstrap/Language/Sylvan/Parser.y @@ -38,17 +38,21 @@ import Language.Sylvan.Parser.Utils (uncheckedParseLName, uncheckedParseModuleNa '-' { Just Token'Minus } '*' { Just Token'Star } '->' { Just Token'Arrow } + 'fn' { Just Token'Fn } 'forall' { Just Token'Forall } 'foreign' { Just Token'Foreign } - 'fn' { Just Token'Fn } 'global' { Just Token'Global } + 'hiding' { Just Token'Hiding } 'import' { Just Token'Import } 'inh' { Just Token'Inh } 'module' { Just Token'Module } 'nt' { Just Token'NT } 'prod' { Just Token'Prod } + 'renaming' { Just Token'Renaming } 'syn' { Just Token'Syn } + 'to' { Just Token'To } 'type' { Just Token'Type } + 'using' { Just Token'Using } '(' { Just Token'LParen } ')' { Just Token'RParen } '{' { Just Token'LBrace } @@ -74,7 +78,12 @@ import Language.Sylvan.Parser.Utils (uncheckedParseLName, uncheckedParseModuleNa File :: { File } : 'module' ModuleName ';' many(Import) many(Decl) { MkFile $2 $4 $5 } Import :: { Import } - : 'import' ModuleName ';' { MkImport $2 } + : 'import' ModuleName many(ImportModifier) ';' { MkImport $2 $3 } + +ImportModifier :: { ImportModifier } + : 'using' '(' manyComma(ID_L) ')' { Using $3 } + | 'hiding' '(' manyComma(ID_L) ')' { Hiding $3 } + | 'renaming' '(' manyComma(pairSep(ID_L, 'to', ID_L)) ')' { Renaming $3 } Decl :: { Decl } : 'fn' ID_L OptGenericDeclArgs '(' DeclFnArgs ')' ':' Type '=' Expr ';' diff --git a/bootstrap/Language/Sylvan/Parser/Lexer.x b/bootstrap/Language/Sylvan/Parser/Lexer.x index e02d7a4..9e138e7 100644 --- a/bootstrap/Language/Sylvan/Parser/Lexer.x @@ -21,17 +21,21 @@ tokens :- "-" { const Token'Minus } "*" { const Token'Star } "->" { const Token'Arrow } + "fn" { const Token'Fn } "forall" { const Token'Forall } "foreign" { const Token'Foreign } - "fn" { const Token'Fn } "global" { const Token'Global } + "hiding" { const Token'Hiding } "import" { const Token'Import } "inh" { const Token'Inh } "module" { const Token'Module } "nt" { const Token'NT } "prod" { const Token'Prod } + "renaming" { const Token'Renaming } "syn" { const Token'Syn } + "to" { const Token'To } "type" { const Token'Type } + "using" { const Token'Using } "(" { const Token'LParen } ")" { const Token'RParen } "{" { const Token'LBrace } @@ -62,13 +66,17 @@ data Token | Token'Foreign | Token'Fn | Token'Global + | Token'Hiding | Token'Import | Token'Inh | Token'Module | Token'NT | Token'Prod + | Token'Renaming | Token'Syn + | Token'To | Token'Type + | Token'Using | Token'LParen | Token'RParen | Token'LBrace @@ -97,13 +105,17 @@ instance Show Token where show Token'Foreign = "\"foreign\"" show Token'Fn = "\"fn\"" show Token'Global = "\"global\"" + show Token'Hiding = "\"hiding\"" show Token'Import = "\"import\"" show Token'Inh = "\"inh\"" show Token'Module = "\"module\"" show Token'NT = "\"nt\"" show Token'Prod = "\"prod\"" + show Token'Renaming = "\"renaming\"" show Token'Syn = "\"syn\"" + show Token'To = "\"to\"" show Token'Type = "\"type\"" + show Token'Using = "\"using\"" show Token'LParen = "\"(\"" show Token'RParen = "\")\"" show Token'LBrace = "\"{\"" diff --git a/src/sylvan/driver/main.syl b/src/sylvan/driver/main.syl index 66069bf..9eb4cf6 100644 --- a/src/sylvan/driver/main.syl +++ b/src/sylvan/driver/main.syl @@ -1,6 +1,6 @@ module sylvan::driver; -import std::prelude; +import std::prelude hiding (foo) renaming (bar to foo) using (baz, asdf); import std::data::set; fn main(ioIn: IOToken): IOToken = print(ioIn, "Hello, world!"); -- 2.33.0