From 729ae78eaef65581585481972ced1d856f47d2e0 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sat, 3 Sep 2022 22:17:05 -0400 Subject: [PATCH] Location is now included in structs via composition --- parser/argument.go | 7 +++--- parser/data.go | 3 ++- parser/enum.go | 3 ++- parser/face.go | 2 +- parser/func.go | 3 ++- parser/node-traits.go | 30 +++++++++++++++++++++++ parser/objt.go | 3 ++- parser/tree.go | 56 +++++++++++++++++++++---------------------- parser/type.go | 3 ++- 9 files changed, 72 insertions(+), 38 deletions(-) create mode 100644 parser/node-traits.go diff --git a/parser/argument.go b/parser/argument.go index 5dd87e3..eae5085 100644 --- a/parser/argument.go +++ b/parser/argument.go @@ -16,7 +16,7 @@ var validArgumentStartTokens = []lexer.TokenKind { } func (parser *ParsingOperation) parseArgument () (argument Argument, err error) { - argument.location = parser.token.Location() + argument.setLocation(parser.token.Location()) err = parser.expect(validArgumentStartTokens...) if err != nil { return } @@ -44,9 +44,8 @@ func (parser *ParsingOperation) parseArgument () (argument Argument, err error) argument.kind = ArgumentKindDeclaration argument.value = Declaration { - location: argument.location, - name: identifier.trail[0], - what: what, + name: identifier.trail[0], + what: what, } } else { argument.kind = ArgumentKindIdentifier diff --git a/parser/data.go b/parser/data.go index eb828af..e4ab680 100644 --- a/parser/data.go +++ b/parser/data.go @@ -11,7 +11,8 @@ func (parser *ParsingOperation) parseDataSection () ( err = parser.expect(lexer.TokenKindName) if err != nil { return } - section = &DataSection { location: parser.token.Location() } + section = &DataSection { } + section.setLocation(parser.token.Location()) err = parser.nextToken(lexer.TokenKindPermission) if err != nil { return } diff --git a/parser/enum.go b/parser/enum.go index 23d318e..5c4463c 100644 --- a/parser/enum.go +++ b/parser/enum.go @@ -11,7 +11,8 @@ func (parser *ParsingOperation) parseEnumSection () ( err = parser.expect(lexer.TokenKindName) if err != nil { return } - section = &EnumSection { location: parser.token.Location() } + section = &EnumSection { } + section.setLocation(parser.token.Location()) // get permission err = parser.nextToken(lexer.TokenKindPermission) diff --git a/parser/face.go b/parser/face.go index 14460cb..7cf715b 100644 --- a/parser/face.go +++ b/parser/face.go @@ -13,9 +13,9 @@ func (parser *ParsingOperation) parseFaceSection () ( if err != nil { return } section = &FaceSection { - location: parser.token.Location(), behaviors: make(map[string] FaceBehavior), } + section.setLocation(parser.token.Location()) // get permission err = parser.nextToken(lexer.TokenKindPermission) diff --git a/parser/func.go b/parser/func.go index 1ee5302..5477a16 100644 --- a/parser/func.go +++ b/parser/func.go @@ -12,7 +12,8 @@ func (parser *ParsingOperation) parseFuncSection () ( err = parser.expect(lexer.TokenKindName) if err != nil { return } - section = &FuncSection { location: parser.token.Location() } + section = &FuncSection { } + section.setLocation(parser.token.Location()) // get permission err = parser.nextToken(lexer.TokenKindPermission) diff --git a/parser/node-traits.go b/parser/node-traits.go new file mode 100644 index 0000000..dc81506 --- /dev/null +++ b/parser/node-traits.go @@ -0,0 +1,30 @@ +package parser + +import "git.tebibyte.media/arf/arf/file" +import "git.tebibyte.media/arf/arf/infoerr" + +// locatable allows a tree node to have a location. +type locatable struct { + location file.Location +} + +// Location returns the location of the node. +func (trait locatable) Location (location file.Location) { + location = trait.location + return +} + +// setLocation sets the location of the node. +func (trait locatable) setLocation (location file.Location) { + trait.location = location +} + +// NewError creates a new error at the node's location. +func (trait locatable) NewError ( + message string, + kind infoerr.ErrorKind, +) ( + err infoerr.Error, +) { + return infoerr.NewError(trait.location, message, kind) +} diff --git a/parser/objt.go b/parser/objt.go index 3c99acb..9603959 100644 --- a/parser/objt.go +++ b/parser/objt.go @@ -13,7 +13,8 @@ func (parser *ParsingOperation) parseObjtSection () ( err = parser.expect(lexer.TokenKindName) if err != nil { return } - section = &ObjtSection { location: parser.token.Location() } + section = &ObjtSection { } + section.setLocation(parser.token.Location()) // get permission err = parser.nextToken(lexer.TokenKindPermission) diff --git a/parser/tree.go b/parser/tree.go index 257960a..64945b8 100644 --- a/parser/tree.go +++ b/parser/tree.go @@ -21,8 +21,8 @@ type SyntaxTree struct { // Identifier represents a chain of arguments separated by a dot. type Identifier struct { - location file.Location - trail []string + locatable + trail []string } // TypeKind represents what kind of type a type is @@ -42,7 +42,7 @@ const ( // Type represents a type specifier type Type struct { - location file.Location + locatable mutable bool kind TypeKind @@ -60,23 +60,23 @@ type Type struct { // Declaration represents a variable declaration. type Declaration struct { - location file.Location - name string - what Type + locatable + name string + what Type } // ObjectInitializationValues represents a list of object member initialization // attributes. type ObjectInitializationValues struct { - location file.Location + locatable attributes map[string] Argument } // ArrayInitializationValues represents a list of attributes initializing an // array. type ArrayInitializationValues struct { - location file.Location - values []Argument + locatable + values []Argument } // ArgumentKind specifies the type of thing the value of an argument should be @@ -139,17 +139,17 @@ const ( // Argument represents a value that can be placed anywhere a value goes. This // allows things like phrases being arguments to other phrases. type Argument struct { - location file.Location - kind ArgumentKind - value any + locatable + kind ArgumentKind + value any // TODO: if there is an argument expansion operator its existence should // be stored here in a boolean. } // DataSection represents a global variable. type DataSection struct { - location file.Location - name string + locatable + name string what Type permission types.Permission @@ -158,8 +158,8 @@ type DataSection struct { // TypeSection represents a blind type definition. type TypeSection struct { - location file.Location - name string + locatable + name string inherits Type permission types.Permission @@ -168,8 +168,8 @@ type TypeSection struct { // ObjtMember represents a part of an object type definition. type ObjtMember struct { - location file.Location - name string + locatable + name string what Type bitWidth uint64 @@ -179,8 +179,8 @@ type ObjtMember struct { // ObjtSection represents an object type definition. type ObjtSection struct { - location file.Location - name string + locatable + name string inherits Identifier permission types.Permission @@ -189,15 +189,15 @@ type ObjtSection struct { // EnumMember represents a member of an enum section. type EnumMember struct { - location file.Location - name string - value Argument + locatable + name string + value Argument } // EnumSection represents an enumerated type section. type EnumSection struct { - location file.Location - name string + locatable + name string what Type permission types.Permission @@ -206,7 +206,7 @@ type EnumSection struct { // FaceBehavior represents a behavior of an interface section. type FaceBehavior struct { - location file.Location + locatable name string inputs []Declaration @@ -215,7 +215,7 @@ type FaceBehavior struct { // FaceSection represents an interface type section. type FaceSection struct { - location file.Location + locatable name string inherits Identifier @@ -268,7 +268,7 @@ type FuncOutput struct { // FuncSection represents a function section. type FuncSection struct { - location file.Location + locatable name string permission types.Permission diff --git a/parser/type.go b/parser/type.go index 04bc9fd..cc4e60f 100644 --- a/parser/type.go +++ b/parser/type.go @@ -13,7 +13,8 @@ func (parser *ParsingOperation) parseTypeSection () ( err = parser.expect(lexer.TokenKindName) if err != nil { return } - section = &TypeSection { location: parser.token.Location() } + section = &TypeSection { } + section.setLocation(parser.token.Location()) // get permission err = parser.nextToken(lexer.TokenKindPermission)