Created
December 29, 2016 16:19
-
-
Save mwoehlke-kitware/a0e58920a1c0bb148df8cf91c5839d63 to your computer and use it in GitHub Desktop.
Simple LCM type parser in Python
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
#!/usr/bin/env python | |
import re | |
import sys | |
#============================================================================== | |
def parse(f): | |
package = [] | |
# Get text | |
text = f.read() | |
# Remove comments | |
text = re.sub('//[^\n]*\n', '\n', text) | |
text = re.sub('/[*]([^*]*[*][^*/])*[^*]*[*]+/', '', text) | |
# Examine statements | |
for statement in re.split('[{};]', text): | |
tokens = statement.split() | |
if not len(tokens): | |
continue | |
if tokens[0] == 'package': | |
package = tokens[1].split('.') | |
elif tokens[0] in {'struct', 'enum'}: | |
print '%s %r' % (tokens[0], '.'.join(package + tokens[1:2])) | |
#============================================================================== | |
def main(args): | |
if len(args) < 2 or args[1] == '-': | |
parse(sys.stdin) | |
else: | |
parse(open(args[1])) | |
#============================================================================== | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This simple parser will parse an LCM type definition file and print the list of fully qualified types that are defined by the file. It is meant to serve as an example of how one might implement such a parser.
This logic is equivalent to the parsing code that ships with LCM to allow CMake users to generate LCM bindings.