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

Merge pull request #662 from github/operator-syntax

Improve Operator Syntax support
This commit is contained in:
Rick Winfrey 2016-08-01 11:55:06 -05:00 committed by GitHub
commit ec2f6fe2b0
4 changed files with 20 additions and 20 deletions

View File

@ -59,17 +59,19 @@ toTermName source term = case unwrap term of
S.Switch expr _ -> toTermName' expr
S.Ternary expr _ -> toTermName' expr
S.MathAssignment id _ -> toTermName' id
S.Operator syntaxes -> mconcat $ toTermName' <$> syntaxes
S.Operator _ -> termNameFromSource term
S.Object kvs -> "{" <> intercalate ", " (toTermName' <$> kvs) <> "}"
S.Pair a b -> toTermName' a <> ": " <> toTermName' b
S.Return expr -> maybe "empty" toTermName' expr
S.For exprs _ -> toText $ Source.slice (unionRangesFrom forRange forClauseRanges) source
where forRange = characterRange $ extract term
forClauseRanges = characterRange . extract <$> exprs
S.For exprs _ -> termNameFromChildren term exprs
S.While expr _ -> toTermName' expr
S.DoWhile _ expr -> toTermName' expr
Comment a -> toCategoryName a
where toTermName' = toTermName source
termNameFromChildren term cs = termNameFromRange (unionRangesFrom (range term) (range <$> cs))
termNameFromSource term = termNameFromRange (range term)
termNameFromRange range = toText $ Source.slice range source
range = characterRange . extract
class HasCategory a where
toCategoryName :: a -> Text
@ -207,7 +209,6 @@ termToDiffInfo blob term = case unwrap term of
-- Currently we cannot express the operator for an operator production from TreeSitter. Eventually we should be able to
-- use the term name of the operator identifier when we have that production value. Until then, I'm using a placeholder value
-- to indicate where that value should be when constructing DiffInfos.
S.Operator _ -> LeafInfo (toCategoryName term) "x"
Commented cs leaf -> BranchInfo (termToDiffInfo' <$> cs <> maybeToList leaf) (toCategoryName term) BCommented
S.Error sourceSpan _ -> ErrorInfo sourceSpan (toCategoryName term)
_ -> LeafInfo (toCategoryName term) (toTermName' term)

View File

@ -17,13 +17,9 @@ import SourceSpan
-- | and aren't pure.
type Parser fields = SourceBlob -> IO (Term Text (Record fields))
-- | Categories that are treated as fixed nodes.
fixedCategories :: Set.Set Category
fixedCategories = Set.fromList [ BinaryOperator, Pair ]
-- | Should these categories be treated as fixed nodes?
isFixed :: Category -> Bool
isFixed = flip Set.member fixedCategories
-- | Whether a category is an Operator Category
isOperator :: Category -> Bool
isOperator = flip Set.member (Set.fromList [ Operator, BinaryOperator ])
-- | Given a function that maps production names to sets of categories, produce
-- | a Constructor.
@ -49,7 +45,7 @@ termConstructor source sourceSpan info = cofree . construct
construct children | SubscriptAccess == category info = case children of
(base:element:[]) -> withDefaultInfo $ S.SubscriptAccess base element
_ -> withDefaultInfo $ S.Error sourceSpan children
construct children | Operator == category info = withDefaultInfo $ S.Operator children
construct children | isOperator (category info) = withDefaultInfo $ S.Operator children
construct children | Function == category info = case children of
(body:[]) -> withDefaultInfo $ S.Function Nothing Nothing body
(params:body:[]) | (info :< _) <- runCofree params, Params == category info ->
@ -92,7 +88,7 @@ termConstructor source sourceSpan info = cofree . construct
toTuple child | S.Leaf c <- unwrap child = [cofree (extract child :< S.Comment c)]
toTuple child = pure child
construct children | isFixed (category info) = withDefaultInfo $ S.Fixed children
construct children | Pair == (category info) = withDefaultInfo $ S.Fixed children
construct children | C.Error == category info =
withDefaultInfo $ S.Error sourceSpan children
construct children | For == (category info), Just (exprs, body) <- unsnoc children =

View File

@ -31,17 +31,20 @@ treeSitterParser language grammar blob = do
categoriesForLanguage :: Language -> Text -> Category
categoriesForLanguage language name = case (language, name) of
(JavaScript, "object") -> Object
(JavaScript, "rel_op") -> BinaryOperator -- relational operator, e.g. >, <, <=, >=, ==, !=
(JavaScript, "bool_op") -> BinaryOperator
(JavaScript, "expression_statement") -> ExpressionStatements
(JavaScript, "this_expression") -> Identifier
(JavaScript, "null") -> Identifier
(JavaScript, "undefined") -> Identifier
(JavaScript, "arrow_function") -> Function
(JavaScript, "generator_function") -> Function
(JavaScript, "delete_op") -> Operator
(JavaScript, "type_op") -> Operator
(JavaScript, "void_op") -> Operator
(JavaScript, "math_op") -> BinaryOperator -- bitwise operator, e.g. +, -, *, /.
(JavaScript, "bool_op") -> BinaryOperator -- boolean operator, e.g. ||, &&.
(JavaScript, "bitwise_op") -> BinaryOperator -- bitwise operator, e.g. ^, &, etc.
(JavaScript, "rel_op") -> BinaryOperator -- relational operator, e.g. >, <, <=, >=, ==, !=.
(JavaScript, "comma_op") -> Operator -- comma operator, e.g. expr1, expr2.
(JavaScript, "delete_op") -> Operator -- delete operator, e.g. delete x[2].
(JavaScript, "type_op") -> Operator -- type operator, e.g. typeof Object.
(JavaScript, "void_op") -> Operator -- void operator, e.g. void 2.
(JavaScript, "for_in_statement") -> For
(JavaScript, "for_of_statement") -> For

@ -1 +1 @@
Subproject commit 0e185203ca83e51f7fc2dbbb3b251f77b876a270
Subproject commit 3af143b53c8f9a2a6761d8e9ea91d47982cc3bc0