Search code examples
pythonyamlpyyaml

Pyyaml custom parser of collections


Tell me what are the ways to customize the pyyaml parser so that it can read such text (a collection without - characters and so that when specifying a line with a% and not wrapped in quotation marks, it does not crash with the error" found character '%' that cannot start any token "):

collection:
    %element1
    element2
    element3

I'm not very clear about which methods need to be overridden


Solution

  • PyYAML is a YAML parser. YAML forbids the use of % as first character (see here). Also, the code you give, if not for the %, would be parsed as single multi-line scalar, not as collection.

    You can, of course, specify your own language, and then write a parser that can parse that language. If it is similar to YAML, you could even start from PyYAML and modify it. But be aware that the result would not be YAML, but your very own language. And specifying and writing a parser for your own language is very much outside of the scope of a StackOverflow answer.