Hi,
Just recently was working with Ruby's code. And found useful again its
(actually from Perl) "approximately equal" operator: =~
The operator is just a sugar for `test' method of RegExp.
if (/ecma/.test("ecmascript")) {
console.log("ECMAScript");
}
is sugared into:
if ("ecmascript" =~ /ecma/) {
console.log("ECMAScript");
}
Unfortunately, we can't use the same operator "=~" since this pair is
already borrowed in ES3. But we may swap them and to use "~=", which
reads even more like "approximately equals to"
if ("ecmascript" ~= /ecma/) {
console.log("ECMAScript");
}
And the other thing is "RegExp-substringing" with using bracket
notation: string[RegExp, startIndex].
"ecmascript"[/ecma/, 0]; // "ecma"
This is actually the sugar for:
"ecmascript".match(/ecma/)[0]; // "ecma"
E.g. a simple lexer:
var code = "var a = 10;"
var cursor = 0;
while (cursor < code.length) {
var chunk = code[cursor .. -1]; // sugar for slice:
code.slice(cursor, cursor.length);
if (identifier = chunk[/\A([a-z]\w*)/, 1]) {
// handle identifier token
}
else if (number = chunk[/\A([0-9]+)/, 1]) {
// handle numbers
}
etc.
}
Thoughts?
Cheers,
Dmitry.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss