annotate python/testparserlib.py @ 232:e621e3ba78d2

Added left shift instruction
author Windel Bouwman
date Sun, 14 Jul 2013 11:50:58 +0200
parents 62386bcee1ba
children
rev   line source
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
1 import unittest
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
2 from parserlib import OneOrMore, Literal, Or, Sequence, Optional
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
3
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
4 class ParserCombinatorTestCase(unittest.TestCase):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
5 def test1(self):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
6 #p = Parser()
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
7 # parse and interpret:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
8 n40 = Literal('40')
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
9 plus = Literal('+')
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
10 n2 = Literal('2')
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
11 n40.ParseAction = int
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
12 plus.ParseAction = replaceWith(0)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
13 n2.ParseAction = int
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
14 p = Sequence([n40,plus,n2])
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
15 p.ParseAction = wordsum
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
16
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
17 result = p.parse('40+2')
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
18 self.assertEqual(42, result[0])
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
19
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
20 def replaceWith(s):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
21 def _repFunc(*args):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
22 return s
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
23 return _repFunc
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
24
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
25 wordsum = lambda t: sum(t)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
26
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
27 class WordToNumTestCase(unittest.TestCase):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
28 def setUp(self):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
29 numWords = OneOrMore()
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
30 def makeLit(s, val):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
31 ret = Literal(s)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
32 ret.ParseAction = replaceWith(val)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
33 return ret
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
34 unitDefs = [('zero', 0), ('three', 3), ('one', 1)]
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
35 units = Or( [makeLit(s, v) for s, v in unitDefs] )
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
36 tensDefs = [('twenty', 20)]
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
37 tens = Or( [makeLit(s, v) for s, v in tensDefs] )
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
38
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
39 numPart = Sequence([Optional(tens), units])
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
40 numPart.ParseAction = wordsum
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
41 self.p = numPart
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
42
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
43 def check(self, i, o):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
44 result = self.p.parse(i)[0]
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
45 self.assertEqual(o, result)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
46
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
47 def test0(self):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
48 self.check('zero', 0)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
49
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
50 def test23(self):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
51 self.check('twentythree', 23)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
52
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
53 @unittest.skip
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
54 def test321(self):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
55 # TODO
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
56 self.check('three hundred and twenty one', 321)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
57
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
58
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
59 if __name__ == '__main__':
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
60 unittest.main()
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
61