Skip to content
Snippets Groups Projects
Commit 0517fba2 authored by dg's avatar dg
Browse files

Upgraded to lpeg 0.10

git-svn-id: http://svn.net-core.org/repos/t-engine4@4155 51575b47-30f0-44d4-a5cc-537603b46e54
parent b27fa754
No related branches found
No related tags found
No related merge requests found
......@@ -40,6 +40,7 @@ collectgarbage("restart")
-- Setup correct lua path
package.path = "/?.lua"
package.moonpath = "/?.moon"
math.randomseed(os.time())
......
-- $Id: re.lua,v 1.32 2008/10/09 20:25:06 roberto Exp $
-- $Id: re.lua,v 1.39 2010/11/04 19:44:18 roberto Exp $
local m = require"lpeg"
local _G = _G
-- imported functions and modules
local tonumber, type, print, error = tonumber, type, print, error
local mt = getmetatable(m.P(0))
local setmetatable = setmetatable
local m = require"lpeg"
-- 'm' will be used to parse expressions, and 'mm' will be used to
-- create expressions; that is, 're' runs on 'm', creating patterns
-- on 'mm'
local mm = m
-- pattern's metatable
local mt = getmetatable(mm.P(0))
-- No more global accesses after this point
local version = _VERSION
if version == "Lua 5.2" then _ENV = nil end
module "re"
local any = m.P(1)
-- Pre-defined names
Predef = { nl = m.P"\n" }
local Predef = { nl = m.P"\n" }
local mem
local fmem
local gmem
local function updatelocale ()
mm.locale(Predef)
Predef.a = Predef.alpha
Predef.c = Predef.cntrl
Predef.d = Predef.digit
Predef.g = Predef.graph
Predef.l = Predef.lower
Predef.p = Predef.punct
Predef.s = Predef.space
Predef.u = Predef.upper
Predef.w = Predef.alnum
Predef.x = Predef.xdigit
Predef.A = any - Predef.a
Predef.C = any - Predef.c
Predef.D = any - Predef.d
Predef.G = any - Predef.g
Predef.L = any - Predef.l
Predef.P = any - Predef.p
Predef.S = any - Predef.s
Predef.U = any - Predef.u
Predef.W = any - Predef.w
Predef.X = any - Predef.x
mem = {} -- restart memoization
fmem = {}
gmem = {}
local mt = {__mode = "v"}
setmetatable(mem, mt)
setmetatable(fmem, mt)
setmetatable(gmem, mt)
end
updatelocale()
local I = m.P(function (s,i) print(i, s:sub(1, i-1)); return i end)
......@@ -31,7 +86,7 @@ local function patt_error (s, i)
end
local function mult (p, n)
local np = m.P(true)
local np = mm.P(true)
while n >= 1 do
if n%2 >= 1 then np = np * p end
p = p * p
......@@ -47,11 +102,13 @@ local function equalcap (s, i, c)
end
local S = (m.S(" \t\n") + "--" * (any - m.S"\n")^0)^0
local S = (m.S(" \f\n\r\t\v") + "--" * (any - Predef.nl)^0)^0
local name = m.R("AZ", "az") * m.R("AZ", "az", "09")^0
local name = m.R("AZ", "az") * m.R("AZ", "az", "__", "09")^0
local exp_follow = m.P"/" + ")" + "}" + ":}" + "~}" + name + -1
local arrow = S * "<-"
local exp_follow = m.P"/" + ")" + "}" + ":}" + "~}" + (name * arrow) + -1
name = m.C(name)
......@@ -65,15 +122,15 @@ local String = "'" * m.C((any - "'")^0) * "'" +
'"' * m.C((any - '"')^0) * '"'
local Cat = "%" * Identifier / function (c,Defs)
local defined = "%" * Identifier / function (c,Defs)
local cat = Defs and Defs[c] or Predef[c]
if not cat then error ("name '" .. c .. "' undefined") end
return cat
end
local Range = m.Cs(any * (m.P"-"/"") * (any - "]")) / m.R
local Range = m.Cs(any * (m.P"-"/"") * (any - "]")) / mm.R
local item = Cat + Range + m.C(any)
local item = defined + Range + m.C(any)
local Class =
"["
......@@ -118,39 +175,34 @@ local exp = m.P{ "Exp",
) * S
)^0, function (a,b,f) return f(a,b) end );
Primary = "(" * m.V"Exp" * ")"
+ String / m.P
+ String / mm.P
+ Class
+ Cat
+ defined
+ "{:" * (name * ":" + m.Cc(nil)) * m.V"Exp" * ":}" /
function (n, p) return m.Cg(p, n) end
+ "=" * name / function (n) return m.Cmt(m.Cb(n), equalcap) end
+ m.P"{}" / m.Cp
+ "{~" * m.V"Exp" * "~}" / m.Cs
+ "{" * m.V"Exp" * "}" / m.C
function (n, p) return mm.Cg(p, n) end
+ "=" * name / function (n) return mm.Cmt(mm.Cb(n), equalcap) end
+ m.P"{}" / mm.Cp
+ "{~" * m.V"Exp" * "~}" / mm.Cs
+ "{" * m.V"Exp" * "}" / mm.C
+ m.P"." * m.Cc(any)
+ "<" * name * ">" / m.V;
Definition = Identifier * S * '<-' * m.V"Exp";
+ name * -arrow / mm.V
+ "<" * name * ">" / mm.V;
Definition = Identifier * arrow * m.V"Exp";
Grammar = m.Cf(m.V"Definition" / firstdef * m.Cg(m.V"Definition")^0, adddef) /
m.P
mm.P
}
local pattern = S * exp / m.P * (-any + patt_error)
local pattern = S * exp / mm.P * (-any + patt_error)
function compile (p, defs)
if m.type(p) == "pattern" then return p end -- already compiled
local function compile (p, defs)
if mm.type(p) == "pattern" then return p end -- already compiled
local cp = pattern:match(p, 1, defs)
if not cp then error("incorrect pattern", 3) end
return cp
end
local mem
local fmem
local gmem
local mt = {__mode = "v"}
function match (s, p, i)
local function match (s, p, i)
local cp = mem[p]
if not cp then
cp = compile(p)
......@@ -159,58 +211,38 @@ function match (s, p, i)
return cp:match(s, i or 1)
end
function find (s, p, i)
local function find (s, p, i)
local cp = fmem[p]
if not cp then
cp = compile(p)
cp = m.P{ m.Cp() * cp + 1 * m.V(1) }
cp = mm.P{ mm.Cp() * cp + 1 * mm.V(1) }
fmem[p] = cp
end
return cp:match(s, i or 1)
end
function gsub (s, p, rep)
gmem[p] = gmem[p] or {}
local cp = gmem[p][rep]
local function gsub (s, p, rep)
local g = gmem[p] or {} -- ensure gmem[p] is not collected while here
gmem[p] = g
local cp = g[rep]
if not cp then
cp = compile(p)
cp = m.Cs((cp / rep + 1)^0)
gmem[p][rep] = cp
cp = mm.Cs((cp / rep + 1)^0)
g[rep] = cp
end
return cp:match(s)
end
function updatelocale ()
m.locale(Predef)
Predef.a = Predef.alpha
Predef.c = Predef.cntrl
Predef.d = Predef.digit
Predef.g = Predef.graph
Predef.l = Predef.lower
Predef.p = Predef.punct
Predef.s = Predef.space
Predef.u = Predef.upper
Predef.w = Predef.alnum
Predef.x = Predef.xdigit
Predef.A = any - Predef.a
Predef.C = any - Predef.c
Predef.D = any - Predef.d
Predef.G = any - Predef.g
Predef.L = any - Predef.l
Predef.P = any - Predef.p
Predef.S = any - Predef.s
Predef.U = any - Predef.u
Predef.W = any - Predef.w
Predef.X = any - Predef.x
mem = {} -- restart memoization
fmem = {}
gmem = {}
_G.setmetatable(mem, mt)
_G.setmetatable(fmem, mt)
_G.setmetatable(gmem, mt)
end
-- exported names
local re = {
compile = compile,
match = match,
find = find,
gsub = gsub,
updatelocale = updatelocale,
}
updatelocale()
if version == "Lua 5.1" then _G.re = re end
return re
This diff is collapsed.
/*
** $Id: lpeg.h,v 1.1 2009/12/23 16:15:36 roberto Exp $
** LPeg - PEG pattern matching for Lua
** Copyright 2009, Lua.org & PUC-Rio (see 'lpeg.html' for license)
** written by Roberto Ierusalimschy
*/
#ifndef lpeg_h
#define lpeg_h
#include "lua.h"
#define KEYNEWPATT "lpeg.newpf"
/*
** type of extension functions that define new "patterns" for LPEG
** It should return the new current position or NULL if match fails
*/
typedef const char *(*PattFunc) (const char *s, /* current position */
const char *e, /* string end */
const char *o, /* string start */
const void *ud); /* user data */
/*
** function to create new patterns based on 'PattFunc' functions.
** This function is available at *registry[KEYNEWPATT]. (Notice
** the extra indirection; the userdata at the registry points to
** a variable that points to the function. In ANSI C a void* cannot
** point to a function.)
*/
typedef void (*Newpf) (lua_State *L,
PattFunc f, /* pattern */
const void *ud, /* (user) data to be passed to 'f' */
size_t l); /* size of data to be passed to 'f' */
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment