local lpeg = require "lpeg"
    function f0() end;
    function f1() return "a" end
    function f2() return "a","b" end
    function ittable(t)
        for key, value in pairs(t) do
            print ("table:", key, value)
        end
    end

local match = lpeg.match -- match a pattern against a string
local P = lpeg.P -- match a string literally
local S = lpeg.S  -- match anything in a set
local R = lpeg.R  -- match anything in a range
local C = lpeg.C  -- captures a match
local Ct = lpeg.Ct -- a table with all captures from the pattern
--[[
lpeg.match (pattern, subject [, init])
匹配函数,尝试用给定模式去匹配目标字串。成功返回,匹配字串后的第一个字符索引,或者捕获值(如果取捕获值,由小括号取得 );失败返回nil
可选数字参数init,指定匹配开始索引位置;负数表示由字串向前查找
--]]

print (match(P'a','aaaa'))    -- 从1开匹配,返回匹配后的位置2
print (match(P'ab','abaa'))   -- 从1开匹配,返回匹配后的位置3
print (match(P'ab','abaabz',4))  -- 从3开匹配,返回匹配后的位置3
print (match(P'ab','abaabz',3))  -- 从3开匹配,返回匹配后的位置3

--[[
lpeg.type (value)
判断给定值是否为模式,是返回“pattern",否返回nil
--]]
print (lpeg.type(S'a'))
print (lpeg.type(P'a'))
print (lpeg.type('a'))

--[[
lpeg.P (value)
按如下规则,将值转为相应的模式
1、如果参数为模式,返回输入模式
2、如果参数为字串,返回模式,该模式匹配输入字串
3、如果参数为非负n, 返回模式,该模式匹配n个字符
4、如果参数为负n, 返回模式,该模式
5、如果参数为boolean,返回模式,该模式匹配总是成功或者失败,由参数值确定, 但匹配不消消耗输入
6、如果参数为table, 按语法进行解析
7、如果参数为function, 返回模式,等价匹配时在空字串上的捕获

--]]
print (match(lpeg.C(P(1)),'abaabz',4))
print (match(lpeg.C(P(true)),'abaabz',4))
print (match((P(true)),'abaabz',4))

function maybe(p) return p^-1 end
digits = R'09'^1
mpm = maybe(S'+-')
dot = '.'
exp = S'eE'
float = mpm * digits * maybe(dot*digits) * maybe(exp*mpm*digits)
print (match(C(float),'2.3'))

--[[
lpeg.B(patt)
Returns a pattern that matches only if the input string at the current position is preceded by patt. Pattern patt must match only strings with some fixed length, and it cannot contain captures.
Like the and predicate, this pattern never consumes any input, independently of success or failure.
--]]

--[[
lpeg.R ({range})
返回模式,该模式匹配一个字符,该字符属于范围中的一个。range返回长度为2个字符,前低后高,两端包含。多个范围用逗号隔开
lpeg.R("09") 匹配任意数字, lpeg.R("az", "AZ") 匹配任意ASCII字符
--]]

--[[
lpeg.S (string)
返回模式,该模式匹配一个字符,该字符在string集中有出现过。
lpeg.S("+-*/")匹配加减乘除符号中
--]]

--[[
lpeg.V (v)
This operation creates a non-terminal (a variable) for a grammar. The created non-terminal refers to the rule indexed by v in the enclosing grammar. (See Grammars for details.)
]]

--[[
lpeg.locale ([table])
返回一个表,KEY分别为alnum, alpha, cntrl, digit, graph, lower, print, punct, space, upper, and xdigit;值为相应的模式
--]]
print (match(C(lpeg.locale({})["digit"]), "1343bad",2))
print (match(C(lpeg.locale({})["digit"]^1), "1343bad"))
print (match(C(lpeg.locale({})["alpha"]^1), "1343bad", 5))

--[[
#patt

Returns a pattern that matches only if the input string matches patt, but without consuming any input, independently of success or failure. (This pattern is called an and predicate and it is equivalent to &patt in the original PEG notation.)
This pattern never produces any capture.
--]]

--[[
-patt
返回一个模式, 该模式仅匹配 (输入不匹配patt)。 不会产生输入消耗;等价!patt
-lpeg.P(1)匹配字串结束
该模式始终不会产生捕获
--]]
print (match(C(-lpeg.locale({})["digit"]), "bad",2))

--[[
patt1 + patt2
返回一个模式,为两个模式的交集, 输入匹配之一即可
]]
print (match( C(P'ac' + P'ab'), "abc"))

--[[
patt1 - patt2
返回一个模式,输入只不匹配PATT2但要匹配PATT1
--]]
print (match( C(P'a'^-2 - P'aac'), "aac"))
print (match( C(P'a'^-2 - P'aac'), "aa"))
--[[
patt1 * patt2
连接匹配, 匹配PATT1然后继续匹配PATT2
--]]
print (match( C(P'a'^1 * P'c'), "aaaaaaccccc"))
--[[
patt^n
如果n为非负, 该模式等价PATT* ,它匹配n或者更多次出现;为负表示最多出现次数
If n is nonnegative, this pattern is equivalent to pattn patt*: It matches n or more occurrences of patt.

Otherwise, when n is negative, this pattern is equivalent to (patt?)-n: It matches at most |n| occurrences of patt.

In particular, patt^0 is equivalent to patt*, patt^1 is equivalent to patt+, and patt^-1 is equivalent to patt? in the original PEG notation.

In all cases, the resulting pattern is greedy with no backtracking (also called a possessive repetition). That is, it matches only the longest possible sequence of matches for patt.
--]]
print (match( C(P'a'^-5), "aaaaaaccccc"))
print (match( C(P'a1'^-5), "aaaaaaccccc"))

--[[
Grammars
语法:
使用LUA变量, 可以递增式定义模式,新模式使用之前的模式。 该技术不允许使用递归模式,
LPEG用表来表达语法,每条记录是一个规则
调用lpeg.V(v)来创建一个模式,“V" 为模式变量名
The call lpeg.V(v) creates a pattern that represents the nonterminal (or variable) with index v in a grammar.
Because the grammar still does not exist when this function is evaluated, the result is an open reference to the respective rule.

A table is fixed when it is converted to a pattern (either by calling lpeg.P or by using it wherein a pattern is expected). Then every open reference created by lpeg.V(v) is corrected to refer to the rule indexed by v in the table.

When a table is fixed, the result is a pattern that matches its initial rule. The entry with index 1 in the table defines its initial rule. If that entry is a string, it is assumed to be the name of the initial rule. Otherwise, LPeg assumes that the entry 1 itself is the initial rule.

As an example, the following grammar matches strings of a's and b's that have the same number of a's and b's:
--]]

--[[
Captures
捕获
就是模式匹配结果。
LPeg提供了多种捕获, 可以产生基于匹配和这些值的组产生新值,每次捕获0个或者多个
--]]

--[[
lpeg.C (patt)
创建一个简单捕获, 获得一个PATT匹配目标字串的一个字串。
--]]
print (match( C(P'a1'^-5), "aaaaaaccccc"))

--[[
lpeg.Carg (n)
创建一个参数捕获,模式匹配了一个空串,并产生一个值,能match的额外第n个参数。
Creates an argument capture. This pattern matches the empty string and produces the value given as the nth extra argument given in the call to lpeg.match.
--]]
print (match( lpeg.Carg(1), "aaaaaaccccc",1,"bb"))

LPEG的更多相关文章

  1. [翻译]lpeg入门教程

    原文地址:http://lua-users.org/wiki/LpegTutorial 简单匹配 LPeg是一个用于文本匹配的有力表达方式,比Lua原生的字符串匹配和标准正则表达式更优异.但是,就像其 ...

  2. 【翻译】LPeg编程指南

    原文:http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html   译者序: 这个是官方的LPeg的文档.这段时间学习LPeg的时候发现国内关于LPeg的文章 ...

  3. 通过 LPeg 介绍解析表达式语法(Parsing Expression Grammars)

    通过 LPeg 介绍解析表达式语法(Parsing Expression Grammars) 译者: FreeBlues 修订版本: 1.00 最新链接: http://www.cnblogs.com ...

  4. 为sproto手写了一个python parser

    这是sproto系列文章的第三篇,可以参考前面的<为sproto添加python绑定>.<为python-sproto添加map支持>. sproto是云风设计的序列化协议,用 ...

  5. Lua: 好的, 坏的, 和坑爹的

                                   好的       小巧: 20000行C代码 可以编译进182K的可执行文件 (Linux下).       可移植: 只要是有ANSI ...

  6. [转]http://lua-users.org/wiki/LpegTutorial

    Simple Matching LPeg is a powerful notation for matching text data, which is more capable than Lua s ...

  7. SLua 中使用 Lua 5.3 的编译工程

    2016-03-05 更新: 之前编译的库,在 Android 下 Lua_Number 和 Lua_Integer 被编译为了32位,导致从 C# 到 Lua 过程中有64位到32位整型转换会出现溢 ...

  8. lua pbc

    先要将proto文件编译成.pb文件,然后再动态绑定实现lua protobuffer,这就需要了解云风做的pbc的项目,地址为:https://github.com/cloudwu/pbc/blob ...

  9. skynet初学

    记录下命令 git clone https://github.com/cloudwu/skynet.git sudo apt-get install autoconf sudo apt-get ins ...

随机推荐

  1. 20145236 《Java程序设计》实验二实验报告

    北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.08 实验名称: Java面向对象程序设计 实验内容: 初步掌握单元测试和T ...

  2. linux下git的安装和使用(转)

    转自:http://www.cnblogs.com/sunada2005/archive/2013/06/06/3121098.html 最近在使用github,感觉不错.在windows下,可使用g ...

  3. MATLAB实现矩阵分块相乘

    要实现一下功能,这里$\bf{x}_i$为行向量 $${\bf{A}} = \left[ \begin{array}{l}{{\bf{x}}_1}\\{{\bf{x}}_2}\end{array} \ ...

  4. 46. Permutations——本质和树DFS遍历无异 fun: for i in nums fun(i)

    Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have ...

  5. PHP 页面编码声明方法详解(header或meta)

    php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header("Content-type: text/html; charset=utf-8&quo ...

  6. [Js]评分星星

    效果: 鼠标移到星星上,这颗星星及之前的全亮,提示文字出现,根绝星星数量显示不同文字,移出灭掉,文字消失 思路: 1.定义一个数组,来存放不同的文字 2.存放星星的索引值(要在i定义赋值后,即在for ...

  7. 把Angular中的$http变成jQuery.ajax()一样,可以让后台(php)轻松接收到参数

    最近接到一个手机项目,我决定用ionic + php + mysql来实现.ionic是一个前端框架,主要用于手机端,它融合了html5.css3.angularJS于一体,用起来很顺手. 开始构建项 ...

  8. HDU 4036 存疑题目,数论 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4036 一开始以为需要用斜抛,结果发现只需要用能量守恒定律?+与最大速度的坏土豆速度保持一致 #include & ...

  9. Redis系列-存储篇set主要操作函数小结

    最近,总是以“太忙“为借口,很久没有blog了,凡事贵在恒,希望我能够坚持不懈,毕竟在blog的时候,也能提升自己.废话不说了,直奔主题”set“ redis set 是string类型对象的无序集合 ...

  10. (DFS)zoj1008-Gnome Tetravex

    现在zoj暂时关了,实际上是在scuoj上做的. 题目地址 看起来题目比较复杂,实际上主要需要思维的是如何恰当的剪枝及合适的DFS角度. 问题等价于将n*n个可能相同的方块放到一个n*n的表中,使满足 ...