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

72 lines
1.7 KiB
Go
Raw Normal View History

package parser
import "path/filepath"
2022-08-29 23:11:10 -06:00
import "git.tebibyte.media/arf/arf/lexer"
import "git.tebibyte.media/arf/arf/infoerr"
2022-08-12 15:22:51 -06:00
// parseMeta parsese the metadata header at the top of an arf file.
2022-10-11 21:57:27 -06:00
func (parser *parsingOperation) parseMeta () (err error) {
2022-08-12 15:22:51 -06:00
for {
err = parser.expect (
lexer.TokenKindName,
lexer.TokenKindSeparator)
if err != nil { return }
if parser.token.Is(lexer.TokenKindSeparator) {
2022-08-14 20:38:57 -06:00
err = parser.nextToken(lexer.TokenKindNewline)
if err != nil { return }
2022-08-12 15:22:51 -06:00
err = parser.nextToken()
return
}
field := parser.token.Value().(string)
err = parser.nextToken(lexer.TokenKindString)
if err != nil { return }
value := parser.token.Value().(string)
switch field {
case "author":
parser.tree.author = value
case "license":
parser.tree.license = value
case "require":
// if import path is relative, get absolute path.
if value[0] == '.' {
value = filepath.Join(parser.modulePath, value)
} else if value[0] != '/' {
// TODO: get arf import path from an env
// variable, and default to this if not found.
// then, search all paths.
value = filepath.Join (
"/usr/local/include/arf/",
value)
}
basename := filepath.Base(value)
_, exists := parser.tree.requires[basename]
if exists {
err = parser.token.NewError (
"cannot require \"" + basename +
"\" multiple times",
infoerr.ErrorKindError)
return
}
parser.tree.requires[basename] = value
2022-08-12 15:22:51 -06:00
default:
parser.token.NewError (
"unrecognized metadata field: " + field,
infoerr.ErrorKindError)
2022-08-12 15:22:51 -06:00
}
err = parser.nextToken(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
2022-08-12 15:22:51 -06:00
}
}