Skip to content

Commit

Permalink
ir: add ^ to the start of regexps
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Jan 29, 2013
1 parent 3cfe160 commit 79acfa2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
7 changes: 6 additions & 1 deletion lib/ometajs/compiler/ir.js
Expand Up @@ -307,7 +307,12 @@ IR.prototype.render = function render(options) {
break;
case 'seq':
case 're':
buf.push('this._seq(', ast[1], ')');
if (/^\/\^/.test(ast[1])) {
buf.push('this._seq(', ast[1], ')');
} else {
var re = ast[1].replace(/^\/(.*)\/(\w*)$/, '/^($1)/$2');
buf.push('this._seq(', re, ')');
}
break;
case 'optional':
buf.push('this._', ast[0], '(function() {return ');
Expand Down
16 changes: 8 additions & 8 deletions lib/ometajs/grammars/bsjs.js
Expand Up @@ -29,15 +29,15 @@ BSJSParser.prototype["space"] = function $space() {
};

BSJSParser.prototype["nameFirst"] = function $nameFirst() {
return this._seq(/^[a-z$_]/i);
return this._seq(/^([a-z$_])/i);
};

BSJSParser.prototype["nameLast"] = function $nameLast() {
return this._seq(/^[a-z0-9$_]/i);
return this._seq(/^([a-z0-9$_])/i);
};

BSJSParser.prototype["iName"] = function $iName() {
return this._seq(/^[a-z$_][a-z0-9$_]*/i);
return this._seq(/^([a-z$_][a-z0-9$_]*)/i);
};

BSJSParser.prototype["isKeyword"] = function $isKeyword() {
Expand Down Expand Up @@ -74,10 +74,10 @@ BSJSParser.prototype["hexLit"] = function $hexLit() {
BSJSParser.prototype["number"] = function $number() {
return this._atomic(function() {
var n;
return this._seq(/^0x[0-9a-f]+/) && (n = this._getIntermediate(), true) && this._exec([ "number", parseInt(n) ]);
return this._seq(/^(0x[0-9a-f]+)/) && (n = this._getIntermediate(), true) && this._exec([ "number", parseInt(n) ]);
}) || this._atomic(function() {
var f;
return this._seq(/^\d+((\.|[eE][\-+]?)\d+)?/) && (f = this._getIntermediate(), true) && this._exec([ "number", parseFloat(f) ]);
return this._seq(/^(\d+((\.|[eE][\-+]?)\d+)?)/) && (f = this._getIntermediate(), true) && this._exec([ "number", parseFloat(f) ]);
});
};

Expand Down Expand Up @@ -125,21 +125,21 @@ BSJSParser.prototype["escapeChar"] = function $escapeChar() {
BSJSParser.prototype["str"] = function $str() {
return this._atomic(function() {
var s;
return this._seq(/^'([^'\\]|\\.)*'/) && (s = this._getIntermediate(), true) && this._exec(function() {
return this._seq(/^('([^'\\]|\\.)*')/) && (s = this._getIntermediate(), true) && this._exec(function() {
function swap(quote) {
return quote === '"' ? "'" : '"';
}
return [ "string", JSON.parse(preparseString(s.replace(/["']/g, swap))).replace(/["']/g, swap) ];
}.call(this));
}) || this._atomic(function() {
var s;
return this._seq(/^"([^"\\]|\\.)*"/) && (s = this._getIntermediate(), true) && this._exec([ "string", JSON.parse(preparseString(s)) ]);
return this._seq(/^("([^"\\]|\\.)*")/) && (s = this._getIntermediate(), true) && this._exec([ "string", JSON.parse(preparseString(s)) ]);
});
};

BSJSParser.prototype["special"] = function $special() {
var s;
return this._seq(/^(>>>|<<<|!==|===|&&=|\|\|=|!=|==|>=|<=|\+\+|\+=|--|-=|\*=|\/=|%=|&&|\|\||>>|&=|\|=|\^=|[\(\){}\[\],;?:><=\+\-\*\/%&|\^~\.!])/) && (s = this._getIntermediate(), true) && this._exec([ s, s ]);
return this._seq(/^((>>>|<<<|!==|===|&&=|\|\|=|!=|==|>=|<=|\+\+|\+=|--|-=|\*=|\/=|%=|&&|\|\||>>|&=|\|=|\^=|[\(\){}\[\],;?:><=\+\-\*\/%&|\^~\.!]))/) && (s = this._getIntermediate(), true) && this._exec([ s, s ]);
};

BSJSParser.prototype["token"] = function $token() {
Expand Down
16 changes: 8 additions & 8 deletions lib/ometajs/grammars/bsjs.ometajs
@@ -1,16 +1,16 @@
ometa BSJSParser {
space = ^@space | /^\/\/[^\n]*/ | /^\/\*(.|[\r\n])*?\*\//,
nameFirst = /^[a-z$_]/i,
nameLast = /^[a-z0-9$_]/i,
iName = /^[a-z$_][a-z0-9$_]*/i,
nameFirst = /[a-z$_]/i,
nameLast = /[a-z0-9$_]/i,
iName = /[a-z$_][a-z0-9$_]*/i,
isKeyword :x = ?BSJSParser._isKeyword(x),
name = @iName:n ~isKeyword(n) -> [#name, n],
keyword = @iName:k isKeyword(k) -> [k, k],
hexDigit = @char:x {BSJSParser.hexDigits.indexOf(x.toLowerCase())}:v ?(v >= 0) -> v,
hexLit = hexLit:n hexDigit:d -> (n * 16 + d)
| hexDigit,
number = /^0x[0-9a-f]+/:n -> [#number, parseInt(n)]
| /^\d+((\.|[eE][\-+]?)\d+)?/:f -> [#number, parseFloat(f)],
number = /0x[0-9a-f]+/:n -> [#number, parseInt(n)]
| /\d+((\.|[eE][\-+]?)\d+)?/:f -> [#number, parseFloat(f)],
escapeChar = <'\\' char>:s -> {
switch (s) {
case '\\"': return '"';
Expand All @@ -28,7 +28,7 @@ ometa BSJSParser {
| 'x' hexDigit hexDigit)>:s -> {
JSON.parse('"' + s + '"')
},
str = /^'([^'\\]|\\.)*'/:s -> {
str = /'([^'\\]|\\.)*'/:s -> {
function swap(quote) {
return quote === '"' ? '\'' : '"';
}
Expand All @@ -38,8 +38,8 @@ ometa BSJSParser {
.replace(/["']/g, swap)
];
}
| /^"([^"\\]|\\.)*"/:s -> [#string, JSON.parse(preparseString(s))],
special = /^(>>>|<<<|!==|===|&&=|\|\|=|!=|==|>=|<=|\+\+|\+=|--|-=|\*=|\/=|%=|&&|\|\||>>|&=|\|=|\^=|[\(\){}\[\],;?:><=\+\-\*\/%&|\^~\.!])/:s -> [s, s],
| /"([^"\\]|\\.)*"/:s -> [#string, JSON.parse(preparseString(s))],
special = /(>>>|<<<|!==|===|&&=|\|\|=|!=|==|>=|<=|\+\+|\+=|--|-=|\*=|\/=|%=|&&|\|\||>>|&=|\|=|\^=|[\(\){}\[\],;?:><=\+\-\*\/%&|\^~\.!])/:s -> [s, s],
token = @spaces (@name | @keyword | @number | @str | @special),
toks = @token*:ts @spaces end -> ts,
spacesNoNl = (~'\n' space)*,
Expand Down

0 comments on commit 79acfa2

Please sign in to comment.