Location is now included in structs via composition

This commit is contained in:
Sasha Koshka 2022-09-03 22:17:05 -04:00
parent 4c01d41723
commit 729ae78eae
9 changed files with 72 additions and 38 deletions

View File

@ -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

View File

@ -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 }

View File

@ -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)

View File

@ -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)

View File

@ -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)

30
parser/node-traits.go Normal file
View File

@ -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)
}

View File

@ -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)

View File

@ -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

View File

@ -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)