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 ...
随机推荐
- OLAP 模型
OLAP分析的基础是多维数据集,按照其数据存储格式的不同可以分为关系型OLAP(Relational OLAP,ROLAP)和多维型OLAP(Multidimensional OLAP,MOLAP). ...
- ANGULAR JS WATCH监听使用
ANGULAR 监听使用: 当angular数据模型发生变化时,我们需要如果需要根据他的变化触发其他的事件. $watch是一个scope函数,用于监听模型变化,当你的模型部分发生变化时它会通知你. ...
- K需要修改的内容
1.需要保存默认案件,所有相关的页面的Title都要显示默认按键信息. 2.播放器需要调整,左侧的是播放信息,用户选择:案件/设备/然后就把该目录下的文件都展示出来.用户选择的时候马上进行播放.右侧有 ...
- php的两个符号@和&---php总会要知道的系列
在写代码的时候,碰到了在函数和变量前家 @和$的的问题,于是就借这个机会,学习下php的传值和传引用这两种方式 首先 @ 运算符只对表达式有效.对新手来说一个简单的规则就是:如果能从某处得到值,就能在 ...
- JavaOne 2016——观众得以一睹JShell的威力
导读 在JavaOne 2016的主题演讲中,Java平台组的首席架构师Mark Reinhold指出Java 9并不仅仅是Jigsaw,针对Java 9,一共包含了85个JEP.我在这里会关注一个他 ...
- 北邮新生排位赛1解题报告a-c
<div class="page-header" style="padding-bottom: 9px; margin: 20px 0px 30px; border ...
- 前端必须掌握30个CSS3选择器
也许你已经学会了CSS的三个简单常用的选择器:#ID,.class,标签选择器,可是这些就足够了吗?随着CSS3的到来,作为前端开发者需要掌握下面三十个基本的选择器,这样才可以在平时开发中得心用手. ...
- ICTCLA中科院分词工具用法(java)
摘要:为解决中文搜索的问题,最开始使用PHP版开源的SCWS,但是处理人名和地名时,会出现截断人名地名出现错误.开始使用NLPIR分词,在分词准确性上效果要比SCWS好.本文介绍如何在windows系 ...
- 向量和矩阵的范数及MATLAB调用函数
范数就是长度的一种推广形式,数学语言叫一种度量.比如有一个平面向量,有两个分量来描述:横坐标和纵坐标.向量的二范数就是欧几里得意义下的这个向量的长度.还有一些诸如极大值范数,就是横坐标或者纵坐标的最大 ...
- CodeForces 468A Program F
Description Little X used to play a card game called "24 Game", but recently he has found ...