Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of quotes during option tokenization #329

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 58 additions & 70 deletions specfile/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from specfile.exceptions import OptionsException
from specfile.formatter import formatted
from specfile.value_parser import Node, StringLiteral, ValueParser
from specfile.value_parser import StringLiteral, ValueParser


class TokenType(Enum):
Expand Down Expand Up @@ -476,79 +476,67 @@ def tokenize(option_string: str) -> List[Token]:
OptionsException if the option string is untokenizable.
"""
result: List[Token] = []

def append_default(s):
if result and result[-1].type == TokenType.DEFAULT:
result[-1].value += s
else:
result.append(Token(TokenType.DEFAULT, s))

token_nodes: List[Node] = []
token = ""
quote = None
inp = []
for node in ValueParser.parse(option_string):
if isinstance(node, StringLiteral):
if token_nodes:
append_default("".join(str(n) for n in token_nodes))
token_nodes = []
token = ""
inp.extend(list(str(node)))
continue
inp.append(str(node))
while inp:
c = inp.pop(0)
if c == quote:
if token:
result.append(
Token(
TokenType.QUOTED
if quote == "'"
else TokenType.DOUBLE_QUOTED,
token,
)
)
token = ""
quote = None
inp = list(str(node))
continue
if quote:
if c == "\\":
if not inp:
raise OptionsException("No escaped character")
c = inp.pop(0)
if c != quote:
token += "\\"
token += c
continue
if c.isspace():
if token:
result.append(Token(TokenType.DEFAULT, token))
token = ""
whitespace = c
while inp:
c = inp.pop(0)
if c == quote:
if token:
result.append(
Token(
TokenType.QUOTED
if quote == "'"
else TokenType.DOUBLE_QUOTED,
token,
)
)
token = ""
quote = None
continue
if quote:
if c == "\\":
if not inp:
raise OptionsException("No escaped character")
c = inp.pop(0)
if c != quote:
token += "\\"
token += c
continue
if c.isspace():
if token:
append_default(token)
token = ""
whitespace = c
while inp:
c = inp.pop(0)
if not c.isspace():
break
whitespace += c
else:
result.append(Token(TokenType.WHITESPACE, whitespace))
break
inp.insert(0, c)
result.append(Token(TokenType.WHITESPACE, whitespace))
continue
if c in ('"', "'"):
if token:
append_default(token)
token = ""
quote = c
continue
if c == "\\":
if not inp:
raise OptionsException("No escaped character")
c = inp.pop(0)
token += c
if quote:
raise OptionsException("No closing quotation")
if not c.isspace():
break
whitespace += c
else:
result.append(Token(TokenType.WHITESPACE, whitespace))
break
inp.insert(0, c)
result.append(Token(TokenType.WHITESPACE, whitespace))
continue
if c in ('"', "'"):
if token:
append_default(token)
else:
token_nodes.append(node)
if token_nodes:
append_default("".join(str(n) for n in token_nodes))
result.append(Token(TokenType.DEFAULT, token))
token = ""
quote = c
continue
if c == "\\":
if not inp:
raise OptionsException("No escaped character")
c = inp.pop(0)
token += c
if quote:
raise OptionsException("No closing quotation")
if token:
result.append(Token(TokenType.DEFAULT, token))
return result
30 changes: 30 additions & 0 deletions tests/unit/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,36 @@ def test_options_find_option(optstring, tokens, option, result):
Token(TokenType.DEFAULT, '%{name}-%{version}%[%{rc}?"-rc":""]'),
],
),
(
"-q -n '%{name}-%{version}'",
[
Token(TokenType.DEFAULT, "-q"),
Token(TokenType.WHITESPACE, " "),
Token(TokenType.DEFAULT, "-n"),
Token(TokenType.WHITESPACE, " "),
Token(TokenType.QUOTED, "%{name}-%{version}"),
],
),
(
'-q -n "%{name}-%{version}"',
[
Token(TokenType.DEFAULT, "-q"),
Token(TokenType.WHITESPACE, " "),
Token(TokenType.DEFAULT, "-n"),
Token(TokenType.WHITESPACE, " "),
Token(TokenType.DOUBLE_QUOTED, "%{name}-%{version}"),
],
),
(
'-q -n \'%{name}-%{version}%[%{rc}?"-rc":""]\'',
[
Token(TokenType.DEFAULT, "-q"),
Token(TokenType.WHITESPACE, " "),
Token(TokenType.DEFAULT, "-n"),
Token(TokenType.WHITESPACE, " "),
Token(TokenType.QUOTED, '%{name}-%{version}%[%{rc}?"-rc":""]'),
],
),
],
)
def test_options_tokenize(option_string, result):
Expand Down