Created
April 10, 2012 20:21
-
-
Save rhelmer/2354202 to your computer and use it in GitHub Desktop.
lexer
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
| import shlex | |
| import re | |
| from cStringIO import StringIO | |
| c1 = 'jobname|1d|4:30' | |
| c2 = 'jobname | 1d | 4:30' | |
| def tokenize(t): | |
| if re.match('(\d+)d', t): return 'DAY' | |
| if re.match('(\d+)h', t): return 'HOUR' | |
| if re.match('(\d+)m', t): return 'MIN' | |
| if re.match('(\d+)s', t): return 'SEC' | |
| if re.match('(\d+):(\d+)', t): return 'TIME' | |
| if re.match('\w+', t): return 'JOB' | |
| return 'UNKNOWN' | |
| for job in [c1, c2]: | |
| lex = shlex.shlex(StringIO(job)) | |
| lex.whitespace += '|' | |
| lex.whitespace_split = True | |
| while 1: | |
| token = lex.get_token() | |
| if token == '': break | |
| print token | |
| print tokenize(token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment