Skip to content

Commit

Permalink
internal/astinternal: more compact representation of relative positions
Browse files Browse the repository at this point in the history
The `.WithRel` suffix reads a little verbose, so use a slightly more
compact format that should still be reasonably intuitive to read.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I84d8a48b5b3d600e34916012cad096d0fb031424
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200226
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
rogpeppe committed Sep 3, 2024
1 parent 33fe828 commit 6f4983e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 154 deletions.
88 changes: 44 additions & 44 deletions encoding/toml/testdata/decode/example.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -41,46 +41,46 @@ color = "gray"
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:4:1").WithRel("newline")
NamePos: token.Pos("example.toml:4:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:4:9")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:6:2").WithRel("newline")
NamePos: token.Pos("example.toml:6:2", newline)
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Lbrace: token.Pos("-", blank)
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:7:1").WithRel("newline")
NamePos: token.Pos("example.toml:7:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:7:8")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
Rbrace: token.Pos("-", newline)
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:11:2").WithRel("newline")
NamePos: token.Pos("example.toml:11:2", newline)
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Lbrace: token.Pos("-", blank)
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:12:1").WithRel("newline")
NamePos: token.Pos("example.toml:12:1", newline)
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:13:1").WithRel("newline")
NamePos: token.Pos("example.toml:13:1", newline)
}
Value: *ast.ListLit{
Elts: []ast.Expr{
Expand All @@ -98,7 +98,7 @@ color = "gray"
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:14:1").WithRel("newline")
NamePos: token.Pos("example.toml:14:1", newline)
}
Value: *ast.ListLit{
Elts: []ast.Expr{
Expand All @@ -124,181 +124,181 @@ color = "gray"
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:15:1").WithRel("newline")
NamePos: token.Pos("example.toml:15:1", newline)
}
Value: *ast.StructLit{
Lbrace: token.Pos("example.toml:15:16")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:15:18").WithRel("blank")
NamePos: token.Pos("example.toml:15:18", blank)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:15:24")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:15:30").WithRel("blank")
NamePos: token.Pos("example.toml:15:30", blank)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:15:37")
}
}
}
Rbrace: token.Pos("-").WithRel("blank")
Rbrace: token.Pos("-", blank)
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
Rbrace: token.Pos("-", newline)
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:17:2").WithRel("newline")
NamePos: token.Pos("example.toml:17:2", newline)
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Rbrace: token.Pos("-").WithRel("newline")
Lbrace: token.Pos("-", blank)
Rbrace: token.Pos("-", newline)
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:19:2").WithRel("newline")
NamePos: token.Pos("example.toml:19:2", newline)
}
Value: *ast.StructLit{
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:19:10").WithRel("blank")
NamePos: token.Pos("example.toml:19:10", blank)
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Lbrace: token.Pos("-", blank)
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:20:1").WithRel("newline")
NamePos: token.Pos("example.toml:20:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:20:6")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:21:1").WithRel("newline")
NamePos: token.Pos("example.toml:21:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:21:8")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
Rbrace: token.Pos("-", newline)
}
}
}
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:23:2").WithRel("newline")
NamePos: token.Pos("example.toml:23:2", newline)
}
Value: *ast.StructLit{
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:23:10").WithRel("blank")
NamePos: token.Pos("example.toml:23:10", blank)
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Lbrace: token.Pos("-", blank)
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:24:1").WithRel("newline")
NamePos: token.Pos("example.toml:24:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:24:6")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:25:1").WithRel("newline")
NamePos: token.Pos("example.toml:25:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:25:8")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
Rbrace: token.Pos("-", newline)
}
}
}
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:27:3").WithRel("newline")
NamePos: token.Pos("example.toml:27:3", newline)
}
Value: *ast.ListLit{
Lbrack: token.Pos("-").WithRel("blank")
Lbrack: token.Pos("-", blank)
Elts: []ast.Expr{
*ast.StructLit{
Lbrace: token.Pos("-").WithRel("newline")
Lbrace: token.Pos("-", newline)
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:28:1").WithRel("newline")
NamePos: token.Pos("example.toml:28:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:28:8")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:29:1").WithRel("newline")
NamePos: token.Pos("example.toml:29:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:29:7")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
Rbrace: token.Pos("-", newline)
}
*ast.StructLit{
Lbrace: token.Pos("-").WithRel("newline")
Rbrace: token.Pos("-").WithRel("newline")
Lbrace: token.Pos("-", newline)
Rbrace: token.Pos("-", newline)
}
*ast.StructLit{
Lbrace: token.Pos("-").WithRel("newline")
Lbrace: token.Pos("-", newline)
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:34:1").WithRel("newline")
NamePos: token.Pos("example.toml:34:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:34:8")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:35:1").WithRel("newline")
NamePos: token.Pos("example.toml:35:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:35:7")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("example.toml:37:1").WithRel("newline")
NamePos: token.Pos("example.toml:37:1", newline)
}
Value: *ast.BasicLit{
ValuePos: token.Pos("example.toml:37:9")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
Rbrace: token.Pos("-", newline)
}
}
Rbrack: token.Pos("-").WithRel("newline")
Rbrack: token.Pos("-", newline)
}
}
}
Expand Down
22 changes: 9 additions & 13 deletions internal/astinternal/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ type debugPrinter struct {
level int
}

var (
typeTokenPos = reflect.TypeFor[token.Pos]()
typeTokenToken = reflect.TypeFor[token.Token]()
)

// value produces the given value, omitting type information if
// its type is the same as implied type. It reports whether
// anything was produced.
Expand Down Expand Up @@ -99,17 +94,18 @@ func (d *debugPrinter) value0(v reflect.Value, impliedType reflect.Type) {
}

t := v.Type()
switch t {
switch v := v.Interface().(type) {
// Simple types which can stringify themselves.
case typeTokenPos, typeTokenToken:
d.printf("%s(%q)", t, v)
case token.Pos:
d.printf("%s(%q", t, v)
// Show relative positions too, if there are any, as they affect formatting.
if t == typeTokenPos {
pos := v.Interface().(token.Pos)
if pos.HasRelPos() {
d.printf(".WithRel(%q)", pos.RelPos())
}
if v.HasRelPos() {
d.printf(", %v", v.RelPos())
}
d.printf(")")
return
case token.Token:
d.printf("%s(%q)", t, v)
return
}

Expand Down
Loading

0 comments on commit 6f4983e

Please sign in to comment.