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

92 lines
2.1 KiB
Go
Raw Normal View History

package parser
import "git.tebibyte.media/sashakoshka/arf/file"
import "git.tebibyte.media/sashakoshka/arf/lexer"
var validArgumentStartTokens = []lexer.TokenKind {
lexer.TokenKindName,
lexer.TokenKindInt,
lexer.TokenKindUInt,
lexer.TokenKindFloat,
lexer.TokenKindString,
lexer.TokenKindRune,
lexer.TokenKindLBrace,
lexer.TokenKindLBracket,
}
func (parser *ParsingOperation) parseArgument () (argument Argument, err error) {
argument.location = parser.token.Location()
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) {
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",
file.ErrorKindError)
return
}
argument.kind = ArgumentKindDeclaration
argument.value = Declaration {
location: argument.location,
name: identifier.trail[0],
what: what,
}
} else {
argument.kind = ArgumentKindIdentifier
argument.value = identifier
}
case lexer.TokenKindInt:
2022-08-16 14:27:52 -06:00
argument.kind = ArgumentKindInt
argument.value = parser.token.Value().(int64)
err = parser.nextToken()
case lexer.TokenKindUInt:
2022-08-16 14:27:52 -06:00
argument.kind = ArgumentKindUInt
argument.value = parser.token.Value().(uint64)
err = parser.nextToken()
case lexer.TokenKindFloat:
2022-08-16 14:27:52 -06:00
argument.kind = ArgumentKindFloat
argument.value = parser.token.Value().(float64)
err = parser.nextToken()
case lexer.TokenKindString:
2022-08-16 14:27:52 -06:00
argument.kind = ArgumentKindString
argument.value = parser.token.Value().(string)
parser.nextToken()
case lexer.TokenKindRune:
2022-08-16 14:27:52 -06:00
argument.kind = ArgumentKindRune
argument.value = parser.token.Value().(rune)
parser.nextToken()
2022-08-16 14:27:52 -06:00
// case lexer.TokenKindLBrace:
2022-08-16 14:27:52 -06:00
// case lexer.TokenKindLBracket:
default:
panic (
"unimplemented argument kind " +
parser.token.Kind().Describe())
}
return
}