Skip to content

Commit

Permalink
Merge pull request #133 from EricRahm/strip_comments
Browse files Browse the repository at this point in the history
Strip comments from templates
  • Loading branch information
robrussell authored Apr 26, 2024
2 parents f94cbfb + 1744629 commit fb5e6da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
8 changes: 5 additions & 3 deletions compiler/back_end/cpp/generated_code_templates
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
// clang-format off

// ** outline ** ///////////////////////////////////////////////////////////////
// Generated by the Emboss compiler. DO NOT EDIT!
/**
* Generated by the Emboss compiler. DO NOT EDIT!
*/
#ifndef ${header_guard}
#define ${header_guard}
#include <stdint.h>
Expand All @@ -39,9 +41,9 @@

${includes}

// NOLINTBEGIN
/* NOLINTBEGIN */
${body}
// NOLINTEND
/* NOLINTEND */

#endif // ${header_guard}

Expand Down
21 changes: 20 additions & 1 deletion compiler/back_end/util/code_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,31 @@ def parse_templates(text):
of the template. [name] should match [A-Za-z][A-Za-z0-9_]* -- that is, it
should be a valid ASCII Python identifier.
Additionally any `//` style comment without leading space of the form:
```C++
// This is an emboss developer related comment, it's useful internally
// but not relevant to end-users of generated code.
```
will be stripped out of the generated code.
If a template wants to define a comment that will be included in the
generated code a C-style comment is recommended:
```C++
/** This will be included in the generated source. */
/**
* So will this!
*/
```
Arguments:
text: The text to parse into templates.
Returns:
A namedtuple object whose attributes are the templates from text.
"""
delimiter_re = re.compile(r"^\W*\*\* ([A-Za-z][A-Za-z0-9_]*) \*\*\W*$")
comment_re = re.compile(r"^\s*//.*$")
templates = {}
name = None
template = []
Expand All @@ -79,7 +97,8 @@ def finish_template(template):
name = delimiter_re.match(line).group(1)
template = []
else:
template.append(line)
if not comment_re.match(line):
template.append(line)
if name:
templates[name] = finish_template(template)
return collections.namedtuple("Templates",
Expand Down

0 comments on commit fb5e6da

Please sign in to comment.