Parser can now parse array and object initializations

This commit is contained in:
Sasha Koshka 2022-08-19 02:34:17 -04:00
parent 63419165dd
commit e25e7bdf14

View File

@ -22,6 +22,9 @@ func (parser *ParsingOperation) parseTypeSection () (
return
}
// parseTypeNode parses a single type definition node recursively. If isRoot is
// true, the parser will assume the current node is the root node of a type
// section and will not search for an indent.
func (parser *ParsingOperation) parseTypeNode (
isRoot bool,
) (
@ -64,8 +67,8 @@ func (parser *ParsingOperation) parseTypeNode (
err = parser.nextToken()
if err != nil { return }
// section.value, err = parser.parseInitializationValues(0)
// if err != nil { return }
err = parser.parseTypeNodeBlock(baseIndent, &node)
if err != nil { return }
} else {
node.defaultValue, err = parser.parseArgument()
if err != nil { return }
@ -77,3 +80,49 @@ func (parser *ParsingOperation) parseTypeNode (
}
return
}
// parseTypeNodeBlock starts on the line after a type node, and parses what
// could be either an array initialization, an object initialization, or more
// child nodes. It is similar to parseInitializationValues. If none of these
// things were found the parser stays at the beginning of the line and the
// method returns.
func (parser *ParsingOperation) parseTypeNodeBlock (
baseIndent int,
parent *TypeNode,
) (
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 }
thingLocation := parser.token.Location()
err = parser.nextToken()
if err != nil { return }
if parser.token.Is(lexer.TokenKindDot) {
// object initialization
parser.previousToken()
initializationArgument := Argument { location: thingLocation }
var initializationValues ObjectInitializationValues
initializationValues, err = parser.parseObjectInitializationValues()
initializationArgument.kind = ArgumentKindObjectInitializationValues
initializationArgument.value = &initializationValues
parent.defaultValue = initializationArgument
} else {
// array initialization
parser.previousToken()
initializationArgument := Argument { location: thingLocation }
var initializationValues ArrayInitializationValues
initializationValues, err = parser.parseArrayInitializationValues()
initializationArgument.kind = ArgumentKindArrayInitializationValues
initializationArgument.value = &initializationValues
parent.defaultValue = initializationArgument
}
return
}