有没有一种确定的方法来编写可以重建其确切输入的解析器? [英] Is there an established way to write parsers that can reconstruct their exact input?
问题描述
$ b $ pre $ data $ Foo = Foo Int - 我所追求的信息。
parseFoo :: Parsec Text Foo
parseFoo = ...
<这很容易产生一个函数 getFoo :: Text - >也许Foo
。
但现在我也想修改的来源, Foo
信息,即基本上我想实现
changeFoo ::(Foo - > Foo) - >文字 - >文本
与属性
changeFoo id≡id
getFoo。 changeFoo f≡fmap f。 getFoo
可以通过将解析器的结果更改为类似镜头的结果来执行此操作
parseFoo :: Parsec文本(Foo,Foo - >文本)
parseFoo = ...
但这会使定义变得更加繁琐–我不能再掩盖不相关的信息,但需要存储每个字符串
子匹配的匹配并手动重新组装。
这可以通过将字符串重组保留在解析器monad周围的 StateT
层中来实现某种程度的自动化,但我不能仅仅使用现有的原语解析器。 / p>
是否存在此问题的解决方案?
双向转型的情况?例如, http://ceur-ws.org/Vol-1571/
特别是Rendel和Osterman的Invertible Syntax Descriptions:Unifying Parsing和Pretty Printing
http://dblp.org/rec/conf/haskell/RendelO10 ,Haskell Symposium 2010(参见 http://lambda-the-ultimate.org/node/4191 )
Say I want to parse a file in language X. Really, I'm only interested in a small part of the information within. It's easy enough to write a parser in one of Haskell's many eDSLs for that purpose (e.g. Megaparsec).
data Foo = Foo Int -- the information I'm after.
parseFoo :: Parsec Text Foo
parseFoo = ...
That readily gives rise to a function getFoo :: Text -> Maybe Foo
.
But now I would also like to modify the source of the Foo
information, i.e. basically I want to implement
changeFoo :: (Foo -> Foo) -> Text -> Text
with the properties
changeFoo id ≡ id
getFoo . changeFoo f ≡ fmap f . getFoo
It's possible to do that by changing the result of the parser to something like a lens
parseFoo :: Parsec Text (Foo, Foo -> Text)
parseFoo = ...
but that makes the definition a lot more cumbersome – I can't just gloss over irrelevant information anymore, but need to store the match of every string
subparse and manually reassemble it.
This could be somewhat automated by keeping the string-reassembage in a StateT
layer around the parser monad, but I couldn't just use the existing primitive parsers.
Is there an existing solution for this problem?
Is this a case of "bidirectional transformation"? E.g., http://ceur-ws.org/Vol-1571/
In particular, "Invertible Syntax Descriptions: Unifying Parsing and Pretty Printing" by Rendel and Osterman http://dblp.org/rec/conf/haskell/RendelO10 , Haskell Symposium 2010 (cf. http://lambda-the-ultimate.org/node/4191 )
这篇关于有没有一种确定的方法来编写可以重建其确切输入的解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!