about summary refs log blame commit diff
path: root/users/wpcarro/scratch/simple-select/parser.py
blob: d26f970e57a23d43b27c5109813e5efeb136be39 (plain) (tree)
1
2
3
4
5
6
7
8
9
10




                               

                                         
 

                                      
 
                      

                                
                              

                        

                             


                       

                                             
 





                                                                       
class Parser(object):
    def __init__(self, tokens):
        self.tokens = tokens
        self.i = 0

    def exhausted(self):
        return self.i >= len(self.tokens)

    def peek(self, n=0):
        return self.tokens[self.i + n]

    def advance(self):
        if not self.exhausted():
            self.i += 1
        return self.peek(n=-1)

    def match(self, xs):
        if self.peek() in xs:
            self.advance()
            return True
        return False

    def test(self, predicate):
        return predicate(self.tokens, self.i)

    def expect(self, predicate):
        if self.exhausted():
            raise Exception("Unexpected EOL")
        if predicate(self.peek()):
            return self.advance()
        raise Exception("Unexpected token: \"{}\"".format(self.peek()))