Permalink
Browse files

refactor

  • Loading branch information...
1 parent be930bb commit bc80bfc64546ee5dad7308fb5e091bfe859c3aeb @fperrad committed Apr 22, 2012
Showing with 27 additions and 26 deletions.
  1. +16 −16 src/lua/lunokhod.lua
  2. +11 −10 src/lua/lunokhod.tp
View
@@ -89,9 +89,8 @@ local number = choice(xnumber, sequence(optional(literal'-'), int, optional(frac
local capt_number = sequence(capture(number), position)
-local gsub = function (s, patt, repl)
- patt = subst(many(choice(replace(patt, repl), any())))
- return patt:match(s)
+local function gsub (patt, repl)
+ return subst(many(choice(replace(patt, repl), any())))
end
local special = {
@@ -109,21 +108,22 @@ local special = {
}
local escape_special = sequence(literal'\\', capture(set"'\"\\/abfnrtv"))
+local gsub_escape_special = gsub(escape_special, special)
local escape_xdigit = sequence(literal'\\x', capture(sequence(xdigit, xdigit)))
+local gsub_escape_xdigit = gsub(escape_xdigit, function (s)
+ return char(tonumber(s, 16))
+ end)
local escape_decimal = sequence(literal'\\', capture(sequence(digit, optional(digit), optional(digit))))
+local gsub_escape_decimal = gsub(escape_decimal, function (s)
+ local n = tonumber(s)
+ if n >= 256 then
+ error("decimal escape too large near " .. s)
+ end
+ return char(n)
+ end)
local unescape = function(str)
- str = gsub(str, escape_decimal, function (s)
- local n = tonumber(s)
- if n >= 256 then
- error("decimal escape too large near " .. s)
- end
- return char(n)
- end)
- str = gsub(str, escape_xdigit, function (s)
- return char(tonumber(s, 16))
- end)
- return gsub(str, escape_special, special)
+ return gsub_escape_special:match(gsub_escape_xdigit:match(gsub_escape_decimal:match(str)))
end
local zap = replace(sequence(literal"\\z", some(set"\n\r")), "")
@@ -1202,8 +1202,8 @@ if fname then
local s = f:slurp()
f:close()
local code = translate(s)
- code = gsub(code, sequence(literal"_G", not_followed_by(literal'"')) , "!G")
- code = gsub(code, sequence(literal"_ENV", not_followed_by(literal'"')), "!ENV")
+ code = gsub(sequence(literal"_G", not_followed_by(literal'"')) , "!G"):match(code)
+ code = gsub(sequence(literal"_ENV", not_followed_by(literal'"')), "!ENV"):match(code)
print "; bootstrap"
print(code)
end
View
@@ -58,23 +58,24 @@
(!define exp (!call sequence (!call set "Ee") (!call optional (!call set "-+")) (!call some digit)))
(!define number (!call choice xnumber (!call sequence (!call optional (!call literal "-")) int (!call optional frac) (!call optional exp))))
(!define capt_number (!call sequence (!call capture number) position))
-(!define gsub (!lambda (s patt repl)
-(!assign patt (!call subst (!call many (!call choice (!call replace patt repl) (!call any )))))
-(!return (!callmeth patt match s))))
+(!define gsub)(!assign gsub (!lambda (patt repl)
+(!return (!call subst (!call many (!call choice (!call replace patt repl) (!call any )))))))
+
(!define special ("'": "'" "\"": "\"" "\\": "\\" "/": "/" "a": "\x07" "b": "\x08" "f": "\x0c" "n": "\x0a" "r": "\x0d" "t": "\x09" "v": "\x0b" ))
(!define escape_special (!call sequence (!call literal "\\") (!call capture (!call set "'\"\\/abfnrtv"))))
+(!define gsub_escape_special (!call gsub escape_special special))
(!define escape_xdigit (!call sequence (!call literal "\\x") (!call capture (!call sequence xdigit xdigit))))
+(!define gsub_escape_xdigit (!call gsub escape_xdigit (!lambda (s)
+(!return (!call char (!call tonumber s 16))))))
(!define escape_decimal (!call sequence (!call literal "\\") (!call capture (!call sequence digit (!call optional digit) (!call optional digit)))))
-(!define unescape (!lambda (str)
-(!assign str (!call gsub str escape_decimal (!lambda (s)
+(!define gsub_escape_decimal (!call gsub escape_decimal (!lambda (s)
(!define n (!call tonumber s))
(!if (!ge n 256)
(!do
(!call error (!concat "decimal escape too large near " s))))
(!return (!call char n)))))
-(!assign str (!call gsub str escape_xdigit (!lambda (s)
-(!return (!call char (!call tonumber s 16))))))
-(!return (!call gsub str escape_special special))))
+(!define unescape (!lambda (str)
+(!return (!callmeth gsub_escape_special match (!callmeth gsub_escape_xdigit match (!callmeth gsub_escape_decimal match str))))))
(!define zap (!call replace (!call sequence (!call literal "\\z") (!call some (!call set "\x0a\x0d"))) ""))
(!define ch_sq (!call choice zap (!call literal "\\\\") (!call literal "\\'") (!call except (!call any ) (!call literal "'") (!call range "\x00\x1f"))))
(!define ch_dq (!call choice zap (!call literal "\\\\") (!call literal "\\\"") (!call except (!call any ) (!call literal "\"") (!call range "\x00\x1f"))))
@@ -962,8 +963,8 @@
(!define s (!callmeth f slurp ))
(!callmeth f close )
(!define code (!call translate s))
-(!assign code (!call gsub code (!call sequence (!call literal "_G") (!call not_followed_by (!call literal "\""))) "!G"))
-(!assign code (!call gsub code (!call sequence (!call literal "_ENV") (!call not_followed_by (!call literal "\""))) "!ENV"))
+(!assign code (!callmeth (!call gsub (!call sequence (!call literal "_G") (!call not_followed_by (!call literal "\""))) "!G") match code))
+(!assign code (!callmeth (!call gsub (!call sequence (!call literal "_ENV") (!call not_followed_by (!call literal "\""))) "!ENV") match code))
(!call print "; bootstrap")
(!call print code)))
; end of generation

0 comments on commit bc80bfc

Please sign in to comment.