diff --git a/prompt_control/parser.py b/prompt_control/parser.py index 785bb64..641dd74 100644 --- a/prompt_control/parser.py +++ b/prompt_control/parser.py @@ -408,7 +408,6 @@ def expand_macros(text): return text for search, replace in replacements: res = substitute_defcall(res, search, replace) - res = substitute_def(res, search, replace) if res == prevres: break prevres = res @@ -427,10 +426,12 @@ def substitute_def(text, search, replace): def substitute_defcall(text, search, replace): name, default_args = search - text, defns = get_function(text, name, defaults=None, placeholder=f"DEFNCALL{search}") - for i, defn in enumerate(defns): - ph = f"\0DEFNCALL{search}{i}\0" - paramvals = [x.strip() for x in defn.split(";")] + text, defns = get_function(text, name, defaults=None, placeholder=f"DEFNCALL{name}") + for i, parameters in enumerate(defns): + ph = f"\0DEFNCALL{name}{i}\0" + paramvals = [] + if parameters is not None: + paramvals = [x.strip() for x in parameters.split(";")] r = replace for i, v in enumerate(paramvals): r = re.sub(rf"\${i+1}\b", v, r) diff --git a/prompt_control/utils.py b/prompt_control/utils.py index 7650fb5..de3544a 100644 --- a/prompt_control/utils.py +++ b/prompt_control/utils.py @@ -88,16 +88,22 @@ def find_closing_paren(text, start): def get_function(text, func, defaults, return_func_name=False, placeholder="", return_dict=False): - rex = re.compile(rf"\b{func}\(", re.MULTILINE) + rex = re.compile(rf"\b{func}\b", re.MULTILINE) instances = [] match = rex.search(text) count = 0 while match: # Match start, content start - start, after_first_paren = match.span() - funcname = text[start : after_first_paren - 1] - end = find_closing_paren(text, after_first_paren) - args = parse_strings(text[after_first_paren:end], defaults) + start, at_paren = match.span() + funcname = text[start:at_paren] + after_first_paren = at_paren + 1 + if text[at_paren:after_first_paren] == "(": + end = find_closing_paren(text, after_first_paren) + args = parse_strings(text[after_first_paren:end], defaults) + end += 1 + else: + end = at_paren + args = None ph = None if placeholder: ph = f"\0{placeholder}{count}\0"