KenKundert / nestedtext

Human readable and writable data interchange format
https://nestedtext.org
MIT License
345 stars 13 forks source link

Parsing error if the document starts with a dash #45

Closed scifinder closed 4 months ago

scifinder commented 4 months ago

Hello! I get a NestedTextError exception when I try to parse an example from the documentation starting with a dash:

-
    cmd: emborg version
    expected: emborg version: \d+\.\d+(\.\d+(\.?\w+\d+)?)?  \(\d\d\d\d-\d\d-\d\d\)
    expected_type: regex
-
    cmd: emborg --quiet list
    expected: home-\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d
    expected_type: regex

My code:

import nestedtext as nt

n = nt.load('test.nt')

print(n)

I have found out experimentally that this error appears whenever the dash is the first character in the document. Even such a document causes this error:

- a
- b

But this one parses without errors:

a:
 - b
 - c

It looks like a bug.

kalekundert commented 4 months ago

By default, the top-level data structure is required to be a dictionary. If you want to allow lists or strings instead, use the top argument to nt.load():

$ cat list.nt
- a
- b
$ python
Python 3.10.0 (default, Oct 20 2021, 17:23:57) [Clang 12.0.1 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import nestedtext as nt
>>> nt.load('list.nt', top='list')
['a', 'b']

That said, the error message should be much clearer about this, and the documentation example should probably mention this issue as well.

scifinder commented 4 months ago

Thank you for the clarification! I read about the top parameter in the documentation, but did not focus on it. Indeed, everything is working now. Thank you for the wonderful format! The representation of all parameters in the form of text, without casting to other types, is exactly what I was looking for to describe the samples of my mineralogical collection! =)

kalekundert commented 4 months ago

The representation of all parameters in the form of text, without casting to other types, is exactly what I was looking for

Us too, glad you like the format!