From 0dd93683938d3e8cea47f60b4e6257ed1a409e0a Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 17 Aug 2022 00:49:49 -0400 Subject: [PATCH] Reworked data section so it stores single initialization argument --- parser/data.go | 44 +++++++++++++++++------------------- parser/tree-tostring.go | 50 +++++++++++++++++++++++++++-------------- parser/tree.go | 18 +++++++++++---- 3 files changed, 68 insertions(+), 44 deletions(-) diff --git a/parser/data.go b/parser/data.go index c404916..995e673 100644 --- a/parser/data.go +++ b/parser/data.go @@ -36,9 +36,8 @@ func (parser *ParsingOperation) parseDataSection () ( section.value, err = parser.parseInitializationValues(0) if err != nil { return } } else { - var argument Argument - argument, err = parser.parseArgument() - section.value = append(section.value, argument) + section.value, err = parser.parseArgument() + if err != nil { return } err = parser.expect(lexer.TokenKindNewline) if err != nil { return } @@ -55,8 +54,8 @@ func (parser *ParsingOperation) parseDataSection () ( func (parser *ParsingOperation) parseInitializationValues ( baseIndent int, ) ( - values []Argument, - err error, + value Argument, + err error, ) { // check if line is indented one more than baseIndent if !parser.token.Is(lexer.TokenKindIndent) { return } @@ -65,23 +64,20 @@ func (parser *ParsingOperation) parseInitializationValues ( err = parser.nextToken() if err != nil { return } - // TODO: make data sections have only one value argument and create a - // similar data structure to ObjectAttributes for arrays - if parser.token.Is(lexer.TokenKindDot) { - var attributes ObjectAttributes - attributes, err = parser.parseObjectInitializationValues ( - baseIndent + 1) + value.location = parser.token.Location() - values = []Argument { - Argument { - location: attributes.location, - value: &attributes, - kind: ArgumentKindObjectAttributes, - }, - } - } else { - values, err = parser.parseArrayInitializationValues ( + if parser.token.Is(lexer.TokenKindDot) { + var values ObjectInitializationValues + values, err = parser.parseObjectInitializationValues ( baseIndent + 1) + value.kind = ArgumentKindObjectInitializationValues + value.value = &values + } else { + var values ArrayInitializationValues + value.value, err = parser.parseArrayInitializationValues ( + baseIndent + 1) + value.kind = ArgumentKindArrayInitializationValues + value.value = &values } return @@ -92,7 +88,7 @@ func (parser *ParsingOperation) parseInitializationValues ( func (parser *ParsingOperation) parseObjectInitializationValues ( indent int, ) ( - values ObjectAttributes, + values ObjectInitializationValues, err error, ) { values.attributes = make(map[string] Argument) @@ -142,9 +138,11 @@ func (parser *ParsingOperation) parseObjectInitializationValues ( func (parser *ParsingOperation) parseArrayInitializationValues ( indent int, ) ( - values []Argument, + values ArrayInitializationValues, err error, ) { + values.location = parser.token.Location() + for { if parser.token.Is(lexer.TokenKindNewline) { err = parser.nextToken() @@ -159,7 +157,7 @@ func (parser *ParsingOperation) parseArrayInitializationValues ( var argument Argument argument, err = parser.parseArgument() if err != nil { return } - values = append(values, argument) + values.values = append(values.values, argument) } return diff --git a/parser/tree-tostring.go b/parser/tree-tostring.go index f579550..6970df7 100644 --- a/parser/tree-tostring.go +++ b/parser/tree-tostring.go @@ -78,14 +78,14 @@ func (declaration *Declaration) ToString () (output string) { return } -func (attributes *ObjectAttributes) ToString ( - indent int, +func (attributes *ObjectInitializationValues) ToString ( + indent int, ) ( output string, ) { for name, value := range attributes.attributes { output += doIndent(indent, ".", name, " ") - if value.kind == ArgumentKindObjectAttributes { + if value.kind == ArgumentKindObjectInitializationValues { output += "\n" output += value.ToString(indent + 1, true) } else { @@ -96,6 +96,18 @@ func (attributes *ObjectAttributes) ToString ( return } +func (values *ArrayInitializationValues) ToString ( + indent int, +) ( + output string, +) { + for _, value := range values.values { + output += value.ToString(indent, true) + } + + return +} + func (phrase *Phrase) ToString (indent int, breakLine bool) (output string) { if breakLine { output += doIndent ( @@ -139,17 +151,19 @@ func (argument *Argument) ToString (indent int, breakLine bool) (output string) switch argument.kind { case ArgumentKindPhrase: - output += doIndent ( - indent, - argument.value.(*Phrase).ToString ( + output += argument.value.(*Phrase).ToString ( indent, - breakLine)) + breakLine) - case ArgumentKindObjectAttributes: + case ArgumentKindObjectInitializationValues: // this should only appear in contexts where breakLine is true - output += doIndent ( - indent, - argument.value.(*ObjectAttributes).ToString (indent)) + output += argument.value.(*ObjectInitializationValues). + ToString (indent) + + case ArgumentKindArrayInitializationValues: + // this should only appear in contexts where breakLine is true + output += argument.value.(*ArrayInitializationValues). + ToString (indent) case ArgumentKindIdentifier: output += doIndent ( @@ -193,16 +207,18 @@ func (section *DataSection) ToString (indent int) (output string) { section.name, ":", section.what.ToString()) - if len(section.value) == 0 { + isComplexInitialization := + section.value.kind == ArgumentKindObjectInitializationValues || + section.value.kind == ArgumentKindArrayInitializationValues + + if section.value.value == nil { output += "\n" - } else if len(section.value) == 1 { - output += " " + section.value[0].ToString(0, false) + } else if isComplexInitialization { output += "\n" + output += section.value.ToString(indent + 1, true) } else { + output += " " + section.value.ToString(0, false) output += "\n" - for _, argument := range(section.value) { - output += argument.ToString(indent + 1, true) - } } return } diff --git a/parser/tree.go b/parser/tree.go index 76b7187..88ffabe 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -60,13 +60,20 @@ type Declaration struct { what Type } -// ObjectAttributes represents a list of object member initialization +// ObjectInitializationValues represents a list of object member initialization // attributes. -type ObjectAttributes struct { +type ObjectInitializationValues struct { location file.Location attributes map[string] Argument } +// ArrayInitializationValues represents a list of attributes initializing an +// array. +type ArrayInitializationValues struct { + location file.Location + values []Argument +} + // Phrase represents a function call or operator. In ARF they are the same // syntactical concept. type Phrase struct { @@ -94,7 +101,10 @@ const ( // .name value // but like, a lot of them - ArgumentKindObjectAttributes + ArgumentKindObjectInitializationValues + + // value value... + ArgumentKindArrayInitializationValues // name.name // name.name.name @@ -144,6 +154,6 @@ type DataSection struct { name string what Type - value []Argument + value Argument permission types.Permission }