Created
November 11, 2020 16:48
-
-
Save Myriad-Dreamin/cf2fefc5e951f1878a4cdf3898250d6b to your computer and use it in GitHub Desktop.
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
| class MacroState(enum.Enum): | |
| Nothing = 0 | |
| IfMatching = 1 | |
| Skipping = 2 | |
| def match_function_body(content, start): | |
| cnt = 0 | |
| hash_reading = None | |
| skip_macro = MacroState.Nothing | |
| for i in range(start, len(content)): | |
| if content[i] == '{' and skip_macro != MacroState.Skipping: | |
| cnt += 1 | |
| elif content[i] == '}' and skip_macro != MacroState.Skipping: | |
| cnt -= 1 | |
| elif content[i] == '#': | |
| hash_reading = '' | |
| elif hash_reading is not None: | |
| hash_reading += content[i] | |
| if len(hash_reading) == 2: | |
| if hash_reading == 'el': | |
| skip_macro = MacroState.Skipping | |
| hash_reading = None | |
| elif hash_reading == 'if': | |
| skip_macro = MacroState.IfMatching | |
| hash_reading = None | |
| elif len(hash_reading) == 5: | |
| if hash_reading == 'endif': | |
| skip_macro = MacroState.Nothing | |
| hash_reading = None | |
| elif len(hash_reading) > 6: | |
| hash_reading = None | |
| if cnt == 0 and skip_macro == MacroState.Nothing: | |
| return i | |
| return None | |
| if __name__ == '__main__': | |
| def assert_content(content): | |
| found = match_function_body(content, 0) | |
| assert found is not None | |
| if found != len(content) - 1: | |
| print(content[:found+1]) | |
| assert False | |
| assert_content("""{ | |
| }""") | |
| assert_content("""{ | |
| #ifdef | |
| } | |
| #else | |
| } | |
| #endif""") | |
| assert_content("""{ | |
| #ifdef | |
| { | |
| #else | |
| { | |
| #endif | |
| }}""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment