2022-08-12 12:33:21 -06:00
|
|
|
package parser
|
|
|
|
|
2022-09-07 15:12:46 -06:00
|
|
|
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":
|
2022-09-07 15:12:46 -06:00
|
|
|
// if import path is relative, get absolute path.
|
|
|
|
if value[0] == '.' {
|
2022-10-12 11:11:36 -06:00
|
|
|
value = filepath.Join(parser.modulePath, value)
|
2022-09-07 15:12:46 -06:00
|
|
|
} 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,
|
2022-08-17 22:58:40 -06:00
|
|
|
infoerr.ErrorKindError)
|
2022-08-12 15:22:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err = parser.nextToken(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
2022-08-12 16:12:38 -06:00
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-08-12 15:22:51 -06:00
|
|
|
}
|
2022-08-12 12:33:21 -06:00
|
|
|
}
|