gpt4 book ai didi

yaml - 如何在Ada中读取yaml文件

转载 作者:行者123 更新时间:2023-12-02 19:24:58 25 4
gpt4 key购买 nike

我试图了解如何使用 Ada 和 AdaYaml 库 ( https://github.com/yaml/AdaYaml/ )。我有几个 yaml 文件,我需要将它们转换为 Ada 记录以进行进一步处理。

在玩了几个小时的库之后,我不知道如何使用 Accessor 访问 yaml 数据。

我的最小工作代码是

with Ada.Text_IO;
with Ada.Command_Line;

with Yaml.Source.File;
with Yaml.Source.Text_IO;
with Yaml.Dom.Loading;
with Yaml.Dom.Node;
with Yaml; use Yaml;
with Text; use Text;

procedure Load_Dom is
Input : Source.Pointer;
begin

if Ada.Command_Line.Argument_Count = 0 then
Input := Yaml.Source.Text_IO.As_Source (Ada.Text_IO.Standard_Input);
else
Input := Yaml.Source.File.As_Source (Ada.Command_Line.Argument (1));
end if;

declare

Document : Yaml.Dom.Document_Reference :=
Yaml.Dom.Loading.From_Source (Input);

begin
Ada.Text_IO.Put_Line ("Root node is a " & Yaml.Dom.Node.Instance'(Document.Root.Value).Kind'Img);
Ada.Text_IO.Put_Line ("Root node is a " & Yaml.Dom.Node.Instance'(Document.Root.Value).Tag);
Ada.Text_IO.Put_Line ("Root node is a " & Yaml.Dom.Node.Instance'(Document.Root.Value).Mapping_Style'Img);
end;
end Load_Dom;

我认为显式转换 Yaml.Dom.Node.Instance'(Document.Root.Value) 不正确,我一定错过了一些东西。

有什么想法或代码示例如何以正确的方式读取 yaml 文件吗?

最佳答案

我是 AdaYaml 的作者。

Document.Root.Value返回的Accessor定义如下:

type Accessor (Data : not null access Node.Instance) is limited private
with Implicit_Dereference => Data;

与具有判别式的任何类型一样,您可以通过其名称 Data 访问 Node.Instance。所以这个显式表达式检索根节点的种类:

Document.Root.Value.Data.all.Kind

现在,Ada 允许我们隐式取消指针引用,删除 .all:

Document.Root.Value.Data.Kind

并且 AccessorImplicit_Dereference 属性允许我们删除 .Data:

Document.Root.Value.Kind

所以你想做的是

declare
Document : Yaml.Dom.Document_Reference :=
Yaml.Dom.Loading.From_Source (Input);
Root : Yaml.Dom.Accessor := Document.Root.Value;
begin
Ada.Text_IO.Put_Line ("Root node is a " & Root.Kind'Img);
Ada.Text_IO.Put_Line ("Root node is a " & Root.Tag);
Ada.Text_IO.Put_Line ("Root node is a " & Root.Mapping_Style'Img);
end;

这在 the documentation 中简要显示但没有解释。如需进一步阅读,this gem可能会感兴趣。

关于yaml - 如何在Ada中读取yaml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62504409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com