1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 12:17: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 + "$")),
CommandMatch: Struct("match", "spec", "prespace", "count", "cmd", "bang", "space", "args"),
commandRegexp: Class.memoize(function () util.regexp(<![CDATA[
^
(
([:\s]*)
( (?:\d+ | %)? )
( (?:<name> | !)? )
(!?)
(\s*)
(?P<spec>
(?P<prespace> [:\s]*)
(?P<count> (?:\d+ | %)? )
(?P<cmd> (?:<name> | !)? )
(?P<bang> !?)
(?P<space> \s*)
)
(
(?P<args>
(?:. | \n)*?
)?
$
@@ -1229,7 +1227,6 @@ var Commands = Module("commands", {
let match = commands.commandRegexp.exec(args.commandString);
if (!match)
return;
match = commands.CommandMatch.apply(null, match);
context.advance(match.prespace.length + match.count.length);
if (!(match.bang || match.space)) {

View File

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

View File

@@ -126,7 +126,7 @@ var Marks = Module("marks", {
let items = array(util.range(0, sh.count));
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);
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}>
<statusbar id="status-bar" highlight="StatusLine">
<!-- insertbefore="dactyl.statusBefore;" insertafter="dactyl.statusAfter;" -->
<hbox key="container" hidden="false" align="center" flex="1">
<stack orient="horizontal" align="stretch" flex="1" class="dactyl-container" highlight="CmdLine StatusCmdLine">
<hbox class="dactyl-container" highlight="CmdLine StatusCmdLine">
<label key="mode" crop="end" class="plain" collapsed="true"/>
<stack flex="1" class="dactyl-container" highlight="CmdLine StatusCmdLine">
<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"/>
<hbox key="container" hidden="false" align="center" flex="1">
<stack orient="horizontal" align="stretch" flex="1" highlight="CmdLine StatusCmdLine" class="dactyl-container">
<hbox highlight="CmdLine StatusCmdLine" class="dactyl-container">
<label key="mode" crop="end" class="plain" collapsed="true"/>
<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="message" crop="end" flex="1" highlight="Normal StatusNormal" class="plain" readonly="true"/>
</stack>
</hbox>

View File

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

View File

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

View File

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

View File

@@ -228,7 +228,7 @@ var Sanitizer = Module("sanitizer", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakRef
Class.objectGlobal(params.action));
},
observe: {
observers: {
"browser:purge-domain-data": function (subject, host) {
storage.fireEvent("sanitize", "domain", host);
// 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
* chrome and content pages.
@@ -330,16 +331,15 @@ var Styles = Module("Styles", {
let match, i = 0;
while ((!match || match[0]) && (match = Styles.propertyPattern.exec(str)))
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[
(?:
(<space>*)
([-a-z]*)
(?P<preSpace> <space>*)
(?P<name> [-a-z]*)
(?:
<space>* : \s* (
<space>* : \s* (?P<value>
(?:
[-\w]
(?:
@@ -355,7 +355,7 @@ var Styles = Module("Styles", {
)
)?
)
(<space>* (?: ; | $) )
(?P<postSpace> <space>* (?: ; | $) )
]]>, "gi",
{
space: /(?: \s | \/\* .*? \*\/ )/,
@@ -550,6 +550,6 @@ var Styles = Module("Styles", {
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:

View File

@@ -93,11 +93,11 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
* @param {object} obj
*/
addObserver: function (obj) {
let observers = obj._observe || obj.observe;
obj._observe = observers;
if (!obj.observers)
obj.observers = obj.observe;
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 {
services.observer[meth](obj, target, true);
}
@@ -109,8 +109,8 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
try {
if (target == "quit-application" || target == "dactyl-cleanup-modules")
register("removeObserver");
if (observers[target])
observers[target].call(obj, subject, data);
if (obj.observers[target])
obj.observers[target].call(obj, subject, data);
}
catch (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("");
},
observe: {
observers: {
"dactyl-cleanup-modules": function () {
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) {
if (isinstance(expr, ["RegExp"]))
expr = expr.source;
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, /\/\/[^\n]*|\/\*[^]*?\*\//gm, "")
.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")),
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*.