1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 14:27:59 +01:00

Add named groups (a la Python) to util.regexp.

This commit is contained in:
Kris Maglione
2011-01-12 12:25:46 -05:00
parent d6e22872d3
commit c3a90cf2b3
10 changed files with 68 additions and 51 deletions

View File

@@ -1049,18 +1049,16 @@ var Commands = Module("commands", {
validName: Class.memoize(function () RegExp("^" + this.nameRegexp.source + "$")), validName: Class.memoize(function () RegExp("^" + this.nameRegexp.source + "$")),
CommandMatch: Struct("match", "spec", "prespace", "count", "cmd", "bang", "space", "args"),
commandRegexp: Class.memoize(function () util.regexp(<![CDATA[ commandRegexp: Class.memoize(function () util.regexp(<![CDATA[
^ ^
( (?P<spec>
([:\s]*) (?P<prespace> [:\s]*)
( (?:\d+ | %)? ) (?P<count> (?:\d+ | %)? )
( (?:<name> | !)? ) (?P<cmd> (?:<name> | !)? )
(!?) (?P<bang> !?)
(\s*) (?P<space> \s*)
) )
( (?P<args>
(?:. | \n)*? (?:. | \n)*?
)? )?
$ $
@@ -1229,7 +1227,6 @@ var Commands = Module("commands", {
let match = commands.commandRegexp.exec(args.commandString); let match = commands.commandRegexp.exec(args.commandString);
if (!match) if (!match)
return; return;
match = commands.CommandMatch.apply(null, match);
context.advance(match.prespace.length + match.count.length); context.advance(match.prespace.length + match.count.length);
if (!(match.bang || match.space)) { if (!(match.bang || match.space)) {

View File

@@ -51,7 +51,7 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
autocommands.trigger("Leave", {}); autocommands.trigger("Leave", {});
}, },
observe: { observers: {
"dactyl-cleanup": function () { "dactyl-cleanup": function () {
let modules = dactyl.modules; let modules = dactyl.modules;

View File

@@ -126,7 +126,7 @@ var Marks = Module("marks", {
let items = array(util.range(0, sh.count)); let items = array(util.range(0, sh.count));
let a = items.slice(0, sh.index).reverse(); let a = items.slice(0, sh.index).reverse();
let b = items.slice(sh.index, sh.count); let b = items.slice(sh.index);
a.length = b.length = Math.max(a.length, b.length); a.length = b.length = Math.max(a.length, b.length);
items = array(a).zip(b).flatten().compact(); items = array(a).zip(b).flatten().compact();

View File

@@ -38,13 +38,13 @@ var StatusLine = Module("statusline", {
let prepend = <e4x xmlns={XUL} xmlns:dactyl={NS}> let prepend = <e4x xmlns={XUL} xmlns:dactyl={NS}>
<statusbar id="status-bar" highlight="StatusLine"> <statusbar id="status-bar" highlight="StatusLine">
<!-- insertbefore="dactyl.statusBefore;" insertafter="dactyl.statusAfter;" --> <!-- insertbefore="dactyl.statusBefore;" insertafter="dactyl.statusAfter;" -->
<hbox key="container" hidden="false" align="center" flex="1"> <hbox key="container" hidden="false" align="center" flex="1">
<stack orient="horizontal" align="stretch" flex="1" class="dactyl-container" highlight="CmdLine StatusCmdLine"> <stack orient="horizontal" align="stretch" flex="1" highlight="CmdLine StatusCmdLine" class="dactyl-container">
<hbox class="dactyl-container" highlight="CmdLine StatusCmdLine"> <hbox highlight="CmdLine StatusCmdLine" class="dactyl-container">
<label key="mode" crop="end" class="plain" collapsed="true"/> <label key="mode" crop="end" class="plain" collapsed="true"/>
<stack flex="1" class="dactyl-container" highlight="CmdLine StatusCmdLine"> <stack flex="1" highlight="CmdLine StatusCmdLine" class="dactyl-container">
<textbox key="url" crop="end" flex="1" class="plain dactyl-status-field-url" readonly="true"/> <textbox key="url" crop="end" flex="1" class="plain dactyl-status-field-url" readonly="true"/>
<textbox key="message" crop="end" flex="1" class="plain" highlight="Normal StatusNormal" readonly="true"/> <textbox key="message" crop="end" flex="1" highlight="Normal StatusNormal" class="plain" readonly="true"/>
</stack> </stack>
</hbox> </hbox>

View File

@@ -212,11 +212,11 @@ var Highlights = Module("Highlight", {
sheetRegexp: util.regexp(<![CDATA[ sheetRegexp: util.regexp(<![CDATA[
^\s* ^\s*
!? \*? !? \*?
( (?:[^;\s]|\s[^;\s])+ ) (?P<group> (?:[^;\s]|\s[^;\s])+ )
(?:; ( (?:[^;\s]|\s[^;\s])+ )? )? (?:; (?P<selector> (?:[^;\s]|\s[^;\s])+ )? )?
(?:; ( (?:[^;\s]|\s[^;\s])+ )? )? (?:; (?P<sites> (?:[^;\s]|\s[^;\s])+ )? )?
(?:; ( (?:[^;\s]|\s[^;\s])+ )? )? (?:; (?P<extends> (?:[^;\s]|\s[^;\s])+ )? )?
\s* (.*) \s* (?P<css> .*)
$ $
]]>), ]]>),

View File

@@ -929,22 +929,25 @@ unlet s:cpo_save
completion.addUrlCompleter("f", "Local files", function (context, full) { completion.addUrlCompleter("f", "Local files", function (context, full) {
let match = util.regexp(<![CDATA[ let match = util.regexp(<![CDATA[
^ ^
( (?P<prefix>
((chrome|resource):\/\/) (?P<proto>
(?P<scheme> chrome|resource)
:\/\/
)
[^\/]* [^\/]*
) )
(\/[^\/]*)? (?P<path> \/[^\/]* )?
$ $
]]>).exec(context.filter); ]]>).exec(context.filter);
if (match) { if (match) {
if (!match[4]) { if (!match.path) {
context.key = match[2]; context.key = match.proto;
context.advance(match[2].length); context.advance(match.proto.length);
context.generate = function () util.chromePackages.map(function (p) [p, match[2] + p + "/"]); context.generate = function () util.chromePackages.map(function (p) [p, match.proto + p + "/"]);
} }
else if (match[3] === "chrome") { else if (match.scheme === "chrome") {
context.key = match[1]; context.key = match.prefix;
context.advance(match[1].length + 1); context.advance(match.prefix.length + 1);
context.generate = function () iter({ context.generate = function () iter({
content: "Chrome content", content: "Chrome content",
locale: "Locale-specific content", locale: "Locale-specific content",
@@ -952,7 +955,7 @@ unlet s:cpo_save
}); });
} }
} }
if (!match || match[3] === "resource" && match[4]) if (!match || match.scheme === "resource" && match.path)
if (/^(\.{0,2}|~)\/|^file:/.test(context.filter) || util.getFile(context.filter) || io.isJarURL(context.filter)) if (/^(\.{0,2}|~)\/|^file:/.test(context.filter) || util.getFile(context.filter) || io.isJarURL(context.filter))
completion.file(context, full); completion.file(context, full);
}); });

View File

@@ -33,7 +33,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
this.branch.removeObserver("", this); this.branch.removeObserver("", this);
}, },
observe: { observers: {
"nsPref:changed": function (subject, data) { "nsPref:changed": function (subject, data) {
let observers = this._observers[data]; let observers = this._observers[data];
if (observers) { if (observers) {

View File

@@ -228,7 +228,7 @@ var Sanitizer = Module("sanitizer", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakRef
Class.objectGlobal(params.action)); Class.objectGlobal(params.action));
}, },
observe: { observers: {
"browser:purge-domain-data": function (subject, host) { "browser:purge-domain-data": function (subject, host) {
storage.fireEvent("sanitize", "domain", host); storage.fireEvent("sanitize", "domain", host);
// If we're sanitizing, our own sanitization functions will already // If we're sanitizing, our own sanitization functions will already

View File

@@ -214,6 +214,7 @@ var Hive = Class("Hive", {
}, },
}); });
try {
/** /**
* Manages named and unnamed user style sheets, which apply to both * Manages named and unnamed user style sheets, which apply to both
* chrome and content pages. * chrome and content pages.
@@ -330,16 +331,15 @@ var Styles = Module("Styles", {
let match, i = 0; let match, i = 0;
while ((!match || match[0]) && (match = Styles.propertyPattern.exec(str))) while ((!match || match[0]) && (match = Styles.propertyPattern.exec(str)))
if (always && !i++ || match[0] && match[3]) if (always && !i++ || match[0] && match[3])
yield this.Property.fromArray(match); yield match;
}, },
Property: Struct("whole", "preSpace", "name", "value", "postSpace"),
propertyPattern: util.regexp(<![CDATA[ propertyPattern: util.regexp(<![CDATA[
(?: (?:
(<space>*) (?P<preSpace> <space>*)
([-a-z]*) (?P<name> [-a-z]*)
(?: (?:
<space>* : \s* ( <space>* : \s* (?P<value>
(?: (?:
[-\w] [-\w]
(?: (?:
@@ -355,7 +355,7 @@ var Styles = Module("Styles", {
) )
)? )?
) )
(<space>* (?: ; | $) ) (?P<postSpace> <space>* (?: ; | $) )
]]>, "gi", ]]>, "gi",
{ {
space: /(?: \s | \/\* .*? \*\/ )/, space: /(?: \s | \/\* .*? \*\/ )/,
@@ -550,6 +550,6 @@ var Styles = Module("Styles", {
endModule(); endModule();
// catch(e){dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack);} } catch(e){dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack);}
// vim: set fdm=marker sw=4 ts=4 et ft=javascript: // vim: set fdm=marker sw=4 ts=4 et ft=javascript:

View File

@@ -93,11 +93,11 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
* @param {object} obj * @param {object} obj
*/ */
addObserver: function (obj) { addObserver: function (obj) {
let observers = obj._observe || obj.observe; if (!obj.observers)
obj._observe = observers; obj.observers = obj.observe;
function register(meth) { function register(meth) {
for (let target in set(["dactyl-cleanup-modules", "quit-application"].concat(Object.keys(observers)))) for (let target in set(["dactyl-cleanup-modules", "quit-application"].concat(Object.keys(obj.observers))))
try { try {
services.observer[meth](obj, target, true); services.observer[meth](obj, target, true);
} }
@@ -109,8 +109,8 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
try { try {
if (target == "quit-application" || target == "dactyl-cleanup-modules") if (target == "quit-application" || target == "dactyl-cleanup-modules")
register("removeObserver"); register("removeObserver");
if (observers[target]) if (obj.observers[target])
observers[target].call(obj, subject, data); obj.observers[target].call(obj, subject, data);
} }
catch (e) { catch (e) {
util.reportError(e); util.reportError(e);
@@ -948,7 +948,7 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
return color ? string : [s for each (s in string)].join(""); return color ? string : [s for each (s in string)].join("");
}, },
observe: { observers: {
"dactyl-cleanup-modules": function () { "dactyl-cleanup-modules": function () {
defineModule.loadLog.push("dactyl: util: observe: dactyl-cleanup-modules"); defineModule.loadLog.push("dactyl: util: observe: dactyl-cleanup-modules");
@@ -1219,14 +1219,31 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
regexp: update(function (expr, flags, tokens) { regexp: update(function (expr, flags, tokens) {
if (isinstance(expr, ["RegExp"])) if (isinstance(expr, ["RegExp"]))
expr = expr.source; expr = expr.source;
if (tokens) if (tokens)
expr = String.replace(expr, /<(\w+)>/g, function (m, n1) set.has(tokens, n1) ? tokens[n1].source || tokens[n1] : m); expr = String.replace(expr, /<(\w+)>/g, function (m, n1) set.has(tokens, n1) ? tokens[n1].source || tokens[n1] : m);
expr = String.replace(expr, /\/\/[^\n]*|\/\*[^]*?\*\//gm, "") expr = String.replace(expr, /\/\/[^\n]*|\/\*[^]*?\*\//gm, "")
.replace(/\s+/g, ""); .replace(/\s+/g, "");
return update(RegExp(expr, flags), {
if (/\(\?P</.test(expr)) {
let groups = ["wholeMatch"];
expr = expr.replace(/((?:[^[(\\]|\\.|\[(?:[^\]]|\\.)*\])*)\((?:\?P<([^>]+)>|(\?))?/gy,
function (m0, m1, m2, m3) {
if (!m3)
groups.push(m2 || "-group-" + groups.length);
return m1 + "(" + (m3 || "");
});
var struct = Struct.apply(null, groups);
}
let res = update(RegExp(expr, flags), {
closure: Class.Property(Object.getOwnPropertyDescriptor(Class.prototype, "closure")), closure: Class.Property(Object.getOwnPropertyDescriptor(Class.prototype, "closure")),
dactylPropertyNames: ["exec", "match", "test", "toSource", "toString", "global", "ignoreCase", "lastIndex", "multiLine", "source", "sticky"] dactylPropertyNames: ["exec", "match", "test", "toSource", "toString", "global", "ignoreCase", "lastIndex", "multiLine", "source", "sticky"]
}); });
if (struct)
update(res, { exec: function exec() struct.fromArray(exec.superapply(this, arguments)) });
return res;
}, { }, {
/** /**
* Escapes Regular Expression special characters in *str*. * Escapes Regular Expression special characters in *str*.