简体   繁体   English

js-yaml是否可以进行行号映射?

[英]Is line number mapping possible with js-yaml?

Say I have a YAML document like this: 说我有一个像这样的YAML文件:

valid_true:
  - true
  - True
  - TRUE

valid_false:
  - false
  - False
  - FALSE

I'd like to end up with an object that correlates the parsed object keys to the line numbers from the YAML document that defined them. 我想最后得到一个对象,该对象将解析的对象关键字与定义它们的YAML文档中的行号相关联。 An output like this might work: 这样的输出可能会起作用:

{
  "valid_true": {
    "value": [
      {"value": true, line: 2},
      {"value": true, line: 3},
      {"value": true, line: 4}
    ],
    "line": 1
  },
  "valid_false": {
    "value": [
      {"value": false, line: 7},
      {"value": false, line: 8},
      {"value": false, line: 9}
    ],
    "line": 6
  }
}

Is this possible with this library? 这个库有可能吗? Or will I need to fork / modify it? 还是我需要派生/修改它?

Many YAML libraries peform the task of loading in multiple stages: 许多YAML库在多个阶段执行加载任务:

  1. scanning document sources into tokens 将文档源扫描成令牌
  2. composing the tokens into nodes (tagged scalars, sequences, mappings) 将标记组成节点(标记的标量,序列,映射)
  3. constructing the language specific objects from the nodes 从节点构造特定于语言的对象

After the last step the line information that you seek is normally no longer available. 完成最后一步后,通常不再提供您要查找的线路信息。 As long as you can hook into the loading task, by either influencing the construction of objects or by building on top of an exposed node structure, then what you want is relatively easy to do. 只要您可以通过影响对象的构造或在裸露的节点结构之上进行构建来挂接加载任务,那么所需的操作就相对容易。

That is eg the case in PyYAML, the YAML 1.1 loader/dumper on which js-yaml originally was based. 例如,PyYAML(js-yaml最初基于的YAML 1.1加载程序/转储程序)就是这种情况。 But js-yaml has been rewritten to support YAML 1.2 since then and the API , doesn't seem to have any options to "interfere" as described in the previous paragraph. 但是自那时以来,js-yaml已被重写为支持YAML 1.2,并且该API似乎没有如上段所述的任何“干扰”选项。

You might want to look into older (PyYAML based) versions of the library, to see if the API for them is more flexible. 您可能需要查看该库的较旧版本(基于PyYAML),以查看它们的API是否更灵活。 I have not looked at the details of the implementation, but my impression is that the rewrite did away with flexibility in favour of speed (not in itself a bad decision, but not something that helps use cases like yours). 我没有看过实现的细节,但我的印象是重写以灵活性为代价而放弃了速度(这本身不是一个错误的决定,但对像您这样的用例却没有帮助)。

If you are not restricted by javascript, then you might want to look at the NimYaml or my own ruamel.yaml (for Python). 如果您不受javascript的限制,则可能需要查看NimYaml或我自己的ruamel.yaml (适用于Python)。 Those are YAML 1.2 loader/dumpers where I know for sure you can hook into the loading process as you'll need to do. 这些是YAML 1.2加载程序/转储程序,我确定可以确定您可以按照需要进行加载过程。

ruamel.yaml , when used in the default round-trip mode, has line/column information already assigned to the objects constructed from YAML mappings and sequences and this can be relatively easily be extended to the available special scalar types used in round-tripping (these include most types including booleans). ruamel.yaml在默认往返模式下使用时,已经将行/列信息分配给了由YAML映射和序列构成的对象,可以相对容易地扩展到往返中使用的可用特殊标量类型(这些包括大多数类型,包括布尔值)。 Alternatively, if your input is guaranteed to have whitespace and/or comment-only lines after the keys, as your example has, the actual linenumber of an item can of course be calculated on the basis of the start of the list. 另外,如您的示例所示,如果您保证输入的键后有空格和/或仅注释行,那么可以根据列表的开头来计算项目的实际行号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM