Skip to content

Instantly share code, notes, and snippets.

@fitzgen
Created August 22, 2011 22:02
Show Gist options
  • Save fitzgen/1163737 to your computer and use it in GitHub Desktop.
Save fitzgen/1163737 to your computer and use it in GitHub Desktop.
# DSL in grammar.coffee
o = (patternString, action, options) ->
patternString = patternString.replace /\s{2,}/g, ' '
return [patternString, '$$ = $1;', options] unless action
action = if match = unwrap.exec action then match[1] else "(#{action}())"
action = action.replace /\bnew /g, '$&yy.'
action = action.replace /\b(?:Block\.wrap|extend)\b/g, 'yy.$&'
action = action.replace /(this\.\$)/g, '@'
console.log action
[patternString, "$$ = #{action};", options]
// Compiled DSL in grammar.js
o = function(patternString, action, options) {
var match;
patternString = patternString.replace(/\s{2,}/g, ' ');
if (!action) {
return [patternString, '$$ = $1;', options];
}
action = (match = unwrap.exec(action)) ? match[1] : "(" + action + "())";
action = action.replace(/\bnew /g, '$&yy.');
action = action.replace(/\b(?:Block\.wrap|extend)\b/g, 'yy.$&');
action = action.replace(/(this\.\$)/g, '@');
console.log(action);
return [patternString, "$$ = " + action + ";", options];
};
// Relevant @ transformation from jison
action = action.replace(/([^'"])\$\$|^\$\$/g, '$1this.$').replace(/@[0$]/g, "this._$")
.replace(/\$(\d+)/g, function (_, n) {
return "$$[$0" + (n - rhs.length || '') + "]";
})
.replace(/@(\d+)/g, function (_, n) {
return "_$[$0" + (n - rhs.length || '') + "]";
});
// Example of how the @ transformation isn't looking right in parser.js
case 31:this.$ = new yy.Literal(this._$.first_line, this._$.first_column, $$[$0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment