Haskell lexical layout rule implementation

I have been working on a pet language, which has Haskell-like syntax. One of the neat things which Haskell does, which I have been trying to replicate, is its insertion of {, } and ; tokens based on code layout, before the parsing step.

I found http://www.haskell.org/onlinereport/syntax-iso.html, which includes a specification of how to implement the layout program, and have made a version of it (modified, of course, for my (much simpler) language).

Unfortunately, I am getting an incorrect output for the following:

f (do xyz) ab

It should be producing the token stream ID ( DO { ID ID ID } ) ID ID , but it is instead producing the token stream ID ( DO { ID ID ID ) ID ID } .

I imagine that this is due to my unsatisfactory implementation of parse-error(t) ( parse-error(t) = false ), but I have no idea how I would go about implementing parse-error(t) efficiently.

How do Haskell compilers like GHC etc. handle this case? Is there an easy way to implement parse-error(t) such that it handles this case (and hopefully others that I haven't noticed yet)?


I ended up implementing a custom version of the parsing algorithm used by JISON's compiled parsers, which takes an immutable state object, and a token, and performs as much work as possible with the token before returning. I am then able to use this implementation to check if a token will produce a parse error, and easily roll back to previous states of the parser.

It works fairly well, although it is a bit of a hack right now. You can find the code here: https://github.com/mystor/myst/blob/cb9b7b7d83e5d00f45bef0f994e3a4ce71c11bee/compiler/layout.js

I tried doing what @augustss suggested, using the error production to fake the token's insertion, but it appears as though JISON doesn't have all of the tools which I need in order to get a reliable implementation, and re-implementing a stripped-down version of the parsing algorithm turned out to be easier, and lined up better with the original document.

链接地址: http://www.djcxy.com/p/43160.html

上一篇: 我应该使用GHC Haskell扩展吗?

下一篇: Haskell词法布局规则的实现