#!/usr/bin/env python # I, Danny Milosavljevic, place this file in the public domain. import exceptions import sys import codecs class ParseError(exceptions.Exception): pass class Scanner(object): def __init__(self, stream, position = 0): self.stream = codecs.getreader("UTF-8")(stream) self.position = position self.input = None def err(self, expected_input, got_input): message_text = "expected '%s', got '%s'" % (expected_input, got_input) raise ParseError(message_text) def consume(self, expected_input = None): old_input = self.input if expected_input is not None: if expected_input != old_input: self.err(expected_input, old_input) self.input = self.stream.read(1) self.position = self.position + 1 return old_input def whitespace_P(input): return input == u" " def combining_P(input): if input == u"": return False ord_input = ord(input) return ((ord_input >= 0x0300) and (ord_input < 0x0370)) or \ ((ord_input >= 0x20D0) and (ord_input < 0x2100))