当前位置: 首页>>代码示例>>Python>>正文


Python Tokenizer.getNextToken方法代码示例

本文整理汇总了Python中tokenizer.Tokenizer.getNextToken方法的典型用法代码示例。如果您正苦于以下问题:Python Tokenizer.getNextToken方法的具体用法?Python Tokenizer.getNextToken怎么用?Python Tokenizer.getNextToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tokenizer.Tokenizer的用法示例。


在下文中一共展示了Tokenizer.getNextToken方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: interpret_line

# 需要导入模块: from tokenizer import Tokenizer [as 别名]
# 或者: from tokenizer.Tokenizer import getNextToken [as 别名]
    def interpret_line(self, line):
        tokenizer = Tokenizer()
        tokenizer.parse(line)

        first_token = tokenizer.getNextToken()
        if first_token.type == Token.NUMBER:
            self.lines[int(first_token.value)] = tokenizer.prog[tokenizer.pos:]
            self.sort_lines()
        else:
            self.run_line(line)
开发者ID:tonyedgecombe,项目名称:pytinybasic,代码行数:12,代码来源:interpreter.py

示例2: TestTokeniser

# 需要导入模块: from tokenizer import Tokenizer [as 别名]
# 或者: from tokenizer.Tokenizer import getNextToken [as 别名]
class TestTokeniser(TestCase):
    def setUp(self):
        self.tokenizer = Tokenizer()

    def test_number(self):
        self.tokenizer.parse('100')
        token = self.tokenizer.getNextToken()

        self.assertEqual(token.NUMBER, token.type)
        self.assertEqual(100, token.value)

    def test_string(self):
        self.tokenizer.parse('"ABC"')
        token = self.tokenizer.getNextToken()

        self.assertEqual(token.STRING, token.type)
        self.assertEqual('ABC', token.value)

    def test_peek_token(self):
        self.tokenizer.parse('1+2')

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.NUMBER, token.type)
        self.assertEqual(1, token.value)

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.OPERATOR, token.type)
        self.assertEqual('+', token.value)

        token = self.tokenizer.peekNextToken()
        self.assertEqual(token.NUMBER, token.type)
        self.assertEqual(2, token.value)

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.NUMBER, token.type)
        self.assertEqual(2, token.value)


    def test_operator(self):
        self.tokenizer.parse('1+2-4')

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.NUMBER, token.type)
        self.assertEqual(1, token.value)

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.OPERATOR, token.type)
        self.assertEqual('+', token.value)

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.NUMBER, token.type)
        self.assertEqual(2, token.value)

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.OPERATOR, token.type)
        self.assertEqual('-', token.value)

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.NUMBER, token.type)
        self.assertEqual(4, token.value)

    def test_operator2(self):
        self.tokenizer.parse('"A"+"B"')

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.STRING, token.type)
        self.assertEqual('A', token.value)

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.OPERATOR, token.type)
        self.assertEqual('+', token.value)

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.STRING, token.type)
        self.assertEqual('B', token.value)

    def test_mult_operator(self):
        self.tokenizer.parse('2*3')

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.NUMBER, token.type)
        self.assertEqual(2, token.value)

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.MULTOPERATOR, token.type)
        self.assertEqual('*', token.value)

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.NUMBER, token.type)
        self.assertEqual(3, token.value)


    def test_mult_operator2(self):
        self.tokenizer.parse('2/3')

        token = self.tokenizer.getNextToken()
        self.assertEqual(token.NUMBER, token.type)
        self.assertEqual(2, token.value)

        token = self.tokenizer.getNextToken()
#.........这里部分代码省略.........
开发者ID:tonyedgecombe,项目名称:pytinybasic,代码行数:103,代码来源:test_tokenizer.py


注:本文中的tokenizer.Tokenizer.getNextToken方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。