组合子 1

Prelude Text.Parsec Text.Parsec.String> parseTest (count 3 (char 'a')) "aaa"
"aaa"
Prelude Text.Parsec Text.Parsec.String> parseTest (between (char '(') (char ')') anyChar) "(3)"
'3'
Prelude Text.Parsec Text.Parsec.String> parseTest (option ' ' (char 'a')) "b"
' '
Prelude Text.Parsec Text.Parsec.String> parseTest (optionMaybe (char 'a')) "b"
Nothing
Prelude Text.Parsec Text.Parsec.String> parseTest (optionMaybe (char 'b')) "b"
(Just 'b')
Prelude Text.Parsec Text.Parsec.String> parseTest ((,) <$> optional (char 'a') <*> getInput) "b"
((),"b")
Prelude Text.Parsec Text.Parsec.String> parseTest ((,) <$> optional (char 'a') <*> getInput) "a"
((),"")
Prelude Text.Parsec Text.Parsec.String> parseTest (optional (string "ab")) "a"
parse error at (line 1, column 1):
unexpected end of input
expecting "ab"
  • count n p 匹配 p 指定(n)次数
  • between open close p依次匹配 open p close,但是只返回 p。
  • option x p 尝试匹配 p,失败时返回 x。
  • optionMaybe p 尝试匹配 p,失败时返回 Nothing,成功时返回 Just p。
  • optional p 尝试匹配 p,失败时不消耗输入。输入不够时出错。不出错时返回空。
  • (,) <$> p <*> getInput 匹配 p,然后用 (,) 运算符连接匹配的结果和剩余的输入。

    这里 getInput 函数返回之前匹配完成之后剩余的输入。

组合子 2

Prelude Text.Parsec Text.Parsec.String> parseTest ((,) <$> skipMany1 (char 'a') <*> getInput) "aab"
((),"b")
Prelude Text.Parsec Text.Parsec.String> parseTest (sepBy (char 'a') (char ',')) "a"
"a"
Prelude Text.Parsec Text.Parsec.String> parseTest (sepBy (char 'a') (char ',')) "a,a,a"
"aaa"
Prelude Text.Parsec Text.Parsec.String> parseTest (sepBy (char 'a') (char ',')) ""
""
Prelude Text.Parsec Text.Parsec.String> parseTest (sepBy (char 'a') (char ',')) "a,"
parse error at (line 1, column 3):
unexpected end of input
expecting "a"
Prelude Text.Parsec Text.Parsec.String> parseTest (sepBy1 (char 'a') (char ',')) ""
parse error at (line 1, column 1):
unexpected end of input
expecting "a"
Prelude Text.Parsec Text.Parsec.String> parseTest (endBy (char 'a') (char ',')) "a,"
"a"
Prelude Text.Parsec Text.Parsec.String> parseTest (endBy (char 'a') (char ',')) "a,a,"
"aa"
Prelude Text.Parsec Text.Parsec.String> parseTest (endBy (char 'a') (char ',')) ""
""
Prelude Text.Parsec Text.Parsec.String> parseTest (endBy (char 'a') (char ',')) "a,a"
parse error at (line 1, column 4):
unexpected end of input
expecting ","
Prelude Text.Parsec Text.Parsec.String> parseTest (endBy1 (char 'a') (char ',')) ""
parse error at (line 1, column 1):
unexpected end of input
expecting "a"
Prelude Text.Parsec Text.Parsec.String> parseTest (sepEndBy (char 'a') (char ',')) "a"
"a"
Prelude Text.Parsec Text.Parsec.String> parseTest (sepEndBy (char 'a') (char ',')) "a,"
"a"
Prelude Text.Parsec Text.Parsec.String> parseTest (sepEndBy (char 'a') (char ',')) "a,a"
"aa"
Prelude Text.Parsec Text.Parsec.String> parseTest (sepEndBy (char 'a') (char ',')) "a,a,"
"aa"
Prelude Text.Parsec Text.Parsec.String> parseTest (sepEndBy (char 'a') (char ',')) ""
""
Prelude Text.Parsec Text.Parsec.String> parseTest (sepEndBy1 (char 'a') (char ',')) ""
parse error at (line 1, column 1):
unexpected end of input
expecting "a"
  • skipMany1 p 尝试匹配并跳过 p 1次或多次,返回空。
  • sepBy p sep 尝试匹配0次或多次以 sep 分隔的 p 的序列(只能以 p 结尾),返回 p 的 list。

    sepBy1 p sep 是1次以上的版本。
  • endBy p sep 尝试匹配0次或多次以 sep 分隔的 p 的序列(只能以 sep 结尾),返回 p 的 list。

    endBy1 p sep 是1次以上的版本。
  • sepEndBy p sep 尝试匹配0次或多次以 sep 分隔的 p 的序列(p 或 sep 结尾均可),返回 p 的 list。

    sepEndBy1 p sep 是1次以上的版本。

组合子 3

Prelude Text.Parsec Text.Parsec.String> parseTest eof ""
()
Prelude Text.Parsec Text.Parsec.String> parseTest eof "a"
parse error at (line 1, column 1):
unexpected 'a'
expecting end of input
Prelude Text.Parsec Text.Parsec.String> parseTest (notFollowedBy (char 'a')) "ab"
parse error at (line 1, column 2):
unexpected 'a'
Prelude Text.Parsec Text.Parsec.String> parseTest ((,) <$> notFollowedBy (char 'a') <*> getInput) "ba"
((),"ba")
Prelude Text.Parsec Text.Parsec.String> parseTest (manyTill (char 'a') (char 'b')) "aab"
"aa"
Prelude Text.Parsec Text.Parsec.String> parseTest (manyTill (char 'a') (char 'b')) "aac"
parse error at (line 1, column 3):
unexpected "c"
expecting "b" or "a"
Prelude Text.Parsec Text.Parsec.String> parseTest ((,) <$> lookAhead (char 'a') <*> getInput) "ab"
('a',"ab")
Prelude Text.Parsec Text.Parsec.String> parseTest (lookAhead (char 'a')) "ba"
parse error at (line 1, column 1):
unexpected "b"
expecting "a"
Prelude Text.Parsec Text.Parsec.String> parseTest anyToken "ab"
'a'
  • eof 匹配结尾
  • notFollowedBy p 仅当 p 匹配失败时成功。
  • manyTill p end 匹配0次或多次 p,直到匹配 end 成功。返回 p 的 list。
  • lookAhead p 尝试匹配 p,但是不消耗输入。
  • anyToken 匹配任何一个 token。

参考链接

Intro to Parsing with Parsec in Haskell

Haskell语言学习笔记(43)Parsec(2)的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  3. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  4. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  5. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  6. Haskell语言学习笔记(41)Parsec(1)

    Parsec Parsec是一个词法及语法分析器. 匹配字符与字符串 Prelude Text.Parsec> parseTest anyChar "a" 'a' Prelu ...

  7. Haskell语言学习笔记(57)Parsec(4)

    Parser 类型 data ParsecT s u m a type Parsec s u = ParsecT s u Identity type Parser = Parsec String () ...

  8. Haskell语言学习笔记(46)Parsec(3)

    Applicative Parsing 使用 Applicative 式的 Parser. 包括使用 (<$>), (<*>), (<$), (<*), (*> ...

  9. Haskell语言学习笔记(72)Free Monad

    安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...

随机推荐

  1. uml 知识点

    Unified Modeling Language (UML)又称统一建模语言或标准建模语言

  2. CDN、浏览器缓存

    CDN是什么? 谈到CDN的作用,可以用8年买火车票的经历来形象比喻: 8年前,还没有火车票代售点一说,12306.cn更是无从说起.那时候火车票还只能在火车站的售票大厅购买,而我所住的小县城并不通火 ...

  3. javascript 变量定义

    一.javascript中,变量定义的位置与写在哪个<script></script>标签对内无关,只分前后顺序,写在前面的后面就能够访问,写在后面的前面会提示“未定义”. 例 ...

  4. 禁止Grid、TreeGrid列排序和列菜单

    Ext的Grid和Treegrid默认提供列菜单的功能,在列菜单中可以进行排序以及控制列显示状态. 在实际项目中,往往有些列是不需要用户看到的,因此就必须屏蔽列菜单的功能. 1.屏蔽Grid,包括Ed ...

  5. Log4j2的基本使用

    Log4j2是Log4j1.x的的升级版,其中也有很大的不同,最大的区别就是由以前的properties配置文件改为xml/json/yaml配置文件. 其中配置文件的位置官方说明如下: Log4j ...

  6. pbuf类型和应用

    下面的讨论仅限于RAW API. 按存储方式分类 1. PBUF_RAM 从一般性的Heap中分配.可用空间大小受MEM_SIZE宏控制.可看作一般意义上的动态内存. 用途: a) 将应用层中的待发送 ...

  7. Oracle 树操作(select…start with…connect by…prior)---转

    原文地址:http://www.cnblogs.com/linjiqin/p/3152674.html -----------

  8. [转]NSIS:常量大全

    原文链接 http://www.flighty.cn/html/bushu/20140915_251.html ;轻狂志www.flighty.cn ;运行后会在桌面生成NSIS常量大全.txt文件 ...

  9. python unittest单元测试框架-3用例执行顺序、多级目录、装饰器、fixtures

    1.用例执行顺序 unittest默认会按照ascii码的顺序,依次执行.类名--方法名排序,使用discover也是默认排序.如果不想使用默认排序,就使用testsuite测试集的方式. impor ...

  10. Linux 简单命令查询CPU、内存、网卡等信息

    [转自]Linux查询CPU.内存.网卡等信息 看CPU信息(型号)# cat /proc/cpuinfo | grep name | cut -f2 -d: |uniq -c      1  Int ...