10 # for background |
10 # for background |
11 |
11 |
12 # takes a tokenizer and elements |
12 # takes a tokenizer and elements |
13 # tokenizer is an iterator that returns (type, value, pos) tuples |
13 # tokenizer is an iterator that returns (type, value, pos) tuples |
14 # elements is a mapping of types to binding strength, prefix, infix and |
14 # elements is a mapping of types to binding strength, prefix, infix and |
15 # optional suffix actions |
15 # suffix actions |
16 # an action is a tree node name, a tree label, and an optional match |
16 # an action is a tree node name, a tree label, and an optional match |
17 # __call__(program) parses program into a labeled tree |
17 # __call__(program) parses program into a labeled tree |
18 |
18 |
19 import error |
19 import error |
20 from i18n import _ |
20 from i18n import _ |
52 if len(prefix) > 2: |
52 if len(prefix) > 2: |
53 self._match(prefix[2], pos) |
53 self._match(prefix[2], pos) |
54 # gather tokens until we meet a lower binding strength |
54 # gather tokens until we meet a lower binding strength |
55 while bind < self._elements[self.current[0]][0]: |
55 while bind < self._elements[self.current[0]][0]: |
56 token, value, pos = self._advance() |
56 token, value, pos = self._advance() |
57 e = self._elements[token] |
57 infix, suffix = self._elements[token][2:] |
58 # check for suffix - next token isn't a valid prefix |
58 # check for suffix - next token isn't a valid prefix |
59 if len(e) == 4 and not self._elements[self.current[0]][1]: |
59 if suffix and not self._elements[self.current[0]][1]: |
60 suffix = e[3] |
|
61 expr = (suffix[0], expr) |
60 expr = (suffix[0], expr) |
62 else: |
61 else: |
63 # handle infix rules |
62 # handle infix rules |
64 if len(e) < 3 or not e[2]: |
63 if not infix: |
65 raise error.ParseError(_("not an infix: %s") % token, pos) |
64 raise error.ParseError(_("not an infix: %s") % token, pos) |
66 infix = e[2] |
|
67 if len(infix) == 3 and infix[2] == self.current[0]: |
65 if len(infix) == 3 and infix[2] == self.current[0]: |
68 self._match(infix[2], pos) |
66 self._match(infix[2], pos) |
69 expr = (infix[0], expr, (None)) |
67 expr = (infix[0], expr, (None)) |
70 else: |
68 else: |
71 expr = (infix[0], expr, self._parse(infix[1])) |
69 expr = (infix[0], expr, self._parse(infix[1])) |