From 90d840879f5d2f38e343fc82a03ede4b23ce1e08 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 12 Oct 2023 23:04:11 -0700 Subject: [PATCH] [Yunq] Add identifier collision checking. --- yunq/example.yunq | 2 +- yunq/yunq.py | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/yunq/example.yunq b/yunq/example.yunq index 391e2be..69307a4 100644 --- a/yunq/example.yunq +++ b/yunq/example.yunq @@ -1,4 +1,4 @@ -interface File { +interface VFS { method open (OpenFileRequest) -> (FileResponse); } diff --git a/yunq/yunq.py b/yunq/yunq.py index 5544610..8e9d73f 100644 --- a/yunq/yunq.py +++ b/yunq/yunq.py @@ -115,6 +115,8 @@ class Message(): Decl = Interface | Message +name_dict: dict[str, Decl] = {} + class Parser(): def __init__(self, tokens: list[Lexeme]): self.tokens = tokens @@ -168,11 +170,19 @@ class Parser(): # "interface" consumed by decl. name = self.consume_identifier() + if name in name_dict.keys(): + sys.exit("Name '%s' already exists." % name) + self.consume_check(LexemeType.LEFT_BRACE) methods: list[Method] = [] + method_names = set() while self.peektype() != LexemeType.RIGHT_BRACE: - methods.append(self.method()) + m = self.method() + if m.name in method_names: + sys.exit("Method %s declared twice on %s" % (m.name, name)) + method_names.add(m.name) + methods.append(m) self.consume_check(LexemeType.RIGHT_BRACE) @@ -203,15 +213,25 @@ class Parser(): name = self.consume_identifier() + if name in name_dict: + sys.exit("Name '%s' already exists." % name) + self.consume_check(LexemeType.LEFT_BRACE) fields: list[Field] = [] + field_names = set() while self.peektype() != LexemeType.RIGHT_BRACE: - fields.append(self.field()) + f = self.field() + if f.name in field_names: + sys.exit("Field %s declared twice on %s" % (f.name, name)) + field_names.add(f.name) + fields.append(f) self.consume_check(LexemeType.RIGHT_BRACE) - return Message(name, fields) + m = Message(name, fields) + name_dict[name] = m + return m def field(self):