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-08-18 15:39:19 -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 }
|
|
|
|
|
2022-09-03 20:58:35 -06:00
|
|
|
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 }
|
2022-09-03 20:56:08 -06:00
|
|
|
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) {
|
|
|
|
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)
|
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)
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
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 }
|
|
|
|
}
|
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
|
|
|
|
}
|