From 741c9d95e8bc5009a841f48d2ac07e53603f6ea1 Mon Sep 17 00:00:00 2001 From: Jamie Hardt Date: Mon, 10 Dec 2018 23:27:44 -0600 Subject: [PATCH] Moved TupleParser to util --- pycmx/parse_cmx.py | 44 ++------------------------------------------ pycmx/util.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/pycmx/parse_cmx.py b/pycmx/parse_cmx.py index 87b49a9..9a33854 100644 --- a/pycmx/parse_cmx.py +++ b/pycmx/parse_cmx.py @@ -1,54 +1,14 @@ # pycmx # (c) 2018 Jamie Hardt +from .util import NamedTupleParser + from .parse_cmx_statements import parse_cmx3600_statements from .cmx_event import CmxEvent, CmxTransition from collections import namedtuple from re import compile, match -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): - self.tokens = tuple_list - self.current_token = None - - def peek(self): - """Returns the token to come after the `current_token` without -popping the current token.""" - return self.tokens[0] - - def at_end(self): - "`True` if the `current_token` is the last one." - return len(self.tokens) == 0 - - def next_token(self): - "Sets `current_token` to the next token popped from the list" - self.current_token = self.peek() - self.tokens = self.tokens[1:] - - 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(): - return False - elif (type(self.peek()).__name__ == type_name ): - self.next_token() - return True - else: - return False - - 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) ) - - class CmxChannelMap: """ Represents a set of all the channels to which an event applies. diff --git a/pycmx/util.py b/pycmx/util.py index 41967df..600b510 100644 --- a/pycmx/util.py +++ b/pycmx/util.py @@ -15,3 +15,45 @@ def collimate(a_string, column_widths): return [element] + collimate(rest, column_widths[1:]) +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): + self.tokens = tuple_list + self.current_token = None + + def peek(self): + """Returns the token to come after the `current_token` without +popping the current token.""" + return self.tokens[0] + + def at_end(self): + "`True` if the `current_token` is the last one." + return len(self.tokens) == 0 + + def next_token(self): + "Sets `current_token` to the next token popped from the list" + self.current_token = self.peek() + self.tokens = self.tokens[1:] + + 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(): + return False + elif (type(self.peek()).__name__ == type_name ): + self.next_token() + return True + else: + return False + + 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) ) + +