diff options
author | Maximilian Hils <git@maximilianhils.com> | 2019-11-19 18:14:00 +0100 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2019-11-19 18:21:14 +0100 |
commit | 76e648410745c61f7a659e864230b6154dc43ced (patch) | |
tree | 3c17752752f382d9ce22bfc668817ec46a6caafe /mitmproxy/command_lexer.py | |
parent | 74f5fa6a7736f116416c242b159e6b0525e6fe5b (diff) | |
download | mitmproxy-76e648410745c61f7a659e864230b6154dc43ced.tar.gz mitmproxy-76e648410745c61f7a659e864230b6154dc43ced.tar.bz2 mitmproxy-76e648410745c61f7a659e864230b6154dc43ced.zip |
fix lexing, sort of
Diffstat (limited to 'mitmproxy/command_lexer.py')
-rw-r--r-- | mitmproxy/command_lexer.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/mitmproxy/command_lexer.py b/mitmproxy/command_lexer.py new file mode 100644 index 00000000..f042f3c9 --- /dev/null +++ b/mitmproxy/command_lexer.py @@ -0,0 +1,49 @@ +import ast +import re + +import pyparsing + +# TODO: There is a lot of work to be done here. +# The current implementation is written in a way that _any_ input is valid, +# which does not make sense once things get more complex. + +PartialQuotedString = pyparsing.Regex( + re.compile( + r''' + (["']) # start quote + (?: + (?!\1)[^\\] # unescaped character that is not our quote nor the begin of an escape sequence. We can't use \1 in [] + | + (?:\\.) # escape sequence + )* + (?:\1|$) # end quote + ''', + re.VERBOSE + ) +) + +expr = pyparsing.ZeroOrMore( + PartialQuotedString + | pyparsing.Word(" \r\n\t") + | pyparsing.CharsNotIn("""'" \r\n\t""") +).leaveWhitespace() + + +def quote(val: str) -> str: + if val and all(char not in val for char in "'\" \r\n\t"): + return val + return repr(val) # TODO: More of a hack. + + +def unquote(x: str) -> str: + quoted = ( + (x.startswith('"') and x.endswith('"')) + or + (x.startswith("'") and x.endswith("'")) + ) + if quoted: + try: + x = ast.literal_eval(x) + except Exception: + x = x[1:-1] + return x |