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 tokenization of option strings #253

Merged
merged 1 commit into from
Jul 13, 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: 73 additions & 55 deletions specfile/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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


class TokenType(Enum):
Expand Down Expand Up @@ -474,63 +475,80 @@ def tokenize(option_string: str) -> List[Token]:
Raises:
OptionsException if the option string is untokenizable.
"""
result = []
token = ""
quote = None
inp = list(option_string)
while inp:
c = inp.pop(0)
if c == quote:
if token:
result.append(
Token(
TokenType.QUOTED
if quote == "'"
else TokenType.DOUBLE_QUOTED,
token,
)
)
token = ""
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] = []
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 = ""
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:
result.append(Token(TokenType.DEFAULT, token))
token = ""
whitespace = c
inp = list(str(node))
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 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 token:
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))
append_default(token)
else:
token_nodes.append(node)
if token_nodes:
append_default("".join(str(n) for n in token_nodes))
return result
10 changes: 10 additions & 0 deletions tests/unit/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,16 @@ def test_options_find_option(optstring, tokens, option, result):
Token(TokenType.WHITESPACE, " "),
],
),
(
'-q -n %{name}-%{version}%[%{rc}?"-rc":""]',
[
Token(TokenType.DEFAULT, "-q"),
Token(TokenType.WHITESPACE, " "),
Token(TokenType.DEFAULT, "-n"),
Token(TokenType.WHITESPACE, " "),
Token(TokenType.DEFAULT, '%{name}-%{version}%[%{rc}?"-rc":""]'),
],
),
],
)
def test_options_tokenize(option_string, result):
Expand Down