Fix bugs with type declaration parsing

This commit is contained in:
Mattia Giambirtone 2024-03-05 19:53:41 +01:00
parent f321644c40
commit eca8d362d2
1 changed files with 19 additions and 15 deletions

View File

@ -1212,27 +1212,31 @@ proc typeDecl(self: Parser): TypeDecl =
self.expect("=", "expecting '=' after type name")
var hasNone = false
case self.peek().kind:
of Ref:
discard self.step()
self.expect("object", "expecting 'object' after 'ref'")
result.isRef = true
of Enum:
discard self.step()
result.isEnum = true
of Object:
discard self.step()
else:
result.value = self.expression()
while not self.check(";"):
case self.peek().lexeme:
of "|": # Untagged type unions
result.value = newBinaryExpr(result.value, self.step(), self.expression())
result.file = self.file
of "~":
result.value = newUnaryExpr(self.step(), result.value)
result.file = self.file
else:
discard
# This is to allow using the ref keyword both to declare a simple
# type alias (i.e. type x = ref y) and to declare referenced structs
# (i.e. type x = ref object {,,,})
if self.check(TokenType.Ref) and self.check(TokenType.Object, 1):
discard self.step()
discard self.step()
result.isRef = true
else:
result.value = self.expression()
while not self.check(";") and not self.done():
case self.peek().lexeme:
of "|": # Untagged type unions
result.value = newBinaryExpr(result.value, self.step(), self.expression())
result.file = self.file
of "~":
result.value = newUnaryExpr(self.step(), result.value)
result.file = self.file
else:
self.error("invalid syntax")
if not result.isEnum and self.match("of"):
# Type has a parent (and is not an enumeration)
result.parent = self.expression()