From a845a1e76f8ede40507b31ac85074424b8c32c05 Mon Sep 17 00:00:00 2001 From: Blank Spruce <32396809+BlankSpruce@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:29:44 +0100 Subject: [PATCH] Make is_keyword a bit cheaper --- gersemi/ast_helpers.py | 34 +++++++++---------- gersemi/custom_command_definition_finder.py | 5 +-- .../two_word_keyword_isolator.py | 4 +-- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/gersemi/ast_helpers.py b/gersemi/ast_helpers.py index 7f55b47..cbdd65b 100644 --- a/gersemi/ast_helpers.py +++ b/gersemi/ast_helpers.py @@ -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: @@ -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 diff --git a/gersemi/custom_command_definition_finder.py b/gersemi/custom_command_definition_finder.py index 619817f..836116c 100644 --- a/gersemi/custom_command_definition_finder.py +++ b/gersemi/custom_command_definition_finder.py @@ -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 @@ -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] diff --git a/gersemi/specializations/two_word_keyword_isolator.py b/gersemi/specializations/two_word_keyword_isolator.py index d0d6266..3f928bc 100644 --- a/gersemi/specializations/two_word_keyword_isolator.py +++ b/gersemi/specializations/two_word_keyword_isolator.py @@ -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: