mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-06-22 15:59:45 +08:00
Replace comfy.parse-based comment parsers with re.sub.
Less code is better, right? But we lost the ability to report errors for unterminated C block comments. Current differences from the js version: We consume unterminated C block comments.
This commit is contained in:
parent
9efdda21a8
commit
ed253b31df
@ -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)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user