2022-08-20 11:26:24 -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"
|
|
|
|
import "git.tebibyte.media/arf/arf/infoerr"
|
2022-08-20 11:26:24 -06:00
|
|
|
|
|
|
|
// parseObjtSection parses an object type definition. This allows for structured
|
|
|
|
// types to be defined, and for member variables to be added and overridden.
|
|
|
|
func (parser *ParsingOperation) parseObjtSection () (
|
|
|
|
section *ObjtSection,
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
err = parser.expect(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-03 20:17:05 -06:00
|
|
|
section = &ObjtSection { }
|
2022-09-03 20:58:35 -06:00
|
|
|
section.location = parser.token.Location()
|
2022-08-20 11:26:24 -06:00
|
|
|
|
|
|
|
// get permission
|
|
|
|
err = parser.nextToken(lexer.TokenKindPermission)
|
|
|
|
if err != nil { return }
|
|
|
|
section.permission = parser.token.Value().(types.Permission)
|
|
|
|
|
|
|
|
// get name
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
section.name = parser.token.Value().(string)
|
|
|
|
|
|
|
|
// parse inherited type
|
|
|
|
err = parser.nextToken(lexer.TokenKindColon)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-08-24 16:52:31 -06:00
|
|
|
section.inherits, err = parser.parseIdentifier()
|
2022-08-20 11:26:24 -06:00
|
|
|
if err != nil { return }
|
2022-08-20 11:43:10 -06:00
|
|
|
err = parser.expect(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
2022-08-20 11:26:24 -06:00
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
// parse members
|
|
|
|
err = parser.parseObjtMembers(section)
|
2022-08-20 13:22:25 -06:00
|
|
|
if err != nil { return }
|
2022-08-20 11:26:24 -06:00
|
|
|
|
|
|
|
if len(section.members) == 0 {
|
|
|
|
infoerr.NewError (
|
|
|
|
section.location,
|
|
|
|
"defining an object with no members",
|
|
|
|
infoerr.ErrorKindWarn).Print()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-20 13:22:25 -06:00
|
|
|
// parseObjtMembers parses a list of members for an object section. Indentation
|
|
|
|
// level is assumed.
|
2022-08-20 11:26:24 -06:00
|
|
|
func (parser *ParsingOperation) parseObjtMembers (
|
|
|
|
into *ObjtSection,
|
|
|
|
) (
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
for {
|
|
|
|
// if we've left the block, stop parsing
|
|
|
|
if !parser.token.Is(lexer.TokenKindIndent) { return }
|
|
|
|
if parser.token.Value().(int) != 1 { return }
|
2022-08-20 13:22:25 -06:00
|
|
|
|
|
|
|
// add member to object section
|
|
|
|
var member ObjtMember
|
|
|
|
member, err = parser.parseObjtMember()
|
2022-08-24 18:19:14 -06:00
|
|
|
into.members = append(into.members, member)
|
2022-08-20 13:22:25 -06:00
|
|
|
if err != nil { return }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseObjtMember parses a single member of an object section. Indentation
|
|
|
|
// level is assumed.
|
|
|
|
func (parser *ParsingOperation) parseObjtMember () (
|
|
|
|
member ObjtMember,
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
// get permission
|
|
|
|
err = parser.nextToken(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)
|
2022-08-20 11:26:24 -06:00
|
|
|
|
2022-08-20 13:22:25 -06:00
|
|
|
// get type
|
|
|
|
err = parser.nextToken(lexer.TokenKindColon)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
member.what, err = parser.parseType()
|
|
|
|
if err != nil { return }
|
2022-08-24 16:29:15 -06:00
|
|
|
|
|
|
|
// if there is a bit width, get it
|
2022-08-24 16:37:44 -06:00
|
|
|
if parser.token.Is(lexer.TokenKindBinaryAnd) {
|
2022-08-24 16:29:15 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindUInt)
|
|
|
|
if err != nil { return }
|
|
|
|
member.bitWidth = parser.token.Value().(uint64)
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
2022-08-20 13:22:25 -06:00
|
|
|
|
|
|
|
// parse default value
|
|
|
|
if parser.token.Is(lexer.TokenKindNewline) {
|
|
|
|
err = parser.nextToken()
|
2022-08-20 11:26:24 -06:00
|
|
|
if err != nil { return }
|
|
|
|
|
2022-08-20 13:22:25 -06:00
|
|
|
member.defaultValue,
|
|
|
|
err = parser.parseInitializationValues(1)
|
|
|
|
if err != nil { return }
|
|
|
|
} else {
|
|
|
|
member.defaultValue, err = parser.parseArgument()
|
2022-08-20 11:26:24 -06:00
|
|
|
if err != nil { return }
|
|
|
|
|
2022-08-20 13:22:25 -06:00
|
|
|
err = parser.expect(lexer.TokenKindNewline)
|
2022-08-20 11:26:24 -06:00
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
2022-08-20 13:22:25 -06:00
|
|
|
|
|
|
|
return
|
2022-08-20 11:26:24 -06:00
|
|
|
}
|