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

ruby: implement variable scoping around scoped constructs

This commit is contained in:
Charlie Somerville 2018-03-26 17:00:30 +11:00
parent 437df993b5
commit ccca1e5857

View File

@ -147,6 +147,18 @@ expressions = makeTerm'' <$> location <*> many expression
parenthesizedExpressions :: Assignment parenthesizedExpressions :: Assignment
parenthesizedExpressions = makeTerm'' <$> symbol ParenthesizedStatements <*> children (Syntax.Paren <$> expressions) parenthesizedExpressions = makeTerm'' <$> symbol ParenthesizedStatements <*> children (Syntax.Paren <$> expressions)
withExtendedScope :: Assignment.Assignment ast grammar a -> Assignment.Assignment ast grammar a
withExtendedScope inner = do
locals <- getRubyLocals
result <- inner
putRubyLocals locals
pure result
withNewScope :: Assignment.Assignment ast grammar a -> Assignment.Assignment ast grammar a
withNewScope inner = withExtendedScope $ do
putRubyLocals []
inner
identifier :: Assignment identifier :: Assignment
identifier = do identifier = do
sym <- symbol Identifier <|> symbol Identifier' sym <- symbol Identifier <|> symbol Identifier'
@ -204,14 +216,14 @@ endBlock :: Assignment
endBlock = makeTerm <$> symbol EndBlock <*> children (Statement.ScopeExit <$> many expression) endBlock = makeTerm <$> symbol EndBlock <*> children (Statement.ScopeExit <$> many expression)
class' :: Assignment class' :: Assignment
class' = makeTerm <$> symbol Class <*> children (Ruby.Syntax.Class <$> expression <*> (superclass <|> pure []) <*> expressions) class' = makeTerm <$> symbol Class <*> (withNewScope . children) (Ruby.Syntax.Class <$> expression <*> (superclass <|> pure []) <*> expressions)
where superclass = pure <$ symbol Superclass <*> children expression where superclass = pure <$ symbol Superclass <*> children expression
singletonClass :: Assignment singletonClass :: Assignment
singletonClass = makeTerm <$> symbol SingletonClass <*> children (Ruby.Syntax.Class <$> expression <*> pure [] <*> expressions) singletonClass = makeTerm <$> symbol SingletonClass <*> (withNewScope . children) (Ruby.Syntax.Class <$> expression <*> pure [] <*> expressions)
module' :: Assignment module' :: Assignment
module' = makeTerm <$> symbol Module <*> children (Ruby.Syntax.Module <$> expression <*> many expression) module' = makeTerm <$> symbol Module <*> (withNewScope . children) (Ruby.Syntax.Module <$> expression <*> many expression)
scopeResolution :: Assignment scopeResolution :: Assignment
scopeResolution = makeTerm <$> symbol ScopeResolution <*> children (Expression.ScopeResolution <$> many expression) scopeResolution = makeTerm <$> symbol ScopeResolution <*> children (Expression.ScopeResolution <$> many expression)
@ -228,24 +240,25 @@ parameter =
where mk s = makeTerm <$> symbol s <*> (Syntax.Identifier . name <$> source) where mk s = makeTerm <$> symbol s <*> (Syntax.Identifier . name <$> source)
method :: Assignment method :: Assignment
method = makeTerm <$> symbol Method <*> children (Declaration.Method <$> pure [] <*> emptyTerm <*> expression <*> params <*> expressions') method = makeTerm <$> symbol Method <*> (withNewScope . children) (Declaration.Method <$> pure [] <*> emptyTerm <*> expression <*> params <*> expressions')
where params = symbol MethodParameters *> children (many parameter) <|> pure [] where params = symbol MethodParameters *> children (many parameter) <|> pure []
expressions' = makeTerm <$> location <*> many expression expressions' = makeTerm <$> location <*> many expression
singletonMethod :: Assignment singletonMethod :: Assignment
singletonMethod = makeTerm <$> symbol SingletonMethod <*> children (Declaration.Method <$> pure [] <*> expression <*> expression <*> params <*> expressions) singletonMethod = makeTerm <$> symbol SingletonMethod <*> (withNewScope . children) (Declaration.Method <$> pure [] <*> expression <*> expression <*> params <*> expressions)
where params = symbol MethodParameters *> children (many parameter) <|> pure [] where params = symbol MethodParameters *> children (many parameter) <|> pure []
lambda :: Assignment lambda :: Assignment
lambda = makeTerm <$> symbol Lambda <*> children ( lambda = makeTerm <$> symbol Lambda <*> (withExtendedScope . children) (
Declaration.Function [] <$> emptyTerm Declaration.Function [] <$> emptyTerm
<*> ((symbol BlockParameters <|> symbol LambdaParameters) *> children (many parameter) <|> pure []) <*> ((symbol BlockParameters <|> symbol LambdaParameters) *> children (many parameter) <|> pure [])
<*> expressions) <*> expressions)
block :: Assignment block :: Assignment
block = makeTerm <$> symbol DoBlock <*> blockChildren block = makeTerm <$> symbol DoBlock <*> scopedBlockChildren
<|> makeTerm <$> symbol Block <*> blockChildren <|> makeTerm <$> symbol Block <*> scopedBlockChildren
where blockChildren = children (Declaration.Function <$> pure [] <*> emptyTerm <*> params <*> expressions) where scopedBlockChildren = withExtendedScope blockChildren
blockChildren = children (Declaration.Function <$> pure [] <*> emptyTerm <*> params <*> expressions)
params = symbol BlockParameters *> children (many parameter) <|> pure [] params = symbol BlockParameters *> children (many parameter) <|> pure []
comment :: Assignment comment :: Assignment