File: tokenizer.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (70 lines) | stat: -rw-r--r-- 2,296 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class GenericTokenizer:
    """
    Tokenizer for a KeyConditionExpression. Should be used as an iterator.
    The final character to be returned will be an empty string, to notify the caller that we've reached the end.
    """

    def __init__(self, expression: str):
        self.expression = expression
        self.token_pos = 0

    def __iter__(self) -> "GenericTokenizer":
        return self

    def is_eof(self) -> bool:
        return self.peek() == ""

    def peek(self, length: int = 1) -> str:
        """
        Peek the next character without changing the position
        """
        try:
            return self.expression[self.token_pos : self.token_pos + length]
        except IndexError:
            return ""

    def __next__(self) -> str:
        """
        Returns the next character, or an empty string if we've reached the end of the string.
        Calling this method again will result in a StopIterator
        """
        try:
            result = self.expression[self.token_pos]
            self.token_pos += 1
            return result
        except IndexError:
            if self.token_pos == len(self.expression):
                self.token_pos += 1
                return ""
            raise StopIteration

    def read_until(self, phrase: str) -> str:
        chars_read = ""
        for char in self:
            chars_read += char
            if self.peek(len(phrase)) == phrase:
                return chars_read
        return chars_read

    def skip_characters(self, phrase: str, case_sensitive: bool = False) -> None:
        """
        Skip the characters in the supplied phrase.
        If any other character is encountered instead, this will fail.
        If we've already reached the end of the iterator, this will fail.
        """
        for ch in phrase:
            if case_sensitive:
                assert self.expression[self.token_pos] == ch
            else:
                assert self.expression[self.token_pos] in [ch.lower(), ch.upper()]
            self.token_pos += 1

    def skip_white_space(self) -> None:
        """
        Skip any whitespace characters that are coming up
        """
        try:
            while self.peek() == " ":
                self.token_pos += 1
        except IndexError:
            pass