我生成了一些haskell代码(在haskell中),我需要能够检测与haskell关键字冲突的名称。是否有某个地方以编程方式检查String是否是Haskell关键字?
我当然可以自己编制它们的清单,但我不想重新发明轮子。此外,如果引入了新关键字(通过扩展或新规范),我希望此列表能够自动更新。
答案 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)
我确定它是完整列表,因为它是GHC词法分析器所理解的保留字。另一个有趣的列表是reservedSymsFS
- 保留符号。