2022-08-16 13:53:32 -06:00
|
|
|
package parser
|
|
|
|
|
2022-08-29 23:11:10 -06:00
|
|
|
import "git.tebibyte.media/arf/arf/lexer"
|
|
|
|
import "git.tebibyte.media/arf/arf/infoerr"
|
2022-08-16 13:53:32 -06:00
|
|
|
|
|
|
|
var validArgumentStartTokens = []lexer.TokenKind {
|
|
|
|
lexer.TokenKindName,
|
|
|
|
|
|
|
|
lexer.TokenKindInt,
|
|
|
|
lexer.TokenKindUInt,
|
|
|
|
lexer.TokenKindFloat,
|
|
|
|
lexer.TokenKindString,
|
|
|
|
|
|
|
|
lexer.TokenKindLBracket,
|
2022-10-11 09:31:44 -06:00
|
|
|
lexer.TokenKindLBrace,
|
2022-09-27 16:03:27 -06:00
|
|
|
lexer.TokenKindLParen,
|
2022-08-16 13:53:32 -06:00
|
|
|
}
|
|
|
|
|
2022-10-11 21:57:27 -06:00
|
|
|
func (parser *parsingOperation) parseArgument () (argument Argument, err error) {
|
2022-09-03 20:58:35 -06:00
|
|
|
argument.location = parser.token.Location()
|
2022-08-16 13:53:32 -06:00
|
|
|
|
|
|
|
err = parser.expect(validArgumentStartTokens...)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
switch parser.token.Kind() {
|
|
|
|
case lexer.TokenKindName:
|
|
|
|
var identifier Identifier
|
|
|
|
identifier, err = parser.parseIdentifier()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if parser.token.Is(lexer.TokenKindColon) {
|
2022-09-02 22:17:25 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-08-16 13:53:32 -06:00
|
|
|
var what Type
|
|
|
|
what, err = parser.parseType()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if len(identifier.trail) != 1 {
|
|
|
|
err = parser.token.NewError (
|
|
|
|
"cannot use member selection in " +
|
|
|
|
"a variable definition",
|
2022-08-17 22:58:40 -06:00
|
|
|
infoerr.ErrorKindError)
|
2022-08-16 13:53:32 -06:00
|
|
|
return
|
|
|
|
}
|
2022-09-03 20:33:34 -06:00
|
|
|
|
2022-09-03 20:56:08 -06:00
|
|
|
declaration := Declaration { }
|
|
|
|
declaration.what = what
|
2022-09-03 20:58:35 -06:00
|
|
|
declaration.name = identifier.trail[0]
|
|
|
|
declaration.location = argument.Location()
|
2022-09-03 20:33:34 -06:00
|
|
|
|
|
|
|
argument.kind = ArgumentKindDeclaration
|
|
|
|
argument.value = declaration
|
|
|
|
|
2022-08-16 13:53:32 -06:00
|
|
|
} else {
|
|
|
|
argument.kind = ArgumentKindIdentifier
|
2022-09-02 22:01:01 -06:00
|
|
|
argument.value = identifier
|
2022-08-16 13:53:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case lexer.TokenKindInt:
|
2022-08-16 14:27:52 -06:00
|
|
|
argument.kind = ArgumentKindInt
|
|
|
|
argument.value = parser.token.Value().(int64)
|
|
|
|
err = parser.nextToken()
|
2022-08-16 13:53:32 -06:00
|
|
|
|
|
|
|
case lexer.TokenKindUInt:
|
2022-08-16 14:27:52 -06:00
|
|
|
argument.kind = ArgumentKindUInt
|
|
|
|
argument.value = parser.token.Value().(uint64)
|
|
|
|
err = parser.nextToken()
|
2022-08-16 13:53:32 -06:00
|
|
|
|
|
|
|
case lexer.TokenKindFloat:
|
2022-08-16 14:27:52 -06:00
|
|
|
argument.kind = ArgumentKindFloat
|
|
|
|
argument.value = parser.token.Value().(float64)
|
|
|
|
err = parser.nextToken()
|
2022-08-16 13:53:32 -06:00
|
|
|
|
|
|
|
case lexer.TokenKindString:
|
2022-08-16 14:27:52 -06:00
|
|
|
argument.kind = ArgumentKindString
|
|
|
|
argument.value = parser.token.Value().(string)
|
|
|
|
parser.nextToken()
|
2022-08-16 13:53:32 -06:00
|
|
|
|
2022-09-02 20:43:48 -06:00
|
|
|
case lexer.TokenKindLBracket:
|
2022-10-11 09:31:44 -06:00
|
|
|
argument.kind = ArgumentKindPhrase
|
2022-09-02 22:01:01 -06:00
|
|
|
argument.value, err = parser.parseArgumentLevelPhrase()
|
2022-08-16 13:53:32 -06:00
|
|
|
|
2022-10-11 09:31:44 -06:00
|
|
|
case lexer.TokenKindLBrace:
|
|
|
|
argument.kind = ArgumentKindDereference
|
|
|
|
argument.value, err = parser.parseDereference()
|
|
|
|
|
2022-09-27 16:03:27 -06:00
|
|
|
case lexer.TokenKindLParen:
|
|
|
|
argument.kind = ArgumentKindList
|
|
|
|
argument.value, err = parser.parseList()
|
|
|
|
|
2022-08-16 13:53:32 -06:00
|
|
|
default:
|
|
|
|
panic (
|
|
|
|
"unimplemented argument kind " +
|
|
|
|
parser.token.Kind().Describe())
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|