Skip to content

Commit a587d75

Browse files
committed
[libclang/python] Refactor TokenKind usage
Unify TokenKind implementation with other enums
1 parent 35bfcfb commit a587d75

File tree

4 files changed

+17
-99
lines changed

4 files changed

+17
-99
lines changed

clang/bindings/python/clang/cindex.py

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@
6464

6565
from ctypes import *
6666

67-
import clang.enumerations
68-
6967
import collections.abc
7068
import os
7169
from enum import Enum
@@ -573,44 +571,6 @@ def get_tokens(tu, extent):
573571
yield token
574572

575573

576-
class TokenKind:
577-
"""Describes a specific type of a Token."""
578-
579-
_value_map = {} # int -> TokenKind
580-
581-
def __init__(self, value, name):
582-
"""Create a new TokenKind instance from a numeric value and a name."""
583-
self.value = value
584-
self.name = name
585-
586-
def __repr__(self):
587-
return "TokenKind.%s" % (self.name,)
588-
589-
@staticmethod
590-
def from_value(value):
591-
"""Obtain a registered TokenKind instance from its value."""
592-
result = TokenKind._value_map.get(value, None)
593-
594-
if result is None:
595-
raise ValueError("Unknown TokenKind: %d" % value)
596-
597-
return result
598-
599-
@staticmethod
600-
def register(value, name):
601-
"""Register a new TokenKind enumeration.
602-
603-
This should only be called at module load time by code within this
604-
package.
605-
"""
606-
if value in TokenKind._value_map:
607-
raise ValueError("TokenKind already registered: %d" % value)
608-
609-
kind = TokenKind(value, name)
610-
TokenKind._value_map[value] = kind
611-
setattr(TokenKind, name, kind)
612-
613-
614574
### Cursor Kinds ###
615575
class BaseEnumeration(Enum):
616576
"""
@@ -635,6 +595,21 @@ def __repr__(self):
635595
)
636596

637597

598+
class TokenKind(BaseEnumeration):
599+
"""Describes a specific type of a Token."""
600+
601+
@classmethod
602+
def from_value(cls, value):
603+
"""Obtain a registered TokenKind instance from its value."""
604+
return cls.from_id(value)
605+
606+
PUNCTUATION = 0
607+
KEYWORD = 1
608+
IDENTIFIER = 2
609+
LITERAL = 3
610+
COMMENT = 4
611+
612+
638613
class CursorKind(BaseEnumeration):
639614
"""
640615
A CursorKind describes the kind of entity that a cursor points to.
@@ -4040,13 +4015,7 @@ def function_exists(self, name):
40404015
return True
40414016

40424017

4043-
def register_enumerations():
4044-
for name, value in clang.enumerations.TokenKinds:
4045-
TokenKind.register(value, name)
4046-
4047-
40484018
conf = Config()
4049-
register_enumerations()
40504019

40514020
__all__ = [
40524021
"AvailabilityKind",

clang/bindings/python/clang/enumerations.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

clang/bindings/python/tests/cindex/test_enums.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22

33
from clang.cindex import (
4+
TokenKind,
45
CursorKind,
56
TemplateArgumentKind,
67
ExceptionSpecificationKind,
@@ -16,6 +17,7 @@
1617

1718
class TestCursorKind(unittest.TestCase):
1819
enums = [
20+
TokenKind,
1921
CursorKind,
2022
TemplateArgumentKind,
2123
ExceptionSpecificationKind,

clang/bindings/python/tests/cindex/test_token_kind.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,6 @@
1010

1111

1212
class TestTokenKind(unittest.TestCase):
13-
def test_constructor(self):
14-
"""Ensure TokenKind constructor works as expected."""
15-
16-
t = TokenKind(5, "foo")
17-
18-
self.assertEqual(t.value, 5)
19-
self.assertEqual(t.name, "foo")
20-
21-
def test_bad_register(self):
22-
"""Ensure a duplicate value is rejected for registration."""
23-
24-
with self.assertRaises(ValueError):
25-
TokenKind.register(2, "foo")
26-
27-
def test_unknown_value(self):
28-
"""Ensure trying to fetch an unknown value raises."""
29-
30-
with self.assertRaises(ValueError):
31-
TokenKind.from_value(-1)
32-
3313
def test_registration(self):
3414
"""Ensure that items registered appear as class attributes."""
3515
self.assertTrue(hasattr(TokenKind, "LITERAL"))

0 commit comments

Comments
 (0)