2022-08-14 20:38:57 -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-16 08:44:02 -06:00
|
|
|
|
|
|
|
// parseData parses a data section.
|
2022-08-15 13:09:07 -06:00
|
|
|
func (parser *ParsingOperation) parseDataSection () (
|
|
|
|
section *DataSection,
|
|
|
|
err error,
|
|
|
|
) {
|
2022-08-16 08:44:02 -06:00
|
|
|
err = parser.expect(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
section = &DataSection { location: parser.token.Location() }
|
|
|
|
|
|
|
|
err = parser.nextToken(lexer.TokenKindPermission)
|
|
|
|
if err != nil { return }
|
|
|
|
section.permission = parser.token.Value().(types.Permission)
|
|
|
|
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
section.name = parser.token.Value().(string)
|
|
|
|
|
|
|
|
err = parser.nextToken(lexer.TokenKindColon)
|
|
|
|
if err != nil { return }
|
2022-08-16 15:21:10 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-08-16 08:44:02 -06:00
|
|
|
section.what, err = parser.parseType()
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-08-16 15:21:10 -06:00
|
|
|
if parser.token.Is(lexer.TokenKindNewline) {
|
2022-08-16 18:10:47 -06:00
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
section.value, err = parser.parseInitializationValues(0)
|
|
|
|
if err != nil { return }
|
2022-08-16 15:21:10 -06:00
|
|
|
} else {
|
2022-08-16 22:49:49 -06:00
|
|
|
section.value, err = parser.parseArgument()
|
|
|
|
if err != nil { return }
|
2022-08-16 15:21:10 -06:00
|
|
|
|
|
|
|
err = parser.expect(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
2022-08-16 08:44:02 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-16 18:10:47 -06:00
|
|
|
// parseInitializationValues starts on the line after a data section, or a set
|
|
|
|
// phrase. It checks for an indent greater than the indent of the aforementioned
|
|
|
|
// data section or set phrase (passed through baseIndent), and if there is,
|
|
|
|
// it parses initialization values.
|
|
|
|
func (parser *ParsingOperation) parseInitializationValues (
|
|
|
|
baseIndent int,
|
|
|
|
) (
|
2022-08-16 22:55:53 -06:00
|
|
|
initializationArgument Argument,
|
|
|
|
err error,
|
2022-08-16 18:10:47 -06:00
|
|
|
) {
|
|
|
|
// check if line is indented one more than baseIndent
|
|
|
|
if !parser.token.Is(lexer.TokenKindIndent) { return }
|
|
|
|
if parser.token.Value().(int) != baseIndent + 1 { return }
|
|
|
|
|
2022-08-16 22:55:53 -06:00
|
|
|
initializationArgument.location = parser.token.Location()
|
2022-08-17 10:53:35 -06:00
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-08-16 22:49:49 -06:00
|
|
|
|
2022-08-16 18:10:47 -06:00
|
|
|
if parser.token.Is(lexer.TokenKindDot) {
|
2022-08-17 11:26:18 -06:00
|
|
|
|
|
|
|
// object initialization
|
2022-08-17 10:53:35 -06:00
|
|
|
parser.previousToken()
|
2022-08-16 22:55:53 -06:00
|
|
|
var initializationValues ObjectInitializationValues
|
2022-08-17 09:30:17 -06:00
|
|
|
initializationValues, err = parser.parseObjectInitializationValues()
|
|
|
|
initializationArgument.kind = ArgumentKindObjectInitializationValues
|
2022-08-16 22:55:53 -06:00
|
|
|
initializationArgument.value = &initializationValues
|
2022-08-17 09:30:17 -06:00
|
|
|
|
2022-08-16 18:10:47 -06:00
|
|
|
} else {
|
2022-08-17 09:30:17 -06:00
|
|
|
|
2022-08-17 11:26:18 -06:00
|
|
|
// array initialization
|
2022-08-17 10:53:35 -06:00
|
|
|
parser.previousToken()
|
2022-08-16 22:55:53 -06:00
|
|
|
var initializationValues ArrayInitializationValues
|
2022-08-17 09:30:17 -06:00
|
|
|
initializationValues, err = parser.parseArrayInitializationValues()
|
|
|
|
initializationArgument.kind = ArgumentKindArrayInitializationValues
|
2022-08-16 22:55:53 -06:00
|
|
|
initializationArgument.value = &initializationValues
|
2022-08-16 18:10:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-16 22:14:55 -06:00
|
|
|
// parseObjectInitializationValues parses a list of object initialization
|
2022-08-17 09:30:17 -06:00
|
|
|
// values until the indentation level drops.
|
|
|
|
func (parser *ParsingOperation) parseObjectInitializationValues () (
|
2022-08-17 10:53:35 -06:00
|
|
|
initializationValues ObjectInitializationValues,
|
|
|
|
err error,
|
2022-08-16 22:14:55 -06:00
|
|
|
) {
|
2022-08-17 10:53:35 -06:00
|
|
|
initializationValues.attributes = make(map[string] Argument)
|
|
|
|
|
|
|
|
baseIndent := 0
|
|
|
|
begin := true
|
|
|
|
|
|
|
|
for {
|
|
|
|
// if there is no indent we can just stop parsing
|
|
|
|
if !parser.token.Is(lexer.TokenKindIndent) { break}
|
|
|
|
indent := parser.token.Value().(int)
|
|
|
|
|
|
|
|
if begin == true {
|
|
|
|
initializationValues.location = parser.token.Location()
|
|
|
|
baseIndent = indent
|
|
|
|
begin = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not parse any further if the indent has changed
|
|
|
|
if indent != baseIndent { break }
|
|
|
|
|
|
|
|
// move on to the beginning of the line, which must contain
|
|
|
|
// a member initialization value
|
|
|
|
err = parser.nextToken(lexer.TokenKindDot)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
name := parser.token.Value().(string)
|
|
|
|
|
|
|
|
// if the member has already been listed, throw an error
|
|
|
|
_, exists := initializationValues.attributes[name]
|
|
|
|
if exists {
|
|
|
|
err = parser.token.NewError (
|
|
|
|
"duplicate member \"" + name + "\" in object " +
|
|
|
|
"member initialization",
|
2022-08-17 22:58:40 -06:00
|
|
|
infoerr.ErrorKindError)
|
2022-08-17 10:53:35 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the argument determining the member initialization
|
|
|
|
// value
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
var value Argument
|
|
|
|
if parser.token.Is(lexer.TokenKindNewline) {
|
|
|
|
|
2022-08-17 11:26:18 -06:00
|
|
|
// recurse
|
2022-08-17 10:53:35 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindIndent)
|
|
|
|
if err != nil { return }
|
2022-08-17 11:26:18 -06:00
|
|
|
|
|
|
|
value, err = parser.parseInitializationValues(baseIndent)
|
|
|
|
initializationValues.attributes[name] = value
|
|
|
|
if err != nil { return }
|
2022-08-17 10:53:35 -06:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// parse as normal argument
|
|
|
|
value, err = parser.parseArgument()
|
2022-08-17 11:26:18 -06:00
|
|
|
initializationValues.attributes[name] = value
|
2022-08-17 10:53:35 -06:00
|
|
|
if err != nil { return }
|
2022-08-17 11:26:18 -06:00
|
|
|
|
2022-08-17 10:53:35 -06:00
|
|
|
err = parser.expect(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-16 22:14:55 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-16 19:31:23 -06:00
|
|
|
// parseArrayInitializationValues parses a list of array initialization values
|
2022-08-17 09:30:17 -06:00
|
|
|
// until the indentation lexel drops.
|
|
|
|
func (parser *ParsingOperation) parseArrayInitializationValues () (
|
2022-08-17 10:53:35 -06:00
|
|
|
initializationValues ArrayInitializationValues,
|
|
|
|
err error,
|
2022-08-16 19:31:23 -06:00
|
|
|
) {
|
2022-08-17 09:30:17 -06:00
|
|
|
baseIndent := 0
|
|
|
|
begin := true
|
2022-08-16 22:49:49 -06:00
|
|
|
|
2022-08-16 19:31:23 -06:00
|
|
|
for {
|
2022-08-17 09:30:17 -06:00
|
|
|
// if there is no indent we can just stop parsing
|
|
|
|
if !parser.token.Is(lexer.TokenKindIndent) { break}
|
|
|
|
indent := parser.token.Value().(int)
|
2022-08-16 22:14:55 -06:00
|
|
|
|
2022-08-17 09:30:17 -06:00
|
|
|
if begin == true {
|
2022-08-17 10:53:35 -06:00
|
|
|
initializationValues.location = parser.token.Location()
|
2022-08-17 09:30:17 -06:00
|
|
|
baseIndent = indent
|
|
|
|
begin = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not parse any further if the indent has changed
|
|
|
|
if indent != baseIndent { break }
|
|
|
|
|
|
|
|
// move on to the beginning of the line, which must contain
|
|
|
|
// arguments
|
|
|
|
err = parser.nextToken(validArgumentStartTokens...)
|
2022-08-16 19:31:23 -06:00
|
|
|
if err != nil { return }
|
2022-08-17 09:30:17 -06:00
|
|
|
|
|
|
|
for {
|
|
|
|
// stop parsing this line and go on to the next if a
|
|
|
|
// newline token is encountered
|
|
|
|
if parser.token.Is(lexer.TokenKindNewline) {
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, parse the argument
|
|
|
|
var argument Argument
|
|
|
|
argument, err = parser.parseArgument()
|
|
|
|
if err != nil { return }
|
2022-08-17 10:53:35 -06:00
|
|
|
initializationValues.values = append (
|
|
|
|
initializationValues.values,
|
|
|
|
argument)
|
2022-08-17 09:30:17 -06:00
|
|
|
}
|
2022-08-16 19:31:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-16 08:44:02 -06:00
|
|
|
// parseType parses a type notation of the form Name, {Name}, etc.
|
|
|
|
func (parser *ParsingOperation) parseType () (what Type, err error) {
|
2022-08-16 15:21:10 -06:00
|
|
|
err = parser.expect(lexer.TokenKindName, lexer.TokenKindLBrace)
|
2022-08-16 08:44:02 -06:00
|
|
|
if err != nil { return }
|
|
|
|
what.location = parser.token.Location()
|
|
|
|
|
|
|
|
if parser.token.Is(lexer.TokenKindLBrace) {
|
|
|
|
what.kind = TypeKindPointer
|
2022-08-16 15:21:10 -06:00
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-08-16 08:44:02 -06:00
|
|
|
|
|
|
|
var points Type
|
|
|
|
points, err = parser.parseType()
|
|
|
|
if err != nil { return }
|
|
|
|
what.points = &points
|
|
|
|
|
2022-08-16 15:21:10 -06:00
|
|
|
err = parser.expect (
|
2022-08-16 08:44:02 -06:00
|
|
|
lexer.TokenKindUInt,
|
2022-08-16 18:55:43 -06:00
|
|
|
lexer.TokenKindRBrace,
|
|
|
|
lexer.TokenKindElipsis)
|
2022-08-16 08:44:02 -06:00
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if parser.token.Is(lexer.TokenKindUInt) {
|
|
|
|
what.kind = TypeKindArray
|
|
|
|
|
|
|
|
what.length = parser.token.Value().(uint64)
|
|
|
|
|
2022-08-16 18:55:43 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindRBrace)
|
|
|
|
if err != nil { return }
|
|
|
|
} else if parser.token.Is(lexer.TokenKindElipsis) {
|
|
|
|
what.kind = TypeKindArray
|
|
|
|
|
2022-08-16 15:21:10 -06:00
|
|
|
err = parser.nextToken(lexer.TokenKindRBrace)
|
2022-08-16 08:44:02 -06:00
|
|
|
if err != nil { return }
|
|
|
|
}
|
2022-08-16 15:21:10 -06:00
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-08-16 08:44:02 -06:00
|
|
|
} else {
|
|
|
|
what.name, err = parser.parseIdentifier()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
|
|
|
|
|
|
|
if parser.token.Is(lexer.TokenKindColon) {
|
|
|
|
err = parser.nextToken(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
qualifier := parser.token.Value().(string)
|
|
|
|
switch qualifier {
|
|
|
|
case "mut":
|
|
|
|
what.mutable = true
|
|
|
|
default:
|
|
|
|
err = parser.token.NewError (
|
|
|
|
"unknown type qualifier \"" + qualifier + "\"",
|
2022-08-17 22:58:40 -06:00
|
|
|
infoerr.ErrorKindError)
|
2022-08-16 08:44:02 -06:00
|
|
|
return
|
|
|
|
}
|
2022-08-16 15:21:10 -06:00
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
2022-08-16 08:44:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseIdentifier parses an identifier made out of dot separated names.
|
|
|
|
func (parser *ParsingOperation) parseIdentifier () (
|
|
|
|
identifier Identifier,
|
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
err = parser.expect(lexer.TokenKindName)
|
|
|
|
if err != nil { return }
|
|
|
|
identifier.location = parser.token.Location()
|
|
|
|
|
|
|
|
for {
|
|
|
|
if !parser.token.Is(lexer.TokenKindName) { break }
|
|
|
|
|
|
|
|
identifier.trail = append (
|
|
|
|
identifier.trail,
|
|
|
|
parser.token.Value().(string))
|
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if !parser.token.Is(lexer.TokenKindDot) { break }
|
2022-08-24 21:54:06 -06:00
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
// allow the identifier to continue on to the next line if there
|
|
|
|
// is a line break right after the dot
|
|
|
|
for parser.token.Is(lexer.TokenKindNewline) ||
|
|
|
|
parser.token.Is(lexer.TokenKindIndent) {
|
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
2022-08-16 08:44:02 -06:00
|
|
|
}
|
|
|
|
|
2022-08-15 12:04:57 -06:00
|
|
|
return
|
2022-08-14 20:38:57 -06:00
|
|
|
}
|