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/enum.go

110 lines
2.7 KiB
Go
Raw Normal View History

2022-08-21 00:42:25 -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-21 00:42:25 -06:00
// parseEnumSection parses an enumerated type section.
2022-10-11 21:57:27 -06:00
func (parser *parsingOperation) parseEnumSection () (
2022-09-04 17:55:47 -06:00
section EnumSection,
2022-08-21 00:42:25 -06:00
err error,
) {
err = parser.expect(lexer.TokenKindName)
if err != nil { return }
section.location = parser.token.Location()
2022-08-21 00:42:25 -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 }
section.what, err = parser.parseType()
if err != nil { return }
err = parser.expect(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
// parse members
2022-09-04 17:55:47 -06:00
err = parser.parseEnumMembers(&section)
2022-08-21 00:42:25 -06:00
if err != nil { return }
if len(section.members) == 0 {
infoerr.NewError (
section.location,
"defining an enum with no members",
infoerr.ErrorKindWarn).Print()
}
return
}
2022-08-21 09:17:56 -06:00
// parseEnumMembers parses a list of members for an enum section. Indentation
// level is assumed.
2022-10-11 21:57:27 -06:00
func (parser *parsingOperation) parseEnumMembers (
2022-08-21 09:17:56 -06:00
into *EnumSection,
) (
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-09-16 22:58:08 -06:00
var member EnumMember
member, err = parser.parseEnumMember()
into.members = append(into.members, member)
if err != nil { return }
2022-08-21 09:17:56 -06:00
}
}
2022-09-16 22:58:08 -06:00
// parseEnumMember parses a single enum member. Indenttion level is assumed.
2022-10-11 21:57:27 -06:00
func (parser *parsingOperation) parseEnumMember () (
2022-09-16 22:58:08 -06:00
member EnumMember,
err error,
) {
err = parser.nextToken(lexer.TokenKindMinus)
2022-09-16 22:58:08 -06:00
if err != nil { return }
// get name
err = parser.nextToken(lexer.TokenKindName)
if err != nil { return }
member.location = parser.token.Location()
member.name = parser.token.Value().(string)
// see if value exists
2022-09-27 15:00:44 -06:00
err = parser.nextToken()
2022-09-16 22:58:08 -06:00
if err != nil { return }
2022-09-27 15:00:44 -06:00
if parser.token.Is(lexer.TokenKindNewline) {
2022-10-13 16:02:35 -06:00
err = parser.nextToken()
if err != nil { return }
2022-09-27 15:00:44 -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 }
2022-09-16 22:58:08 -06:00
}
2022-09-27 15:00:44 -06:00
// get value
member.argument, err = parser.parseArgument()
err = parser.expect(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
2022-09-16 22:58:08 -06:00
return
}