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, ...@@ -147,11 +147,14 @@ rules.add(CheckNameDecl(kind=iirs.Iir_Kind.Architecture_Body,
name='ArchNames')) name='ArchNames'))
# [Constants] [M] Constants name # [Constants] [M] Constants name
rules.add(CheckNameDecl(kind=iirs.Iir_Kind.Constant_Declaration, rules.add(CheckNameDecl(
predicate=(lambda n: len(n) >= 3 kind=iirs.Iir_Kind.Constant_Declaration,
and n[:2] == 'c_' predicate=[lambda s: ("constant name '{}' must start with 'c_'".format(s)
and n[2:].isupper()), if not s.startswith('c_') else None),
name='Constants')) 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 # [GenericsName] [M] Generics name
rules.add(CheckGenerics(name='GenericsName')) rules.add(CheckGenerics(name='GenericsName'))
......
...@@ -19,17 +19,37 @@ class CheckNameDecl(SyntaxNodeRule): ...@@ -19,17 +19,37 @@ class CheckNameDecl(SyntaxNodeRule):
self.kind = kind self.kind = kind
self.predicate = predicate 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): def check(self, input, node):
if iirs.Get_Kind(node) == self.kind: if iirs.Get_Kind(node) == self.kind:
s = nodeutils.get_identifier_str(node) s = nodeutils.get_identifier_str(node)
if not self.predicate(s): if isinstance(self.predicate, list):
self.error(Location.from_node(node), for p in self.predicate:
"incorrect name '{}'".format(s)) if self.check_predicate(p, node, s):
break
else:
self.check_predicate(self.predicate, node, s)
@staticmethod @staticmethod
def test(runner): def test(runner):
rule = CheckNameDecl(kind=iirs.Iir_Kind.Constant_Declaration, rule = CheckNameDecl(
predicate=(lambda s: s != 'Reserved')) 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", TestRunOK(runner, "File without attributes",
rule, "hello.vhdl") rule, "hello.vhdl")
TestRunFail(runner, "Incorrect name", 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