Skip to content

Commit

Permalink
internal/astinternal: print relative positions as well
Browse files Browse the repository at this point in the history
Stringifying a token.Pos shows the filename, line, and column numbers,
but it omits a crucial piece of information: the relative position.

This can be important for encodings or formatting.
For instance, some decoders produce token.NoPos with relative positions
for the sake of ensuring good formatting, such as a node being printed
following a newline or the correct amount of whitespace.

We would print such missing positions with a relative position
as token.Pos("-") even with DebugConfig.OmitEmpty,
as the token.Pos value was not invalid nor empty.
We would now print, for example, token.Pos("-").WithRel("newline").

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I444ae9915cb96d2797775937f45e44599b7deb43
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1200205
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Matthew Sackman <[email protected]>
  • Loading branch information
mvdan committed Aug 29, 2024
1 parent ae3ad16 commit 6e5ec5a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 97 deletions.
7 changes: 7 additions & 0 deletions internal/astinternal/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ func (d *debugPrinter) value(dst []byte, v reflect.Value, impliedType reflect.Ty
// Simple types which can stringify themselves.
case typeTokenPos, typeTokenToken:
dst = d.printf(dst, "%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() {
dst = d.printf(dst, ".WithRel(%q)", pos.RelPos())
}
}
return dst
}

Expand Down
48 changes: 24 additions & 24 deletions internal/astinternal/testdata/debugprint/comprehensions.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,96 +12,96 @@ for k, v in input if v > 2 {
*ast.Comprehension{
Clauses: []ast.Clause{
*ast.IfClause{
If: token.Pos("comprehensions.cue:1:1")
If: token.Pos("comprehensions.cue:1:1").WithRel("nospace")
Condition: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:1:4")
NamePos: token.Pos("comprehensions.cue:1:4").WithRel("blank")
Name: "condition"
}
}
}
Value: *ast.StructLit{
Lbrace: token.Pos("comprehensions.cue:1:14")
Lbrace: token.Pos("comprehensions.cue:1:14").WithRel("blank")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:2:2")
NamePos: token.Pos("comprehensions.cue:2:2").WithRel("newline")
Name: "a"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("comprehensions.cue:2:3")
TokenPos: token.Pos("comprehensions.cue:2:3").WithRel("nospace")
Token: token.Token(":")
Value: *ast.BasicLit{
ValuePos: token.Pos("comprehensions.cue:2:5")
ValuePos: token.Pos("comprehensions.cue:2:5").WithRel("blank")
Kind: token.Token("true")
Value: "true"
}
Attrs: []*ast.Attribute{}
}
}
Rbrace: token.Pos("comprehensions.cue:3:1")
Rbrace: token.Pos("comprehensions.cue:3:1").WithRel("newline")
}
}
*ast.Comprehension{
Clauses: []ast.Clause{
*ast.ForClause{
For: token.Pos("comprehensions.cue:4:1")
For: token.Pos("comprehensions.cue:4:1").WithRel("newline")
Key: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:4:5")
NamePos: token.Pos("comprehensions.cue:4:5").WithRel("blank")
Name: "k"
}
Colon: token.Pos("comprehensions.cue:4:6")
Colon: token.Pos("comprehensions.cue:4:6").WithRel("nospace")
Value: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:4:8")
NamePos: token.Pos("comprehensions.cue:4:8").WithRel("blank")
Name: "v"
}
In: token.Pos("comprehensions.cue:4:10")
In: token.Pos("comprehensions.cue:4:10").WithRel("blank")
Source: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:4:13")
NamePos: token.Pos("comprehensions.cue:4:13").WithRel("blank")
Name: "input"
}
}
*ast.IfClause{
If: token.Pos("comprehensions.cue:4:19")
If: token.Pos("comprehensions.cue:4:19").WithRel("blank")
Condition: *ast.BinaryExpr{
X: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:4:22")
NamePos: token.Pos("comprehensions.cue:4:22").WithRel("blank")
Name: "v"
}
OpPos: token.Pos("comprehensions.cue:4:24")
OpPos: token.Pos("comprehensions.cue:4:24").WithRel("blank")
Op: token.Token(">")
Y: *ast.BasicLit{
ValuePos: token.Pos("comprehensions.cue:4:26")
ValuePos: token.Pos("comprehensions.cue:4:26").WithRel("blank")
Kind: token.Token("INT")
Value: "2"
}
}
}
}
Value: *ast.StructLit{
Lbrace: token.Pos("comprehensions.cue:4:28")
Lbrace: token.Pos("comprehensions.cue:4:28").WithRel("blank")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.ParenExpr{
Lparen: token.Pos("comprehensions.cue:5:2")
Lparen: token.Pos("comprehensions.cue:5:2").WithRel("newline")
X: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:5:3")
NamePos: token.Pos("comprehensions.cue:5:3").WithRel("nospace")
Name: "k"
}
Rparen: token.Pos("comprehensions.cue:5:4")
Rparen: token.Pos("comprehensions.cue:5:4").WithRel("nospace")
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("comprehensions.cue:5:5")
TokenPos: token.Pos("comprehensions.cue:5:5").WithRel("nospace")
Token: token.Token(":")
Value: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:5:7")
NamePos: token.Pos("comprehensions.cue:5:7").WithRel("blank")
Name: "v"
}
Attrs: []*ast.Attribute{}
}
}
Rbrace: token.Pos("comprehensions.cue:6:1")
Rbrace: token.Pos("comprehensions.cue:6:1").WithRel("newline")
}
}
}
Expand Down
80 changes: 40 additions & 40 deletions internal/astinternal/testdata/debugprint/fields.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,73 @@ embed: {
Decls: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:1:1")
NamePos: token.Pos("fields.cue:1:1").WithRel("nospace")
Name: "a"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:1:2")
TokenPos: token.Pos("fields.cue:1:2").WithRel("nospace")
Token: token.Token(":")
Value: *ast.BasicLit{
ValuePos: token.Pos("fields.cue:1:4")
ValuePos: token.Pos("fields.cue:1:4").WithRel("blank")
Kind: token.Token("INT")
Value: "1"
}
Attrs: []*ast.Attribute{
{
At: token.Pos("fields.cue:1:6")
At: token.Pos("fields.cue:1:6").WithRel("blank")
Text: "@xml(,attr)"
}
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:2:1")
NamePos: token.Pos("fields.cue:2:1").WithRel("newline")
Name: "b"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:2:2")
TokenPos: token.Pos("fields.cue:2:2").WithRel("nospace")
Token: token.Token(":")
Value: *ast.BasicLit{
ValuePos: token.Pos("fields.cue:2:4")
ValuePos: token.Pos("fields.cue:2:4").WithRel("blank")
Kind: token.Token("INT")
Value: "2"
}
Attrs: []*ast.Attribute{
{
At: token.Pos("fields.cue:2:6")
At: token.Pos("fields.cue:2:6").WithRel("blank")
Text: "@foo(a,b=4)"
}
{
At: token.Pos("fields.cue:2:18")
At: token.Pos("fields.cue:2:18").WithRel("blank")
Text: "@go(Foo)"
}
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:3:1")
NamePos: token.Pos("fields.cue:3:1").WithRel("newline")
Name: "c"
}
Optional: token.Pos("fields.cue:3:2")
Optional: token.Pos("fields.cue:3:2").WithRel("nospace")
Constraint: token.Token("!")
TokenPos: token.Pos("fields.cue:3:3")
TokenPos: token.Pos("fields.cue:3:3").WithRel("nospace")
Token: token.Token(":")
Value: *ast.StructLit{
Lbrace: token.Pos("-")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:3:5")
NamePos: token.Pos("fields.cue:3:5").WithRel("blank")
Name: "d"
}
Optional: token.Pos("fields.cue:3:6")
Optional: token.Pos("fields.cue:3:6").WithRel("nospace")
Constraint: token.Token("?")
TokenPos: token.Pos("fields.cue:3:7")
TokenPos: token.Pos("fields.cue:3:7").WithRel("nospace")
Token: token.Token(":")
Value: *ast.Ident{
NamePos: token.Pos("fields.cue:3:9")
NamePos: token.Pos("fields.cue:3:9").WithRel("blank")
Name: "string"
}
Attrs: []*ast.Attribute{}
Expand All @@ -95,46 +95,46 @@ embed: {
*ast.Field{
Label: *ast.Alias{
Ident: *ast.Ident{
NamePos: token.Pos("fields.cue:4:1")
NamePos: token.Pos("fields.cue:4:1").WithRel("newline")
Name: "X"
}
Equal: token.Pos("fields.cue:4:2")
Equal: token.Pos("fields.cue:4:2").WithRel("nospace")
Expr: *ast.Ident{
NamePos: token.Pos("fields.cue:4:3")
NamePos: token.Pos("fields.cue:4:3").WithRel("nospace")
Name: "e"
}
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:4:4")
TokenPos: token.Pos("fields.cue:4:4").WithRel("nospace")
Token: token.Token(":")
Value: *ast.StructLit{
Lbrace: token.Pos("-")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.ListLit{
Lbrack: token.Pos("fields.cue:4:6")
Lbrack: token.Pos("fields.cue:4:6").WithRel("blank")
Elts: []ast.Expr{
*ast.Alias{
Ident: *ast.Ident{
NamePos: token.Pos("fields.cue:4:7")
NamePos: token.Pos("fields.cue:4:7").WithRel("nospace")
Name: "Y"
}
Equal: token.Pos("fields.cue:4:8")
Equal: token.Pos("fields.cue:4:8").WithRel("nospace")
Expr: *ast.Ident{
NamePos: token.Pos("fields.cue:4:9")
NamePos: token.Pos("fields.cue:4:9").WithRel("nospace")
Name: "string"
}
}
}
Rbrack: token.Pos("fields.cue:4:15")
Rbrack: token.Pos("fields.cue:4:15").WithRel("nospace")
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:4:16")
TokenPos: token.Pos("fields.cue:4:16").WithRel("nospace")
Token: token.Token(":")
Value: *ast.Ident{
NamePos: token.Pos("fields.cue:4:18")
NamePos: token.Pos("fields.cue:4:18").WithRel("blank")
Name: "int"
}
Attrs: []*ast.Attribute{}
Expand All @@ -146,60 +146,60 @@ embed: {
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:5:1")
NamePos: token.Pos("fields.cue:5:1").WithRel("newline")
Name: "#Schema"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:5:8")
TokenPos: token.Pos("fields.cue:5:8").WithRel("nospace")
Token: token.Token(":")
Value: *ast.StructLit{
Lbrace: token.Pos("fields.cue:5:10")
Lbrace: token.Pos("fields.cue:5:10").WithRel("blank")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:6:2")
NamePos: token.Pos("fields.cue:6:2").WithRel("newline")
Name: "name"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:6:6")
TokenPos: token.Pos("fields.cue:6:6").WithRel("nospace")
Token: token.Token(":")
Value: *ast.Ident{
NamePos: token.Pos("fields.cue:6:8")
NamePos: token.Pos("fields.cue:6:8").WithRel("blank")
Name: "string"
}
Attrs: []*ast.Attribute{}
}
*ast.Ellipsis{
Ellipsis: token.Pos("fields.cue:7:2")
Ellipsis: token.Pos("fields.cue:7:2").WithRel("newline")
Type: nil
}
}
Rbrace: token.Pos("fields.cue:8:1")
Rbrace: token.Pos("fields.cue:8:1").WithRel("newline")
}
Attrs: []*ast.Attribute{}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:9:1")
NamePos: token.Pos("fields.cue:9:1").WithRel("newline")
Name: "embed"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:9:6")
TokenPos: token.Pos("fields.cue:9:6").WithRel("nospace")
Token: token.Token(":")
Value: *ast.StructLit{
Lbrace: token.Pos("fields.cue:9:8")
Lbrace: token.Pos("fields.cue:9:8").WithRel("blank")
Elts: []ast.Decl{
*ast.EmbedDecl{
Expr: *ast.Ident{
NamePos: token.Pos("fields.cue:10:2")
NamePos: token.Pos("fields.cue:10:2").WithRel("newline")
Name: "#Schema"
}
}
}
Rbrace: token.Pos("fields.cue:11:1")
Rbrace: token.Pos("fields.cue:11:1").WithRel("newline")
}
Attrs: []*ast.Attribute{}
}
Expand Down
Loading

0 comments on commit 6e5ec5a

Please sign in to comment.