示例#1
0
 def test_append(self):
     r = peggy.Range([6, 7])
     r.append(1)
     self.assertEqual(r, [1, 1, 6, 7])
     r.append(2)
     self.assertEqual(r, [1, 2, 6, 7])
     r.append(5)
     self.assertEqual(r, [1, 2, 5, 7])
示例#2
0
 def test_contains(self):
     r = peggy.Range()
     r.add(1, 3)
     r.add(5, 7)
     self.assertNotIn(0, r)
     self.assertIn(1, r)
     self.assertIn(2, r)
     self.assertIn(3, r)
     self.assertNotIn(4, r)
     self.assertIn(5, r)
     self.assertIn(6, r)
     self.assertIn(7, r)
     self.assertNotIn(8, r)
示例#3
0
 def test_add(self):
     r = peggy.Range([6, 7])
     self.assertEqual(r, [6, 7])
     r.add(0, 1)
     self.assertEqual(r, [0, 1, 6, 7])
     r.add(3, 4)
     self.assertEqual(r, [0, 1, 3, 4, 6, 7])
     r.add(2, 2)
     self.assertEqual(r, [0, 4, 6, 7])
     r.add(5, 5)
     self.assertEqual(r, [0, 7])
     r.add(9, 10)
     self.assertEqual(r, [0, 7, 9, 10])
     r.add(0, 0x10000)
     self.assertEqual(r, [0, 0x10000])
示例#4
0
def unit2json(ast, data):
	unit = {'ast': 'BUILD'}
	prefix = ''
	if ast['children'][0]['step']['type'] == 'CALL' and ast['children'][0][
	    'step']['data'] == 'Prefix':
		prefix = peggy.astdata(ast['children'][0], data)
		ast['children'].pop(0)

	if ast['children'][0]['step']['type'] == 'CALL' and ast['children'][0][
	    'step']['data'] == 'Literal':
		unit['type'] = 'STRING'
		unit['data'] = peggy.astdata(
		    ast['children'][0], data
		).strip()[1:-1].encode('utf-8').decode('unicode_escape')
		unit['ast'] = 'VOID'
	elif ast['children'][0]['step']['type'] == 'CALL' and ast['children'][0][
	    'step']['data'] == 'CaseLiteral':
		unit['type'] = 'STRINGI'
		unit['data'] = peggy.astdata(
		    ast['children'][0], data
		).strip()[1:-1].encode('utf-8').decode('unicode_escape')
		unit['ast'] = 'VOID'
	elif ast['children'][0]['step']['type'] == 'CALL' and ast['children'][0][
	    'step']['data'] == 'Identifier':
		unit['type'] = 'CALL'
		unit['data'] = peggy.astdata(ast['children'][0],
		                             data).strip().split()[0]
	elif ast['children'][0]['step']['type'] == 'CALL' and ast['children'][0][
	    'step']['data'] == 'CharClass':
		unit['type'] = 'RANGE'
		unit['data'] = peggy.Range()
		unit['ast'] = 'VOID'
		for child in ast['children'][0]['children']:
			range2json(child, data, unit)
	elif ast['children'][0]['step']['type'] == 'CALL' and ast['children'][0][
	    'step']['data'] == 'WildCard':
		unit['type'] = 'WILDCARD'
		unit['ast'] = 'VOID'
	else:
		unit = node2json(ast['children'][0], data)

	if prefix:
		if prefix == '*':
			unit['min'] = 0
			unit['max'] = sys.maxsize
		elif prefix == '+':
			unit['min'] = 1
			unit['max'] = sys.maxsize
		elif prefix == '?':
			unit['min'] = 0
			unit['max'] = 1
		elif prefix == '&':
			unit['predicate'] = True
			unit['ast'] = 'VOID'
		elif prefix == '!':
			unit['predicate'] = False
			unit['ast'] = 'VOID'
		elif prefix == ':':
			unit['ast'] = 'VOID'
		else:
			raise NotImplementedError(
			    'prefix {} not implemented'.format(prefix)
			)

	return unit
示例#5
0
 def test_extend(self):
     r = peggy.Range([6, 7])
     r.extend(peggy.Range([0, 1, 3, 5, 9, 0xFFFF]))
     self.assertEqual(r, [0, 1, 3, 7, 9, 0xFFFF])
     r.extend(peggy.Range([0, 0x10000]))
     self.assertEqual(r, [0, 0x10000])