mirror of
https://github.com/iluvcapra/pycmx.git
synced 2026-01-02 01:40:58 +00:00
Added some docstrings
This commit is contained in:
@@ -8,22 +8,33 @@ from collections import namedtuple
|
|||||||
from re import compile, match
|
from re import compile, match
|
||||||
|
|
||||||
class NamedTupleParser:
|
class NamedTupleParser:
|
||||||
|
"""
|
||||||
|
Accepts a list of namedtuple and the client can step through the list with
|
||||||
|
parser operations such as `accept()` and `expect()`
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, tuple_list):
|
def __init__(self, tuple_list):
|
||||||
self.tokens = tuple_list
|
self.tokens = tuple_list
|
||||||
self.current_token = None
|
self.current_token = None
|
||||||
|
|
||||||
def peek(self):
|
def peek(self):
|
||||||
|
"""Returns the token to come after the `current_token` without
|
||||||
|
popping the current token."""
|
||||||
return self.tokens[0]
|
return self.tokens[0]
|
||||||
|
|
||||||
def at_end(self):
|
def at_end(self):
|
||||||
|
"`True` if the `current_token` is the last one."
|
||||||
return len(self.tokens) == 0
|
return len(self.tokens) == 0
|
||||||
|
|
||||||
def next_token(self):
|
def next_token(self):
|
||||||
|
"Sets `current_token` to the next token popped from the list"
|
||||||
self.current_token = self.peek()
|
self.current_token = self.peek()
|
||||||
self.tokens = self.tokens[1:]
|
self.tokens = self.tokens[1:]
|
||||||
|
|
||||||
def accept(self, type_name):
|
def accept(self, type_name):
|
||||||
|
"""If the next token.__name__ is `type_name`, returns true and advances
|
||||||
|
to the next token with `next_token()`."""
|
||||||
if self.at_end():
|
if self.at_end():
|
||||||
return False
|
return False
|
||||||
elif (type(self.peek()).__name__ == type_name ):
|
elif (type(self.peek()).__name__ == type_name ):
|
||||||
@@ -33,6 +44,9 @@ class NamedTupleParser:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def expect(self, type_name):
|
def expect(self, type_name):
|
||||||
|
"""
|
||||||
|
If the next token.__name__ is `type_name`, the parser is advanced.
|
||||||
|
If it is not, an assertion failure occurs."""
|
||||||
assert( self.accept(type_name) )
|
assert( self.accept(type_name) )
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user