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

本文共 5443 字,大约阅读时间需要 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"))

转载地址:http://clvpl.baihongyu.com/

你可能感兴趣的文章
Spring Cloud微服务分布式云架构—集成项目简介
查看>>
SQLServer之删除存储过程
查看>>
盒马鲜生颠覆传统生鲜市场的胜算几何?
查看>>
“无人化时代”正在逼近,网友:再不努力就无工可打啦!
查看>>
【Node】常用基础 API 整理
查看>>
传神成进博会唯一指定智能翻译硬件提供商 力助无障碍沟通
查看>>
微信小程序实现slideUp、slideDown滑动效果及点击空白隐藏功能示例
查看>>
Java程序员须知:分布式微服务为什么很难?
查看>>
SQLServer之创建唯一聚集索引
查看>>
好程序员web前端技术之CSS3过渡
查看>>
java B2B2C源码电子商务平台 - Zuul回退机制
查看>>
记录Docker in Docker 安装(CentOS7)
查看>>
简单的写一个发布订阅器
查看>>
重学前端-js的类型问题
查看>>
Function类型
查看>>
Python学习
查看>>
你有多渴望赚钱
查看>>
ES6之let和const
查看>>
关于跨域
查看>>
一个半路出家的前端工程师的2018 | 掘金年度征文
查看>>