Compare commits

...

1 commit

Author SHA1 Message Date
80f70b2940 genset: create a prototype C code generator. 2015-05-31 22:10:47 +02:00
2 changed files with 54 additions and 0 deletions

37
support/genset/genset.py Normal file
View file

@ -0,0 +1,37 @@
"""Example of a parser that handles a subset of C and can extract
structs and typedefs for later generation.
See test.h for example input.
"""
import fileinput
import pprint
from pyparsing import *
struct = Keyword('struct')
lbrace = Literal('{')
rbrace = Literal('}')
semi = Literal(';')
identifier = Word(alphas, alphanums + '_')
typedef = Keyword('typedef')
comment = Literal('//').suppress() + restOfLine
comments = Group(OneOrMore(comment))
type_alias = Optional(comments) + typedef + identifier + identifier
field_def = Optional(comments) + identifier + identifier + semi
struct_def = struct + Optional(identifier) + lbrace + OneOrMore(field_def) + rbrace
typedef_def = Optional(comments) + typedef + struct_def + identifier
bnf = ZeroOrMore((struct_def | typedef_def | type_alias) + semi)
def main():
for v in bnf.scanString('\n'.join(fileinput.input())):
results, start, end = v
results.pprint()
if __name__ == '__main__':
main()

17
support/genset/test.h Normal file
View file

@ -0,0 +1,17 @@
// Typedef to allow reuse of ranges and units.
// range: 1000...2000; units: ns
typedef int16_t servoDrive_t;
// Comment
// and more
typedef struct servoParam_s2 {
// Short description. Longer description to show in detailed
// help. Basics are inherited from the typedef.
//
// default: 1000
servoDrive_t min;
// default: 2000
servoDrive_t max;
// default: 1500
servoDrive_t mid;
} foo_t;