Skip to content

Instantly share code, notes, and snippets.

@DylanLukes
Created January 25, 2016 22:39
Show Gist options
  • Save DylanLukes/79478f8bbcfa37844bbf to your computer and use it in GitHub Desktop.
Save DylanLukes/79478f8bbcfa37844bbf to your computer and use it in GitHub Desktop.
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