Created
January 25, 2016 22:39
-
-
Save DylanLukes/79478f8bbcfa37844bbf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def lex(line): | |
| """ | |
| Consumes a line and yields tokens for numbers and relevant | |
| symbols and words. | |
| """ | |
| for s in re.split('[^a-zA-Z0-9_%$/.]+', line): | |
| # Skip empty strings. We don't need them. | |
| if not s: | |
| continue | |
| if re.match('^-?[0-9]+$', s): | |
| yield Token.Integer(s) | |
| elif re.match('^\/[0-9]+$', s): | |
| yield Token.Denominator(s) | |
| elif re.match('^\.$', s): | |
| yield Token.Symbol(s) | |
| else: | |
| yield s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment