14 # elements is a mapping of types to binding strength, prefix and infix actions |
14 # elements is a mapping of types to binding strength, prefix and infix actions |
15 # an action is a tree node name, a tree label, and an optional match |
15 # an action is a tree node name, a tree label, and an optional match |
16 # __call__(program) parses program into a labelled tree |
16 # __call__(program) parses program into a labelled tree |
17 |
17 |
18 import error |
18 import error |
|
19 from i18n import _ |
19 |
20 |
20 class parser(object): |
21 class parser(object): |
21 def __init__(self, tokenizer, elements, methods=None): |
22 def __init__(self, tokenizer, elements, methods=None): |
22 self._tokenizer = tokenizer |
23 self._tokenizer = tokenizer |
23 self._elements = elements |
24 self._elements = elements |
32 pass |
33 pass |
33 return t |
34 return t |
34 def _match(self, m, pos): |
35 def _match(self, m, pos): |
35 'make sure the tokenizer matches an end condition' |
36 'make sure the tokenizer matches an end condition' |
36 if self.current[0] != m: |
37 if self.current[0] != m: |
37 raise error.ParseError("unexpected token: %s" % self.current[0], |
38 raise error.ParseError(_("unexpected token: %s") % self.current[0], |
38 self.current[2]) |
39 self.current[2]) |
39 self._advance() |
40 self._advance() |
40 def _parse(self, bind=0): |
41 def _parse(self, bind=0): |
41 token, value, pos = self._advance() |
42 token, value, pos = self._advance() |
42 # handle prefix rules on current token |
43 # handle prefix rules on current token |
43 prefix = self._elements[token][1] |
44 prefix = self._elements[token][1] |
44 if not prefix: |
45 if not prefix: |
45 raise error.ParseError("not a prefix: %s" % token, pos) |
46 raise error.ParseError(_("not a prefix: %s") % token, pos) |
46 if len(prefix) == 1: |
47 if len(prefix) == 1: |
47 expr = (prefix[0], value) |
48 expr = (prefix[0], value) |
48 else: |
49 else: |
49 if len(prefix) > 2 and prefix[2] == self.current[0]: |
50 if len(prefix) > 2 and prefix[2] == self.current[0]: |
50 self._match(prefix[2], pos) |
51 self._match(prefix[2], pos) |
62 suffix = e[3] |
63 suffix = e[3] |
63 expr = (suffix[0], expr) |
64 expr = (suffix[0], expr) |
64 else: |
65 else: |
65 # handle infix rules |
66 # handle infix rules |
66 if len(e) < 3 or not e[2]: |
67 if len(e) < 3 or not e[2]: |
67 raise error.ParseError("not an infix: %s" % token, pos) |
68 raise error.ParseError(_("not an infix: %s") % token, pos) |
68 infix = e[2] |
69 infix = e[2] |
69 if len(infix) == 3 and infix[2] == self.current[0]: |
70 if len(infix) == 3 and infix[2] == self.current[0]: |
70 self._match(infix[2], pos) |
71 self._match(infix[2], pos) |
71 expr = (infix[0], expr, (None)) |
72 expr = (infix[0], expr, (None)) |
72 else: |
73 else: |