diff options
-rw-r--r-- | mitmproxy/command.py | 1 | ||||
-rw-r--r-- | mitmproxy/lexer.py | 67 | ||||
-rw-r--r-- | test/mitmproxy/test_lexer.py | 1 | ||||
-rw-r--r-- | test/mitmproxy/tools/console/test_defaultkeys.py | 1 |
4 files changed, 8 insertions, 62 deletions
diff --git a/mitmproxy/command.py b/mitmproxy/command.py index 4fbae533..3da3a298 100644 --- a/mitmproxy/command.py +++ b/mitmproxy/command.py @@ -13,6 +13,7 @@ from mitmproxy import exceptions from mitmproxy import lexer import mitmproxy.types + def verify_arg_signature(f: typing.Callable, args: list, kwargs: dict) -> None: sig = inspect.signature(f) try: diff --git a/mitmproxy/lexer.py b/mitmproxy/lexer.py index 31ae382c..a7024ca2 100644 --- a/mitmproxy/lexer.py +++ b/mitmproxy/lexer.py @@ -1,7 +1,6 @@ from enum import Enum import io -from typing import Union -import pdb +from typing import Union, List class State(Enum): @@ -12,13 +11,12 @@ class State(Enum): class Lexer: - def __init__(self, text: Union[str, io.StringIO]): - self._tokens = [] + def __init__(self, text: Union[str, io.StringIO]) -> None: self._count = 0 self._parsed = False self._state = State.TEXT - self._states = [] + self._states: List[State] = [] self._text_pos = 0 self._quote_start_pos = 0 @@ -39,47 +37,24 @@ class Lexer: return t def get_token(self): - try: return self.parse() - except ValueError as e: + except ValueError: raise - if len(self._tokens) > 0: - ret = self._tokens[0] - self._tokens = self._tokens[1:] - else: - ret = None - return ret - - #def get_remainder(self): - # try: - # self.parse() - # except ValueError as e: - # return self.text - # - - # return ' '.join(self._tokens) - def parse(self): acc = '' - quote = '' # used by the parser - tokens = [] + quote = '' self._state = State.TEXT - text = self.text - i = 0 whitespace = "\r\n\t " - #self.text.seek(self._text_pos) + self.text.seek(self._text_pos) while True: ch = self.text.read(1) self._text_pos += 1 - #pdb.set_trace() - - # If this is the last char of the string, let's save the token if ch == '' or ch is None: break @@ -110,7 +85,6 @@ class Lexer: self._state = State.QUOTE acc += ch elif ch == '\\': - # TODO: Does it make sense to go to State.ESCAPE from State.TEXT? self._states.append(self._state) self._state = State.ESCAPE acc += ch @@ -126,32 +100,3 @@ class Lexer: raise ValueError("No closing quotation for quote in position %d" % self._quote_start_pos) return self._token - - -if __name__ == '__main__': - - cases = [] - cases.append(r'abc') - cases.append(r'Hello World') - cases.append(r'"Hello \" World"') - cases.append(r"'Hello \' World'") - cases.append(r'"\""') - cases.append(r'abc "def\" \x bla \z \\ \e \ " xpto') - cases.append(r'') - cases.append(r' ') - cases.append(r' ') - cases.append(r' ') - cases.append(r' ') - cases.append(r'Hello World ') - cases.append('\n\n\rHello\n World With Spaces\n\n') - - for s in cases: - lex = Lexer(s) - tokens = list(lex) - - if len(tokens) == 1: - print('%s = %d token' % (str(tokens), len(tokens))) - else: - print('%s = %d tokens' % (str(tokens), len(tokens))) - - diff --git a/test/mitmproxy/test_lexer.py b/test/mitmproxy/test_lexer.py index c8b30fc6..ae706407 100644 --- a/test/mitmproxy/test_lexer.py +++ b/test/mitmproxy/test_lexer.py @@ -61,4 +61,3 @@ class TestScripts: lex = lexer.Lexer(text) with pytest.raises(ValueError, match="No closing quotation"): assert list(lex) - diff --git a/test/mitmproxy/tools/console/test_defaultkeys.py b/test/mitmproxy/tools/console/test_defaultkeys.py index be8e39f8..035f71f7 100644 --- a/test/mitmproxy/tools/console/test_defaultkeys.py +++ b/test/mitmproxy/tools/console/test_defaultkeys.py @@ -6,6 +6,7 @@ from mitmproxy import command import pytest + @pytest.mark.asyncio async def test_commands_exist(): km = keymap.Keymap(None) |