博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LPEG
阅读量:7233 次
发布时间:2019-06-29

本文共 5497 字,大约阅读时间需要 18 分钟。

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"))

转载于:https://www.cnblogs.com/freebird92/p/4674346.html

你可能感兴趣的文章
ensp实战之防火墙安全转发策略
查看>>
Activity和Fragment之间解耦
查看>>
modbus协议说明(转)
查看>>
vc编辑器常用设置
查看>>
你的学习标配
查看>>
58到家数据库30条军规解读
查看>>
XAF-Domain Components 技术 使用接口来定义ORM业务对象
查看>>
执行shell脚本提示“-bash: ./checkP.sh: /bin/sh^M: bad interpreter: No such file or directory”解决方法...
查看>>
内核工作队列【转】
查看>>
[Java并发编程(五)] Java volatile 的实现原理
查看>>
读书笔记,《刻意练习》,第一章,有目的的练习
查看>>
sqlserver导出为EXcel--CSV格式
查看>>
UVA 357 Let Me Count The Ways(全然背包)
查看>>
wpf z
查看>>
Android 学习笔记之Bitmap位图虽触摸点移动
查看>>
Android项目——传感器的使用
查看>>
ajax不运行success回调而是运行error回调
查看>>
Android View体系(一)视图坐标系
查看>>
WeUI移动页面实现时间选择器(年-月-日-时-分)
查看>>
采用config方式灵活配置我们的Quarz.net中的Job,Trigger
查看>>