예제 #1
0
    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)
예제 #2
0
    def test_parse_statements(self):
        """
        (letStatement | ifStatement | whileStatement | doStatement | returnStatement)*
        """
        tokens = (
            Token('keyword', 'let'), 
            Token('keyword', 'if'),
            Token('keyword', 'while'),
            Token('keyword', 'do'),   
            Token('keyword', 'return'), 
            Token('symbol', '}')  # Expect unparsed
        )

        expected = Token('statements', [
            Token('dummy', 'dummy'), 
            Token('dummy', 'dummy'), 
            Token('dummy', 'dummy'), 
            Token('dummy', 'dummy'), 
            Token('dummy', 'dummy'), 
        ])

        parser = Parser(tokens)

        parser.parse_let_statement = self._mock_parse(parser)
        parser.parse_if_statement = self._mock_parse(parser)
        parser.parse_while_statement = self._mock_parse(parser)
        parser.parse_do_statement = self._mock_parse(parser)
        parser.parse_return_statement = self._mock_parse(parser)
        
        actual = parser.parse_statements()
        self.assertEqual(expected, actual)