YAML and line numbers

J

Jamis Buck

Didn't see this discussed in the archives...if I just missed it, I would
appreciate a pointer to the discussion.

Is it possible (or would it be difficult to add a way) to discover the
line number on which a value in a YAML document was defined? I'm
thinking of Copland, which uses YAML configuration files; it would be
nice to say to a user: "oops, you specified a service that doesn't exist
*at this line number in this configuration file*". It would make it a
lot easier to debug Copland-based apps, anyway.

--
Jamis Buck
(e-mail address removed)
http://www.jamisbuck.org/jamis

"I use octal until I get to 8, and then I switch to decimal."
 
W

why the lucky stiff

Jamis said:
Is it possible (or would it be difficult to add a way) to discover the
line number on which a value in a YAML document was defined?

Not in the Ruby extension. This data is available in Syck, so it's
possible. Hmm-m-m. Do you have any suggestions for how you want to
retrieve this? Psuedo-code?

_why
 
J

Jamis Buck

why said:
Not in the Ruby extension. This data is available in Syck, so it's
possible. Hmm-m-m. Do you have any suggestions for how you want to
retrieve this? Psuedo-code?

_why

.

Perhaps something like this:

conf = YAML.load( File.read( "package.yml" ) )
p conf['id'] #-> "my.package.id"
p conf['id'].line_number #-> 2

The above approach would give you the number of the first line on which
the requested value was defined. To find the line number on which the
key was defined, maybe something like this:

conf.keys.each do { |key| p key.line_number }

Because dynamically adding a line_number attribute to every object read
by the YAML parser would be prohibitively expensive for some
applications, you may even want to make it optional:

YAML.line_numbers = true
conf = YAML.load(...)
...

Anyway, something like that would be sufficient for my needs. :)

--
Jamis Buck
(e-mail address removed)
http://www.jamisbuck.org/jamis

"I use octal until I get to 8, and then I switch to decimal."
 
A

Ara.T.Howard

Perhaps something like this:

conf = YAML.load( File.read( "package.yml" ) )
p conf['id'] #-> "my.package.id"
p conf['id'].line_number #-> 2

The above approach would give you the number of the first line on which
the requested value was defined. To find the line number on which the
key was defined, maybe something like this:

conf.keys.each do { |key| p key.line_number }

Because dynamically adding a line_number attribute to every object read
by the YAML parser would be prohibitively expensive for some
applications, you may even want to make it optional:

YAML.line_numbers = true
conf = YAML.load(...)
...

Anyway, something like that would be sufficient for my needs. :)


but how would that work with something like this:

yaml = <<-yaml
---
- array element 0
- array element 1
- array element 2
yaml

array = YAML.load yaml

p array.line_number # => 1

so each object returns would have the method 'line_number' added? guess that
could work. perhaps something like

obj.yaml_lineno

though, since 'line_number' is a bit generic.

cheers.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
W

why the lucky stiff

Jamis said:
Because dynamically adding a line_number attribute to every object
read by the YAML parser would be prohibitively expensive for some
applications, you may even want to make it optional:

YAML.line_numbers = true
conf = YAML.load(...)
...
Well, yeah. You'll never need the line number for every object, so
here's what I'm thinkin:

conf = YAML.load_file( "package.yml" )
YAML.line_number_of( conf['id'] )

Which basically amounts to:

parser = YAML::parser.new
conf = parser.load( "package.yml" )
line = parser.line_number_of( conf['id'] )

The lookup table gets stored in the extension.

_why
 
J

Jamis Buck

why said:
Well, yeah. You'll never need the line number for every object, so
here's what I'm thinkin:

conf = YAML.load_file( "package.yml" )
YAML.line_number_of( conf['id'] )

Which basically amounts to:

parser = YAML::parser.new
conf = parser.load( "package.yml" )
line = parser.line_number_of( conf['id'] )

The lookup table gets stored in the extension.

That would work fine for my purposes as well. Whichever ends up being
easier to code, I guess. I'm not picky. :)

- Jamis

--
Jamis Buck
(e-mail address removed)
http://www.jamisbuck.org/jamis

"I use octal until I get to 8, and then I switch to decimal."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top