Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit fccef8b

Browse files
add docstrings to scanner
1 parent 5157156 commit fccef8b

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

‎scanner.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,88 @@
22

33

44
class Token:
5-
def __init__(self, token, lexim, position):
6-
self.token = token
7-
self.lexim = lexim
5+
"""Represents an token.
6+
"""
7+
8+
def __init__(self, token_type, value, position):
9+
"""
10+
Args:
11+
token_type (str): type of token like: "ID", "SEMICOLON".
12+
value (str): the actual value of token: "some_variable", ";".
13+
position (int): location of token in the buffer.
14+
"""
15+
self.token_type = token_type
16+
self.value = value
817
self.position = position
918

1019
def __str__(self):
11-
return f'{self.position}\t{self.lexim}\t{self.token}'
20+
return f'{self.position}\t{self.value}\t{self.token_type}'
1221

1322
def __repr__(self):
1423
return self.__str__()
1524

1625

1726
class UnknownTokenError(Exception):
27+
"""A custom Exception for reporting scanner error.
28+
"""
29+
1830
def __init__(self, buffer, position):
31+
"""
32+
Args:
33+
buffer (str): we can found the error in the buffer.
34+
position ([type]): location of error in the buffer.
35+
"""
1936
super().__init__()
20-
self.buffer = buffer.strip()
37+
self.buffer = buffer
2138
self.position = position
2239

2340
def __str__(self):
2441
return f'\nLexerError: Unknown token!\n\n\n{self.buffer[self.position:self.position + 30]}'
2542

2643

2744
class Scanner:
45+
"""Lexical analyzer
46+
"""
47+
2848
def __init__(self, rules, buffer):
49+
"""
50+
Args:
51+
rules (list): a list of tuples like this [("token", "regex"),...]
52+
buffer (str): content that we want to scan.
53+
"""
2954
rules_list = [f'(?P<{typ}>{reg})' for typ, reg in rules]
3055
self.regex = re.compile('|'.join(rules_list))
3156
self.buffer = buffer
57+
self.position = 0
3258

3359
def token(self):
60+
"""Get the next token that we found.
61+
62+
Raises:
63+
UnknownTokenError: raises if find a token that not matches any rules.
64+
65+
Returns:
66+
Token: token that we found.
67+
"""
3468
if self.position < len(self.buffer):
3569
if match := re.compile('\S').search(self.buffer, self.position):
3670
self.position = match.start()
3771
else:
3872
return None
3973

4074
if match := self.regex.match(self.buffer, self.position):
41-
token = Token(token=match.lastgroup, lexim=match.group(match.lastgroup), position=self.position)
75+
token = Token(token_type=match.lastgroup, value=match.group(match.lastgroup), position=self.position)
4276
self.position = match.end()
4377
return token
4478
else:
4579
raise UnknownTokenError(self.buffer, self.position)
4680

47-
def tokens_generator(self):
81+
def token_generator(self):
82+
"""generates all the tokens to the end.
83+
84+
Yields:
85+
Token: token that we found.
86+
"""
4887
self.position = 0
4988
while token := self.token():
5089
yield token

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /