Commit 4da68d6b authored by Tristan Gingold's avatar Tristan Gingold

Improve check_name_decl rule: can now give a dedicated error message.

parent f78b4f38
......@@ -147,11 +147,14 @@ rules.add(CheckNameDecl(kind=iirs.Iir_Kind.Architecture_Body,
name='ArchNames'))
# [Constants] [M] Constants name
rules.add(CheckNameDecl(kind=iirs.Iir_Kind.Constant_Declaration,
predicate=(lambda n: len(n) >= 3
and n[:2] == 'c_'
and n[2:].isupper()),
name='Constants'))
rules.add(CheckNameDecl(
kind=iirs.Iir_Kind.Constant_Declaration,
predicate=[lambda s: ("constant name '{}' must start with 'c_'".format(s)
if not s.startswith('c_') else None),
lambda s: (
"constant name '{}' must be in upper case after 'c_'".format(s)
if not s[2:].isupper() else None)],
name='Constants'))
# [GenericsName] [M] Generics name
rules.add(CheckGenerics(name='GenericsName'))
......
......@@ -19,17 +19,37 @@ class CheckNameDecl(SyntaxNodeRule):
self.kind = kind
self.predicate = predicate
def check_predicate(self, p, node, s):
"""Test predicate :param p: on string :param s:. The predicate shall
return an error message in case of error or None if the name is
correct."""
res = p(s)
if isinstance(res, str):
self.error(Location.from_node(node), res)
return True
elif res is None or res:
return False
else:
self.error(Location.from_node(node),
"incorrect name '{}'".format(s))
return True
def check(self, input, node):
if iirs.Get_Kind(node) == self.kind:
s = nodeutils.get_identifier_str(node)
if not self.predicate(s):
self.error(Location.from_node(node),
"incorrect name '{}'".format(s))
if isinstance(self.predicate, list):
for p in self.predicate:
if self.check_predicate(p, node, s):
break
else:
self.check_predicate(self.predicate, node, s)
@staticmethod
def test(runner):
rule = CheckNameDecl(kind=iirs.Iir_Kind.Constant_Declaration,
predicate=(lambda s: s != 'Reserved'))
rule = CheckNameDecl(
kind=iirs.Iir_Kind.Constant_Declaration,
predicate=(lambda s: ("name {} must not be 'Reserved'".format(s)
if s == 'Reserved' else None)))
TestRunOK(runner, "File without attributes",
rule, "hello.vhdl")
TestRunFail(runner, "Incorrect name",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment