mirror of
https://github.com/iluvcapra/pycmx.git
synced 2026-01-01 17:30:55 +00:00
Moved TupleParser to util
This commit is contained in:
@@ -1,54 +1,14 @@
|
|||||||
# pycmx
|
# pycmx
|
||||||
# (c) 2018 Jamie Hardt
|
# (c) 2018 Jamie Hardt
|
||||||
|
|
||||||
|
from .util import NamedTupleParser
|
||||||
|
|
||||||
from .parse_cmx_statements import parse_cmx3600_statements
|
from .parse_cmx_statements import parse_cmx3600_statements
|
||||||
from .cmx_event import CmxEvent, CmxTransition
|
from .cmx_event import CmxEvent, CmxTransition
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from re import compile, match
|
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:
|
class CmxChannelMap:
|
||||||
"""
|
"""
|
||||||
Represents a set of all the channels to which an event applies.
|
Represents a set of all the channels to which an event applies.
|
||||||
|
|||||||
@@ -15,3 +15,45 @@ def collimate(a_string, column_widths):
|
|||||||
return [element] + collimate(rest, column_widths[1:])
|
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) )
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user