#!/usr/bin/env python
# I, Danny Milosavljevic, hereby place this file into the public domain

class Scanner(object):
    def __init__(self, stream):
        self.stream = stream
        self.input = None
        self.position = 0
        self.lineNumber = 1
    def err(self, expectedText, gotText = None):
        raise RuntimeError("expected %r but got %r near line %d" % (expectedText, gotText or self.input, self.lineNumber))
    def push(self, stream, position, lineNumber):
        self.stream = stream
        self.position = position
        self.lineNumber = lineNumber
    def consume(self, expectedText = None):
        if expectedText is not None and expectedText != self.input:
            self.err(expectedText)
        oldInput = self.input
        if oldInput == "\n":
            self.lineNumber += 1
        self.position += 1
        self.input = self.stream.read(1)
        if self.input == "":
            self.input = None
        return(oldInput)
    def parseOptionalWhitespace(self):
        while self.input in [" ", "\t"]:
            self.consume()
    def parseWhitespace(self):
        assert(self.input in [" ", "\n"])
        self.parseOptionalWhitespace()
        