FParsec identifiers vs keywords

前端 未结 2 1114
孤城傲影
孤城傲影 2020-12-18 06:58

For languages with keywords, some special trickery needs to happen to prevent for example \"if\" from being interpreted as an identifier and \"ifSomeVariableName\" from beco

2条回答
  •  情歌与酒
    2020-12-18 07:57

    You can define a parser for whitespace and check if keyword or identifier is followed by it. For example some generic whitespace parser will look like

    let pWhiteSpace = pLineComment <|> pMultilineComment <|> pSpaces
    

    this will require at least one whitespace

    let ws1 = skipMany1 pWhiteSpace
    

    then if will look like

    let pIf = pstring "if" .>> ws1
    

提交回复
热议问题