def invalidIdentTest(): src = '0xef' try: tokenize(src) except ParserException: return assert 0 == 1
def parse(src): tokens = tokenize(src) success, unparsed, declarations = parseDeclarations(tokens) if not success: raise ParserException( 'not a declaration: invalid token {} in line {}'.format( unparsed[0], unparsed[0].getLineNumber())) return declarations
def exampleDeclarationTest(): src = ''' # This is an example declaration ({q0, q1}, "a") -> (q1, "b", R) ''' tokens = tokenize(src) assert len([t for t in tokens if isinstance(t, ControlToken)]) == 11 assert len([t for t in tokens if isinstance(t, IdentToken)]) == 4 assert len([t for t in tokens if isinstance(t, StringToken)]) == 2
def whitespaceTest(): src = ' \n \n\n \t \n\t ' tokens = tokenize(src) assert len(tokens) == 0
def controlTest(): src = '( } , -> ) {' tokens = tokenize(src) assert len(tokens) == 6 for token in tokens: assert isinstance(token, ControlToken)
def stringTest(): src = '"hello" "\\" my" "world ###"' tokens = tokenize(src) assert len(tokens) == 3 for token in tokens: assert isinstance(token, StringToken)
def identTest(): src = 'q0 q1q2 q' tokens = tokenize(src) assert len(tokens) == 3 for token in tokens: assert isinstance(token, IdentToken)
def blockCommentTest(): src = '### (}{) stuff\n more "stuff"\n\n () ###' tokens = tokenize(src) assert len(tokens) == 0
def lineCommentTest(): src = '# ## # ## (hello, "W") -> (o, ' r', L), d\n' tokens = tokenize(src) assert len(tokens) == 0