本文整理汇总了Python中nine.lexer.lex函数的典型用法代码示例。如果您正苦于以下问题:Python lex函数的具体用法?Python lex怎么用?Python lex使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了lex函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testNewParser
def testNewParser(self):
program = lex(
"print 'Hello World!'\n"
"print 42"
)
ast = parse(program)
示例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)
示例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)
示例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)
)
示例5: testBadParse
def testBadParse(self):
tokens = lex('()')
self.failUnlessRaises(
error.SyntaxError,
lambda: ParenthExpression.parse(tokens)
)
示例6: testBadSemantic
def testBadSemantic(self):
program = "var x = 5 * 'bork'\n"
self.failUnlessRaises(
error.TypeError,
lambda: semantic(parse(lex(program)))
)
示例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))
示例8: testGoodParse
def testGoodParse(self):
program = util.source('''
def foo():
pass
''')
result = FunctionDecl.parse(lex(program))
assert isinstance(result, FunctionDecl), result
示例9: testComments
def testComments(self):
result = lex(util.source('''
this is tokens!
# this is a comment
this is not
'''))
assert '#' not in result
示例10: testGoodProgram
def testGoodProgram(self):
source = lex(
"print 'Hello World!'"
)
ast = parse(source)
result = semantic.semantic(ast)
示例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)))
)
示例12: testBadParse
def testBadParse(self):
program = util.source('''
def foo():
''')
self.failUnlessRaises(
error.SyntaxError,
lambda: FunctionDecl.parse(lex(program))
)
示例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)
)
示例14: doIt
def doIt():
result = lex(util.source('''
0
4
4
2
''')
)
示例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')