[vlc-commits] Improve enum parsing

Jean Brouwers git at videolan.org
Fri Apr 15 16:55:01 CEST 2016


vlc/python | branch: master | Jean Brouwers <MrJean1 at Gmail.com> | Fri Apr 15 16:36:51 2016 +0200| [913fcb2546deca6f1b92e0197ca325502207aa75] | committer: Olivier Aubert

Improve enum parsing

Signed-off-by: Olivier Aubert <contact at olivieraubert.net>

> http://git.videolan.org/gitweb.cgi/vlc/python.git/?a=commit;h=913fcb2546deca6f1b92e0197ca325502207aa75
---

 generator/generate.py |   44 ++++++++++++++++++++------------------------
 1 file changed, 20 insertions(+), 24 deletions(-)

diff --git a/generator/generate.py b/generator/generate.py
index f1e7ee0..ccceae6 100755
--- a/generator/generate.py
+++ b/generator/generate.py
@@ -480,31 +480,27 @@ class Parser(object):
         @return: yield an Enum instance for each enum.
         """
         for typ, name, enum, docs, line in self.parse_groups(enum_type_re.match, enum_re.match):
-            vals, e, h = [], -1, {}  # enum value(s)
+            vals, locs, e = [], {}, -1  # enum value(s)
             for t in paramlist_re.split(enum):
-                t = t.strip()
-                if t[:2] not in ('/*', '//'):
-                    if '=' in t:  # has value
-                        n, v = enum_pair_re.split(t)
-                        try:
-                            if v[:2] in ('0x', '0X'):
-                                e = int(v, 16)
-                                h[n] = v
-                            else:
-                                e = int(v)
-                        except ValueError:
-                            try:  # .. an enum expression
-                                e = eval(v, dict(vals))
-                            except (SyntaxError, ValueError, TypeError):
-                                errorf('%s %s: %s', typ, name, t)
-                                raise
-                        vals.append((n, e))
-                    elif t:  # only name
-                        e += 1
-                        vals.append((t, e))
-            # convert to list of Val instances, preserving
-            # enums originally specified with a hex value
-            vals = [Val(n, h.get(n, str(e))) for n, e in vals]
+                n = t.split('/*')[0].strip()
+                if '=' in n:  # has value
+                    n, v = enum_pair_re.split(n)
+                    try:  # handle expressions
+                        e = eval(v, locs)
+                    except (SyntaxError, TypeError, ValueError):
+                        errorf('%s %s: %s', typ, name, t)
+                        raise
+                    locs[n] = e
+                    # preserve hex values
+                    if v[:2] in ('0x', '0X'):
+                        v = hex(e)
+                    else:
+                        v = str(e)
+                    vals.append(Val(n, v))
+                elif n:  # only name
+                    e += 1
+                    locs[n] = e
+                    vals.append(Val(n, str(e)))
 
             name = name.strip()
             if not name:  # anonymous



More information about the vlc-commits mailing list