X-Git-Url: https://code.kerkeslager.com/?p=fur;a=blobdiff_plain;f=tokenization.py;fp=tokenization.py;h=3131c3581d6597efbd8f9c7e12709bf95f00abee;hp=ff79307194d84c427a8569b9e4aa6d5035b438f4;hb=730c0a3faa442985f6fdb73d91a1e6b9f72e6165;hpb=eab88b322191c40168553b8671b968d1b1558084 diff --git a/tokenization.py b/tokenization.py index ff79307..3131c35 100644 --- a/tokenization.py +++ b/tokenization.py @@ -43,6 +43,7 @@ _TOKEN_MATCHERS = [ ('assignment_operator', r'='), ('addition_level_operator', r'(\+|-)'), ('multiplication_level_operator', r'(\*|//|%)'), + ('newline', r'\n'), ] _TOKEN_MATCHERS = list(map(_make_token_matcher, _TOKEN_MATCHERS)) @@ -67,11 +68,13 @@ def tokenize(source): break if not success: - raise Exception('Unexpected character "{}"'.format(source[index])) + raise Exception('Unexpected character "{}" on line {}'.format( + source[index], + line, + )) - while index < len(source) and source[index] in set(['\n']): + if token.type == 'newline': line += 1 - index += 1 if __name__ == '__main__': import unittest @@ -264,12 +267,12 @@ if __name__ == '__main__': ),), ) - def test_handles_trailing_newline(self): + def test_tokenizes_newline(self): self.assertEqual( - tokenize('print\n'), + tokenize('\n'), (Token( - type='symbol', - match='print', + type='newline', + match='\n', index=0, line=1, ),), @@ -296,6 +299,12 @@ if __name__ == '__main__': index=0, line=1, ), + Token( + type='newline', + match='\n', + index=5, + line=1, + ), Token( type='open_parenthese', match='(',