2022-08-25 18:01:12 -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"
|
2022-09-01 15:42:56 -06:00
|
|
|
import "git.tebibyte.media/arf/arf/infoerr"
|
2022-08-25 18:01:12 -06:00
|
|
|
|
|
|
|
// parseFunc parses a function section.
|
|
|
|
func (parser *ParsingOperation) parseFuncSection () (
|
2022-09-04 17:55:47 -06:00
|
|
|
section FuncSection,
|
2022-08-25 18:01:12 -06:00
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
err = parser.expect(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-03 20:58:35 -06:00
|
|
|
section.location = parser.token.Location()
|
2022-08-25 18:01:12 -06:00
|
|
|
|
2022-09-01 19:43:56 -06:00
|
|
|
// get permission
|
2022-08-25 18:01:12 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindPermission)
|
|
|
|
if err != nil { return }
|
|
|
|
section.permission = parser.token.Value().(types.Permission)
|
|
|
|
|
2022-09-01 19:43:56 -06:00
|
|
|
// get name
|
2022-08-25 18:01:12 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
section.name = parser.token.Value().(string)
|
|
|
|
|
2022-09-01 19:43:56 -06:00
|
|
|
// get arguments
|
2022-08-25 18:01:12 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
2022-09-01 19:43:56 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-09-04 17:55:47 -06:00
|
|
|
err = parser.parseFuncArguments(§ion)
|
2022-09-01 19:43:56 -06:00
|
|
|
if err != nil { return }
|
2022-09-01 23:25:22 -06:00
|
|
|
|
|
|
|
// check to see if the function is external
|
|
|
|
if !parser.token.Is(lexer.TokenKindIndent) { return }
|
|
|
|
if parser.token.Value().(int) != 1 { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if parser.token.Is(lexer.TokenKindName) &&
|
|
|
|
parser.token.Value().(string) == "external" {
|
|
|
|
|
|
|
|
section.external = true
|
|
|
|
err = parser.nextToken(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it isn't, backtrack to the start of the line
|
|
|
|
parser.previousToken()
|
|
|
|
|
|
|
|
// parse root block
|
|
|
|
section.root, err = parser.parseBlock(1)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if len(section.root) == 0 {
|
|
|
|
infoerr.NewError (section.location,
|
|
|
|
"this function has nothing in it",
|
|
|
|
infoerr.ErrorKindWarn).Print()
|
|
|
|
}
|
2022-08-25 18:01:12 -06:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-01 15:42:56 -06:00
|
|
|
// parseFuncArguments parses a function's inputs, outputs, and reciever if that
|
|
|
|
// exists.
|
|
|
|
func (parser *ParsingOperation) parseFuncArguments (
|
|
|
|
into *FuncSection,
|
|
|
|
) (
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
for {
|
|
|
|
// if we've left the block, stop parsing
|
2022-09-01 15:55:57 -06:00
|
|
|
if !parser.token.Is(lexer.TokenKindIndent) ||
|
|
|
|
parser.token.Value().(int) != 1 {
|
|
|
|
|
|
|
|
if into.receiver != nil {
|
|
|
|
err = parser.token.NewError (
|
|
|
|
"func section terminated without a " +
|
|
|
|
"separator token",
|
|
|
|
infoerr.ErrorKindError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-09-01 15:42:56 -06:00
|
|
|
|
|
|
|
// determine whether this is an input, output, or the method
|
|
|
|
// reciever
|
|
|
|
err = parser.nextToken (
|
|
|
|
lexer.TokenKindAt,
|
|
|
|
lexer.TokenKindLessThan,
|
|
|
|
lexer.TokenKindGreaterThan,
|
|
|
|
lexer.TokenKindSeparator)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
startToken := parser.token
|
|
|
|
if parser.token.Is(lexer.TokenKindSeparator) {
|
|
|
|
// if we have encountered a separator, that means our
|
|
|
|
// work is done here.
|
2022-09-01 20:10:57 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
2022-09-01 15:42:56 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch startToken.Kind() {
|
|
|
|
case lexer.TokenKindAt:
|
|
|
|
reciever := Declaration { }
|
|
|
|
reciever.location = parser.token.Location()
|
|
|
|
|
|
|
|
// get name
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
reciever.name = parser.token.Value().(string)
|
2022-09-01 20:10:57 -06:00
|
|
|
|
|
|
|
// get type
|
|
|
|
err = parser.nextToken(lexer.TokenKindColon)
|
|
|
|
if err != nil { return }
|
2022-09-01 15:42:56 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-09-01 20:10:57 -06:00
|
|
|
reciever.what, err = parser.parseType()
|
|
|
|
if err != nil { return }
|
2022-09-01 15:42:56 -06:00
|
|
|
|
|
|
|
if into.receiver != nil {
|
|
|
|
err = startToken.NewError (
|
|
|
|
"cannot have more than one method " +
|
|
|
|
"receiver",
|
|
|
|
infoerr.ErrorKindError)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
into.receiver = &reciever
|
|
|
|
}
|
|
|
|
|
|
|
|
err = parser.expect(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
case lexer.TokenKindGreaterThan:
|
|
|
|
input := Declaration { }
|
|
|
|
input.location = parser.token.Location()
|
|
|
|
|
|
|
|
// get name
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
input.name = parser.token.Value().(string)
|
2022-09-01 20:10:57 -06:00
|
|
|
|
|
|
|
// get type
|
|
|
|
err = parser.nextToken(lexer.TokenKindColon)
|
|
|
|
if err != nil { return }
|
2022-09-01 15:42:56 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-09-01 20:10:57 -06:00
|
|
|
input.what, err = parser.parseType()
|
|
|
|
if err != nil { return }
|
2022-09-01 15:42:56 -06:00
|
|
|
|
|
|
|
into.inputs = append(into.inputs, input)
|
|
|
|
|
|
|
|
err = parser.expect(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
case lexer.TokenKindLessThan:
|
|
|
|
output := FuncOutput { }
|
|
|
|
output.location = parser.token.Location()
|
|
|
|
|
|
|
|
// get name
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
output.name = parser.token.Value().(string)
|
2022-09-01 20:10:57 -06:00
|
|
|
|
|
|
|
// get type
|
|
|
|
err = parser.nextToken(lexer.TokenKindColon)
|
|
|
|
if err != nil { return }
|
2022-09-01 15:42:56 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-09-01 20:10:57 -06:00
|
|
|
output.what, err = parser.parseType()
|
|
|
|
if err != nil { return }
|
2022-09-01 15:42:56 -06:00
|
|
|
|
|
|
|
// parse default value
|
|
|
|
if parser.token.Is(lexer.TokenKindNewline) {
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-04 01:31:35 -06:00
|
|
|
output.value, err =
|
2022-09-01 15:42:56 -06:00
|
|
|
parser.parseInitializationValues(1)
|
|
|
|
into.outputs = append(into.outputs, output)
|
|
|
|
if err != nil { return }
|
|
|
|
} else {
|
2022-09-04 01:31:35 -06:00
|
|
|
output.value, err =
|
2022-09-01 15:42:56 -06:00
|
|
|
parser.parseArgument()
|
|
|
|
into.outputs = append(into.outputs, output)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
err = parser.expect(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|