Show More
@@ -1,98 +1,95 b'' | |||||
1 | # parser.py - simple top-down operator precedence parser for mercurial |
|
1 | # parser.py - simple top-down operator precedence parser for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2010 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2010 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | # see http://effbot.org/zone/simple-top-down-parsing.htm and |
|
8 | # see http://effbot.org/zone/simple-top-down-parsing.htm and | |
9 | # http://eli.thegreenplace.net/2010/01/02/top-down-operator-precedence-parsing/ |
|
9 | # http://eli.thegreenplace.net/2010/01/02/top-down-operator-precedence-parsing/ | |
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 pairs |
|
13 | # tokenizer is an iterator that returns type, value pairs | |
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 labeled tree |
|
16 | # __call__(program) parses program into a labeled tree | |
17 |
|
17 | |||
18 | import error |
|
18 | import error | |
19 | from i18n import _ |
|
19 | from i18n import _ | |
20 |
|
20 | |||
21 | class parser(object): |
|
21 | class parser(object): | |
22 | def __init__(self, tokenizer, elements, methods=None): |
|
22 | def __init__(self, tokenizer, elements, methods=None): | |
23 | self._tokenizer = tokenizer |
|
23 | self._tokenizer = tokenizer | |
24 | self._elements = elements |
|
24 | self._elements = elements | |
25 | self._methods = methods |
|
25 | self._methods = methods | |
26 | self.current = None |
|
26 | self.current = None | |
27 | def _advance(self): |
|
27 | def _advance(self): | |
28 | 'advance the tokenizer' |
|
28 | 'advance the tokenizer' | |
29 | t = self.current |
|
29 | t = self.current | |
30 | try: |
|
30 | self.current = next(self._iter, None) | |
31 | self.current = self._iter.next() |
|
|||
32 | except StopIteration: |
|
|||
33 | pass |
|
|||
34 | return t |
|
31 | return t | |
35 | def _match(self, m, pos): |
|
32 | def _match(self, m, pos): | |
36 | 'make sure the tokenizer matches an end condition' |
|
33 | 'make sure the tokenizer matches an end condition' | |
37 | if self.current[0] != m: |
|
34 | if self.current[0] != m: | |
38 | raise error.ParseError(_("unexpected token: %s") % self.current[0], |
|
35 | raise error.ParseError(_("unexpected token: %s") % self.current[0], | |
39 | self.current[2]) |
|
36 | self.current[2]) | |
40 | self._advance() |
|
37 | self._advance() | |
41 | def _parse(self, bind=0): |
|
38 | def _parse(self, bind=0): | |
42 | token, value, pos = self._advance() |
|
39 | token, value, pos = self._advance() | |
43 | # handle prefix rules on current token |
|
40 | # handle prefix rules on current token | |
44 | prefix = self._elements[token][1] |
|
41 | prefix = self._elements[token][1] | |
45 | if not prefix: |
|
42 | if not prefix: | |
46 | raise error.ParseError(_("not a prefix: %s") % token, pos) |
|
43 | raise error.ParseError(_("not a prefix: %s") % token, pos) | |
47 | if len(prefix) == 1: |
|
44 | if len(prefix) == 1: | |
48 | expr = (prefix[0], value) |
|
45 | expr = (prefix[0], value) | |
49 | else: |
|
46 | else: | |
50 | if len(prefix) > 2 and prefix[2] == self.current[0]: |
|
47 | if len(prefix) > 2 and prefix[2] == self.current[0]: | |
51 | self._match(prefix[2], pos) |
|
48 | self._match(prefix[2], pos) | |
52 | expr = (prefix[0], None) |
|
49 | expr = (prefix[0], None) | |
53 | else: |
|
50 | else: | |
54 | expr = (prefix[0], self._parse(prefix[1])) |
|
51 | expr = (prefix[0], self._parse(prefix[1])) | |
55 | if len(prefix) > 2: |
|
52 | if len(prefix) > 2: | |
56 | self._match(prefix[2], pos) |
|
53 | self._match(prefix[2], pos) | |
57 | # gather tokens until we meet a lower binding strength |
|
54 | # gather tokens until we meet a lower binding strength | |
58 | while bind < self._elements[self.current[0]][0]: |
|
55 | while bind < self._elements[self.current[0]][0]: | |
59 | token, value, pos = self._advance() |
|
56 | token, value, pos = self._advance() | |
60 | e = self._elements[token] |
|
57 | e = self._elements[token] | |
61 | # check for suffix - next token isn't a valid prefix |
|
58 | # check for suffix - next token isn't a valid prefix | |
62 | if len(e) == 4 and not self._elements[self.current[0]][1]: |
|
59 | if len(e) == 4 and not self._elements[self.current[0]][1]: | |
63 | suffix = e[3] |
|
60 | suffix = e[3] | |
64 | expr = (suffix[0], expr) |
|
61 | expr = (suffix[0], expr) | |
65 | else: |
|
62 | else: | |
66 | # handle infix rules |
|
63 | # handle infix rules | |
67 | if len(e) < 3 or not e[2]: |
|
64 | if len(e) < 3 or not e[2]: | |
68 | raise error.ParseError(_("not an infix: %s") % token, pos) |
|
65 | raise error.ParseError(_("not an infix: %s") % token, pos) | |
69 | infix = e[2] |
|
66 | infix = e[2] | |
70 | if len(infix) == 3 and infix[2] == self.current[0]: |
|
67 | if len(infix) == 3 and infix[2] == self.current[0]: | |
71 | self._match(infix[2], pos) |
|
68 | self._match(infix[2], pos) | |
72 | expr = (infix[0], expr, (None)) |
|
69 | expr = (infix[0], expr, (None)) | |
73 | else: |
|
70 | else: | |
74 | expr = (infix[0], expr, self._parse(infix[1])) |
|
71 | expr = (infix[0], expr, self._parse(infix[1])) | |
75 | if len(infix) == 3: |
|
72 | if len(infix) == 3: | |
76 | self._match(infix[2], pos) |
|
73 | self._match(infix[2], pos) | |
77 | return expr |
|
74 | return expr | |
78 | def parse(self, message, lookup=None): |
|
75 | def parse(self, message, lookup=None): | |
79 | 'generate a parse tree from a message' |
|
76 | 'generate a parse tree from a message' | |
80 | if lookup: |
|
77 | if lookup: | |
81 | self._iter = self._tokenizer(message, lookup) |
|
78 | self._iter = self._tokenizer(message, lookup) | |
82 | else: |
|
79 | else: | |
83 | self._iter = self._tokenizer(message) |
|
80 | self._iter = self._tokenizer(message) | |
84 | self._advance() |
|
81 | self._advance() | |
85 | res = self._parse() |
|
82 | res = self._parse() | |
86 | token, value, pos = self.current |
|
83 | token, value, pos = self.current | |
87 | return res, pos |
|
84 | return res, pos | |
88 | def eval(self, tree): |
|
85 | def eval(self, tree): | |
89 | 'recursively evaluate a parse tree using node methods' |
|
86 | 'recursively evaluate a parse tree using node methods' | |
90 | if not isinstance(tree, tuple): |
|
87 | if not isinstance(tree, tuple): | |
91 | return tree |
|
88 | return tree | |
92 | return self._methods[tree[0]](*[self.eval(t) for t in tree[1:]]) |
|
89 | return self._methods[tree[0]](*[self.eval(t) for t in tree[1:]]) | |
93 | def __call__(self, message): |
|
90 | def __call__(self, message): | |
94 | 'parse a message into a parse tree and evaluate if methods given' |
|
91 | 'parse a message into a parse tree and evaluate if methods given' | |
95 | t = self.parse(message) |
|
92 | t = self.parse(message) | |
96 | if self._methods: |
|
93 | if self._methods: | |
97 | return self.eval(t) |
|
94 | return self.eval(t) | |
98 | return t |
|
95 | return t |
General Comments 0
You need to be logged in to leave comments.
Login now