diff --git a/comfy/comments.py b/comfy/comments.py index 3f46fa9f4..b865a0d48 100644 --- a/comfy/comments.py +++ b/comfy/comments.py @@ -1,67 +1,12 @@ -import comfy.parse as parse -from comfy.parse import ParseError, ParseLogicError +import re def strip_c_comments(text, strict=True): - ''' - Processes the text and strips out any C-style block "/* ... */" or line "// ..." comments found. - ''' - out = [] - input = parse.Cursor(text) - while True: - if 0: pass - elif input.match(r'\/'): - # C-style block "/* */" or line "//" comment - comment = input.prior() - if 0: pass - elif m := input.match(r'\/'): - # // line comment - if not input.match(r'.*?(?:\n|$)'): - if strict: - raise ParseLogicError(comment, f"Failed to find end of C-style // line comment") - input.match(r'.*') # consume unterminated comment (however that might be possible) - out.append('\n') - elif m := input.match(r'\*'): - # /* ... */ block comment - if not input.match(r'.*?\*\/'): - if strict: - raise ParseError(comment, f"Unterminated C-style /* ... */ block comment") - input.match(r'.*') # consume unterminated comment - out.append(' ') - else: - # it was a literal /, not a comment after all - out.append('/'); - elif m := input.match(r'[^/]+'): - out.append(m.group(0)) - elif input.match(r'$'): - break; - else: - raise ParseLogicError(input, f"Failed to match") - - return ''.join(out) + # Processes the text and strips out any C-style block "/* ... */" or line "// ..." comments found. + # from old dynamicPrompts.js: return str.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g,''); + return re.sub(r'/\*[.\n]*?(?:\*/|$)|//.*', '', text) def strip_hash_comments(text, strict=True): - ''' - Processes the text and strips out any hash "# ... " comments found. - ''' - out = [] - input = parse.Cursor(text) - while True: - if 0: pass - elif input.match(r'\#'): - comment = input.prior() - # scripting-language-style "# ...\n" comment - if not input.match(r'.*?(?:\n|$)'): - if strict: - raise ParseLogicError(comment, f"Failed to find end of script-style # ... comment") - input.match(r'.*') # consume unterminated comment (however that might be possible) - out.append('\n') - elif m := input.match(r'[^#]+'): - out.append(m.group(0)) - elif input.match(r'$'): - break; - else: - raise ParseLogicError(input, f"Failed to match") - - return ''.join(out) + # Processes the text and strips out any hash "# ... " comments found. + return re.sub(r'#.*', '', text)