def test_parse_while_statement(self): """ 'while' '(' expression ')' '{' statements '}' """ tokens = ( Token('keyword', 'while'), Token('symbol', '('), Token('dummy', 'dummy'), # Dummy expression Token('symbol', ')'), Token('symbol', '{'), Token('dummy', 'dummy'), # Dummy expression Token('symbol', '}'), ) expected = Token('whileStatement', [ Token('keyword', 'while'), Token('symbol', '('), Token('dummy', 'dummy'), # Dummy expression Token('symbol', ')'), Token('symbol', '{'), Token('dummy', 'dummy'), # Dummy statements Token('symbol', '}'), ]) parser = Parser(tokens) parser.parse_expression = self._mock_parse(parser) parser.parse_statements = self._mock_parse(parser) actual = parser.parse_while_statement() self.assertEqual(expected, actual)
def test_parse_let_statement_index(self): """ 'let' varName ('[' expression ']')? '=' expression ';' """ tokens = ( Token('keyword', 'let'), Token('identifier', 'foo'), Token('symbol', '['), Token('dummy', 'dummy'), # Dummy expression Token('symbol', ']'), Token('symbol', '='), Token('dummy', 'dummy'), # Dummy expression Token('symbol', ';'), ) expected = Token('letStatement', [ Token('keyword', 'let'), Token('identifier', 'foo'), Token('symbol', '['), Token('dummy', 'dummy'), # Dummy expression Token('symbol', ']'), Token('symbol', '='), Token('dummy', 'dummy'), # Dummy expression Token('symbol', ';'), ]) parser = Parser(tokens) parser.parse_expression = self._mock_parse(parser) actual = parser.parse_let_statement() self.assertEqual(expected, actual)
def test_parse_return_statement(self): """ 'return' expression? ';' """ tokens = ( Token('keyword', 'return'), Token('dummy', 'dummy'), # Dummy expression Token('symbol', ';'), ) expected = Token('returnStatement', [ Token('keyword', 'return'), Token('dummy', 'dummy'), # Dummy expression Token('symbol', ';'), ]) parser = Parser(tokens) parser.parse_expression = self._mock_parse(parser) actual = parser.parse_return_statement() self.assertEqual(expected, actual)
def test_parse_expression(self): """ term (op term)* """ tokens = ( Token('integerConstant', '1'), Token('symbol', '+'), Token('integerConstant', '1'), Token('symbol', '+'), Token('integerConstant', '1'), ) expected = Token('expression', [ Token('dummy', 'dummy'), Token('symbol', '+'), Token('dummy', 'dummy'), Token('symbol', '+'), Token('dummy', 'dummy'), ]) parser = Parser(tokens) parser.parse_term = self._mock_parse(parser) actual = parser.parse_expression() self.assertEqual(expected, actual)