Skip to content

Commit 21782de

Browse files
authored
Allow sample code in .rst files to span lines (#453)
Signed-off-by: Russell McGuire <russell.w.mcguire@intel.com>
1 parent 44e5621 commit 21782de

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

scripts/generate_docs.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def _generate_valid_rst(fin, fout, namespace, tags, ver, rev, meta):
9292
print("Generating %s..."%fout)
9393

9494
outlines = []
95-
for iline, line in enumerate(util.textRead(fin)):
95+
lines = util.textRead(fin)
96+
for iline, line in enumerate(lines):
9697

9798
if re.match(RE_ENABLE, line) or re.match(RE_PYCODE_BLOCK_END, line):
9899
enable = True
@@ -134,10 +135,25 @@ def _generate_valid_rst(fin, fout, namespace, tags, ver, rev, meta):
134135
continue
135136

136137
if code_block and 'function' == symbol_type:
137-
words = re.sub(RE_EXTRACT_PARAMS, r"\1", line)
138-
words = line.split(",")
139-
if len(words) != len(meta['function'][symbol]['params']):
140-
print("%s(%s) : error : %s parameter count mismatch - %s actual vs. %s expected"%(fin, iline+1, symbol, len(words), len(meta['function'][symbol]['params'])))
138+
# A function call in sample code may be formatted across multiple
139+
# lines for readability. Start with the current line and accumulate
140+
# subsequent lines until parentheses are balanced (i.e. the call is
141+
# complete), so that all parameters are visible for counting.
142+
full_call = line.rstrip('\n')
143+
lookahead = iline + 1
144+
while full_call.count('(') != full_call.count(')') and lookahead < len(lines):
145+
# Strip leading whitespace from continuation lines so they join
146+
# cleanly without affecting the comma-split count below.
147+
full_call += ' ' + lines[lookahead].strip()
148+
lookahead += 1
149+
# Extract only the argument list between the outermost parentheses
150+
# using RE_EXTRACT_PARAMS, then count arguments by splitting on ','.
151+
# Without this extraction step, splitting the raw line would also
152+
# count commas inside nested calls or template arguments incorrectly.
153+
param_str = re.sub(RE_EXTRACT_PARAMS, r"\1", full_call)
154+
param_count = len(param_str.split(","))
155+
if param_count != len(meta['function'][symbol]['params']):
156+
print("%s(%s) : error : %s parameter count mismatch - %s actual vs. %s expected"%(fin, iline+1, symbol, param_count, len(meta['function'][symbol]['params'])))
141157
print("line = %s"%line)
142158

143159
ref = _make_ref(fin, iline, symbol, symbol_type, meta)

0 commit comments

Comments
 (0)