@@ -77,6 +77,7 @@ def emit(self, t: TokenType) -> None:
7777
7878 def next (self ) -> str :
7979 """Return the next character, or the empty string if no more characters."""
80+ # TODO: benchmark ty/except approach
8081 if self .pos >= len (self .query ):
8182 return ""
8283
@@ -100,18 +101,18 @@ def backup(self) -> None:
100101
101102 def peek (self ) -> str :
102103 """Return the next character without advancing the pointer."""
104+ # TODO: benchmark try/except without self.next()
103105 c = self .next ()
104106 if c :
105107 self .backup ()
106108 return c
107109
108- def accept (self , pattern : Pattern [str ]) -> bool :
109- """Increment the pointer if the current character matches _pattern_."""
110- c = self .next ()
111- if pattern .match (c ):
110+ def accept (self , s : str ) -> bool :
111+ """Increment the pointer if the current position starts with _s_."""
112+ # TODO: benchmark using accept instead of accept_match for known words
113+ if self .query .startswith (s , self .pos ):
114+ self .pos += len (s )
112115 return True
113- if c :
114- self .backup ()
115116 return False
116117
117118 def accept_match (self , pattern : Pattern [str ]) -> bool :
@@ -140,13 +141,16 @@ def ignore_whitespace(self) -> bool:
140141
141142 def error (self , msg : str ) -> None :
142143 """Emit an error token."""
144+ # TODO: move msg out of Token.value. We'll need the value too when implementing
145+ # better error messages.
143146 self .tokens .append (Token (TokenType .ERROR , msg , self .pos , self .query ))
144147
145148
146149StateFn = Callable [[Lexer ], Optional ["StateFn" ]]
147150
148151
149152def lex_root (l : Lexer ) -> Optional [StateFn ]: # noqa: D103
153+ # TODO: benchmark peek/next instead of next/backup
150154 c = l .next ()
151155
152156 if c != "$" :
@@ -392,6 +396,7 @@ def lex_inside_filter(l: Lexer) -> Optional[StateFn]: # noqa: D103, PLR0915, PL
392396 l .backup ()
393397
394398 # numbers
399+ # TODO: try accept_match(RE_FLOAT), including negative exponent
395400 if l .accept_match (RE_INT ):
396401 if l .peek () == "." :
397402 # A float
@@ -474,6 +479,7 @@ def _lex_string(l: Lexer) -> Optional[StateFn]:
474479 l .next ()
475480 continue
476481
482+ # TODO: replace use of `head` with peek
477483 if c == "\\ " and not RE_ESCAPE .match (head ):
478484 l .error ("invalid escape" )
479485 return None
0 commit comments