Haskell源编码

Gyö*_*sek 7 encoding haskell

Haskell 2010语言报告说:

Haskell使用Unicode [2]字符集.但是,源程序目前偏向于早期版本的Haskell中使用的ASCII字符集.

这是否意味着UTF-8?

在ghc-7.0.4/compiler/parser/Lexer.x.source中:

$unispace    = \x05 -- Trick Alex into handling Unicode. See alexGetChar.
$whitechar   = [\ \n\r\f\v $unispace]
$white_no_nl = $whitechar # \n
$tab         = \t

$ascdigit  = 0-9
$unidigit  = \x03 -- Trick Alex into handling Unicode. See alexGetChar.
$decdigit  = $ascdigit -- for now, should really be $digit (ToDo)
$digit     = [$ascdigit $unidigit]

$special   = [\(\)\,\;\[\]\`\{\}]
$ascsymbol = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~]
$unisymbol = \x04 -- Trick Alex into handling Unicode. See alexGetChar.
$symbol    = [$ascsymbol $unisymbol] # [$special \_\:\"\']

$unilarge  = \x01 -- Trick Alex into handling Unicode. See alexGetChar.
$asclarge  = [A-Z]
$large     = [$asclarge $unilarge]

$unismall  = \x02 -- Trick Alex into handling Unicode. See alexGetChar.
$ascsmall  = [a-z]
$small     = [$ascsmall $unismall \_]

$unigraphic = \x06 -- Trick Alex into handling Unicode. See alexGetChar.
$graphic   = [$small $large $symbol $digit $special $unigraphic \:\"\']
Run Code Online (Sandbox Code Playgroud)

......我不知道该怎么做.alexGetChar真的没有用.

ham*_*mar 7

有人建议将UTF-8标准化为Haskell源文件的标准编码,但我不确定它是否被接受.

在实践中,GHC假定所有输入文件都是UTF-8,但它忽略了注释中格式错误的字节序列.

  • 从GHC 6.6开始就是如此 (2认同)

Iva*_*lov 6

Unicode是字符集.UTF-8,UTF-16等是Unicode代码点的具体物理编码.试着在这里阅读.差异在那里解释得很好.

引用报告的部分只是声明Haskell源使用Unicode字符集.它没有说明应该使用哪种编码.换句话说,它说明哪些字符可以出现在源代码中,但没有说明如何用普通字节来编写它们.

  • (以下注释是对已删除的注释的回应:)要编写任何类型的解析器,您必须知道特定文件是如何编码的.Unicode字符集有许多可接受的编码,包括UTF-8,UTF-16,UTF-32等.您的解析器应该能够处理所有这些编码.实际上,这不是问题,因为您将读取器函数中的字节转换本地化为字符. (2认同)