annotate python/parserlib.py @ 247:dd8bbb963458

project remove
author Windel Bouwman
date Fri, 26 Jul 2013 10:44:26 +0200
parents 62386bcee1ba
children
rev   line source
212
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
1
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
2
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
3
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
4 class Token:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
5 pass
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
6
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
7 # Base functions:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
8 class Pbase:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
9 def __init__(self):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
10 self.pa = None
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
11 def parse(self, txt):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
12 r = self.do(txt)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
13 if r:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
14 match, rest = r
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
15 # Apply action:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
16 if self.ParseAction:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
17 match = self.ParseAction(match)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
18 return match, rest
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
19 else:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
20 # TODO: fail in some way
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
21 pass
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
22 def getParseAction(self):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
23 return self.pa
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
24 def setParseAction(self, pa):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
25 self.pa = pa
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
26
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
27 ParseAction = property(getParseAction, setParseAction)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
28
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
29 # basic elements:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
30
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
31 class Literal(Pbase):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
32 def __init__(self, s):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
33 super().__init__()
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
34 self.pat = s
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
35 def do(self, txt):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
36 if txt.startswith(self.pat):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
37 return self.pat, txt[len(self.pat):]
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
38
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
39 class Or(Pbase):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
40 def __init__(self, options):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
41 super().__init__()
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
42 self.options = options
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
43 def do(self, txt):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
44 for option in self.options:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
45 r = option.parse(txt)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
46 if r:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
47 return r
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
48
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
49 class And:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
50 def __init__(self, options):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
51 self.options = options
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
52
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
53 class Sequence(Pbase):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
54 def __init__(self, seq):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
55 super().__init__()
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
56 self.seq = seq
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
57 def do(self, txt):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
58 results = []
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
59 for thung in self.seq:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
60 r = thung.parse(txt)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
61 if r:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
62 res, txt = r
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
63 results.append(res)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
64 else:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
65 return
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
66 return results, txt
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
67
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
68 class Optional(Pbase):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
69 def __init__(self, thung):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
70 super().__init__()
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
71 self.thung = thung
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
72 def do(self, txt):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
73 r = self.thung.do(txt)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
74 if r:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
75 return r
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
76 return (0, txt)
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
77
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
78 # Contraptions of basic blocks:
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
79
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
80 def OneOrMore():
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
81 def __init__(self, thingy):
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
82 pass
62386bcee1ba Added parser combinator lib
Windel Bouwman
parents:
diff changeset
83