Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing line numbers in the API #108

Closed
dmandelin opened this issue Jun 24, 2015 · 11 comments
Closed

Exposing line numbers in the API #108

dmandelin opened this issue Jun 24, 2015 · 11 comments

Comments

@dmandelin
Copy link

I'd like to be able to get line/col numbers for each YAML element. I'm contributing to a project using YAML as input to a compiler, and we want to show position information in compiler error messages.

I'm interested in adding that feature if you'd take pull requests for it.

It looks like go-yaml tracks all the positions internally, but doesn't expose them through the API. The hard part seems to be designing an API to expose positions. I have a few thoughts, but I figured I'd see if there is interest before going too far into the details.

clarete added a commit to clarete/yaml that referenced this issue Jul 6, 2015
The `linenum` and `colnum` flags were introduced to mark fields that
will save the line number and column number of an element.

The field should not really map to any field names on the YAML document,
since the parser won't really save it. Once line or column number is
saved to a field that carries the flags mentioned here, the parser is
done with that field.

Might fix go-yaml#108
@subesokun
Copy link

Any chances that this feature lands someday in go-yaml? The YAML decoder seems to know already the line and column number of nodes but this information doesn't get exposed yet.

@clarete @yobert Just out of curiosity, have you found over the time any alternative solutions for reading out the line and column numbers?

Related issues:
#343
#126

@niemeyer
Copy link
Contributor

@subesokun The upcoming and in-progress yaml.v3 will offer an intermediate representation that will allow that kind of detail to be exposed.

@subesokun
Copy link

@niemeyer That are awesome news! Will there be any example that demonstrates how these line and column numbers can be read out?

@niemeyer
Copy link
Contributor

It will be documented and pretty straightforward.

@thallgren
Copy link

Any chance to have an early peek at v3? I'd like to take it for a spin and give feedback.

@tliron
Copy link

tliron commented May 21, 2018

I would also love to see this implementation. I'm working on an application for which this feature is critical. I've even considered forking an adding it myself.

Why not push the v3 branch to GitHub? The community is happy to help with your effort!

@tliron
Copy link

tliron commented Feb 21, 2019

A very long time has passed since this issue has been opened and since the v3 release was teased.

@niemeyer Is this work continuing at all? If not, would you be open to having other members of the community take over this work?

@dprotaso
Copy link

Seems like this is possible here:
https://godoc.org/gopkg.in/yaml.v3#Node

@tliron
Copy link

tliron commented Oct 18, 2019

@dprotaso Yes, that does work, and I've managed to implement full line/column number error messages using a Node-based lookup system. I believe this issue can be closed.

@niemeyer
Copy link
Contributor

Yeah, that's sorted in v3 indeed.

@mathieucaroff
Copy link

With API v3, here's how to do it:

import (
	"fmt"

	"gopkg.in/yaml.v3"
)

var node yaml.Node
err := yaml.Unmarshal([]byte(testYaml), &node)

Also see:


Full minimal example:

package main

import (
	"fmt"

	"gopkg.in/yaml.v3"
)

var testYaml = `
a: Easy!
b:
  c: 2
  d: [3, 4]
`

func main() {
	var node yaml.Node
	err := yaml.Unmarshal([]byte(testYaml), &node)

	if err != nil {
		panic(err)
	}

	fmt.Printf("Result node: %s", node)
    // Result node: {%!s(yaml.Kind=1) %!s(yaml.Style=0)    %!s(*yaml.Node=<nil>) [%!s(*yaml.Node=&{4 0 !!map   <nil> [0xc00009a5a0 0xc00009a640 0xc00009a6e0 0xc00009a780]    2 1})]    %!s(int=2) %!s(int=1)}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants