Skip to content

Commit

Permalink
lang: parser: Better semantics for setting positions
Browse files Browse the repository at this point in the history
  • Loading branch information
ffrank committed Mar 24, 2024
1 parent c9be853 commit 913c100
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 87 deletions.
25 changes: 14 additions & 11 deletions lang/ast/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ type TextArea struct {
}

// Locate is used by the parser to store the token positions in AST nodes
func (a TextArea) Locate(line int, col int, endline int, endcol int) {
func (a *TextArea) Locate(line int, col int, endline int, endcol int) {
a.startLine = line
a.startColumn = col
a.endLine = endline
Expand All @@ -200,6 +200,8 @@ func (a TextArea) Locate(line int, col int, endline int, endcol int) {
// position. It is implemented by node types that embed TextArea.
type LocalNode interface {
Locate(int, int, int, int)
GetPosition() (int, int)
GetEndPosition() (int, int)
}

// GetPosition returns the starting line/column of an AST node
Expand All @@ -222,7 +224,7 @@ type StmtBind struct {

// String returns a short representation of this statement.
func (obj *StmtBind) String() string {
return fmt.Sprintf("bind(%s)", obj.Ident)
return fmt.Sprintf("bind(%s) @ (%d %d)", obj.Ident, obj.startLine+1, obj.startColumn+1)
}

// Apply is a general purpose iterator method that operates on any AST node. It
Expand Down Expand Up @@ -255,10 +257,9 @@ func (obj *StmtBind) Interpolate() (interfaces.Stmt, error) {
if err != nil {
return nil, err
}
return &StmtBind{
Ident: obj.Ident,
Value: interpolated,
}, nil
result := *obj
result.Value = interpolated
return &result, nil
}

// Copy returns a light copy of this struct. Anything static will not be copied.
Expand Down Expand Up @@ -2610,6 +2611,8 @@ func (obj *StmtIf) String() string {
s += fmt.Sprintf(" else { %s }", obj.ElseBranch.String())
}

s += fmt.Sprintf(" @ (%d %d)", obj.startLine+1, obj.startColumn+1)

return s
}

Expand Down Expand Up @@ -2676,11 +2679,11 @@ func (obj *StmtIf) Interpolate() (interfaces.Stmt, error) {
return nil, errwrap.Wrapf(err, "could not interpolate ElseBranch")
}
}
return &StmtIf{
Condition: condition,
ThenBranch: thenBranch,
ElseBranch: elseBranch,
}, nil
result := *obj
result.Condition = condition
result.ThenBranch = thenBranch
result.ElseBranch = elseBranch
return &result, nil
}

// Copy returns a light copy of this struct. Anything static will not be copied.
Expand Down
6 changes: 6 additions & 0 deletions lang/gapi/gapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ func (obj *GAPI) Cli(info *gapi.Info) (*gapi.Deploy, error) {
return nil, nil // success!
}

fmt.Println("The Interpolated Tree: %+v")
iast.Apply(func (n interfaces.Node) error {
fmt.Println(n)
return nil
})

if !args.SkipUnify {
// apply type unification
unificationLogf := func(format string, v ...interface{}) {
Expand Down
Loading

0 comments on commit 913c100

Please sign in to comment.