Permalink
Browse files

performance fixes

- yaap only parses function header now
- removed meld-dependencies and used native wrappers instead
  • Loading branch information...
1 parent 9c1175f commit 7271e52d9a63c7517d0560077c9fbe61a730ddfb @warmuuh committed Mar 13, 2013
View
@@ -14,7 +14,6 @@
"Peter Mucha <ptmucha@gmail.com>"
],
"dependencies": {
- "meld": "1.2.0",
"PanPG": "0.0.9",
"underscore": "1.4.4",
"when": "1.8.0",

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,250 @@
+; The entire ECMAScript language as a Parser Expression Grammar
+
+; Parser rules which share a name with ECMA-262 productions are intended to match the same language (though not necessarily with the same derivation).
+
+; for the sake of just parsing anonymous Functions, added FunctionExpression
+Program ←
+ (S? (FunctionDeclaration / FunctionExpression))* S?
+
+FunctionDeclaration ←
+ FunctionTok S? Identifier S? "(" FormalParameterList? S? ")" S?
+
+FunctionExpression ←
+ FunctionTok S? Identifier? S? "(" FormalParameterList? S? ")" S?
+
+FormalParameterList ←
+ FormalParameter ("," FormalParameter)*
+
+FormalParameter ←
+ S? Identifier
+
+
+
+
+
+
+; Lexical tokens
+
+Literal
+ ← NullLiteral
+ / BooleanLiteral
+ / NumericLiteral
+ / StringLiteral
+ / RegularExpressionLiteral
+
+NullLiteral
+ ← NullTok
+
+BooleanLiteral
+ ← TrueTok
+ / FalseTok
+
+NumericLiteral ← DecimalLiteral !(IdentifierStart)
+ / HexIntegerLiteral !(IdentifierStart)
+
+DecimalLiteral ← DecimalIntegerLiteral "." DecimalDigit* ExponentPart?
+ / "." DecimalDigit+ ExponentPart?
+ / DecimalIntegerLiteral ExponentPart?
+
+DecimalIntegerLiteral ← "0"
+ / [1-9] DecimalDigit*
+
+DecimalDigit ← [0-9]
+
+ExponentPart ← [eE] SignedInteger
+
+SignedInteger ← DecimalDigit+
+ / "+" DecimalDigit+
+ / "-" DecimalDigit+
+
+HexIntegerLiteral ← "0x" HexDigit+
+ / "0X" HexDigit+
+
+DQ ← [U+0022]
+SQ ← [U+0027]
+
+StringLiteral ← DQ DoubleStringCharacter* DQ
+ / SQ SingleStringCharacter* SQ
+
+RS ← [U+005C]
+
+DoubleStringCharacter ← !( DQ / RS / LineTerminator ) SourceCharacter
+ / RS EscapeSequence
+ / LineContinuation
+
+SingleStringCharacter ← !( SQ / RS / LineTerminator ) SourceCharacter
+ / RS EscapeSequence
+ / LineContinuation
+
+LineContinuation ← RS LineTerminatorSequence
+
+EscapeSequence ← CharacterEscapeSequence
+ / "0" !(DecimalDigit)
+ / HexEscapeSequence
+ / UnicodeEscapeSequence
+
+CharacterEscapeSequence ← SingleEscapeCharacter
+ / NonEscapeCharacter
+
+SingleEscapeCharacter ← [ U+0027 U+0022 U+005C b f n r t v ]
+
+NonEscapeCharacter ← !(EscapeCharacter / LineTerminator) SourceCharacter
+
+EscapeCharacter ← SingleEscapeCharacter
+ / DecimalDigit
+ / [ x u ]
+
+HexEscapeSequence ← "x" HexDigit HexDigit
+
+UnicodeEscapeSequence ← "u" HexDigit{4}
+
+RegularExpressionLiteral ← "/" RegularExpressionBody "/" RegularExpressionFlags
+
+RegularExpressionBody ← RegularExpressionFirstChar RegularExpressionChar*
+
+RegularExpressionFirstChar ← !( LineTerminator / [ * U+005C / [ ] ) SourceCharacter
+ / RegularExpressionBackslashSequence
+ / RegularExpressionClass
+
+RegularExpressionChar ← !( LineTerminator / [ U+005C / [ ] ) SourceCharacter
+ / RegularExpressionBackslashSequence
+ / RegularExpressionClass
+
+RegularExpressionBackslashSequence ← RS !(LineTerminator) SourceCharacter
+
+RegularExpressionClass ← "[" RegularExpressionClassChar* "]"
+
+RegularExpressionClassChar ← !(LineTerminator / [ U+005C U+005D ]) SourceCharacter
+ / RegularExpressionBackslashSequence
+
+RegularExpressionFlags ← IdentifierPart*
+
+SourceCharacter ← [ U+0000-U+10FFFF ]
+
+WhiteSpace ← [ U+0009
+ U+000B
+ U+000C
+ U+0020
+ U+0085
+ U+00A0
+ U+200B
+ U+FEFF
+ [:Zs:] ]
+
+LF ← [ U+000A ]
+CR ← [ U+000D ]
+LS ← [ U+2028 ]
+PS ← [ U+2029 ]
+
+LineTerminator ← LF / CR / LS / PS
+
+LineTerminatorSequence ← LF / CR LF / CR / LS / PS
+
+Comment ← AnnotationComment
+ / SingleLineComment
+ / MultiLineComment
+
+MultiLineComment ← "/*" (!("*/") SourceCharacter)* "*/"
+
+
+
+AnnotationComment ← "/*" SnoComment? AnnotationFunction (SnoComment? AnnotationFunction)* SnoComment? "*/"
+AnnotationFunction ← AnnotationName SnoComment? AnnotationParameterList?
+AnnotationName ← "@" Identifier
+AnnotationParameterList ← "(" SnoComment? AnnotationParameter SnoComment? ( "," SnoComment? AnnotationParameter)* SnoComment?")"
+AnnotationParameter ← StringLiteral / NumericLiteral
+
+
+MultiLineCommentNoLB ← "/*" (!("*/") !(LineTerminator) SourceCharacter)* "*/"
+
+SingleLineComment ← "//" [^ U+000A U+000D U+2028 U+2029 ]*
+
+S ← ( WhiteSpace / LineTerminatorSequence / Comment )+
+SnoComment ← ( WhiteSpace / LineTerminatorSequence )+
+
+SnoLB ← ( WhiteSpace / SingleLineComment / MultiLineCommentNoLB )+
+
+; end of statement
+EOS
+ ← S? ";"
+ / SnoLB? LineTerminatorSequence
+ / SnoLB? &("}")
+ / S? EOF
+
+; end of statement in a "no linebreak here" context
+; EOSnoLB will consume a linebreak, but it won't extend to the next line
+EOSnoLB
+ ← SnoLB? ";"
+ / SnoLB? LineTerminatorSequence
+ / SnoLB? &("}")
+ / SnoLB? EOF
+
+EOF
+ ← !(SourceCharacter)
+
+ReservedWord
+ ← ( Keyword
+ / FutureReservedWord
+ / "null"
+ / "true"
+ / "false"
+ ) !(IdentifierPart)
+
+Keyword
+ ← "break" / "case" / "catch" / "continue" / "debugger" / "default" / "delete" / "do" / "else" / "finally" / "for" / "function" / "if" / "instanceof" / "in" / "new" / "return" / "switch" / "this" / "throw" / "try" / "typeof" / "var" / "void" / "while" / "with"
+
+FutureReservedWord
+ ← "abstract" / "boolean" / "byte" / "char" / "class" / "const" / "double" / "enum" / "export" / "extends" / "final" / "float" / "goto" / "implements" / "import" / "interface" / "int" / "long" / "native" / "package" / "private" / "protected" / "public" / "short" / "static" / "super" / "synchronized" / "throws" / "transient" / "volatile"
+
+Identifier ← !(ReservedWord) IdentifierName
+
+IdentifierName ← IdentifierStart IdentifierPart*
+
+IdentifierStart ← UnicodeLetter
+ / "$"
+ / "_"
+ / RS UnicodeEscapeSequence
+
+IdentifierPart ← IdentifierStart
+ / [ [:Mn:] [:Mc:]
+ [:Nd:]
+ [:Pc:] ]
+
+UnicodeLetter ← [ [:Lu:]
+ [:Ll:]
+ [:Lt:]
+ [:Lm:]
+ [:Lo:]
+ [:Nl:] ]
+
+HexDigit ← [ 0-9 a-f A-F ]
+
+FalseTok ← "false" !(IdentifierPart)
+TrueTok ← "true" !(IdentifierPart)
+NullTok ← "null" !(IdentifierPart)
+BreakTok ← "break" !(IdentifierPart)
+ContinueTok ← "continue" !(IdentifierPart)
+DebuggerTok ← "debugger" !(IdentifierPart)
+InTok ← "in" !(IdentifierPart)
+InstanceOfTok ← "instanceof" !(IdentifierPart)
+DeleteTok ← "delete" !(IdentifierPart)
+FunctionTok ← "function" !(IdentifierPart)
+NewTok ← "new" !(IdentifierPart)
+ThisTok ← "this" !(IdentifierPart)
+TypeofTok ← "typeof" !(IdentifierPart)
+VoidTok ← "void" !(IdentifierPart)
+IfTok ← "if" !(IdentifierPart)
+ElseTok ← "else" !(IdentifierPart)
+DoTok ← "do" !(IdentifierPart)
+WhileTok ← "while" !(IdentifierPart)
+ForTok ← "for" !(IdentifierPart)
+VarTok ← "var" !(IdentifierPart)
+ReturnTok ← "return" !(IdentifierPart)
+CaseTok ← "case" !(IdentifierPart)
+DefaultTok ← "default" !(IdentifierPart)
+SwitchTok ← "switch" !(IdentifierPart)
+ThrowTok ← "throw" !(IdentifierPart)
+CatchTok ← "catch" !(IdentifierPart)
+FinallyTok ← "finally" !(IdentifierPart)
+TryTok ← "try" !(IdentifierPart)
+WithTok ← "with" !(IdentifierPart)
@@ -8,7 +8,7 @@
*
* @author Peter Mucha
*
- * @version 0.0.2
+ * @version 0.0.3
*/
"use strict";
@@ -0,0 +1,25 @@
+/** @license MIT License (c) copyright 2013 original author or authors */
+
+/**
+ * build script for the ECMA-parser used in Yaap
+ *
+ * Licensed under the MIT License at:
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ * @author Peter Mucha
+ *
+ * @version 0.0.3
+ */
+"use strict";
+
+var pg = require("./PanPG");
+var fs = require("fs");
+
+var grammar = fs.readFileSync("ECMAScript_5_strippedDown.peg").toString();
+var parser = pg.generateParser(grammar);
+
+
+parser = "(function(define) {\ndefine([],function(){\n\n" + parser + "return {Program:Program};\n\n});})(typeof define == 'function'? define: function(deps, factory) {module.exports = factory.apply(this, deps.map(function(x) {return require(x);}));});";
+
+
+fs.writeFileSync("ECMA5Parser_min.js", parser);
View
@@ -8,7 +8,7 @@
*
* @author Peter Mucha
*
- * @version 0.0.2
+ * @version 0.0.3
*/
"use strict";
(function(define) {
@@ -59,6 +59,7 @@ return {
return children;
},
FormalParameter: function(match, children){
+
children = _.flatten(children);
var paramObj = {
name: _(children).chain().pluck('name').compact().first().value(),
@@ -88,14 +89,16 @@ return {
children[i].index = i;
return {parameters: children};
- },
+ }
+
+ /*, //commented out because strippedDown-parser does not need this:
FunctionBody: function(match, children){
children = _.flatten(children);
var bodyObj = {
annotations: _(children).chain().pluck('annotations').flatten().compact().value()
};
//console.log("fn body annotations found: " + JSON.stringify(bodyObj));
- }
+ }*/
};
@@ -10,25 +10,39 @@
*
* @author Peter Mucha
*
- * @version 0.0.2
+ * @version 0.0.3
*/
"use strict";
(function(define) {
-define(["underscore", "meld"],
-function(_, meld) {
+define([],
+function() {
return {
annotation: "@Default",
processParameter: function(obj, fnObj, param, annotationParams) {
//console.log("@Default("+ annotationParams +") attached at parameter: " + param.name);
- meld.around(obj, fnObj.name, function(joinpoint){
- var args = joinpoint.args;
- if (_(args[param.index]).isNull() || _(args[param.index]).isUndefined())
- args[param.index] = annotationParams[0];
- return joinpoint.proceed();
- });
+
+ var origFn = obj[fnObj.name];
+
+ obj[fnObj.name] = function(){
+ while (arguments.length -1 < param.index)
+ [].push.call(arguments, undefined);
+
+ if (arguments[param.index] == null) //tests for null or undefined because 'null==undefined'
+ arguments[param.index] = annotationParams[0];
+
+
+ switch(arguments.length){
+ case 0: return origFn.call(obj);
+ case 1: return origFn.call(obj, arguments[0]);
+ case 2: return origFn.call(obj, arguments[0], arguments[1]);
+ case 3: return origFn.call(obj, arguments[0], arguments[1], arguments[2]);
+ default: return origFn.apply(obj, arguments);
+ }
+ };
+
}
};
Oops, something went wrong.

0 comments on commit 7271e52

Please sign in to comment.