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) { func (parser *ParsingOperation) parseArgument () (argument Argument, err error) {
argument.location = parser.token.Location() argument.setLocation(parser.token.Location())
err = parser.expect(validArgumentStartTokens...) err = parser.expect(validArgumentStartTokens...)
if err != nil { return } if err != nil { return }
@ -44,9 +44,8 @@ func (parser *ParsingOperation) parseArgument () (argument Argument, err error)
argument.kind = ArgumentKindDeclaration argument.kind = ArgumentKindDeclaration
argument.value = Declaration { argument.value = Declaration {
location: argument.location, name: identifier.trail[0],
name: identifier.trail[0], what: what,
what: what,
} }
} else { } else {
argument.kind = ArgumentKindIdentifier argument.kind = ArgumentKindIdentifier

View File

@ -11,7 +11,8 @@ func (parser *ParsingOperation) parseDataSection () (
err = parser.expect(lexer.TokenKindName) err = parser.expect(lexer.TokenKindName)
if err != nil { return } if err != nil { return }
section = &DataSection { location: parser.token.Location() } section = &DataSection { }
section.setLocation(parser.token.Location())
err = parser.nextToken(lexer.TokenKindPermission) err = parser.nextToken(lexer.TokenKindPermission)
if err != nil { return } if err != nil { return }

View File

@ -11,7 +11,8 @@ func (parser *ParsingOperation) parseEnumSection () (
err = parser.expect(lexer.TokenKindName) err = parser.expect(lexer.TokenKindName)
if err != nil { return } if err != nil { return }
section = &EnumSection { location: parser.token.Location() } section = &EnumSection { }
section.setLocation(parser.token.Location())
// get permission // get permission
err = parser.nextToken(lexer.TokenKindPermission) err = parser.nextToken(lexer.TokenKindPermission)

View File

@ -13,9 +13,9 @@ func (parser *ParsingOperation) parseFaceSection () (
if err != nil { return } if err != nil { return }
section = &FaceSection { section = &FaceSection {
location: parser.token.Location(),
behaviors: make(map[string] FaceBehavior), behaviors: make(map[string] FaceBehavior),
} }
section.setLocation(parser.token.Location())
// get permission // get permission
err = parser.nextToken(lexer.TokenKindPermission) err = parser.nextToken(lexer.TokenKindPermission)

View File

@ -12,7 +12,8 @@ func (parser *ParsingOperation) parseFuncSection () (
err = parser.expect(lexer.TokenKindName) err = parser.expect(lexer.TokenKindName)
if err != nil { return } if err != nil { return }
section = &FuncSection { location: parser.token.Location() } section = &FuncSection { }
section.setLocation(parser.token.Location())
// get permission // get permission
err = parser.nextToken(lexer.TokenKindPermission) 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) err = parser.expect(lexer.TokenKindName)
if err != nil { return } if err != nil { return }
section = &ObjtSection { location: parser.token.Location() } section = &ObjtSection { }
section.setLocation(parser.token.Location())
// get permission // get permission
err = parser.nextToken(lexer.TokenKindPermission) err = parser.nextToken(lexer.TokenKindPermission)

View File

@ -21,8 +21,8 @@ type SyntaxTree struct {
// Identifier represents a chain of arguments separated by a dot. // Identifier represents a chain of arguments separated by a dot.
type Identifier struct { type Identifier struct {
location file.Location locatable
trail []string trail []string
} }
// TypeKind represents what kind of type a type is // TypeKind represents what kind of type a type is