gpt4 book ai didi

ada - Ada 程序文本的实际字符集在哪里定义?

转载 作者:行者123 更新时间:2023-12-02 18:10:03 29 4
gpt4 key购买 nike

我正在尝试创建一个 tree-sitter解析器,以便 IDE(在本例中为 Vim)可以解析 Ada 程序文本并进行更高级的操作,例如 extract-subprogram 和 rename-variable。但定义字符集似乎存在一些问题。

Ada 2012 Reference Manual ,我找到了一个模糊类别描述的列表,其形式为“一般类别为 X 的任何字符”,这意味着,例如,除了下划线之外,all of these ( ‿ ⁀ ⁔ ︳ ︴ ﹍ ﹎ ﹏ _) 也允许出现在标识符中,这看起来很荒谬,GNAT 会以“非法字符”拒绝。该列表的开头是这样的声明:

“未指定实现 Ada 程序文本的可视化表示所使用的实际图形符号集。”

这真的意味着无法知道应该接受哪些字符吗?

Two pages on ,这些示例被明确指定为有效标识符,但 GNAT 2021 拒绝它们:

procedure Main is
Πλάτων : constant := 12; -- Plato
Чайковский : constant := 12; -- Tchaikovsky
θ, φ : constant := 12; -- Angles
begin
null;
end Main;
$ gprbuild
using project file foo.gpr
Compile
[Ada] main.adb
main.adb:2:04: error: declaration expected
main.adb:2:05: error: illegal character
main.adb:3:04: error: declaration expected
main.adb:3:05: error: illegal character
main.adb:4:05: error: illegal character
gprbuild: *** compilation phase failed

Ada 程序的实际字符集在哪里定义? GNAT 2021 搞错了吗?

下面是一个在标识符中使用 Unicode 字符的示例程序,供您进行实验。请注意,在文字字符串中使用宽字符超出了问题的范围。

main.adb:

with Ada.Wide_Text_IO; use Ada.Wide_Text_IO;

procedure Main is
δεδομένα_πράμα : constant Wide_String := "Ο Πλάτων θα ενέκρινε";
begin
Put_Line (Δεδομένα_πράμα);
end Main;

foo.gpr

project foo is

for Source_Dirs use (".");
for Main use ("main.adb");

package Compiler is
for Default_Switches ("ada") use ("-gnatW8", "-gnatiw");
end Compiler;

end foo;

构建并运行:

gprbuild
./main

最佳答案

自 Ada 2005 以来的所有 Ada 版本都要求实现支持 UTF-8 源代码,但是对于 Ada 83 和 95 兼容性,不要求它是默认编码。 GNAT的默认源编码是Latin-1 ,尽管如果 byte-order mark 则它有助于切换到 UTF-8被发现。要显式指定文件编码,您可以传递 -gnatW8 标志 or one of a number of other options .

但是,虽然这允许源文件中使用 UTF-8,但标识符在 GNAT 中仍然限于 Latin-1,您还必须传递 -gnatiw 标志以允许标识符中使用宽字符。看来 GNAT 并没有默认它,因为您可以制作非常奇怪的标识符(如您所指出的),而且还因为标识符将不再正确区分大小写; GNAT 在任何宽字符集上进行最小大小写折叠,other than characters present in other encodings it supports .

ARM § 2.3指定标识符的要求:标识符::= 标识符_开始 {标识符_开始 |标识符扩展},其中identifier_start可以概括为Unicode通用类别L中的任何内容,其余字符可以是数字,punctuation_connector,小数点标记和非空白组合标记 - 带有“标识符不得包含 punctuation_connector 类别中的两个连续字符,或以该类别中的字符结尾。” ”

超越你的问题,请注意,尽管有所有这些标志,字符串仍然编码为Latin-1(相互冲突的是,字符串文字是UTF-8,只是不是底层字符串:/)。您需要使用Ada.Strings.UTF_EncodingWide_Wide_String 和/或库,例如 VSS用于 Unicode 字符串处理。

关于ada - Ada 程序文本的实际字符集在哪里定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72532258/

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