本文整理汇总了Python中parser.ParserError方法的典型用法代码示例。如果您正苦于以下问题:Python parser.ParserError方法的具体用法?Python parser.ParserError怎么用?Python parser.ParserError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parser
的用法示例。
在下文中一共展示了parser.ParserError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: roundtrip
# 需要导入模块: import parser [as 别名]
# 或者: from parser import ParserError [as 别名]
def roundtrip(self, f, s):
st1 = f(s)
t = st1.totuple()
try:
st2 = parser.sequence2st(t)
except parser.ParserError, why:
self.fail("could not roundtrip %r: %s" % (s, why))
示例2: check_bad_tree
# 需要导入模块: import parser [as 别名]
# 或者: from parser import ParserError [as 别名]
def check_bad_tree(self, tree, label):
try:
parser.sequence2st(tree)
except parser.ParserError:
pass
else:
self.fail("did not detect invalid tree for %r" % label)
示例3: roundtrip
# 需要导入模块: import parser [as 别名]
# 或者: from parser import ParserError [as 别名]
def roundtrip(self, f, s):
st1 = f(s)
t = st1.totuple()
try:
st2 = parser.sequence2st(t)
except parser.ParserError as why:
self.fail("could not roundtrip %r: %s" % (s, why))
self.assertEqual(t, st2.totuple(),
"could not re-generate syntax tree")
示例4: test_dict_comprehensions
# 需要导入模块: import parser [as 别名]
# 或者: from parser import ParserError [as 别名]
def test_dict_comprehensions(self):
self.check_expr('{x:x for x in seq}')
self.check_expr('{x**2:x[3] for x in seq if condition(x)}')
self.check_expr('{x:x for x in seq1 for y in seq2 if condition(x, y)}')
#
# Second, we take *invalid* trees and make sure we get ParserError
# rejections for them.
#
示例5: test_assert
# 需要导入模块: import parser [as 别名]
# 或者: from parser import ParserError [as 别名]
def test_assert(self):
self.check_suite("assert alo < ahi and blo < bhi\n")
#
# Second, we take *invalid* trees and make sure we get ParserError
# rejections for them.
#
示例6: test_position
# 需要导入模块: import parser [as 别名]
# 或者: from parser import ParserError [as 别名]
def test_position(self):
# An absolutely minimal test of position information. Better
# tests would be a big project.
code = "def f(x):\n return x + 1"
st = parser.suite(code)
def walk(tree):
node_type = tree[0]
next = tree[1]
if isinstance(next, (tuple, list)):
for elt in tree[1:]:
for x in walk(elt):
yield x
else:
yield tree
expected = [
(1, 'def', 1, 0),
(1, 'f', 1, 4),
(7, '(', 1, 5),
(1, 'x', 1, 6),
(8, ')', 1, 7),
(11, ':', 1, 8),
(4, '', 1, 9),
(5, '', 2, -1),
(1, 'return', 2, 4),
(1, 'x', 2, 11),
(14, '+', 2, 13),
(2, '1', 2, 15),
(4, '', 2, 16),
(6, '', 2, -1),
(4, '', 2, -1),
(0, '', 2, -1),
]
self.assertEqual(list(walk(st.totuple(line_info=True, col_info=True))),
expected)
self.assertEqual(list(walk(st.totuple())),
[(t, n) for t, n, l, c in expected])
self.assertEqual(list(walk(st.totuple(line_info=True))),
[(t, n, l) for t, n, l, c in expected])
self.assertEqual(list(walk(st.totuple(col_info=True))),
[(t, n, c) for t, n, l, c in expected])
self.assertEqual(list(walk(st.tolist(line_info=True, col_info=True))),
[list(x) for x in expected])
self.assertEqual(list(walk(parser.st2tuple(st, line_info=True,
col_info=True))),
expected)
self.assertEqual(list(walk(parser.st2list(st, line_info=True,
col_info=True))),
[list(x) for x in expected])
#
# Second, we take *invalid* trees and make sure we get ParserError
# rejections for them.
#
示例7: test_position
# 需要导入模块: import parser [as 别名]
# 或者: from parser import ParserError [as 别名]
def test_position(self):
# An absolutely minimal test of position information. Better
# tests would be a big project.
code = "def f(x):\n return x + 1"
st1 = parser.suite(code)
st2 = st1.totuple(line_info=1, col_info=1)
def walk(tree):
node_type = tree[0]
next = tree[1]
if isinstance(next, tuple):
for elt in tree[1:]:
for x in walk(elt):
yield x
else:
yield tree
terminals = list(walk(st2))
self.assertEqual([
(1, 'def', 1, 0),
(1, 'f', 1, 4),
(7, '(', 1, 5),
(1, 'x', 1, 6),
(8, ')', 1, 7),
(11, ':', 1, 8),
(4, '', 1, 9),
(5, '', 2, -1),
(1, 'return', 2, 4),
(1, 'x', 2, 11),
(14, '+', 2, 13),
(2, '1', 2, 15),
(4, '', 2, 16),
(6, '', 2, -1),
(4, '', 2, -1),
(0, '', 2, -1)],
terminals)
#
# Second, we take *invalid* trees and make sure we get ParserError
# rejections for them.
#
示例8: decode
# 需要导入模块: import parser [as 别名]
# 或者: from parser import ParserError [as 别名]
def decode(self, text, location=None):
self.doc = pyglet.text.document.FormattedDocument()
self.length = 0
self.attributes = {}
next_trailing_space = True
trailing_newline = True
for m in _pattern.finditer(text):
group = m.lastgroup
trailing_space = True
if group == 'text':
t = m.group('text')
self.append(t)
trailing_space = t.endswith(' ')
trailing_newline = False
elif group == 'nl_soft':
if not next_trailing_space:
self.append(' ')
trailing_newline = False
elif group in ('nl_hard1', 'nl_hard2'):
self.append('\n')
trailing_newline = True
elif group == 'nl_para':
self.append(m.group('nl_para'))
trailing_newline = True
elif group == 'attr':
try:
ast = parser.expr(m.group('attr_val'))
if self.safe(ast):
val = eval(ast.compile())
else:
val = None
except (parser.ParserError, SyntaxError):
val = None
name = m.group('attr_name')
if name[0] == '.':
if trailing_newline:
self.attributes[name[1:]] = val
else:
self.doc.set_paragraph_style(self.length, self.length,
{name[1:]: val})
else:
self.attributes[name] = val
elif group == 'escape_dec':
self.append(unichr(int(m.group('escape_dec_val'))))
elif group == 'escape_hex':
self.append(unichr(int(m.group('escape_hex_val'), 16)))
elif group == 'escape_lbrace':
self.append('{')
elif group == 'escape_rbrace':
self.append('}')
next_trailing_space = trailing_space
return self.doc