LPEG
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的更多相关文章
- [翻译]lpeg入门教程
原文地址:http://lua-users.org/wiki/LpegTutorial 简单匹配 LPeg是一个用于文本匹配的有力表达方式,比Lua原生的字符串匹配和标准正则表达式更优异.但是,就像其 ...
- 【翻译】LPeg编程指南
原文:http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html 译者序: 这个是官方的LPeg的文档.这段时间学习LPeg的时候发现国内关于LPeg的文章 ...
- 通过 LPeg 介绍解析表达式语法(Parsing Expression Grammars)
通过 LPeg 介绍解析表达式语法(Parsing Expression Grammars) 译者: FreeBlues 修订版本: 1.00 最新链接: http://www.cnblogs.com ...
- 为sproto手写了一个python parser
这是sproto系列文章的第三篇,可以参考前面的<为sproto添加python绑定>.<为python-sproto添加map支持>. sproto是云风设计的序列化协议,用 ...
- Lua: 好的, 坏的, 和坑爹的
好的 小巧: 20000行C代码 可以编译进182K的可执行文件 (Linux下). 可移植: 只要是有ANSI ...
- [转]http://lua-users.org/wiki/LpegTutorial
Simple Matching LPeg is a powerful notation for matching text data, which is more capable than Lua s ...
- SLua 中使用 Lua 5.3 的编译工程
2016-03-05 更新: 之前编译的库,在 Android 下 Lua_Number 和 Lua_Integer 被编译为了32位,导致从 C# 到 Lua 过程中有64位到32位整型转换会出现溢 ...
- lua pbc
先要将proto文件编译成.pb文件,然后再动态绑定实现lua protobuffer,这就需要了解云风做的pbc的项目,地址为:https://github.com/cloudwu/pbc/blob ...
- skynet初学
记录下命令 git clone https://github.com/cloudwu/skynet.git sudo apt-get install autoconf sudo apt-get ins ...
随机推荐
- AngularJS理论基础
AngularJS理论基础 AngularJs是一个用于设计动态web应用的结构框架. 它是一个框架,不是类库,是像EXT一样提供一整套方案用于设计web应用.它不仅仅是一个javascript框架, ...
- jquery.query.js 插件的用法
转载自:http://www.cnblogs.com/dachie/archive/2010/09/16/1827840.html 代码如下: var url = location.search; & ...
- struts2拦截器与过滤器
转载:http://www.cnblogs.com/JohnLiang/archive/2011/12/15/2288376.html 过滤器,是在java web中,你传入的request,resp ...
- CentOS Hadoop格式化HDFS异常java.net.UnknownHostException
#bin/hadoop namenode -format DEPRECATED: Use of this script to execute hdfs command is deprecated. I ...
- 小记:利用递归调用循环寻找MP3文件的方法。
private void findMp3Data(File mp3file) { File[] filelist = mp3file.listFiles(); if (filelist != null ...
- SQL语句的用法
1.增加字段 alter table docdsp add dspcodechar(200)2.删除字段 ALTER TABLE table_NAME DROP COLUMNc ...
- ASP.NET伪静态 UrlRewrite(Url重写) 实现和配置
核心提示:大家一定经常在网络上看到很多网站的地址后缀都是用XX.HTML或者XX.ASPX等类似静态文件的标示来操作的吧,那么大家有怀疑过他真的是一个一个的静态生成的文件么,静态文件的生成的优缺有好有 ...
- 转: ORACLE索引介绍和使用
1.什么是索引 索引是建立在表的一列或多个列上的辅助对象,目的是加快访问表中的数据: Oracle存储索引的数据结构是B*树,位图索引也是如此,只不过是叶子节点不同B*数索引: 索引由根节点.分支节点 ...
- LA 5061 LCA tarjan 算法
题目大意: 给定所有点的权值都为0,给定一棵树以后,每次询问都要求给定两点 x , y 和一个权值w,要求x,y路径上所有点权值加上w,最后求出每一个节点的值 这里因为查询和点都特别多,所以希望能最后 ...
- 分布式一致性原理—CAP
背景 随着分布式事务的出现,传统的单机事务模型(ACID)已经无法胜任,尤其是对于一个高访问量.高并发的互联网分布式系统来说. 如果我们要求严格一致性,很可能就需要牺牲掉系统的可用性,反之亦然.但两者 ...