Untested type section parsing yay

This commit is contained in:
Sasha Koshka 2022-09-29 02:01:31 -04:00
parent 58af5f3f15
commit 6c02e45e2e
4 changed files with 105 additions and 9 deletions

View File

@ -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 }

View File

@ -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() + " "

View File

@ -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

View File

@ -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
}