This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
arf/parser/type.go

137 lines
3.6 KiB
Go
Raw Normal View History

2022-08-18 15:39:19 -06:00
package parser
2022-08-29 23:11:10 -06:00
import "git.tebibyte.media/arf/arf/types"
import "git.tebibyte.media/arf/arf/lexer"
2022-08-18 18:09:04 -06:00
2022-09-29 00:01:31 -06:00
// parseTypeSection parses a type definition. It can inherit from other types,
// and define new members on them.
2022-10-11 21:57:27 -06:00
func (parser *parsingOperation) parseTypeSection () (
2022-09-04 17:55:47 -06:00
section TypeSection,
2022-08-18 15:39:19 -06:00
err error,
) {
2022-08-18 18:09:04 -06:00
err = parser.expect(lexer.TokenKindName)
if err != nil { return }
section.location = parser.token.Location()
2022-08-18 18:09:04 -06:00
// get permission
2022-08-20 11:26:24 -06:00
err = parser.nextToken(lexer.TokenKindPermission)
2022-08-18 18:09:04 -06:00
if err != nil { return }
2022-08-20 11:26:24 -06:00
section.permission = parser.token.Value().(types.Permission)
2022-08-18 18:09:04 -06:00
// get name
err = parser.nextToken(lexer.TokenKindName)
if err != nil { return }
2022-08-20 11:26:24 -06:00
section.name = parser.token.Value().(string)
2022-08-18 18:09:04 -06:00
2022-08-20 11:26:24 -06:00
// parse inherited type
2022-08-18 18:09:04 -06:00
err = parser.nextToken(lexer.TokenKindColon)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
section.what, err = parser.parseType()
2022-08-18 18:09:04 -06:00
if err != nil { return }
2022-09-29 00:01:31 -06:00
// see if value exists
if parser.token.Is(lexer.TokenKindNewline) {
2022-10-12 12:21:19 -06:00
err = parser.nextToken()
if err != nil { return }
2022-09-29 00:01:31 -06:00
// 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 }
}
}
// parseTypeSectionMember parses a type section member variable.
2022-10-11 21:57:27 -06:00
func (parser *parsingOperation) parseTypeSectionMember () (
2022-09-29 00:01:31 -06:00
member TypeSectionMember,
err error,
) {
// get permission
err = parser.expect(lexer.TokenKindPermission)
2022-09-13 14:31:08 -06:00
if err != nil { return }
2022-09-29 00:01:31 -06:00
member.permission = parser.token.Value().(types.Permission)
2022-10-12 10:39:00 -06:00
member.location = parser.token.Location()
2022-09-29 00:01:31 -06:00
// get name
err = parser.nextToken(lexer.TokenKindName)
if err != nil { return }
member.name = parser.token.Value().(string)
// if there is a type, get it
2022-09-13 14:31:08 -06:00
err = parser.nextToken()
if err != nil { return }
2022-09-29 00:01:31 -06:00
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) {
2022-10-12 22:18:32 -06:00
err = parser.nextToken()
if err != nil { return }
2022-09-29 00:01:31 -06:00
// 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 }
}
2022-09-13 14:31:08 -06:00
2022-09-29 00:01:31 -06:00
err = parser.expect(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
2022-08-18 15:39:19 -06:00
return
}