1
0
Fork 0

builtin functions: fixed parser to correctly skip quoted vars / funcs

Now it skips:
\${VAR}
\${f:FUNC}

Instead of:
\${f:FUNC\}

This second form is now not supported.

Also simplified the parser.

Signed-off-by: Jaroslav Škarvada <jskarvad@redhat.com>
This commit is contained in:
Jaroslav Škarvada 2017-04-06 10:41:19 +02:00
parent a1b9e60644
commit c4c7387978
No known key found for this signature in database
GPG key ID: D8E1C00E076E840B

View file

@ -32,8 +32,8 @@ class Functions():
def _curr_substr(self, _len):
return self._str[self._cnt:self._cnt + _len]
def _push_pos(self):
self._stack.append(self._cnt)
def _push_pos(self, esc):
self._stack.append((esc, self._cnt))
def _sub(self, a, b, s):
self._str = self._str[:a] + s + self._str[b + 1:]
@ -63,20 +63,21 @@ class Functions():
def _process(self, s):
self._parse_init(s)
while self._cnt < self._len:
if self._esc:
self._esc = False
if self._curr_char() == "}":
try:
si = self._stack.pop()
except IndexError:
log.error("invalid variable syntax, non pair '}' in: '%s'" % s)
return self._str
# if not escaped
if not si[0]:
self._process_func(si[1])
elif self._curr_substr(2) == "${":
self._push_pos(self._esc)
if self._curr_char() == "\\":
self._esc = True
else:
if self._curr_char() == "\\":
self._esc = True
elif self._curr_char() == "}":
try:
_from = self._stack.pop()
except IndexError:
log.error("invalid variable syntax, non pair '}' in: '%s'" % s)
return self._str
self._process_func(_from)
elif self._curr_substr(2) == "${":
self._push_pos()
self._esc = False
self._cnt += 1
if len(self._stack):
log.error("invalid varialbe syntax, non pair '{' in: '%s'" % s)
@ -86,4 +87,4 @@ class Functions():
if s is None or s == "":
return s
# expand functions and convert all \${f:*} to ${f:*} (unescape)
return re.sub(r'\\(\${f:.*\\})', r'\1', self._process(s))
return re.sub(r'\\(\${f:.*})', r'\1', self._process(s))