2022-08-23 13:13:00 -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-23 13:13:00 -06:00
|
|
|
|
|
|
|
// parseFaceSection parses an interface section.
|
|
|
|
func (parser *ParsingOperation) parseFaceSection () (
|
2022-09-04 17:55:47 -06:00
|
|
|
section FaceSection,
|
2022-08-23 13:13:00 -06:00
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
err = parser.expect(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-29 13:45:25 -06:00
|
|
|
section.location = parser.token.Location()
|
2022-08-23 13:13:00 -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 interface
|
|
|
|
err = parser.nextToken(lexer.TokenKindColon)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
2022-08-24 16:57:07 -06:00
|
|
|
section.inherits, err = parser.parseIdentifier()
|
2022-08-23 13:13:00 -06:00
|
|
|
if err != nil { return }
|
2022-09-29 13:45:25 -06:00
|
|
|
err = parser.expect(lexer.TokenKindNewline)
|
2022-08-23 13:13:00 -06:00
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-09-29 13:45:25 -06:00
|
|
|
|
|
|
|
if !parser.token.Is(lexer.TokenKindIndent) { return }
|
|
|
|
if parser.token.Value().(int) != 1 { return }
|
|
|
|
|
|
|
|
err = parser.nextToken (
|
|
|
|
lexer.TokenKindName,
|
|
|
|
lexer.TokenKindGreaterThan,
|
|
|
|
lexer.TokenKindLessThan)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if parser.token.Is(lexer.TokenKindName) {
|
|
|
|
// parse type interface
|
2022-09-29 13:52:14 -06:00
|
|
|
section.kind = FaceKindType
|
2022-09-29 13:45:25 -06:00
|
|
|
parser.previousToken()
|
|
|
|
section.behaviors, err = parser.parseFaceBehaviors()
|
|
|
|
if err != nil { return }
|
|
|
|
} else {
|
|
|
|
// parse function interface
|
2022-09-29 13:52:14 -06:00
|
|
|
section.kind = FaceKindFunc
|
2022-09-29 13:45:25 -06:00
|
|
|
parser.previousToken()
|
|
|
|
section.inputs,
|
|
|
|
section.outputs, err = parser.parseFaceBehaviorArguments(1)
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-08-23 13:13:00 -06:00
|
|
|
|
2022-09-29 13:45:25 -06:00
|
|
|
// parseFaceBehaviors parses a list of interface behaviors for an object
|
|
|
|
// interface.
|
|
|
|
func (parser *ParsingOperation) parseFaceBehaviors () (
|
|
|
|
behaviors map[string] FaceBehavior,
|
|
|
|
err error,
|
|
|
|
) {
|
2022-08-23 13:13:00 -06:00
|
|
|
// parse members
|
2022-09-29 13:45:25 -06:00
|
|
|
behaviors = make(map[string] FaceBehavior)
|
2022-08-23 13:13:00 -06:00
|
|
|
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-29 13:45:25 -06:00
|
|
|
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
behaviorBeginning := parser.token.Location()
|
|
|
|
if err != nil { return }
|
2022-08-23 13:13:00 -06:00
|
|
|
|
|
|
|
// parse behavior
|
|
|
|
var behavior FaceBehavior
|
2022-09-29 13:45:25 -06:00
|
|
|
behavior, err = parser.parseFaceBehavior(1)
|
2022-08-23 13:13:00 -06:00
|
|
|
|
|
|
|
// add to section
|
2022-09-29 13:45:25 -06:00
|
|
|
_, exists := behaviors[behavior.name]
|
2022-08-23 13:13:00 -06:00
|
|
|
if exists {
|
|
|
|
err = infoerr.NewError (
|
|
|
|
behaviorBeginning,
|
|
|
|
"multiple behaviors named " + behavior.name +
|
|
|
|
" in this interface",
|
|
|
|
infoerr.ErrorKindError)
|
|
|
|
return
|
|
|
|
}
|
2022-09-29 13:45:25 -06:00
|
|
|
behaviors[behavior.name] = behavior
|
2022-08-23 13:13:00 -06:00
|
|
|
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 13:45:25 -06:00
|
|
|
// parseFaceBehavior parses a single interface behavior.
|
|
|
|
func (parser *ParsingOperation) parseFaceBehavior (
|
|
|
|
indent int,
|
|
|
|
) (
|
2022-08-23 13:13:00 -06:00
|
|
|
behavior FaceBehavior,
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
// get name
|
2022-09-29 13:45:25 -06:00
|
|
|
err = parser.expect(lexer.TokenKindName)
|
2022-08-23 13:13:00 -06:00
|
|
|
if err != nil { return }
|
|
|
|
behavior.name = parser.token.Value().(string)
|
|
|
|
|
2022-08-23 22:25:52 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-09-29 13:45:25 -06:00
|
|
|
|
|
|
|
behavior.inputs,
|
|
|
|
behavior.outputs,
|
|
|
|
err = parser.parseFaceBehaviorArguments(indent + 1)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (parser *ParsingOperation) parseFaceBehaviorArguments (
|
|
|
|
indent int,
|
|
|
|
) (
|
|
|
|
inputs []Declaration,
|
|
|
|
outputs []Declaration,
|
|
|
|
err error,
|
|
|
|
) {
|
2022-08-23 22:25:52 -06:00
|
|
|
|
2022-08-23 13:13:00 -06:00
|
|
|
for {
|
2022-09-29 13:45:25 -06:00
|
|
|
// if we've left the behavior, stop parsing
|
2022-08-23 13:13:00 -06:00
|
|
|
if !parser.token.Is(lexer.TokenKindIndent) { return }
|
2022-09-29 13:45:25 -06:00
|
|
|
if parser.token.Value().(int) != indent { return }
|
2022-08-23 13:13:00 -06:00
|
|
|
|
|
|
|
// get preceding symbol
|
|
|
|
err = parser.nextToken (
|
|
|
|
lexer.TokenKindGreaterThan,
|
|
|
|
lexer.TokenKindLessThan)
|
|
|
|
if err != nil { return }
|
|
|
|
kind := parser.token.Kind()
|
|
|
|
|
|
|
|
var declaration Declaration
|
|
|
|
|
|
|
|
// get name
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
declaration.name = parser.token.Value().(string)
|
|
|
|
|
|
|
|
// parse inherited type
|
|
|
|
err = parser.nextToken(lexer.TokenKindColon)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
declaration.what, err = parser.parseType()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if kind == lexer.TokenKindGreaterThan {
|
2022-09-29 13:45:25 -06:00
|
|
|
inputs = append(inputs, declaration)
|
2022-08-23 13:13:00 -06:00
|
|
|
} else {
|
2022-09-29 13:45:25 -06:00
|
|
|
outputs = append(outputs, declaration)
|
2022-08-23 13:13:00 -06:00
|
|
|
}
|
2022-09-29 13:52:14 -06:00
|
|
|
|
|
|
|
err = parser.expect(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-08-23 13:13:00 -06:00
|
|
|
}
|
|
|
|
}
|