Created
August 22, 2011 22:02
-
-
Save fitzgen/1163737 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 || '') + "]"; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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