Enum parsing#18
Merged
Merged
Conversation
using the CEnum class one create Python Enums easily using a C style syntax.
```
class Test(CEnum):
__enum__ = """
A = 1,
B = 2,
C
"""
```
Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
- renamed `parse_def` to `parse_struct_def` to allow for `parse_enum_def` function Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
Using Pythons metaclass new function, we look for valid C Enum classes (__enum__ = True) and register them there in the `base.ENUMS` variable - `__size__` is a required parameter for Enums - introduced `CEnumException` Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
It wasn't used so far and I'm not sure if that will change. Enum parsing is far more simple than struct parsing in this regard Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
Previously defined named CEnums are correctly parsed when used in a struct definition now. It is unclear whether unnamed and `AbstractCEnum.parse` enums can also be used. Presumably not. Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
Parser couldn't handle the case that an enum-constant without explicit
value and no following `,` was the last thing in the enum definition.
Example error case
```
enum {
A
}
```
Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
This function was not in a working state previously. While making the function work a few things needed to be changed too. For example should `len(enum)` return the number of elements instead of the size for CEnums and such. Introduced default enum size of 4 bytes Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
A warning will now be displayed and the enum size will be set to `base.DEFAULT_ENUM_SIZE` instead Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
This includes named and unnamed enums that are defined in a struct,
similar to this:
```
struct {
enum {A, B, C} v;
}
```
Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
I'm not fully sure what the test case was that would have resulted in an error, but looking over the code there was a break at the wrong position that could have resulted in an unparsed enum constant. Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
Contributor
Author
|
And yeah I'm missing test cases for now |
This hits upon a larger issue. It doesn't seem fully clear what type a compiler will use to represent an enum, but signed integer seems to be a good enough solution as enum-constants (not the enum itself) are defined as int signed. See following patch note https://gcc.gnu.org/legacy-ml/gcc-patches/2000-07/msg00993.html Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
Python 3.10 supports using `type[…]` or `tuple[…]` as type hints. This is relatively new and as such not support in older pythons Signed-off-by: Sophie Tyalie <dev@flowerpot.me>
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This merge would implement extensive enum support in python-cstruct. Named, unnamed, inline, ... enum definitions are possible with this. All enums are subclasses from Python
enum.IntEnumand can be used as such without any further thought, they just contain an additional.parseand.sizemethod / property.One big issue I've hit upon is that I'm unsure about the underlying data type of enums / how they are stored in a buffer. This seems to be pretty much compiler and environment defined. I've used the
__size__parameter that is exposed through the.sizeproperty in order to state how many bytes the enum will take. This will default to 4 bytes if not further specified. I'm also assuming a signed case.There is a bit of contradicting information out there on what exactly the underlying data type of an enum is, I'm not fully sure what the best way forward is from here.
Examples
Possible
__def__for an MemCStructThere is also a
cstruct.CEnumclass with which one can easily define enums like the following