Skip to content

Commit

Permalink
Make is_keyword a bit cheaper
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankSpruce committed Jan 13, 2025
1 parent 694b370 commit a845a1e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
34 changes: 16 additions & 18 deletions gersemi/ast_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,20 @@ def contains_line_comment(nodes) -> bool:
return any(map(lambda node: isinstance(node, Tree) and visit(node), nodes))


def is_keyword(keyword):
def impl(node):
if is_unquoted_argument(node):
return node.children[0] == keyword
def is_keyword(keyword, node):
if is_unquoted_argument(node):
return node.children[0] == keyword

if is_quoted_argument(node):
return (len(node.children) > 0) and (node.children[0] == keyword)
if is_quoted_argument(node):
return (len(node.children) > 0) and (node.children[0] == keyword)

if is_bracket_argument(node):
return (len(node.children) > 0) and (node.children[0] == keyword)
if is_bracket_argument(node):
return (len(node.children) > 0) and (node.children[0] == keyword)

if is_commented_argument(node):
return (len(node.children) > 0) and impl(node.children[0])
if is_commented_argument(node):
return (len(node.children) > 0) and is_keyword(keyword, node.children[0])

return False

return impl
return False


class KeywordMatcher:
Expand All @@ -82,13 +79,14 @@ def __init__(self, keywords):
def __call__(self, other):
for k in self.keywords:
if isinstance(k, str):
if is_keyword(k)(other):
if is_keyword(k, other):
return True

if isinstance(k, tuple) and is_keyword_argument(other):
front, *_, back = other.children
elif isinstance(k, tuple) and is_keyword_argument(other):
front_pattern, back_pattern = k
if is_keyword(front_pattern)(front) and is_keyword(back_pattern)(back):
if not is_keyword(front_pattern, other.children[0]):
continue

if is_keyword(back_pattern, other.children[-1]):
return True

return False
Expand Down
5 changes: 1 addition & 4 deletions gersemi/custom_command_definition_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ def line_comment(self, children):
bracket_comment = _discard


is_parse_argv = is_keyword("PARSE_ARGV")


class CMakeInterpreter(Interpreter):
def __init__(self, filepath, stack=None):
self.stack = {} if stack is None else stack
Expand Down Expand Up @@ -81,7 +78,7 @@ def _new_command(self, arguments):
raise RuntimeError

def _cmake_parse_arguments(self, arguments):
if is_parse_argv(arguments.children[0]):
if is_keyword("PARSE_ARGV", arguments.children[0]):
keywords = arguments.children[3:6]
else:
keywords = arguments.children[1:4]
Expand Down
4 changes: 2 additions & 2 deletions gersemi/specializations/two_word_keyword_isolator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def __init__(self, lhs, rhs):
self.rhs = rhs

def _is_lhs(self, node):
return is_keyword(self.lhs)(node)
return is_keyword(self.lhs, node)

def _is_rhs(self, node):
return is_keyword(self.rhs)(node)
return is_keyword(self.rhs, node)

def arguments(self, children):
if len(children) <= 1:
Expand Down

0 comments on commit a845a1e

Please sign in to comment.