2022-09-03 18:16:49 -06:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import "git.tebibyte.media/arf/arf/lexer"
|
|
|
|
import "git.tebibyte.media/arf/arf/infoerr"
|
|
|
|
|
2022-09-12 13:27:29 -06:00
|
|
|
// TODO:
|
|
|
|
// (parser *ParsingOperation) parseDefaultValues
|
|
|
|
|
|
|
|
// (parser *ParsingOperation) parseDefaultMemberValues (return tree of new members and a tree of member values)
|
|
|
|
// (parser *ParsingOperation) parseDefaultArrayValues
|
|
|
|
|
|
|
|
// (parser *ParsingOperation) parseDefaultMemberValue
|
|
|
|
// (parser *ParsingOperation) parseMemberDeclaration
|
|
|
|
|
2022-09-13 09:09:41 -06:00
|
|
|
// parsedefaultValues starts on the line after a data section, or a set
|
2022-09-03 18:16:49 -06:00
|
|
|
// 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.
|
2022-09-13 09:12:38 -06:00
|
|
|
func (parser *ParsingOperation) parseDefaultValues (
|
2022-09-03 18:16:49 -06:00
|
|
|
baseIndent int,
|
|
|
|
) (
|
2022-09-13 09:09:41 -06:00
|
|
|
argument Argument,
|
2022-09-03 18:16:49 -06:00
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
// 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-09-13 09:09:41 -06:00
|
|
|
argument.location = parser.token.Location()
|
2022-09-03 18:16:49 -06:00
|
|
|
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
if parser.token.Is(lexer.TokenKindDot) {
|
|
|
|
|
|
|
|
// object initialization
|
|
|
|
parser.previousToken()
|
2022-09-13 09:09:41 -06:00
|
|
|
var values ObjectDefaultValues
|
|
|
|
values, err = parser.parseObjectdefaultValues()
|
|
|
|
argument.kind = ArgumentKindObjectDefaultValues
|
|
|
|
argument.value = values
|
2022-09-03 18:16:49 -06:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// array initialization
|
|
|
|
parser.previousToken()
|
2022-09-13 09:09:41 -06:00
|
|
|
var values ArrayDefaultValues
|
|
|
|
values, err = parser.parseArrayDefaultValues()
|
|
|
|
argument.kind = ArgumentKindArrayDefaultValues
|
|
|
|
argument.value = values
|
2022-09-03 18:16:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:09:41 -06:00
|
|
|
// parseObjectdefaultValues parses a list of object initialization
|
2022-09-03 18:16:49 -06:00
|
|
|
// values until the indentation level drops.
|
2022-09-13 09:09:41 -06:00
|
|
|
func (parser *ParsingOperation) parseObjectdefaultValues () (
|
|
|
|
defaultValues ObjectDefaultValues,
|
2022-09-03 18:16:49 -06:00
|
|
|
err error,
|
|
|
|
) {
|
2022-09-13 09:09:41 -06:00
|
|
|
defaultValues.attributes = make(map[string] Argument)
|
2022-09-03 18:16:49 -06:00
|
|
|
|
|
|
|
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 {
|
2022-09-13 09:09:41 -06:00
|
|
|
defaultValues.location = parser.token.Location()
|
2022-09-03 18:16:49 -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
|
|
|
|
// 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
|
2022-09-13 09:09:41 -06:00
|
|
|
_, exists := defaultValues.attributes[name]
|
2022-09-03 18:16:49 -06:00
|
|
|
if exists {
|
|
|
|
err = parser.token.NewError (
|
|
|
|
"duplicate member \"" + name + "\" in object " +
|
|
|
|
"member initialization",
|
|
|
|
infoerr.ErrorKindError)
|
|
|
|
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) {
|
|
|
|
|
|
|
|
// recurse
|
|
|
|
err = parser.nextToken(lexer.TokenKindIndent)
|
|
|
|
if err != nil { return }
|
|
|
|
|
2022-09-13 09:12:38 -06:00
|
|
|
value, err = parser.parseDefaultValues(baseIndent)
|
2022-09-13 09:09:41 -06:00
|
|
|
defaultValues.attributes[name] = value
|
2022-09-03 18:16:49 -06:00
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// parse as normal argument
|
|
|
|
value, err = parser.parseArgument()
|
2022-09-13 09:09:41 -06:00
|
|
|
defaultValues.attributes[name] = value
|
2022-09-03 18:16:49 -06:00
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
err = parser.expect(lexer.TokenKindNewline)
|
|
|
|
if err != nil { return }
|
|
|
|
err = parser.nextToken()
|
|
|
|
if err != nil { return }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:09:41 -06:00
|
|
|
// parseArrayDefaultValues parses a list of array initialization values until
|
|
|
|
// the indentation lexel drops.
|
|
|
|
func (parser *ParsingOperation) parseArrayDefaultValues () (
|
|
|
|
defaultValues ArrayDefaultValues,
|
2022-09-03 18:16:49 -06:00
|
|
|
err error,
|
|
|
|
) {
|
|
|
|
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 {
|
2022-09-13 09:09:41 -06:00
|
|
|
defaultValues.location = parser.token.Location()
|
2022-09-03 18:16:49 -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...)
|
|
|
|
if err != nil { return }
|
|
|
|
|
|
|
|
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-09-13 09:09:41 -06:00
|
|
|
defaultValues.values = append (
|
|
|
|
defaultValues.values,
|
2022-09-03 18:16:49 -06:00
|
|
|
argument)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|