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

ruby: make sendSelector Maybe and implement dot-call syntax

This commit is contained in:
Charlie Somerville 2018-04-10 15:57:49 +10:00
parent 0013bf1975
commit 7b62c84ac7
2 changed files with 24 additions and 12 deletions

View File

@ -292,14 +292,20 @@ subscript = makeTerm <$> symbol ElementReference <*> children (Expression.Subscr
pair :: Assignment
pair = makeTerm <$> symbol Pair <*> children (Literal.KeyValue <$> expression <*> (expression <|> emptyTerm))
methodCall :: Assignment
methodCall = makeTerm' <$> symbol MethodCall <*> children (require <|> load <|> send)
where
send = inj <$> (regularCall <|> funcCall <|> scopeCall)
funcCall = Ruby.Syntax.Send Nothing <$> methodSelector <*> args <*> optional block
regularCall = symbol Call *> children (Ruby.Syntax.Send <$> (Just <$> postContextualize heredoc expression) <*> methodSelector) <*> args <*> optional block
scopeCall = symbol ScopeResolution *> children (Ruby.Syntax.Send . Just <$> expression <*> methodSelector) <*> args <*> optional block
args :: Assignment' [Term]
args = (symbol ArgumentList <|> symbol ArgumentListWithParens) *> children (many expression) <|> many expression
methodCall :: Assignment
methodCall = makeTerm' <$> symbol MethodCall <*> children (require <|> load <|> send)
where
send = inj <$> ((regularCall <|> funcCall <|> scopeCall <|> dotCall) <*> optional block)
funcCall = Ruby.Syntax.Send Nothing <$> selector <*> args
regularCall = symbol Call *> children (Ruby.Syntax.Send <$> (Just <$> postContextualize heredoc expression) <*> selector) <*> args
scopeCall = symbol ScopeResolution *> children (Ruby.Syntax.Send <$> (Just <$> expression) <*> selector) <*> args
dotCall = symbol Call *> children (Ruby.Syntax.Send <$> (Just <$> term expression) <*> pure Nothing <*> args)
selector = Just <$> methodSelector
require = inj <$> ((symbol Identifier <|> symbol Identifier') *> do
s <- source
guard (s `elem` ["require", "require_relative"])
@ -308,7 +314,6 @@ methodCall = makeTerm' <$> symbol MethodCall <*> children (require <|> load <|>
s <- source
guard (s == "load")
Ruby.Syntax.Load <$> loadArgs)
args = (symbol ArgumentList <|> symbol ArgumentListWithParens) *> children (many expression) <|> many expression
loadArgs = (symbol ArgumentList <|> symbol ArgumentListWithParens) *> children (some expression)
nameExpression = (symbol ArgumentList <|> symbol ArgumentListWithParens) *> children expression
@ -322,7 +327,9 @@ methodSelector = makeTerm <$> symbols <*> (Syntax.Identifier <$> (name <$> sourc
<|> symbol Super -- TODO(@charliesome): super calls are *not* method calls and need to be assigned into their own syntax terms
call :: Assignment
call = makeTerm <$> symbol Call <*> children (Ruby.Syntax.Send <$> (Just <$> term expression) <*> methodSelector <*> many expression <*> optional block)
call = makeTerm <$> symbol Call <*> children (
(Ruby.Syntax.Send <$> (Just <$> term expression) <*> (Just <$> methodSelector) <*> pure [] <*> pure Nothing) <|>
(Ruby.Syntax.Send <$> (Just <$> term expression) <*> pure Nothing <*> args <*> pure Nothing))
rescue :: Assignment
rescue = rescue'

View File

@ -36,7 +36,7 @@ maybeFailNotFound name = maybeFail notFound
cleanNameOrPath :: ByteString -> String
cleanNameOrPath = BC.unpack . dropRelativePrefix . stripQuotes
data Send a = Send { sendReceiver :: Maybe a, sendSelector :: a, sendArgs :: [a], sendBlock :: Maybe a }
data Send a = Send { sendReceiver :: Maybe a, sendSelector :: Maybe a, sendArgs :: [a], sendBlock :: Maybe a }
deriving (Diffable, Eq, Foldable, Functor, GAlign, Generic1, Mergeable, Ord, Show, Traversable, FreeVariables1)
instance Eq1 Send where liftEq = genericLiftEq
@ -45,11 +45,16 @@ instance Show1 Send where liftShowsPrec = genericLiftShowsPrec
instance Evaluatable Send where
eval Send{..} = do
let sel = case sendSelector of
Just sel -> subtermValue sel
Nothing -> variable (name "call")
func <- case sendReceiver of
Just recv -> do
recvEnv <- subtermValue recv >>= scopedEnvironment
localEnv (mappend recvEnv) (subtermValue sendSelector)
Nothing -> subtermValue sendSelector -- TODO Does this require `localize` so we don't leak terms when resolving `sendSelector`?
localEnv (mappend recvEnv) sel
Nothing -> sel -- TODO Does this require `localize` so we don't leak terms when resolving `sendSelector`?
call func (map subtermValue sendArgs) -- TODO pass through sendBlock
data Require a = Require { requireRelative :: Bool, requirePath :: !a }