我在哪里可以找到保留的Haskell关键字列表为[String]

时间:2014-07-12 10:08:52

标签: haskell ghc

我生成了一些haskell代码(在haskell中),我需要能够检测与haskell关键字冲突的名称。是否有某个地方以编程方式检查String是否是Haskell关键字?

我当然可以自己编制它们的清单,但我不想重新发明轮子。此外,如果引入了新关键字(通过扩展或新规范),我希望此列表能够自动更新。

2 个答案:

答案 0 :(得分:7)

hscolour uses such a simple list for its tokenise function

keywords =
  ["case","class","data","default","deriving","do","else","forall"
  ,"if","import","in","infix","infixl","infixr","instance","let","module"
  ,"newtype","of","qualified","then","type","where","_"
  ,"foreign","ccall","as","safe","unsafe"]
keyglyphs =
  ["..","::","=","\\","|","<-","->","@","~","=>","[","]"]
layoutchars =
  map (:[]) ";{}(),"
symbols =
  "!#$%&*+./<=>?@\\^|-~"

你可以

import Language.Haskell.HsColour.Classify

isKeyword :: String -> Bool
isKeyword = (== [Keyword]) . map fst . tokenise

答案 1 :(得分:5)

这是:https://github.com/ghc/ghc/blob/d784bdeb62a6b11831c5235a97449ff2a86dcc52/compiler/parser/Lexer.x#L708-L765

我确定它是完整列表,因为它是GHC词法分析器所理解的保留字。另一个有趣的列表是reservedSymsFS - 保留符号。