mirror of
https://github.com/github/semantic.git
synced 2024-12-21 05:41:54 +03:00
Introduce expressions and declarations to Python assignment to cleanup code
This commit is contained in:
parent
6bf440d240
commit
fc9851334f
@ -108,6 +108,9 @@ declaration = classDefinition
|
|||||||
<|> statement
|
<|> statement
|
||||||
<|> parseError
|
<|> parseError
|
||||||
|
|
||||||
|
declarations :: Assignment
|
||||||
|
declarations = makeTerm <$> location <*> many declaration
|
||||||
|
|
||||||
statement :: Assignment
|
statement :: Assignment
|
||||||
statement = assertStatement
|
statement = assertStatement
|
||||||
<|> assignment'
|
<|> assignment'
|
||||||
@ -176,6 +179,9 @@ expression = argument
|
|||||||
<|> unaryOperator
|
<|> unaryOperator
|
||||||
<|> parseError
|
<|> parseError
|
||||||
|
|
||||||
|
expressions :: Assignment
|
||||||
|
expressions = makeTerm <$> location <*> many expression
|
||||||
|
|
||||||
argument :: Assignment
|
argument :: Assignment
|
||||||
argument = makeTerm <$> symbol ListSplatArgument <*> (Syntax.Identifier <$> source)
|
argument = makeTerm <$> symbol ListSplatArgument <*> (Syntax.Identifier <$> source)
|
||||||
<|> makeTerm <$> symbol DictionarySplatArgument <*> (Syntax.Identifier <$> source)
|
<|> makeTerm <$> symbol DictionarySplatArgument <*> (Syntax.Identifier <$> source)
|
||||||
@ -202,33 +208,33 @@ withStatement :: Assignment
|
|||||||
withStatement = makeTerm <$> symbol WithStatement <*> children (uncurry Statement.Let . swap <$> (symbol WithItem *> children ((,) <$> identifier <*> identifier)) <*> expression)
|
withStatement = makeTerm <$> symbol WithStatement <*> children (uncurry Statement.Let . swap <$> (symbol WithItem *> children ((,) <$> identifier <*> identifier)) <*> expression)
|
||||||
|
|
||||||
forStatement :: Assignment
|
forStatement :: Assignment
|
||||||
forStatement = symbol ForStatement >>= \ loc -> children (make loc <$> (makeTerm <$> symbol Variables <*> children (many expression)) <*> expressionList <*> (makeTerm <$> location <*> many expression) <*> (optional (makeTerm <$> symbol ElseClause <*> children (many declaration))))
|
forStatement = symbol ForStatement >>= \ loc -> children (make loc <$> (makeTerm <$> symbol Variables <*> children (many expression)) <*> expressionList <*> expressions <*> optional (makeTerm <$> symbol ElseClause <*> children (many declaration)))
|
||||||
where
|
where
|
||||||
make loc variables expressionList forBody forElseClause = case forElseClause of
|
make loc variables expressionList forBody forElseClause = case forElseClause of
|
||||||
Nothing -> makeTerm loc (Statement.ForEach variables expressionList forBody)
|
Nothing -> makeTerm loc (Statement.ForEach variables expressionList forBody)
|
||||||
Just a -> makeTerm loc (Statement.Else (makeTerm loc $ Statement.ForEach variables expressionList forBody) a)
|
Just a -> makeTerm loc (Statement.Else (makeTerm loc $ Statement.ForEach variables expressionList forBody) a)
|
||||||
|
|
||||||
whileStatement :: Assignment
|
whileStatement :: Assignment
|
||||||
whileStatement = symbol WhileStatement >>= \ loc -> children (make loc <$> expression <*> (makeTerm <$> location <*> many expression) <*> (optional (makeTerm <$> symbol ElseClause <*> children (many declaration))))
|
whileStatement = symbol WhileStatement >>= \ loc -> children (make loc <$> expression <*> expressions <*> optional (makeTerm <$> symbol ElseClause <*> children (many declaration)))
|
||||||
where
|
where
|
||||||
make loc whileCondition whileBody whileElseClause = case whileElseClause of
|
make loc whileCondition whileBody whileElseClause = case whileElseClause of
|
||||||
Nothing -> makeTerm loc (Statement.While whileCondition whileBody)
|
Nothing -> makeTerm loc (Statement.While whileCondition whileBody)
|
||||||
Just a -> makeTerm loc (Statement.Else (makeTerm loc $ Statement.While whileCondition whileBody) a)
|
Just a -> makeTerm loc (Statement.Else (makeTerm loc $ Statement.While whileCondition whileBody) a)
|
||||||
|
|
||||||
tryStatement :: Assignment
|
tryStatement :: Assignment
|
||||||
tryStatement = makeTerm <$> symbol TryStatement <*> children (Statement.Try <$> expression <*> (many (expression <|> elseClause)))
|
tryStatement = makeTerm <$> symbol TryStatement <*> children (Statement.Try <$> expression <*> many (expression <|> elseClause))
|
||||||
where elseClause = makeTerm <$> symbol ElseClause <*> children (Statement.Else <$> emptyTerm <*> (makeTerm <$> location <*> (many expression)))
|
where elseClause = makeTerm <$> symbol ElseClause <*> children (Statement.Else <$> emptyTerm <*> (makeTerm <$> location <*> many expression))
|
||||||
|
|
||||||
exceptClause :: Assignment
|
exceptClause :: Assignment
|
||||||
exceptClause = makeTerm <$> symbol ExceptClause <*> children
|
exceptClause = makeTerm <$> symbol ExceptClause <*> children
|
||||||
(Statement.Catch <$> ((makeTerm <$> location <*> (uncurry Statement.Let . swap <$> ((,) <$> identifier <* symbol AnonAs <*> identifier) <*> emptyTerm))
|
(Statement.Catch <$> ((makeTerm <$> location <*> (uncurry Statement.Let . swap <$> ((,) <$> identifier <* symbol AnonAs <*> identifier) <*> emptyTerm))
|
||||||
<|> (makeTerm <$> location <*> (many identifier)))
|
<|> makeTerm <$> location <*> many identifier)
|
||||||
<*> (makeTerm <$> location <*> (many expression)))
|
<*> expressions)
|
||||||
|
|
||||||
functionDefinition :: Assignment
|
functionDefinition :: Assignment
|
||||||
functionDefinition = (symbol FunctionDefinition >>= \ loc -> children (makeFunctionDeclaration loc <$> identifier <*> (symbol Parameters *> children (many expression)) <*> (optional (symbol Type *> children expression)) <*> (makeTerm <$> location <*> many declaration)))
|
functionDefinition = (symbol FunctionDefinition >>= \ loc -> children (makeFunctionDeclaration loc <$> identifier <*> (symbol Parameters *> children (many expression)) <*> optional (symbol Type *> children expression) <*> declarations))
|
||||||
<|> (symbol AsyncFunctionDefinition >>= \ loc -> children (makeAsyncFunctionDeclaration loc <$> async' <*> identifier <*> (symbol Parameters *> children (many expression)) <*> (optional (symbol Type *> children expression)) <*> (makeTerm <$> location <*> many declaration)))
|
<|> (symbol AsyncFunctionDefinition >>= \ loc -> children (makeAsyncFunctionDeclaration loc <$> async' <*> identifier <*> (symbol Parameters *> children (many expression)) <*> optional (symbol Type *> children expression) <*> declarations))
|
||||||
<|> (symbol Lambda >>= \ loc -> children (makeFunctionDeclaration loc <$> (makeTerm <$> symbol AnonLambda <*> (Syntax.Identifier <$> source)) <*> ((symbol LambdaParameters *> children (many expression)) <|> (pure [])) <*> (optional (symbol Type *> children expression)) <*> (makeTerm <$> location <*> many declaration)))
|
<|> (symbol Lambda >>= \ loc -> children (makeFunctionDeclaration loc <$> (makeTerm <$> symbol AnonLambda <*> (Syntax.Identifier <$> source)) <*> ((symbol LambdaParameters *> children (many expression)) <|> pure []) <*> optional (symbol Type *> children expression) <*> declarations))
|
||||||
where
|
where
|
||||||
makeFunctionDeclaration loc functionName' functionParameters ty functionBody = makeTerm loc $ Type.Annotation (makeTerm loc $ Declaration.Function functionName' functionParameters functionBody) (maybe (makeTerm loc Syntax.Empty) identity ty)
|
makeFunctionDeclaration loc functionName' functionParameters ty functionBody = makeTerm loc $ Type.Annotation (makeTerm loc $ Declaration.Function functionName' functionParameters functionBody) (maybe (makeTerm loc Syntax.Empty) identity ty)
|
||||||
makeAsyncFunctionDeclaration loc async' functionName' functionParameters ty functionBody = makeTerm loc $ Type.Annotation (makeTerm loc $ Type.Annotation (makeTerm loc $ Declaration.Function functionName' functionParameters functionBody) (maybe (makeTerm loc Syntax.Empty) identity ty)) async'
|
makeAsyncFunctionDeclaration loc async' functionName' functionParameters ty functionBody = makeTerm loc $ Type.Annotation (makeTerm loc $ Type.Annotation (makeTerm loc $ Declaration.Function functionName' functionParameters functionBody) (maybe (makeTerm loc Syntax.Empty) identity ty)) async'
|
||||||
@ -237,7 +243,7 @@ async' :: Assignment
|
|||||||
async' = makeTerm <$> symbol AnonAsync <*> (Syntax.Identifier <$> source)
|
async' = makeTerm <$> symbol AnonAsync <*> (Syntax.Identifier <$> source)
|
||||||
|
|
||||||
classDefinition :: Assignment
|
classDefinition :: Assignment
|
||||||
classDefinition = makeTerm <$> symbol ClassDefinition <*> children (Declaration.Class <$> identifier <*> argumentList <*> (many declaration))
|
classDefinition = makeTerm <$> symbol ClassDefinition <*> children (Declaration.Class <$> identifier <*> argumentList <*> many declaration)
|
||||||
where argumentList = symbol ArgumentList *> children (many expression)
|
where argumentList = symbol ArgumentList *> children (many expression)
|
||||||
<|> pure []
|
<|> pure []
|
||||||
|
|
||||||
@ -276,7 +282,7 @@ keyword :: Assignment
|
|||||||
keyword = makeTerm <$> symbol KeywordIdentifier <*> children (Syntax.Identifier <$> source)
|
keyword = makeTerm <$> symbol KeywordIdentifier <*> children (Syntax.Identifier <$> source)
|
||||||
|
|
||||||
tuple :: Assignment
|
tuple :: Assignment
|
||||||
tuple = makeTerm <$> symbol Tuple <*> children (Literal.Tuple <$> (many expression))
|
tuple = makeTerm <$> symbol Tuple <*> children (Literal.Tuple <$> many expression)
|
||||||
|
|
||||||
-- TODO: Consider flattening single element lists
|
-- TODO: Consider flattening single element lists
|
||||||
expressionList :: Assignment
|
expressionList :: Assignment
|
||||||
@ -400,7 +406,7 @@ deleteStatement = makeTerm <$> symbol DeleteStatement <*> children (Expression.C
|
|||||||
where deleteIdentifier = makeTerm <$> symbol AnonDel <*> (Syntax.Identifier <$> source)
|
where deleteIdentifier = makeTerm <$> symbol AnonDel <*> (Syntax.Identifier <$> source)
|
||||||
|
|
||||||
raiseStatement :: Assignment
|
raiseStatement :: Assignment
|
||||||
raiseStatement = makeTerm <$> symbol RaiseStatement <*> children (Statement.Throw <$> (makeTerm <$> location <*> many expression))
|
raiseStatement = makeTerm <$> symbol RaiseStatement <*> children (Statement.Throw <$> expressions)
|
||||||
|
|
||||||
ifStatement :: Assignment
|
ifStatement :: Assignment
|
||||||
ifStatement = makeTerm <$> symbol IfStatement <*> children (Statement.If <$> expression <*> statements <*> (flip (foldr makeElif) <$> many elifClause <*> optionalElse))
|
ifStatement = makeTerm <$> symbol IfStatement <*> children (Statement.If <$> expression <*> statements <*> (flip (foldr makeElif) <$> many elifClause <*> optionalElse))
|
||||||
|
Loading…
Reference in New Issue
Block a user