• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python lexer.lex函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中nine.lexer.lex函数的典型用法代码示例。如果您正苦于以下问题:Python lex函数的具体用法?Python lex怎么用?Python lex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: testNewParser

    def testNewParser(self):
        program = lex(
            "print 'Hello World!'\n"
            "print 42"
        )

        ast = parse(program)
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:7,代码来源:parsertest.py


示例2: testBadParse

    def testBadParse(self):
        program = 'if 1<2\n    pass\n'
        tokens = lex(program)
        def ifParse():
            IfStatement.parse(tokens)

        self.failUnlessRaises(error.SyntaxError, ifParse)
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:7,代码来源:iftest.py


示例3: testIndentation

    def testIndentation(self):
        result = lex(util.source('''
            0
                4

                4
                    8

            0
              2
                  6'''))

        EOS = END_OF_STATEMENT
        BB = BEGIN_BLOCK
        EB = END_BLOCK
        EOF = END_OF_FILE

        expected = [
            '0', EOS,  BB, '4',  EOS, '4', EOS,
             BB, '8', EOS,  EB,   EB, EOS, '0', EOS,
             BB, '2', EOS,  BB,  '6', EOS,  EB,  EB, EOS, EOF
        ]

        self.failUnlessEqual(len(result), len(expected))
        self.failUnlessEqual(result, expected)
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:25,代码来源:lexertest.py


示例4: testSemantic

    def testSemantic(self):
        '''Also tests variable declaration/expression things.
        The basic jist is that, once semantic testing has been done,
        an Identifier should no longer be an Identifier; it should be
        a VariableExpression.
        '''
        from ast.vardecl import VarDecl
        from ast.variableexpression import VariableExpression

        decl = VarDecl('testvar', (0, '<test>'), vartypes.IntType)

        scope = Scope(parent=None)
        scope['testvar'] = decl

        tokens = lex('testvar nonexist')
        expr1 = Identifier.parse(tokens)
        expr2 = Identifier.parse(tokens)

        result = expr1.semantic(scope)

        self.failUnlessEqual(type(result), VariableExpression)
        self.failUnlessEqual(result.variable, decl)

        self.failUnlessRaises(
            error.NameError,
            lambda: expr2.semantic(scope)
        )
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:27,代码来源:identifiertest.py


示例5: testBadParse

    def testBadParse(self):
        tokens = lex('()')

        self.failUnlessRaises(
            error.SyntaxError,
            lambda: ParenthExpression.parse(tokens)
        )
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:7,代码来源:parenthtest.py


示例6: testBadSemantic

    def testBadSemantic(self):
        program = "var x = 5 * 'bork'\n"

        self.failUnlessRaises(
            error.TypeError,
            lambda: semantic(parse(lex(program)))
        )
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:7,代码来源:multiplytest.py


示例7: testGoodStringParse

    def testGoodStringParse(self):
        tokens = lex("'print' \"print\"")

        result = LiteralExpression.parse(tokens)
        self.failUnless(isinstance(result, StringLiteral))

        result2 = LiteralExpression.parse(tokens)
        self.failUnless(isinstance(result, StringLiteral))
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:8,代码来源:literaltest.py


示例8: testGoodParse

    def testGoodParse(self):
        program = util.source('''
            def foo():
                pass
        ''')

        result = FunctionDecl.parse(lex(program))
        assert isinstance(result, FunctionDecl), result
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:8,代码来源:functiondecltest.py


示例9: testComments

    def testComments(self):
        result = lex(util.source('''
            this is tokens!
            # this is a comment
            this is not
        '''))

        assert '#' not in result
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:8,代码来源:lexertest.py


示例10: testGoodProgram

    def testGoodProgram(self):
        source = lex(
            "print 'Hello World!'"
        )

        ast = parse(source)

        result = semantic.semantic(ast)
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:8,代码来源:semantictest.py


示例11: testTypeMismatch2

 def testTypeMismatch2(self):
     program = util.source('''
         def foo(i as int) as void:
             return i
     ''')
     self.failUnlessRaises(
         error.TypeError,
         lambda: semantic(parse(lex(program)))
     )
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:9,代码来源:returntest.py


示例12: testBadParse

    def testBadParse(self):
        program = util.source('''
            def foo():
        ''')

        self.failUnlessRaises(
            error.SyntaxError,
            lambda: FunctionDecl.parse(lex(program))
        )
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:9,代码来源:functiondecltest.py


示例13: testTypeMismatch

    def testTypeMismatch(self):
        ast = parse(lex('var x as string = 42'))

        assert ast is not None

        self.failUnlessRaises(
            error.TypeError,
            lambda: semantic(ast)
        )
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:9,代码来源:vardecltest.py


示例14: doIt

        def doIt():
            result = lex(util.source('''
                    0
                        4

                        4
                      2
                ''')
            )
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:9,代码来源:lexertest.py


示例15: testLexKeyword

 def testLexKeyword(self):
     result = lex('int print abc string char if 0')
     self.failUnlessEqual(result[0].type, 'keyword')
     self.failUnlessEqual(result[1].type, 'keyword')
     self.failUnlessEqual(result[2].type, 'identifier')
     self.failUnlessEqual(result[3].type, 'keyword')
     self.failUnlessEqual(result[4].type, 'keyword')
     self.failUnlessEqual(result[5].type, 'keyword')
     self.failUnlessEqual(result[6].type, 'literal')
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:9,代码来源:lexertest.py


示例16: testABugAndyFound

    def testABugAndyFound(self):
        from nine.lexer import lex
        from nine.parser import parse
        from nine.semantic import semantic

        result = semantic(parse(lex(util.source('''
            if true:
                print 'True!'
            print 'This caused a spurious syntax error because there is no END_OF_STATEMENT after the dedent!'
        '''))))
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:10,代码来源:iftest.py


示例17: testParseAttributeNoParameters

    def testParseAttributeNoParameters(self):
        source = '[Thingie]'
        tok = lexer.lex(source)

        result = Attribute.parse(tok)

        self.assertTrue(isinstance(result, Attribute))
        self.assertTrue(isinstance(result.className, Identifier))
        self.assertEqual(result.className.name, 'Thingie')
        self.assertEqual(result.params, [])
开发者ID:andyfriesen,项目名称:nine,代码行数:10,代码来源:attributetest.py


示例18: testParseArgs

    def testParseArgs(self):
        tokens = lex(util.source('''
            def foo(x as string, y as int, z as boolean):
                print x
                print y
                print z
        '''))

        result = FunctionDecl.parse(tokens)
        assert isinstance(result, FunctionDecl), tokens.peek()
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:10,代码来源:functiondecltest.py


示例19: testMissingCloseBrace

    def testMissingCloseBrace(self):
        source = util.source('''
            hey look we forgot to close this (!
            oops!
        ''')

        self.assertRaises(
            error.LexError,
            lambda: lex(source)
        )
开发者ID:BackupTheBerlios,项目名称:nine-svn,代码行数:10,代码来源:lexertest.py


示例20: testRenameIdentifier

    def testRenameIdentifier(self):
        from nine.lexer import lex
        from ast.qualifiedname import QualifiedName

        name = QualifiedName.parse(lex('Foo.Bar.Baz'))

        newName = Attribute.renameIdentifier(name)

        self.assertEqual(newName.lhs, name.lhs)
        self.assertEqual(newName.lhs.rhs.name, name.lhs.rhs.name)
        self.assertEqual(newName.rhs.name, 'BazAttribute')
开发者ID:andyfriesen,项目名称:nine,代码行数:11,代码来源:attributetest.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python file_manager.create_path函数代码示例发布时间:2022-05-27
下一篇:
Python nine.str函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap