本文整理汇总了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;未经允许,请勿转载。 |
请发表评论