diff options
author | Marius <mariausol@gmail.com> | 2012-08-16 23:40:21 +0300 |
---|---|---|
committer | Marius <mariausol@gmail.com> | 2012-08-16 23:40:21 +0300 |
commit | ecadb4b576efc36822610c9857a7ccb8967dd80a (patch) | |
tree | 2644288993eb0b1d8390df9ac3dfe78feb5de9d1 /tex/context/base/l-lpeg.lua | |
parent | e44911a5d56ff3686f7c852425ae7f9456340867 (diff) | |
download | context-ecadb4b576efc36822610c9857a7ccb8967dd80a.tar.gz |
beta 2012.08.16 22:20
Diffstat (limited to 'tex/context/base/l-lpeg.lua')
-rw-r--r-- | tex/context/base/l-lpeg.lua | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/tex/context/base/l-lpeg.lua b/tex/context/base/l-lpeg.lua index 70c5f14e1..652772d79 100644 --- a/tex/context/base/l-lpeg.lua +++ b/tex/context/base/l-lpeg.lua @@ -66,7 +66,7 @@ local report = texio and texio.write_nl or print -- function lpeg.Cmt (l) local p = lpcmt (l) report("LPEG Cmt =") lpprint(l) return p end -- function lpeg.Carg (l) local p = lpcarg(l) report("LPEG Carg =") lpprint(l) return p end -local type = type +local type, next = type, next local byte, char, gmatch, format = string.byte, string.char, string.gmatch, string.format -- Beware, we predefine a bunch of patterns here and one reason for doing so @@ -127,6 +127,10 @@ patterns.utf8char = utf8char patterns.validutf8 = validutf8char patterns.validutf8char = validutf8char +local eol = S("\n\r") +local spacer = S(" \t\f\v") -- + char(0xc2, 0xa0) if we want utf (cf mail roberto) +local whitespace = eol + spacer + patterns.digit = digit patterns.sign = sign patterns.cardinal = sign^0 * digit^1 @@ -146,16 +150,16 @@ patterns.letter = patterns.lowercase + patterns.uppercase patterns.space = space patterns.tab = P("\t") patterns.spaceortab = patterns.space + patterns.tab -patterns.eol = S("\n\r") -patterns.spacer = S(" \t\f\v") -- + char(0xc2, 0xa0) if we want utf (cf mail roberto) +patterns.eol = eol +patterns.spacer = spacer +patterns.whitespace = whitespace patterns.newline = newline patterns.emptyline = newline^1 -patterns.nonspacer = 1 - patterns.spacer -patterns.whitespace = patterns.eol + patterns.spacer -patterns.nonwhitespace = 1 - patterns.whitespace +patterns.nonspacer = 1 - spacer +patterns.nonwhitespace = 1 - whitespace patterns.equal = P("=") patterns.comma = P(",") -patterns.commaspacer = P(",") * patterns.spacer^0 +patterns.commaspacer = P(",") * spacer^0 patterns.period = P(".") patterns.colon = P(":") patterns.semicolon = P(";") @@ -387,8 +391,8 @@ end function lpeg.replacer(one,two) if type(one) == "table" then local no = #one + local p if no > 0 then - local p for i=1,no do local o = one[i] local pp = P(o[1]) / o[2] @@ -398,8 +402,17 @@ function lpeg.replacer(one,two) p = pp end end - return Cs((p + 1)^0) + else + for k, v in next, one do + local pp = P(k) / v + if p then + p = p + pp + else + p = pp + end + end end + return Cs((p + 1)^0) else two = two or "" return Cs((P(one)/two + 1)^0) @@ -793,3 +806,11 @@ local replacer = lpeg.replacer("@","%%") -- Watch the escaped % in lpeg! function string.tformat(fmt,...) return format(lpegmatch(replacer,fmt),...) end + +-- strips leading and trailing spaces and collapsed all other spaces + +local pattern = Cs(whitespace^0/"" * ((whitespace^1 * P(-1) / "") + (whitespace^1/" ") + P(1))^0) + +function string.collapsespaces(str) + return lpegmatch(pattern,str) +end |