Skip to content

Commit 4c14db3

Browse files
committed
lexer: inline position_after_whitespace function
Replicates graphql/graphql-js@e86b4e4
1 parent 637655b commit 4c14db3

File tree

1 file changed

+45
-47
lines changed

1 file changed

+45
-47
lines changed

src/graphql/language/lexer.py

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -102,59 +102,57 @@ def read_token(self, prev: Token) -> Token:
102102
body = source.body
103103
body_length = len(body)
104104

105-
pos = self.position_after_whitespace(body, prev.end)
106-
line = self.line
107-
col = 1 + pos - self.line_start
105+
pos = prev.end
106+
while pos < body_length:
107+
char = body[pos] # SourceCharacter
108108

109-
if pos >= body_length:
110-
return Token(TokenKind.EOF, body_length, body_length, line, col, prev)
111-
112-
char = body[pos]
113-
kind = _KIND_FOR_PUNCT.get(char)
114-
if kind:
115-
return Token(kind, pos, pos + 1, line, col, prev)
116-
if char == "#":
117-
return self.read_comment(pos, line, col, prev)
118-
elif char == ".":
119-
if body[pos + 1 : pos + 3] == "..":
120-
return Token(TokenKind.SPREAD, pos, pos + 3, line, col, prev)
121-
elif "A" <= char <= "Z" or "a" <= char <= "z" or char == "_":
122-
return self.read_name(pos, line, col, prev)
123-
elif "0" <= char <= "9" or char == "-":
124-
return self.read_number(pos, char, line, col, prev)
125-
elif char == '"':
126-
if body[pos + 1 : pos + 3] == '""':
127-
return self.read_block_string(pos, line, col, prev)
128-
return self.read_string(pos, line, col, prev)
129-
130-
raise GraphQLSyntaxError(source, pos, unexpected_character_message(char))
131-
132-
def position_after_whitespace(self, body: str, start_position: int) -> int:
133-
"""Go to next position after a whitespace.
134-
135-
Reads from body starting at start_position until it finds a non-whitespace
136-
character, then returns the position of that character for lexing.
137-
"""
138-
body_length = len(body)
139-
position = start_position
140-
while position < body_length:
141-
char = body[position]
142109
if char in " \t,\ufeff":
143-
position += 1
110+
pos += 1
111+
continue
144112
elif char == "\n":
145-
position += 1
113+
pos += 1
146114
self.line += 1
147-
self.line_start = position
148-
elif char == "\r": # pragma: no cover
149-
if body[position + 1 : position + 2] == "\n":
150-
position += 2
115+
self.line_start = pos
116+
continue
117+
elif char == "\r":
118+
if body[pos + 1 : pos + 2] == "\n":
119+
pos += 2
151120
else:
152-
position += 1
121+
pos += 1
153122
self.line += 1
154-
self.line_start = position
155-
else: # pragma: no cover
156-
break
157-
return position
123+
self.line_start = pos
124+
continue
125+
126+
line = self.line
127+
col = 1 + pos - self.line_start
128+
129+
kind = _KIND_FOR_PUNCT.get(char)
130+
if kind:
131+
return Token(kind, pos, pos + 1, line, col, prev)
132+
133+
if "A" <= char <= "Z" or "a" <= char <= "z" or char == "_":
134+
return self.read_name(pos, line, col, prev)
135+
136+
if "0" <= char <= "9" or char == "-":
137+
return self.read_number(pos, char, line, col, prev)
138+
139+
if char == "#":
140+
return self.read_comment(pos, line, col, prev)
141+
142+
if char == '"':
143+
if body[pos + 1 : pos + 3] == '""':
144+
return self.read_block_string(pos, line, col, prev)
145+
return self.read_string(pos, line, col, prev)
146+
147+
if char == ".":
148+
if body[pos + 1 : pos + 3] == "..":
149+
return Token(TokenKind.SPREAD, pos, pos + 3, line, col, prev)
150+
151+
raise GraphQLSyntaxError(source, pos, unexpected_character_message(char))
152+
153+
line = self.line
154+
col = 1 + pos - self.line_start
155+
return Token(TokenKind.EOF, body_length, body_length, line, col, prev)
158156

159157
def read_comment(self, start: int, line: int, col: int, prev: Token) -> Token:
160158
"""Read a comment token from the source file."""

0 commit comments

Comments
 (0)