在Parsec中,有没有办法防止lexeme消耗换行符? [英] In Parsec, is there a way to prevent lexeme from consuming newlines?

查看:84
本文介绍了在Parsec中,有没有办法防止lexeme消耗换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Text.Parsec.Token 中的所有解析器都礼貌地使用 lexeme 来在令牌之后吃掉空格。不幸的是,对于我来说,空格包含了新行,我想用它作为表达式终止符。有没有办法说服 lexeme 离开一个新行?

不,它不是。这里是相关的代码。



From Text.Parsec.Token

  lexeme p 
= do {x < - p;空白符;返回x}


--whiteSpace
whiteSpace
| noLine&& noMulti = skipMany(简单空间<?>)
| noLine = skipMany(simpleSpace< |> multiLineComment<>)
| noMulti = skipMany(simpleSpace< |> oneLineComment<>)
|否则= skipMany(simpleSpace< |> oneLineComment< |> multiLineComment<>)
其中
noLine = null(commentLine languageDef)
noMulti = null(commentStart languageDef)

有人会注意到的空白部分,唯一的选择就是处理评论。 lexeme 函数使用 whitespace ,它在 parsec.token






2015年9月28日更新



对我来说,最终的解决方案是使用合适的词法分析器( alex )。 Parsec作为一个解析库做得非常好,这是设计的一个功劳,它可以被改编成词法分析,但除了小而简单的项目之外,它很快就会变得笨拙。我现在使用alex创建一个线性的令牌集,然后Parsec将它们变成AST。


All of the parsers in Text.Parsec.Token politely use lexeme to eat whitespace after a token. Unfortunately for me, whitespace includes new lines, which I want to use as expression terminators. Is there a way to convince lexeme to leave a new line?

解决方案

No, it is not. Here is the relevant code.

From Text.Parsec.Token:

lexeme p
    = do{ x <- p; whiteSpace; return x  }


--whiteSpace
whiteSpace
    | noLine && noMulti  = skipMany (simpleSpace <?> "")
    | noLine             = skipMany (simpleSpace <|> multiLineComment <?> "")
    | noMulti            = skipMany (simpleSpace <|> oneLineComment <?> "")
    | otherwise          = skipMany (simpleSpace <|> oneLineComment <|> multiLineComment <?> "")
    where
      noLine  = null (commentLine languageDef)
      noMulti = null (commentStart languageDef)

One will notice in the where clause of whitespace that the only only options looked at deal with comments. The lexeme function uses whitespace and it is used liberally in the rest of parsec.token.


Update Sept. 28, 2015

The ultimate solution for me was to use a proper lexical analyser (alex). Parsec does a very good job as a parsing library and it is a credit to the design that it can be mangled into doing lexical analysis, but for all but small and simple projects it will quickly become unwieldy. I now use alex to create a linear set of tokens and then Parsec turns them into an AST.

这篇关于在Parsec中,有没有办法防止lexeme消耗换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆