Custom whiteSpace using Haskell Parsec

徘徊边缘 提交于 2019-12-07 03:47:47

问题


I would like to use Parsec's makeTokenParser to build my parser, but I want to use my own definition of whiteSpace. Doing the following replaces whiteSpace with my definition, but all the lexeme parsers still use the old definition (e.g. P.identifier lexer will use the old whiteSpace).

...
lexer :: P.TokenParser ()
lexer      = l { P.whiteSpace = myWhiteSpace }
   where l = P.makeTokenParser myLanguageDef
...

Looking at the code for makeTokenParser I think I understand why it works this way. I want to know if there are any workarounds to avoid completely duplicating the code for makeTokenParser?


回答1:


Sadly, I don't think there is a way. The local definitions used in makeTokenParser refer recursively to themselves, and so, as you've noted, lexeme uses whiteSpace as defined there, rather than the whiteSpace record member you replace in your lexer object.

The code is taunting because it uses the same names as both local functions in makeTokenParser and as record members of the TokenParser constructor. They are in fact totally distinct entities.



来源:https://stackoverflow.com/questions/2641737/custom-whitespace-using-haskell-parsec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!