1
1
mirror of https://github.com/github/semantic.git synced 2024-12-12 14:45:40 +03:00

Define Python.Term at * -> *.

This commit is contained in:
Rob Rix 2019-10-18 15:38:04 -04:00
parent 98c6e06d10
commit 7efbbcb424
No known key found for this signature in database
GPG Key ID: F188A01508EA1CF7
2 changed files with 82 additions and 82 deletions

View File

@ -118,17 +118,17 @@ type Syntax =
, []
]
type Term = Term.Term (Sum Syntax) Loc
type Term = Term.Term (Sum Syntax)
type Assignment = Assignment.Assignment [] Grammar
-- | Assignment from AST in Python's grammar onto a program in Python's syntax.
assignment :: Assignment Term
assignment :: Assignment (Term Loc)
assignment = handleError $ makeTerm <$> symbol Module <*> children (Statement.Statements <$> manyTerm expression) <|> parseError
expression :: Assignment Term
expression :: Assignment (Term Loc)
expression = handleError (choice expressionChoices)
expressionChoices :: [Assignment Term]
expressionChoices :: [Assignment (Term Loc)]
expressionChoices =
-- Long-term, can we de/serialize assignments and avoid paying the cost of construction altogether?
[ argumentList
@ -196,34 +196,34 @@ expressionChoices =
, yield
]
expressions :: Assignment Term
expressions :: Assignment (Term Loc)
expressions = makeTerm'' <$> location <*> manyTerm expression
block :: Assignment Term
block :: Assignment (Term Loc)
block = symbol Block *> children (makeTerm'' <$> location <*> manyTerm expression)
block' :: Assignment Term
block' :: Assignment (Term Loc)
block' = symbol Block *> children (makeTerm <$> location <*> manyTerm expression)
expressionStatement :: Assignment Term
expressionStatement :: Assignment (Term Loc)
expressionStatement = makeTerm'' <$> symbol ExpressionStatement <*> children (someTerm expression)
expressionList :: Assignment Term
expressionList :: Assignment (Term Loc)
expressionList = makeTerm'' <$> symbol ExpressionList <*> children (someTerm expression)
listSplat :: Assignment Term
listSplat :: Assignment (Term Loc)
listSplat = makeTerm <$> symbol ListSplat <*> (Syntax.Identifier . name <$> source)
dictionarySplat :: Assignment Term
dictionarySplat :: Assignment (Term Loc)
dictionarySplat = makeTerm <$> symbol DictionarySplat <*> (Syntax.Identifier . name <$> source)
keywordArgument :: Assignment Term
keywordArgument :: Assignment (Term Loc)
keywordArgument = makeTerm <$> symbol KeywordArgument <*> children (Statement.Assignment [] <$> term expression <*> term expression)
parenthesizedExpression :: Assignment Term
parenthesizedExpression :: Assignment (Term Loc)
parenthesizedExpression = symbol ParenthesizedExpression *> children expressions
parameter :: Assignment Term
parameter :: Assignment (Term Loc)
parameter = makeTerm <$> symbol DefaultParameter <*> children (Statement.Assignment [] <$> term expression <*> term expression)
<|> makeTerm <$> symbol TypedParameter <*> children (Type.Annotation <$> term expression <*> term type')
<|> makeAnnotation <$> symbol TypedDefaultParameter <*> children ((,,) <$> term expression <*> term expression <*> term expression)
@ -231,45 +231,45 @@ parameter = makeTerm <$> symbol DefaultParameter <*> children (Statement.Assign
makeAnnotation loc (identifier', type', value') = makeTerm loc (Type.Annotation (makeAssignment loc identifier' value') type')
makeAssignment loc identifier' value' = makeTerm loc (Statement.Assignment [] identifier' value')
decoratedDefinition :: Assignment Term
decoratedDefinition :: Assignment (Term Loc)
decoratedDefinition = symbol DecoratedDefinition *> children (term decorator)
where
decorator = makeTerm <$> symbol Decorator <*> (children (Declaration.Decorator <$> term expression <*> manyTerm expression) <*> term (decorator <|> functionDefinition <|> classDefinition))
argumentList :: Assignment Term
argumentList :: Assignment (Term Loc)
argumentList = symbol ArgumentList *> children expressions
withStatement :: Assignment Term
withStatement :: Assignment (Term Loc)
withStatement = symbol WithStatement *> children (flip (foldr make) <$> some withItem <*> term block')
where
make (val, name) = makeTerm1 . Statement.Let name val
withItem = symbol WithItem *> children ((,) <$> term expression <*> term (expression <|> emptyTerm))
forStatement :: Assignment Term
forStatement :: Assignment (Term Loc)
forStatement = symbol ForStatement >>= \ loc -> children (make loc <$> (symbol Variables *> children expressions) <*> term expressionList <*> term block' <*> optional (symbol ElseClause *> children expressions))
where
make loc binding subject body forElseClause = case forElseClause of
Nothing -> makeTerm loc (Statement.ForEach binding subject body)
Just a -> makeTerm loc (Statement.Else (makeTerm loc $ Statement.ForEach binding subject body) a)
whileStatement :: Assignment Term
whileStatement :: Assignment (Term Loc)
whileStatement = symbol WhileStatement >>= \ loc -> children (make loc <$> term expression <*> term block <*> optional (symbol ElseClause *> children expressions))
where
make loc whileCondition whileBody whileElseClause = case whileElseClause of
Nothing -> makeTerm loc (Statement.While whileCondition whileBody)
Just a -> makeTerm loc (Statement.Else (makeTerm loc $ Statement.While whileCondition whileBody) a)
tryStatement :: Assignment Term
tryStatement :: Assignment (Term Loc)
tryStatement = makeTerm <$> symbol TryStatement <*> children (Statement.Try <$> term block <*> manyTerm (expression <|> elseClause))
where elseClause = makeTerm <$> symbol ElseClause <*> children (Statement.Else <$> emptyTerm <*> term block)
exceptClause :: Assignment Term
exceptClause :: Assignment (Term Loc)
exceptClause = makeTerm <$> symbol ExceptClause <*> children
(Statement.Catch <$> term ((makeTerm <$> location <*> (uncurry (flip Statement.Let) <$> ((,) <$> term expression <* symbol AnonAs <*> term expression) <*> emptyTerm))
<|> expressions)
<*> expressions)
functionParam :: Assignment Term
functionParam :: Assignment (Term Loc)
functionParam = (makeParameter <$> location <*> identifier)
<|> tuple
<|> parameter
@ -277,7 +277,7 @@ functionParam = (makeParameter <$> location <*> identifier)
<|> dictionarySplat
where makeParameter loc term = makeTerm loc (Declaration.RequiredParameter term)
functionDefinition :: Assignment Term
functionDefinition :: Assignment (Term Loc)
functionDefinition =
makeFunctionDeclaration <$> symbol FunctionDefinition <*> children ((,,,) <$> term expression <* symbol Parameters <*> children (manyTerm functionParam) <*> optional (symbol Type *> children (term expression)) <*> term block')
<|> makeFunctionDeclaration <$> (symbol Lambda' <|> symbol Lambda) <*> children ((,,,) <$ token AnonLambda <*> emptyTerm <*> (symbol LambdaParameters *> children (manyTerm expression) <|> pure []) <*> optional (symbol Type *> children (term expression)) <*> expressions')
@ -287,22 +287,22 @@ functionDefinition =
= let fn = makeTerm loc (Declaration.Function [] functionName' functionParameters functionBody)
in maybe fn (makeTerm loc . Type.Annotation fn) ty
classDefinition :: Assignment Term
classDefinition :: Assignment (Term Loc)
classDefinition = makeTerm <$> symbol ClassDefinition <*> children (Declaration.Class [] <$> term expression <*> argumentList <*> term block')
where
argumentList = symbol ArgumentList *> children (manyTerm expression)
<|> pure []
type' :: Assignment Term
type' :: Assignment (Term Loc)
type' = symbol Type *> children (term expression)
finallyClause :: Assignment Term
finallyClause :: Assignment (Term Loc)
finallyClause = makeTerm <$> symbol FinallyClause <*> children (Statement.Finally <$> expressions)
ellipsis :: Assignment Term
ellipsis :: Assignment (Term Loc)
ellipsis = makeTerm <$> token Grammar.Ellipsis <*> pure Python.Syntax.Ellipsis
comparisonOperator :: Assignment Term
comparisonOperator :: Assignment (Term Loc)
comparisonOperator = symbol ComparisonOperator *> children (expression `chainl1Term` choice
[ (makeTerm1 .) . Expression.LessThan <$ token AnonLAngle
, (makeTerm1 .) . Expression.LessThanEqual <$ token AnonLAngleEqual
@ -317,19 +317,19 @@ comparisonOperator = symbol ComparisonOperator *> children (expression `chainl1T
])
where invert cons a b = Expression.Not (makeTerm1 (cons a b))
notOperator :: Assignment Term
notOperator :: Assignment (Term Loc)
notOperator = makeTerm <$> symbol NotOperator <*> children (Expression.Not <$> term expression)
tuple :: Assignment Term
tuple :: Assignment (Term Loc)
tuple = makeTerm <$> symbol Tuple <*> children (Literal.Tuple <$> manyTerm expression)
unaryOperator :: Assignment Term
unaryOperator :: Assignment (Term Loc)
unaryOperator = symbol UnaryOperator >>= \ location -> arithmetic location <|> bitwise location <|> children ( symbol AnonPlus *> term expression )
where
arithmetic location = makeTerm location . Expression.Negate <$> children ( symbol AnonMinus *> term expression )
bitwise location = makeTerm location . Expression.Complement <$> children ( symbol AnonTilde *> term expression )
binaryOperator :: Assignment Term
binaryOperator :: Assignment (Term Loc)
binaryOperator = makeTerm' <$> symbol BinaryOperator <*> children (infixTerm expression (term expression)
[ (inject .) . Expression.Plus <$ symbol AnonPlus
, (inject .) . Expression.Minus <$ symbol AnonMinus
@ -346,13 +346,13 @@ binaryOperator = makeTerm' <$> symbol BinaryOperator <*> children (infixTerm exp
, (inject .) . Expression.RShift <$ symbol AnonRAngleRAngle
])
booleanOperator :: Assignment Term
booleanOperator :: Assignment (Term Loc)
booleanOperator = makeTerm' <$> symbol BooleanOperator <*> children (infixTerm expression (term expression)
[ (inject .) . Expression.And <$ symbol AnonAnd
, (inject .) . Expression.Or <$ symbol AnonOr
])
assignment' :: Assignment Term
assignment' :: Assignment (Term Loc)
assignment' = makeAssignment <$> symbol Assignment <*> children ((,,) <$> term expressionList <*> optional (symbol Type *> children (term expression)) <*> term rvalue)
<|> makeTerm' <$> symbol AugmentedAssignment <*> children (infixTerm expressionList (term rvalue)
[ assign Expression.Plus <$ symbol AnonPlusEqual
@ -371,43 +371,43 @@ assignment' = makeAssignment <$> symbol Assignment <*> children ((,,) <$> term
])
where rvalue = expressionList <|> assignment' <|> yield <|> emptyTerm
makeAssignment loc (lhs, maybeType, rhs) = makeTerm loc (Statement.Assignment (maybeToList maybeType) lhs rhs)
assign :: (f :< Syntax) => (Term -> Term -> f Term) -> Term -> Term -> Sum Syntax Term
assign :: (f :< Syntax) => (Term Loc -> Term Loc -> f (Term Loc)) -> Term Loc -> Term Loc -> Sum Syntax (Term Loc)
assign c l r = inject (Statement.Assignment [] l (makeTerm1 (c l r)))
yield :: Assignment Term
yield :: Assignment (Term Loc)
yield = makeTerm <$> symbol Yield <*> (Statement.Yield <$> children (term ( expression <|> emptyTerm )))
identifier :: Assignment Term
identifier :: Assignment (Term Loc)
identifier = makeTerm <$> (symbol Identifier <|> symbol DottedName) <*> (Syntax.Identifier . name <$> source)
set :: Assignment Term
set :: Assignment (Term Loc)
set = makeTerm <$> symbol Set <*> children (Literal.Set <$> manyTerm expression)
dictionary :: Assignment Term
dictionary :: Assignment (Term Loc)
dictionary = makeTerm <$> symbol Dictionary <*> children (Literal.Hash <$> manyTerm expression)
pair :: Assignment Term
pair :: Assignment (Term Loc)
pair = makeTerm' <$> symbol Pair <*> children (infixTerm expression (term expression) [ (inject .) . Literal.KeyValue <$ symbol AnonColon ])
list' :: Assignment Term
list' :: Assignment (Term Loc)
list' = makeTerm <$> symbol List <*> children (Literal.Array <$> manyTerm expression)
string :: Assignment Term
string :: Assignment (Term Loc)
string = makeTerm <$> symbol String <*> (Literal.TextElement <$> source)
concatenatedString :: Assignment Term
concatenatedString :: Assignment (Term Loc)
concatenatedString = makeTerm <$> symbol ConcatenatedString <*> children (manyTerm string)
float :: Assignment Term
float :: Assignment (Term Loc)
float = makeTerm <$> symbol Float <*> (Literal.Float <$> source)
integer :: Assignment Term
integer :: Assignment (Term Loc)
integer = makeTerm <$> symbol Integer <*> (Literal.Integer <$> source)
comment :: Assignment Term
comment :: Assignment (Term Loc)
comment = makeTerm <$> symbol Comment <*> (Comment.Comment <$> source)
import' :: Assignment Term
import' :: Assignment (Term Loc)
import' = makeTerm'' <$> symbol ImportStatement <*> children (manyTerm (aliasedImport <|> plainImport))
<|> makeTerm <$> symbol ImportFromStatement <*> children (Python.Syntax.Import <$> importPath <*> (wildcard <|> some (aliasImportSymbol <|> importSymbol)))
<|> makeTerm <$> symbol FutureImportStatement <*> children (Python.Syntax.FutureImport <$> some (aliasImportSymbol <|> importSymbol))
@ -432,10 +432,10 @@ import' = makeTerm'' <$> symbol ImportStatement <*> children (manyTerm (aliase
makeNameAliasPair location alias = makeTerm location (Python.Syntax.Alias alias alias)
mkIdentifier location source = makeTerm location (Syntax.Identifier (name source))
assertStatement :: Assignment Term
assertStatement :: Assignment (Term Loc)
assertStatement = makeTerm <$> symbol AssertStatement <*> children (Expression.Call [] <$> (makeTerm <$> symbol AnonAssert <*> (Syntax.Identifier . name <$> source)) <*> manyTerm expression <*> emptyTerm)
printStatement :: Assignment Term
printStatement :: Assignment (Term Loc)
printStatement = do
location <- symbol PrintStatement
children $ do
@ -446,26 +446,26 @@ printStatement = do
redirectCallTerm location identifier = makeTerm location <$ symbol Chevron <*> (flip Python.Syntax.Redirect <$> children (term expression) <*> term (printCallTerm location identifier))
printCallTerm location identifier = makeTerm location <$> (Expression.Call [] identifier <$> manyTerm expression <*> emptyTerm)
nonlocalStatement :: Assignment Term
nonlocalStatement :: Assignment (Term Loc)
nonlocalStatement = makeTerm <$> symbol NonlocalStatement <*> children (Expression.Call [] <$> term (makeTerm <$> symbol AnonNonlocal <*> (Syntax.Identifier . name <$> source)) <*> manyTerm expression <*> emptyTerm)
globalStatement :: Assignment Term
globalStatement :: Assignment (Term Loc)
globalStatement = makeTerm <$> symbol GlobalStatement <*> children (Expression.Call [] <$> term (makeTerm <$> symbol AnonGlobal <*> (Syntax.Identifier . name <$> source)) <*> manyTerm expression <*> emptyTerm)
await :: Assignment Term
await :: Assignment (Term Loc)
await = makeTerm <$> symbol Await <*> children (Expression.Call [] <$> term (makeTerm <$> symbol AnonAwait <*> (Syntax.Identifier . name <$> source)) <*> manyTerm expression <*> emptyTerm)
returnStatement :: Assignment Term
returnStatement :: Assignment (Term Loc)
returnStatement = makeTerm <$> symbol ReturnStatement <*> children (Statement.Return <$> term (expressionList <|> emptyTerm))
deleteStatement :: Assignment Term
deleteStatement :: Assignment (Term Loc)
deleteStatement = makeTerm <$> symbol DeleteStatement <*> children (Expression.Call [] <$> term deleteIdentifier <* symbol ExpressionList <*> children (manyTerm expression) <*> emptyTerm)
where deleteIdentifier = makeTerm <$> symbol AnonDel <*> (Syntax.Identifier . name <$> source)
raiseStatement :: Assignment Term
raiseStatement :: Assignment (Term Loc)
raiseStatement = makeTerm <$> symbol RaiseStatement <*> children (Statement.Throw <$> expressions)
ifStatement :: Assignment Term
ifStatement :: Assignment (Term Loc)
ifStatement = makeTerm <$> symbol IfStatement <*> children if'
where
if' = Statement.If <$> term expression <*> thenClause <*> (elseClause <|> emptyTerm)
@ -474,86 +474,86 @@ ifStatement = makeTerm <$> symbol IfStatement <*> children if'
elif = makeTerm <$> symbol ElifClause <*> children if'
else' = symbol ElseClause *> children expressions
execStatement :: Assignment Term
execStatement :: Assignment (Term Loc)
execStatement = makeTerm <$> symbol ExecStatement <*> children (Expression.Call [] <$> term (makeTerm <$> location <*> (Syntax.Identifier . name <$> source)) <*> manyTerm (string <|> expression) <*> emptyTerm)
passStatement :: Assignment Term
passStatement :: Assignment (Term Loc)
passStatement = makeTerm <$> symbol PassStatement <*> (Statement.NoOp <$> emptyTerm <* advance)
breakStatement :: Assignment Term
breakStatement :: Assignment (Term Loc)
breakStatement = makeTerm <$> symbol BreakStatement <*> (Statement.Break <$> emptyTerm <* advance)
continueStatement :: Assignment Term
continueStatement :: Assignment (Term Loc)
continueStatement = makeTerm <$> symbol ContinueStatement <*> (Statement.Continue <$> emptyTerm <* advance)
memberAccess :: Assignment Term
memberAccess :: Assignment (Term Loc)
memberAccess = makeTerm <$> symbol Attribute <*> children (Expression.MemberAccess <$> term expression <*> identifier)
subscript :: Assignment Term
subscript :: Assignment (Term Loc)
subscript = makeTerm <$> symbol Subscript <*> children (Expression.Subscript <$> term expression <*> manyTerm expression)
slice :: Assignment Term
slice :: Assignment (Term Loc)
slice = makeTerm <$> symbol Slice <*> children
(Expression.Enumeration <$> ((emptyTerm <* token AnonColon) <|> (term expression <* token AnonColon))
<*> ((emptyTerm <* token AnonColon) <|> (term expression <* token AnonColon) <|> (term expression <|> emptyTerm))
<*> (term expression <|> emptyTerm))
call :: Assignment Term
call :: Assignment (Term Loc)
call = makeTerm <$> symbol Call <*> children (Expression.Call [] <$> term (identifier <|> expression) <*> (symbol ArgumentList *> children (manyTerm expression) <|> someTerm comprehension) <*> emptyTerm)
boolean :: Assignment Term
boolean :: Assignment (Term Loc)
boolean = makeTerm <$> token Grammar.True <*> pure Literal.true
<|> makeTerm <$> token Grammar.False <*> pure Literal.false
none :: Assignment Term
none :: Assignment (Term Loc)
none = makeTerm <$> symbol None <*> (Literal.Null <$ rawSource)
comprehension :: Assignment Term
comprehension :: Assignment (Term Loc)
comprehension = makeTerm <$> symbol ListComprehension <*> children (Declaration.Comprehension <$> term expression <*> expressions)
<|> makeTerm <$> symbol GeneratorExpression <*> children (Declaration.Comprehension <$> term expression <*> expressions)
<|> makeTerm <$> symbol SetComprehension <*> children (Declaration.Comprehension <$> term expression <*> expressions)
<|> makeTerm <$> symbol DictionaryComprehension <*> children (Declaration.Comprehension <$> term expression <*> expressions)
forInClause :: Assignment Term
forInClause :: Assignment (Term Loc)
forInClause = symbol ForInClause *> children expressions
variables :: Assignment Term
variables :: Assignment (Term Loc)
variables = symbol Variables *> children expressions
ifClause :: Assignment Term
ifClause :: Assignment (Term Loc)
ifClause = symbol IfClause *> children expressions
conditionalExpression :: Assignment Term
conditionalExpression :: Assignment (Term Loc)
conditionalExpression = makeTerm <$> symbol ConditionalExpression <*> children (flip Statement.If <$> term expression <*> term expression <*> expressions)
-- Helpers
-- | Match a term optionally preceded by comment(s), or a sequence of comments if the term is not present.
manyTerm :: Assignment Term -> Assignment [Term]
manyTerm :: Assignment (Term Loc) -> Assignment [Term Loc]
manyTerm term = many (contextualize comment term <|> makeTerm1 <$> (Syntax.Context <$> some1 comment <*> emptyTerm))
someTerm :: Assignment Term -> Assignment [Term]
someTerm :: Assignment (Term Loc) -> Assignment [Term Loc]
someTerm term = some (contextualize comment term <|> makeTerm1 <$> (Syntax.Context <$> some1 comment <*> emptyTerm))
term :: Assignment Term -> Assignment Term
term :: Assignment (Term Loc) -> Assignment (Term Loc)
term term = contextualize comment (postContextualize comment term)
term' :: Assignment Term -> Assignment Term
term' :: Assignment (Term Loc) -> Assignment (Term Loc)
term' term = contextualize comment' (postContextualize comment' term)
where comment' = choice [ comment, symbol AnonLambda *> empty ]
-- | Match a left-associated infix chain of terms, optionally followed by comments. Like 'chainl1' but assigning comment nodes automatically.
chainl1Term :: Assignment Term -> Assignment (Term -> Term -> Term) -> Assignment Term
chainl1Term :: Assignment (Term Loc) -> Assignment (Term Loc -> Term Loc -> Term Loc) -> Assignment (Term Loc)
chainl1Term expr op = term' expr `chainl1` op
-- | Match a series of terms or comments until a delimiter is matched.
manyTermsTill :: Assignment Term -> Assignment b -> Assignment [Term]
manyTermsTill :: Assignment (Term Loc) -> Assignment b -> Assignment [Term Loc]
manyTermsTill step end = manyTill (step <|> comment) end
-- | Match infix terms separated by any of a list of operators, assigning any comments following each operand.
infixTerm :: Assignment Term
-> Assignment Term
-> [Assignment (Term -> Term -> Sum Syntax Term)]
-> Assignment (Sum Syntax Term)
infixTerm :: Assignment (Term Loc)
-> Assignment (Term Loc)
-> [Assignment (Term Loc -> Term Loc -> Sum Syntax (Term Loc))]
-> Assignment (Sum Syntax (Term Loc))
infixTerm = infixContext comment

View File

@ -86,7 +86,7 @@ rubyParser = AssignmentParser (ASTParser tree_sitter_ruby) Ruby.assignment
phpParser :: Parser PHP.Term
phpParser = AssignmentParser (ASTParser tree_sitter_php) PHP.assignment
pythonParser :: Parser Python.Term
pythonParser :: Parser (Python.Term Loc)
pythonParser = AssignmentParser (ASTParser tree_sitter_python) Python.assignment
typescriptParser :: Parser TypeScript.Term