From 6c02e45e2ec76fcec123eb1f3b7d22975968d29b Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 29 Sep 2022 02:01:31 -0400 Subject: [PATCH] Untested type section parsing yay --- parser/data.go | 2 +- parser/tree-tostring.go | 2 +- parser/tree.go | 5 +- parser/type.go | 105 +++++++++++++++++++++++++++++++++++++--- 4 files changed, 105 insertions(+), 9 deletions(-) diff --git a/parser/data.go b/parser/data.go index 27a23c0..0b0f0f3 100644 --- a/parser/data.go +++ b/parser/data.go @@ -38,7 +38,7 @@ func (parser *ParsingOperation) parseDataSection () ( // see if value exists if parser.token.Is(lexer.TokenKindNewline) { parser.nextToken() - // if we have exited the member, return + // if we have exited the section, return if !parser.token.Is(lexer.TokenKindIndent) { return } if parser.token.Value().(int) != 1 { return } diff --git a/parser/tree-tostring.go b/parser/tree-tostring.go index 1f866e6..4ae2019 100644 --- a/parser/tree-tostring.go +++ b/parser/tree-tostring.go @@ -66,7 +66,7 @@ func (identifier Identifier) ToString () (output string) { return } -func (member TypeMember) ToString (indent int, breakLine bool) (output string) { +func (member TypeSectionMember) ToString (indent int, breakLine bool) (output string) { output += doIndent(indent, ".") output += member.permission.ToString() + " " diff --git a/parser/tree.go b/parser/tree.go index f89be1e..6051312 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -36,9 +36,12 @@ type Identifier struct { type TypeKind int const ( + // TypeKindNil means that the type is unspecified. + TypeKindNil TypeKind = iota + // TypeKindBasic means its a normal type and inherits from something. // Basic types can define new members on their parent types. - TypeKindBasic TypeKind = iota + TypeKindBasic // TypeKindPointer means it's a pointer. TypeKindPointer diff --git a/parser/type.go b/parser/type.go index 14c8aa6..292ec32 100644 --- a/parser/type.go +++ b/parser/type.go @@ -3,8 +3,8 @@ package parser import "git.tebibyte.media/arf/arf/types" import "git.tebibyte.media/arf/arf/lexer" -// parseTypeSection parses a blind type definition, meaning it can inherit from -// anything including primitives, but cannot define structure. +// parseTypeSection parses a type definition. It can inherit from other types, +// and define new members on them. func (parser *ParsingOperation) parseTypeSection () ( section TypeSection, err error, @@ -32,10 +32,103 @@ func (parser *ParsingOperation) parseTypeSection () ( section.what, err = parser.parseType() if err != nil { return } - parser.expect(lexer.TokenKindNewline) - if err != nil { return } - err = parser.nextToken() - if err != nil { return } + // see if value exists + if parser.token.Is(lexer.TokenKindNewline) { + parser.nextToken() + // if we have exited the section, return + if !parser.token.Is(lexer.TokenKindIndent) { return } + if parser.token.Value().(int) != 1 { return } + + err = parser.nextToken() + if err != nil { return } + } + + // if we have not encountered members, get value and return. + if !parser.token.Is(lexer.TokenKindPermission) { + section.argument, err = parser.parseArgument() + err = parser.expect(lexer.TokenKindNewline) + if err != nil { return } + + err = parser.nextToken() + if err != nil { return } + + return + } + + parser.previousToken() + + for { + // if we have exited the section, return + if !parser.token.Is(lexer.TokenKindIndent) { return } + if parser.token.Value().(int) != 1 { return } + + err = parser.nextToken(lexer.TokenKindPermission) + if err != nil { return } + var member TypeSectionMember + member, err = parser.parseTypeSectionMember() + section.members = append(section.members, member) + if err != nil { return } + } return } + +// parseTypeSectionMember parses a type section member variable. +func (parser *ParsingOperation) parseTypeSectionMember () ( + member TypeSectionMember, + err error, +) { + // get permission + err = parser.expect(lexer.TokenKindPermission) + if err != nil { return } + member.permission = parser.token.Value().(types.Permission) + + // get name + err = parser.nextToken(lexer.TokenKindName) + if err != nil { return } + member.name = parser.token.Value().(string) + + // if there is a type, get it + err = parser.nextToken() + if err != nil { return } + if parser.token.Is(lexer.TokenKindColon) { + err = parser.nextToken(lexer.TokenKindName) + if err != nil { return } + member.what, err = parser.parseType() + if err != nil { return } + } + + // see if value exists + if parser.token.Is(lexer.TokenKindNewline) { + parser.nextToken() + // if we have exited the member, return + if !parser.token.Is(lexer.TokenKindIndent) { return } + if parser.token.Value().(int) != 2 { return } + + err = parser.nextToken() + if err != nil { return } + } + + // if default value exists, get it + if !parser.token.Is(lexer.TokenKindBinaryAnd) { + member.argument, err = parser.parseArgument() + } + + // if there is a bit width specifier, get it + if parser.token.Is(lexer.TokenKindBinaryAnd) { + err = parser.nextToken(lexer.TokenKindUInt) + if err != nil { return } + member.bitWidth = parser.token.Value().(uint64) + + err = parser.nextToken() + if err != nil { return } + } + + err = parser.expect(lexer.TokenKindNewline) + if err != nil { return } + + err = parser.nextToken() + if err != nil { return } + + return +}