2022-09-13 14:31:08 -06:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import "git.tebibyte.media/arf/arf/lexer"
|
|
|
|
import "git.tebibyte.media/arf/arf/infoerr"
|
2022-09-15 12:54:13 -06:00
|
|
|
import "git.tebibyte.media/arf/arf/types"
|
2022-09-13 14:31:08 -06:00
|
|
|
|
|
|
|
// parseType parses a type notation of the form Name, {Name}, etc.
|
|
|
|
func (parser *ParsingOperation) parseType () (what Type, err error) {
|
|
|
|
err = parser.expect(lexer.TokenKindName, lexer.TokenKindLBrace)
|
|
|
|
if err != nil { return }
|
|
|
|
what.location = parser.token.Location()
|
|
|
|
|
|
|
|
if parser.token.Is(lexer.TokenKindLBrace) {
|
|
|
|
what.kind = TypeKindPointer
|
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
var points Type
|
|
|
|
points, err = parser.parseType()
|
|
|
|
if err != nil { return }
|
|
|
|
what.points = &points
|
|
|
|
|
|
|
|
err = parser.expect (
|
|
|
|
lexer.TokenKindRBrace,
|
|
|
|
lexer.TokenKindElipsis)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if parser.token.Is(lexer.TokenKindElipsis) {
|
|
|
|
what.kind = TypeKindVariableArray
|
|
|
|
|
|
|
|
err = parser.nextToken(lexer.TokenKindRBrace)
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
} else {
|
|
|
|
what.name, err = parser.parseIdentifier()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if !parser.token.Is(lexer.TokenKindColon) { break }
|
2022-09-16 20:22:07 -06:00
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.skipWhitespace()
|
|
|
|
if err != nil { return }
|
2022-09-17 09:50:27 -06:00
|
|
|
|
2022-09-16 20:22:07 -06:00
|
|
|
err = parser.expect(
|
2022-09-14 13:16:56 -06:00
|
|
|
lexer.TokenKindName,
|
|
|
|
lexer.TokenKindUInt,
|
|
|
|
lexer.TokenKindLParen,
|
|
|
|
lexer.TokenKindLessThan)
|
2022-09-13 14:31:08 -06:00
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if parser.token.Is(lexer.TokenKindName) {
|
|
|
|
// parse type qualifier
|
|
|
|
qualifier := parser.token.Value().(string)
|
|
|
|
switch qualifier {
|
|
|
|
case "mut":
|
|
|
|
what.mutable = true
|
|
|
|
default:
|
|
|
|
err = parser.token.NewError (
|
|
|
|
"unknown type qualifier \"" +
|
|
|
|
qualifier + "\"",
|
|
|
|
infoerr.ErrorKindError)
|
|
|
|
return
|
|
|
|
}
|
2022-09-17 09:50:27 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-14 13:16:56 -06:00
|
|
|
} else if parser.token.Is(lexer.TokenKindUInt) {
|
2022-09-13 14:31:08 -06:00
|
|
|
// parse fixed array length
|
|
|
|
what.length = parser.token.Value().(uint64)
|
2022-09-16 22:58:08 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-09-14 13:16:56 -06:00
|
|
|
|
|
|
|
} else if parser.token.Is(lexer.TokenKindLessThan) {
|
|
|
|
// parse default value
|
|
|
|
what.defaultValue, err = parser.parseBasicDefaultValue()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
} else if parser.token.Is(lexer.TokenKindLParen) {
|
|
|
|
// parse members and member default values
|
|
|
|
what.defaultValue,
|
|
|
|
what.members,
|
|
|
|
err = parser.parseObjectDefaultValueAndMembers()
|
|
|
|
if err != nil { return }
|
2022-09-13 14:31:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 16:16:18 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseBasicDefaultValue parses a default value of a non-object type.
|
|
|
|
func (parser *ParsingOperation) parseBasicDefaultValue () (
|
|
|
|
value Argument,
|
|
|
|
err error,
|
|
|
|
) {
|
2022-09-14 13:16:56 -06:00
|
|
|
value.location = parser.token.Location()
|
|
|
|
|
2022-09-13 16:16:18 -06:00
|
|
|
err = parser.expect(lexer.TokenKindLessThan)
|
|
|
|
if err != nil { return }
|
2022-09-13 18:45:21 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-09-13 16:16:18 -06:00
|
|
|
|
2022-09-14 13:16:56 -06:00
|
|
|
var attributes []Argument
|
2022-09-13 16:16:18 -06:00
|
|
|
|
|
|
|
defer func () {
|
|
|
|
// if we have multiple values, we need to return the full array
|
|
|
|
// instead.
|
2022-09-14 13:16:56 -06:00
|
|
|
if len(attributes) > 1 {
|
2022-09-13 16:16:18 -06:00
|
|
|
value.kind = ArgumentKindArrayDefaultValues
|
2022-09-14 13:16:56 -06:00
|
|
|
value.value = ArrayDefaultValues(attributes)
|
2022-09-13 16:16:18 -06:00
|
|
|
}
|
|
|
|
} ()
|
|
|
|
|
|
|
|
for {
|
2022-09-16 10:27:13 -06:00
|
|
|
err = parser.skipWhitespace()
|
|
|
|
if err != nil { return }
|
2022-09-13 16:16:18 -06:00
|
|
|
if parser.token.Is(lexer.TokenKindGreaterThan) { break }
|
|
|
|
|
|
|
|
value, err = parser.parseArgument()
|
|
|
|
if err != nil { return }
|
2022-09-14 13:16:56 -06:00
|
|
|
attributes = append(attributes, value)
|
2022-09-13 16:16:18 -06:00
|
|
|
}
|
2022-09-16 22:58:08 -06:00
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-13 14:31:08 -06:00
|
|
|
return
|
|
|
|
}
|
2022-09-13 18:45:21 -06:00
|
|
|
|
2022-09-14 13:16:56 -06:00
|
|
|
// parseObjectDefaultValueAndMembers parses default values and new members of an
|
|
|
|
// object type.
|
|
|
|
func (parser *ParsingOperation) parseObjectDefaultValueAndMembers () (
|
2022-09-13 18:45:21 -06:00
|
|
|
value Argument,
|
|
|
|
members []TypeMember,
|
|
|
|
err error,
|
|
|
|
) {
|
2022-09-14 13:16:56 -06:00
|
|
|
value.location = parser.token.Location()
|
|
|
|
|
2022-09-13 18:45:21 -06:00
|
|
|
err = parser.expect(lexer.TokenKindLParen)
|
|
|
|
if err != nil { return }
|
|
|
|
parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-14 13:16:56 -06:00
|
|
|
var attributes ObjectDefaultValues
|
|
|
|
|
2022-09-13 18:45:21 -06:00
|
|
|
for {
|
2022-09-16 10:27:13 -06:00
|
|
|
err = parser.skipWhitespace()
|
|
|
|
if err != nil { return }
|
2022-09-14 13:16:56 -06:00
|
|
|
if parser.token.Is(lexer.TokenKindRParen) { break }
|
|
|
|
|
2022-09-13 18:45:21 -06:00
|
|
|
err = parser.expect(lexer.TokenKindDot)
|
|
|
|
if err != nil { return }
|
2022-09-13 18:45:59 -06:00
|
|
|
parser.nextToken(lexer.TokenKindName, lexer.TokenKindPermission)
|
2022-09-13 18:45:21 -06:00
|
|
|
if err != nil { return }
|
2022-09-13 18:45:59 -06:00
|
|
|
|
2022-09-14 13:16:56 -06:00
|
|
|
if parser.token.Is(lexer.TokenKindName) {
|
|
|
|
// parsing a defalut value for an inherited member
|
|
|
|
var memberName string
|
|
|
|
var memberValue Argument
|
|
|
|
|
|
|
|
memberName,
|
2022-09-15 12:54:13 -06:00
|
|
|
memberValue, err = parser.parseObjectInheritedMember()
|
2022-09-14 13:16:56 -06:00
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if value.kind == ArgumentKindNil {
|
|
|
|
// create default value map if it doesn't
|
|
|
|
// already exist
|
|
|
|
value.kind = ArgumentKindObjectDefaultValues
|
|
|
|
attributes = make(ObjectDefaultValues)
|
|
|
|
value.value = attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: error on duplicate
|
|
|
|
if memberValue.kind != ArgumentKindNil {
|
|
|
|
attributes[memberName] = memberValue
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if parser.token.Is(lexer.TokenKindPermission) {
|
|
|
|
// parsing a member declaration
|
|
|
|
var member TypeMember
|
2022-09-17 00:07:50 -06:00
|
|
|
member, err = parser.parseObjectNewMember()
|
2022-09-14 13:16:56 -06:00
|
|
|
|
2022-09-15 12:54:13 -06:00
|
|
|
// TODO: error on duplicate
|
2022-09-14 13:16:56 -06:00
|
|
|
members = append(members, member)
|
2022-09-17 00:07:50 -06:00
|
|
|
if err != nil { return }
|
2022-09-14 13:16:56 -06:00
|
|
|
}
|
2022-09-13 18:45:21 -06:00
|
|
|
}
|
|
|
|
|
2022-09-16 22:58:08 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-13 18:45:21 -06:00
|
|
|
return
|
|
|
|
}
|
2022-09-14 13:16:56 -06:00
|
|
|
|
|
|
|
// parseObjectDefaultValue parses member default values only, and will throw an
|
|
|
|
// error when it encounteres a new member definition.
|
|
|
|
func (parser *ParsingOperation) parseObjectDefaultValue () (
|
|
|
|
value Argument,
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
value.location = parser.token.Location()
|
|
|
|
|
|
|
|
err = parser.expect(lexer.TokenKindLParen)
|
|
|
|
if err != nil { return }
|
|
|
|
parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
var attributes ObjectDefaultValues
|
|
|
|
|
|
|
|
for {
|
2022-09-16 10:27:13 -06:00
|
|
|
err = parser.skipWhitespace()
|
|
|
|
if err != nil { return }
|
2022-09-14 13:16:56 -06:00
|
|
|
if parser.token.Is(lexer.TokenKindRParen) { break }
|
|
|
|
|
|
|
|
err = parser.expect(lexer.TokenKindDot)
|
|
|
|
if err != nil { return }
|
|
|
|
parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if value.kind == ArgumentKindNil {
|
|
|
|
value.kind = ArgumentKindObjectDefaultValues
|
|
|
|
attributes = make(ObjectDefaultValues)
|
|
|
|
value.value = attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
var memberName string
|
|
|
|
var memberValue Argument
|
|
|
|
memberName,
|
2022-09-15 12:54:13 -06:00
|
|
|
memberValue, err = parser.parseObjectInheritedMember()
|
2022-09-14 13:16:56 -06:00
|
|
|
|
|
|
|
attributes[memberName] = memberValue
|
|
|
|
}
|
|
|
|
|
2022-09-16 22:58:08 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-14 13:16:56 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-15 12:54:13 -06:00
|
|
|
// .name:<value>
|
2022-09-14 13:16:56 -06:00
|
|
|
|
2022-09-15 12:54:13 -06:00
|
|
|
// parseObjectInheritedMember parses a new default value for an inherited
|
2022-09-14 13:16:56 -06:00
|
|
|
// member.
|
2022-09-15 12:54:13 -06:00
|
|
|
func (parser *ParsingOperation) parseObjectInheritedMember () (
|
2022-09-14 13:16:56 -06:00
|
|
|
name string,
|
|
|
|
value Argument,
|
|
|
|
err error,
|
|
|
|
) {
|
2022-09-15 11:04:31 -06:00
|
|
|
// get the name of the inherited member
|
2022-09-14 13:16:56 -06:00
|
|
|
err = parser.expect(lexer.TokenKindName)
|
2022-09-15 12:54:13 -06:00
|
|
|
value.location = parser.token.Location()
|
2022-09-14 13:16:56 -06:00
|
|
|
if err != nil { return }
|
2022-09-15 11:04:31 -06:00
|
|
|
name = parser.token.Value().(string)
|
|
|
|
|
2022-09-15 12:54:13 -06:00
|
|
|
// we require a default value to be present
|
2022-09-15 11:04:31 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindColon)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken(lexer.TokenKindLParen, lexer.TokenKindLessThan)
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-15 12:03:32 -06:00
|
|
|
if parser.token.Is(lexer.TokenKindLessThan) {
|
|
|
|
// parse default value
|
|
|
|
value, err = parser.parseBasicDefaultValue()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
} else if parser.token.Is(lexer.TokenKindLParen) {
|
|
|
|
// parse member default values
|
|
|
|
value, err = parser.parseObjectDefaultValue()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
2022-09-14 13:16:56 -06:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-15 12:54:13 -06:00
|
|
|
// .ro name:Type:qualifier:<value>
|
2022-09-14 13:16:56 -06:00
|
|
|
|
2022-09-15 12:54:13 -06:00
|
|
|
// parseObjectNewMember parses an object member declaration, and its
|
2022-09-14 13:16:56 -06:00
|
|
|
// default value if it exists.
|
2022-09-15 12:54:13 -06:00
|
|
|
func (parser *ParsingOperation) parseObjectNewMember () (
|
2022-09-14 13:16:56 -06:00
|
|
|
member TypeMember,
|
|
|
|
err error,
|
|
|
|
) {
|
2022-09-15 12:54:13 -06:00
|
|
|
// get member permission
|
2022-09-14 13:16:56 -06:00
|
|
|
err = parser.expect(lexer.TokenKindPermission)
|
2022-09-15 12:54:13 -06:00
|
|
|
member.location = parser.token.Location()
|
|
|
|
if err != nil { return }
|
|
|
|
member.permission = parser.token.Value().(types.Permission)
|
|
|
|
|
|
|
|
// get member name
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
member.name = parser.token.Value().(string)
|
|
|
|
|
|
|
|
// get type
|
|
|
|
err = parser.nextToken(lexer.TokenKindColon)
|
|
|
|
if err != nil { return }
|
2022-09-16 16:29:39 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindName, lexer.TokenKindLBrace)
|
|
|
|
if err != nil { return }
|
2022-09-15 12:54:13 -06:00
|
|
|
member.what, err = parser.parseType()
|
2022-09-14 13:16:56 -06:00
|
|
|
if err != nil { return }
|
2022-09-15 20:43:02 -06:00
|
|
|
|
2022-09-16 22:58:08 -06:00
|
|
|
// get bit width
|
2022-09-16 20:48:24 -06:00
|
|
|
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-14 13:16:56 -06:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|