1
1
mirror of https://github.com/github/semantic.git synced 2024-12-22 22:31:36 +03:00

Wildcard imports

This commit is contained in:
Timothy Clem 2018-03-01 14:50:54 -08:00
parent ad225f7d89
commit 5e9a9b33a5
2 changed files with 21 additions and 7 deletions

View File

@ -272,15 +272,12 @@ instance Eq1 Import where liftEq = genericLiftEq
instance Ord1 Import where liftCompare = genericLiftCompare instance Ord1 Import where liftCompare = genericLiftCompare
instance Show1 Import where liftShowsPrec = genericLiftShowsPrec instance Show1 Import where liftShowsPrec = genericLiftShowsPrec
instance ( Show l instance ( Semigroup (Cell l (Value l t))
, Show t
, Semigroup (Cell l (Value l t)) -- lookupOrAlloc
, Members (Evaluating (Value l t)) es , Members (Evaluating (Value l t)) es
, Addressable l es , Addressable l es
, Evaluatable es t (Value l t) (Base t) , Evaluatable es t (Value l t) (Base t)
, Recursive t , Recursive t
, FreeVariables t , FreeVariables t
-- , HasField fields Range
) )
=> Evaluatable es t (Value l t) Import where => Evaluatable es t (Value l t) Import where
eval (Import from alias _) = do eval (Import from alias _) = do
@ -294,13 +291,28 @@ instance ( Show l
-- Restore previous global environment, adding the imported env -- Restore previous global environment, adding the imported env
(name, addr) <- lookupOrAlloc' (qualifiedName (subterm alias)) interface env (name, addr) <- lookupOrAlloc' (qualifiedName (subterm alias)) interface env
modify (const (envInsert name addr env)) -- const is important—we are throwing away the modified global env and specifically crafting a new environment. modify (const (envInsert name addr env)) -- const is important. We are throwing away the modified global env and specifically crafting a new one.
pure interface pure interface
instance Member Fail es => Evaluatable es t Type.Type Import instance Member Fail es => Evaluatable es t Type.Type Import
-- | A wildcard import
data WildcardImport a = WildcardImport { wildcardImportFrom :: !a, wildcardImportSymbol :: !a }
deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable, FreeVariables1)
instance Eq1 WildcardImport where liftEq = genericLiftEq
instance Ord1 WildcardImport where liftCompare = genericLiftCompare
instance Show1 WildcardImport where liftShowsPrec = genericLiftShowsPrec
instance ( Members (Evaluating v) es
, FreeVariables t
)
=> Evaluatable es t v WildcardImport where
eval (WildcardImport from _) = require @v (qualifiedName (subterm from))
-- | An imported symbol -- | An imported symbol
data ImportSymbol a = ImportSymbol { importSymbolName :: !a, importSymbolAlias :: !a } data ImportSymbol a = ImportSymbol { importSymbolName :: !a, importSymbolAlias :: !a }
deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable, FreeVariables1) deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable, FreeVariables1)

View File

@ -37,6 +37,7 @@ type Syntax =
, Declaration.Import , Declaration.Import
, Declaration.ImportSymbol , Declaration.ImportSymbol
, Declaration.Variable , Declaration.Variable
, Declaration.WildcardImport
, Expression.Arithmetic , Expression.Arithmetic
, Expression.Boolean , Expression.Boolean
, Expression.Bitwise , Expression.Bitwise
@ -374,11 +375,12 @@ comment = makeTerm <$> symbol Comment <*> (Comment.Comment <$> source)
import' :: Assignment import' :: Assignment
import' = makeTerm'' <$> symbol ImportStatement <*> children (manyTerm (aliasedImport <|> plainImport)) import' = makeTerm'' <$> symbol ImportStatement <*> children (manyTerm (aliasedImport <|> plainImport))
<|> makeTerm <$> symbol ImportFromStatement <*> children (Declaration.Import <$> (dottedName <|> emptyTerm) <*> emptyTerm <*> someTerm (wildCard <|> dottedName <|> aliasedSymbol <|> importSymbol)) <|> makeTerm <$> symbol ImportFromStatement <*> children (Declaration.Import <$> (dottedName <|> emptyTerm) <*> emptyTerm <*> someTerm (dottedName <|> aliasedSymbol <|> importSymbol))
<|> makeTerm <$> symbol ImportFromStatement <*> children (Declaration.WildcardImport <$> dottedName <*> wildcard)
where where
importSymbol = makeTerm <$> location <*> (Declaration.ImportSymbol <$> expression <*> emptyTerm) importSymbol = makeTerm <$> location <*> (Declaration.ImportSymbol <$> expression <*> emptyTerm)
aliasedSymbol = makeTerm <$> symbol AliasedImport <*> children (Declaration.ImportSymbol <$> expression <*> expression) aliasedSymbol = makeTerm <$> symbol AliasedImport <*> children (Declaration.ImportSymbol <$> expression <*> expression)
wildCard = makeTerm <$> symbol WildcardImport <*> (Syntax.Identifier <$> source) wildcard = makeTerm <$> symbol WildcardImport <*> (Syntax.Identifier <$> source)
aliasedImport = makeImport <$> symbol AliasedImport <*> children ((,) <$> expression <*> (Just <$> expression)) aliasedImport = makeImport <$> symbol AliasedImport <*> children ((,) <$> expression <*> (Just <$> expression))
plainImport = makeImport <$> symbol DottedName <*> children ((,) <$> expressions <*> pure Nothing) plainImport = makeImport <$> symbol DottedName <*> children ((,) <$> expressions <*> pure Nothing)