Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add proper regexps to the language

  • Loading branch information...
commit a2abe820c125cc91f6cc4c249dd24968ea602aff 1 parent 2d99b30
Scott Taylor authored
1  lib/loop/eval.js
View
@@ -110,6 +110,7 @@ var loopEval = function(syntaxTree) {
case 'string':
case 'number':
case 'comment':
+ case 'regexp':
return syntaxTree;
default:
throw new Error('unknown type of syntaxTree, type: ' + syntaxTree.type);
2  lib/loop/grammar.jison
View
@@ -4,6 +4,7 @@
";".* return "COMMENT";
\"(\\.|[^\\"])*\" return "STRING";
\'(\\.|[^\\'])*\' return "STRING";
+\/(\\.|[^\\/])*\/([a-zA-Z]*) return "REGEXP";
\s+ /* skip whitespace */
\n+ /* skip newlines */
\r+ /* same */
@@ -94,6 +95,7 @@ atom
| float { $$ = $1; }
| INT { $$ = require("./grammar/token-builders").makeNumber($1, @1); }
| STRING { $$ = require("./grammar/token-builders").makeString($1, @1); }
+ | REGEXP { $$ = require("./grammar/token-builders").makeRegexp($1, @1); }
| SYMBOL { $$ = require("./grammar/token-builders").makeSymbol($1, @1); }
;
16 lib/loop/grammar/token-builders.js
View
@@ -47,6 +47,22 @@ tokenBuilders.makeString = function(str, sourceInfo) {
};
};
+tokenBuilders.makeRegexp = function(str, sourceInfo) {
+ str = str.slice(1);
+ var parts = str.split("/");
+ var regexPart = parts[0];
+ var modifiers = parts[1];
+
+ return {
+ type: 'regexp',
+ contents: {
+ regex: regexPart,
+ modifiers: modifiers
+ },
+ sourceInfo: sourceInfo
+ };
+};
+
tokenBuilders.makeObjectLiteral = function(listMembers, sourceInfo) {
if (listMembers) {
var list = [];
3  lib/loop/uglify.js
View
@@ -382,6 +382,8 @@ var progn = function(tree) {
return ['name', tree.contents];
case 'string':
return ['string', tree.contents];
+ case 'regexp':
+ return ['regexp', tree.contents.regex, tree.contents.modifiers];
case 'number':
return ['num', tree.contents];
case 'comment':
@@ -397,6 +399,7 @@ processStatementOrExpression = function(statementOrExpression) {
case 'string':
case 'prop-access':
case 'number':
+ case 'regexp':
return stmt(progn(statementOrExpression));
case 'comment':
return progn(statementOrExpression);
15 spec/integration/regex-spec.js
View
@@ -0,0 +1,15 @@
+var vows = require("vows");
+var assert = require("assert");
+var loop = require(__dirname + "/../../lib/loop");
+
+vows.describe("macro matching").addBatch({
+ 'it should parse a simple regexp': function() {
+ assert.equal(loop.compile('/foobar/'), '/foobar/');
+ },
+ 'it should parse a regex as a regex': function() {
+ assert.equal(loop.compile('/^WHORU\\:(.*)\\n$/'), '/^WHORU\\:(.*)\\n$/');
+ },
+ 'it should parse a regex as a regex with a modifier': function() {
+ assert.equal(loop.compile('/foobar/i'), '/foobar/i');
+ }
+}).export(module);
Please sign in to comment.
Something went wrong with that request. Please try again.