Skip to content

Commit ee44158

Browse files
committed
Check for 0 instance name
1 parent 3031782 commit ee44158

2 files changed

Lines changed: 42 additions & 8 deletions

File tree

parser/errors.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,30 @@ def __str__(self):
105105
return (
106106
f"Invalid number of parameters for HEADER field '{self.field}'. "
107107
f"Expected {self.expected_len}, found {self.found_len}."
108-
)
108+
)
109+
110+
111+
class InvalidNameError(_ValidationError):
112+
def __init__(self, filecontent, name, linenumbers):
113+
self.name = name
114+
self.filecontent = filecontent
115+
self.linenumbers = linenumbers
116+
117+
def asdict(self, with_message=True):
118+
return {
119+
"type": "invalid_name",
120+
"name": self.name,
121+
"lineno": self.linenumbers[0],
122+
"line": self.filecontent.split("\n")[self.linenumbers[0] - 1],
123+
**({"message": str(self)} if with_message else {}),
124+
}
125+
126+
def __str__(self):
127+
d = self.asdict(with_message=False)
128+
129+
def build():
130+
yield f"On line {d['lineno']}:\nInvalid instance name #{d['name']}"
131+
yield f"{d['lineno']:05d} | {d['line']}"
132+
yield " " * 8 + "^" * len(d["line"].rstrip())
133+
134+
return "\n".join(build())

parser/parse.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,18 @@ def process_tree(filecontent, file_tree, with_progress, error_collector):
4343
sys.stdout.flush()
4444
ent = create_step_entity(entity_tree)
4545
id_ = int(ent["id"])
46+
if id_ == 0:
47+
error_collector.add(InvalidNameError(filecontent, ent["id"], ent["lines"]))
4648
if ents[id_]:
47-
error_collector.add(DuplicateNameError(filecontent, ent["id"], ent["lines"]))
49+
error_collector.add(
50+
DuplicateNameError(filecontent, ent["id"], ent["lines"])
51+
)
4852
else:
4953
ents[id_].append(ent)
5054

5155
return header, ents
5256

57+
5358
def parse(
5459
*,
5560
filename=None,
@@ -148,19 +153,22 @@ def replace_fn(match):
148153

149154
if with_tree:
150155
header, data = process_tree(filecontent, ast, with_progress, error_collector)
151-
error_collector.raise_if_any()
152-
return ParseResult(
153-
header = header,
154-
entities = data
155-
)
156+
error_collector.raise_if_any()
157+
return ParseResult(header=header, entities=data)
156158
else:
157159
# process_tree() would take care of duplicate identifiers,
158160
# but we need to do it ourselves now using our rudimentary
159161
# transformer
160162
seen = set()
161163
for iden, lineno in instance_identifiers:
164+
if iden == 0:
165+
error_collector.add(
166+
InvalidNameError(filecontent, iden, [lineno, lineno])
167+
)
162168
if iden in seen:
163-
error_collector.add(DuplicateNameError(filecontent, iden, [lineno, lineno]))
169+
error_collector.add(
170+
DuplicateNameError(filecontent, iden, [lineno, lineno])
171+
)
164172
else:
165173
seen.add(iden)
166174
error_collector.raise_if_any()

0 commit comments

Comments
 (0)