robotpy / robotpy-cppheaderparser

DEPRECATED: use cxxheaderparser instead
Other
123 stars 39 forks source link

[BUG]: Parsing fails when enum contains #define #77

Closed i01y closed 2 years ago

i01y commented 2 years ago

Problem description

I try to parsI try to parse the header file as follows:

enum Intrinsic {
#define V8_DECL_INTRINSIC(name, iname) k##name,
  V8_INTRINSICS_LIST(V8_DECL_INTRINSIC)
#undef V8_DECL_INTRINSIC
};

It's OK in C++ programs。

But parsing with robotpy-cppheaderparser will report the following error:

Traceback (most recent call last):
  File "D:\Program\Anaconda3\lib\site-packages\CppHeaderParser\CppHeaderParser.py", line 2963, in __init__
    self._evaluate_stack()
  File "D:\Program\Anaconda3\lib\site-packages\CppHeaderParser\CppHeaderParser.py", line 3398, in _evaluate_stack
    self._parse_enum()
  File "D:\Program\Anaconda3\lib\site-packages\CppHeaderParser\CppHeaderParser.py", line 3584, in _parse_enum
    self._parse_enumerator_list(newEnum["values"])
  File "D:\Program\Anaconda3\lib\site-packages\CppHeaderParser\CppHeaderParser.py", line 3644, in _parse_enumerator_list
    name_tok = self._next_token_must_be("}", "NAME")
  File "D:\Program\Anaconda3\lib\site-packages\CppHeaderParser\CppHeaderParser.py", line 3164, in _next_token_must_be
    raise self._parse_error((tok,), "' or '".join(tokenTypes))
CppHeaderParser.CppHeaderParser.CppParseError: unexpected '#define V8_DECL_INTRINSIC(name, iname) k##name,', expected '}' or 'NAME'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\Program\Anaconda3\lib\site-packages\CppHeaderParser\CppHeaderParser.py", line 3097, in __init__
    raise_exc(
  File "<string>", line 1, in raise_exc
CppHeaderParser.CppHeaderParser.CppParseError: Not able to parse D:\Data\Source\v8\v8-8.0.426.17\include\v8.h on line 5853 evaluating '#define V8_DECL_INTRINSIC(name, iname) k##name,': unexpected '#define V8_DECL_INTRINSIC(name, iname) k##name,', expected '}' or 'NAME'
Error around: enum Intrinsic
python-BaseException

Process finished with exit code 1

It seems that the #define in the enumeration cannot be recognized。

Operating System

Windows

Installed Python Packages

No response

Reproducible example code

#define V8_INTRINSICS_LIST(F)                    \
  F(ArrayProto_entries, array_entries_iterator)  \
  F(ArrayProto_forEach, array_for_each_iterator) \
  F(ArrayProto_keys, array_keys_iterator)        \
  F(ArrayProto_values, array_values_iterator)    \
  F(ErrorPrototype, initial_error_prototype)     \
  F(IteratorPrototype, initial_iterator_prototype)

enum Intrinsic {
#define V8_DECL_INTRINSIC(name, iname) k##name,
  V8_INTRINSICS_LIST(V8_DECL_INTRINSIC)
#undef V8_DECL_INTRINSIC
};
virtuald commented 2 years ago

As per the README, you need to use a separate preprocessor to handle #define:

CppHeaderParser only does some very minimal interpretation of preprocessor directives -- and we're looking at removing some of that from this library. If you need anything complex, you should preprocess the code yourself. You can use the excellent pure python preprocessor pcpp, or the preprocessing facilities provided by your favorite compiler.