-
-
Save nedbat/3676fdc9b3135b8f6a6c44447af906e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% python3 tokens.py | |
id : text='x' | |
punct : text='=' | |
int : text='123' | |
punct : text='+' | |
string : text='"hello #99"' | |
id : text='print' | |
punct : text='(' | |
id : text='hello' | |
punct : text='.' | |
id : text='bye' | |
punct : text='[' | |
int : text='0' | |
punct : text=']' | |
punct : text='+' | |
float : text='3.14' | |
punct : text=',' | |
string : text="'single'" | |
punct : text=')' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
TOKEN_RX = r"""(?xm) | |
(?P<string> ".*?"|'.*?' )| | |
(?P<float> \d*(\d\.|\.\d)\d* )| | |
(?P<int> \d+ )| | |
(?P<id> [_a-zA-Z][_a-zA-Z0-9]* )| | |
(?P<punct> [(){}:\[\]=.,+*/-] )| | |
( \#.*$ )| | |
( \s+ ) | |
""" | |
def tokens(text): | |
for match in re.finditer(TOKEN_RX, text): | |
if match.lastgroup: | |
yield (match.lastgroup, match[0]) | |
TEXT = """ | |
x = 123 + "hello #99" # ignore me! | |
print(hello.bye[0] + 3.14, 'single') | |
""" | |
for kind, text in tokens(TEXT): | |
print(f"{kind:7}: {text=}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment