Skip to content

Commit

Permalink
parser: Use *ast.ID for identifers (#5136)
Browse files Browse the repository at this point in the history
The commit changes the AST representation for declaration identifiers
as well as parameters in user-defined ops/funcs and over expresssions.
Previously these were represented as strings but are now represented as
*ast.ID which has the advantage of having location information- meaning
errors having to do with these nodes can be set to them on not on the
parent.
  • Loading branch information
mattnibs authored Jun 9, 2024
1 parent 711bf9f commit 3fa8549
Show file tree
Hide file tree
Showing 11 changed files with 2,876 additions and 2,829 deletions.
52 changes: 25 additions & 27 deletions compiler/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ func (c *Conditional) End() int { return c.Else.End() }
// a function call has the standard semantics where it takes one or more arguments
// and returns a result.
type Call struct {
Kind string `json:"kind" unpack:""`
Name string `json:"name"`
NamePos int `json:"name_pos"`
Args []Expr `json:"args"`
Rparen int `json:"rparen"`
Where Expr `json:"where"`
Kind string `json:"kind" unpack:""`
Name *ID `json:"name"`
Args []Expr `json:"args"`
Rparen int `json:"rparen"`
Where Expr `json:"where"`
}

func (c *Call) Pos() int { return c.NamePos }
func (c *Call) Pos() int { return c.Name.Pos() }

func (c *Call) End() int {
if c.Where != nil {
Expand Down Expand Up @@ -185,7 +184,7 @@ func (r *Regexp) End() int { return r.PatternPos + len(r.Pattern) + 2 }
type String struct {
Kind string `json:"kind" unpack:""`
Text string `json:"text"`
TextPos int `json:"start_pos"`
TextPos int `json:"text_pos"`
}

func (s *String) Pos() int { return s.TextPos }
Expand Down Expand Up @@ -364,32 +363,32 @@ func (*FString) ExprAST() {}
type ConstDecl struct {
Kind string `json:"kind" unpack:""`
KeywordPos int `json:"keyword_pos"`
Name string `json:"name"`
Name *ID `json:"name"`
Expr Expr `json:"expr"`
}

func (c *ConstDecl) Pos() int { return c.KeywordPos }
func (c *ConstDecl) End() int { return c.Expr.End() }

type FuncDecl struct {
Kind string `json:"kind" unpack:""`
KeywordPos int `json:"keyword_pos"`
Name string `json:"name"`
Params []string `json:"params"`
Expr Expr `json:"expr"`
Rparen int `json:"rparen"`
Kind string `json:"kind" unpack:""`
KeywordPos int `json:"keyword_pos"`
Name *ID `json:"name"`
Params []*ID `json:"params"`
Expr Expr `json:"expr"`
Rparen int `json:"rparen"`
}

func (f *FuncDecl) Pos() int { return f.KeywordPos }
func (f *FuncDecl) End() int { return f.Rparen }

type OpDecl struct {
Kind string `json:"kind" unpack:""`
KeywordPos int `json:"keyword_pos"`
Name string `json:"name"`
Params []string `json:"params"`
Body Seq `json:"body"`
Rparen int `json:"rparen"`
Kind string `json:"kind" unpack:""`
KeywordPos int `json:"keyword_pos"`
Name *ID `json:"name"`
Params []*ID `json:"params"`
Body Seq `json:"body"`
Rparen int `json:"rparen"`
}

func (o *OpDecl) Pos() int { return o.KeywordPos }
Expand All @@ -398,7 +397,7 @@ func (o *OpDecl) End() int { return o.Rparen }
type TypeDecl struct {
Kind string `json:"kind" unpack:""`
KeywordPos int `json:"keyword_pos"`
Name string `json:"name"`
Name *ID `json:"name"`
Type astzed.Type `json:"type"`
}

Expand Down Expand Up @@ -731,18 +730,17 @@ func (a Assignments) End() int { return a[len(a)-1].End() }
// referenced. This is used for const blocks in Sequential and var blocks
// in a let scope.
type Def struct {
Name string `json:"name"`
NamePos int `json:"name_pos"`
Expr Expr `json:"expr"`
Name *ID `json:"name"`
Expr Expr `json:"expr"`
}

func (d Def) Pos() int { return d.NamePos }
func (d Def) Pos() int { return d.Name.Pos() }

func (d Def) End() int {
if d.Expr != nil {
d.Expr.End()
}
return d.NamePos + len(d.Name)
return d.Name.End()
}

func (*Scope) OpAST() {}
Expand Down
Loading

0 comments on commit 3fa8549

Please sign in to comment.