-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generate.py to generate enum declarations into tests/py/dcl.enum/
close #95
- Loading branch information
1 parent
8893e61
commit 5efe3ef
Showing
14 changed files
with
43 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#! python3 | ||
|
||
import exrex | ||
import os | ||
|
||
def ToplevelFolder(): | ||
scriptPath = os.path.realpath(__file__) | ||
parentDirectory = os.path.dirname(scriptPath) | ||
return os.path.join(parentDirectory, "py") | ||
|
||
def EnumDeclarationsFolder(): | ||
return os.path.join(ToplevelFolder(), "dcl.enum") | ||
|
||
|
||
def GenerateEnumDeclarations(): | ||
#https://eel.is/c++draft/enum | ||
enum_base = "(|: short|: unsigned int|: const long|: const volatile long long|: decltype\(0\))" | ||
enumerator_initializer = "(| = 0| = true \? 1,2 : 3| = \!\+\[\]\(\)\{\})" | ||
enumerators = " (|(A" + enumerator_initializer + "|A" + enumerator_initializer + ", B " + enumerator_initializer + "),?) " | ||
regex = "enum (|EnumName|class EnumName|struct EnumName) " + enum_base + " (\{" + enumerators + "\})? ;" | ||
generator = exrex.generate(regex) | ||
declarations = list(generator) | ||
# Unfortunately, the regex produces some invalid declarations. We remove them by hand as of now. | ||
for invalidDeclaration in ["enum ;", "enum : short ;", "enum : unsigned int ;", | ||
"enum : const long ;", "enum : const volatile long long ;", | ||
"enum : decltype(0) ;", "enum EnumName ;"]: | ||
try: | ||
declarations.remove(invalidDeclaration) | ||
except ValueError: | ||
print("The invalid declaration \"" + invalidDeclaration + "\" gets no longer generated, so it can be removed from the list of invalid declarations.") | ||
return declarations | ||
|
||
|
||
def GenerateIndexedCppFiles(parentDirectory, fileContents): | ||
os.makedirs(parentDirectory, exist_ok=True) | ||
for index, aDeclaration in enumerate(fileContents): | ||
fileName = str(index) + ".cpp" | ||
filePath = os.path.join(parentDirectory, fileName) | ||
with open(filePath, "w") as f: | ||
f.write(aDeclaration) | ||
|
||
|
||
GenerateIndexedCppFiles(EnumDeclarationsFolder(), GenerateEnumDeclarations()) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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