-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic Arrow function support + array methods #123
Closed
Closed
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
967d540
- Add basic arrow function support: () => expr
6utt3rfly 4f0b52a
Moved the []. check from gobbleExpression to gobbleToken() so that it…
6utt3rfly 6c1a81f
Merge branch 'master' into basic-arrow
6utt3rfly 7808f67
Add optional expression parsers. Default parsers for ternary and arro…
6utt3rfly 0e5b7d6
Merge branch 'master' into basic-arrow
6utt3rfly d3e6bdb
JSEP hooks
6utt3rfly e864f77
- Fix additional_identifier_chars with `_`
6utt3rfly 220b031
- Fix space/tab. Added plugins to `lint` script
6utt3rfly 1a17a8d
Merge branch 'master' into basic-arrow
6utt3rfly File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
LITERAL = 'Literal', | ||
THIS_EXP = 'ThisExpression', | ||
CALL_EXP = 'CallExpression', | ||
ARROW_EXP = 'ArrowFunctionExpression', | ||
UNARY_EXP = 'UnaryExpression', | ||
BINARY_EXP = 'BinaryExpression', | ||
LOGICAL_EXP = 'LogicalExpression', | ||
|
@@ -33,6 +34,8 @@ | |
QUMARK_CODE = 63, // ? | ||
SEMCOL_CODE = 59, // ; | ||
COLON_CODE = 58, // : | ||
EQUAL_CODE = 61, // = | ||
GTHAN_CODE = 62, // > | ||
|
||
throwError = function(message, index) { | ||
var error = new Error(message + ' at character ' + index); | ||
|
@@ -62,6 +65,47 @@ | |
}, | ||
// Additional valid identifier chars, apart from a-z, A-Z and 0-9 (except on the starting char) | ||
additional_identifier_chars = {'$': t, '_': t}, | ||
// Expression Parsers, keyed by starting character. See ExpressionParser | ||
// { [key: string]: (node) => node } | ||
expression_parsers = { | ||
'?': function ternary(test) { | ||
// Ternary expression: test ? consequent : alternate | ||
var consequent, alternate; | ||
consequent = this.gobbleExpression(); | ||
if (!consequent) { | ||
throwError('Expected expression', this.index); | ||
} | ||
this.gobbleSpaces(); | ||
if (this.exprICode(this.index) === COLON_CODE) { | ||
this.index++; | ||
alternate = this.gobbleExpression(); | ||
if (!alternate) { | ||
throwError('Expected expression', this.index); | ||
} | ||
return { | ||
type: CONDITIONAL_EXP, | ||
test: test, | ||
consequent: consequent, | ||
alternate: alternate | ||
}; | ||
} else { | ||
this.throwError('Expected :', this.index); | ||
} | ||
}, | ||
'=': function arrow(node) { | ||
// arrow expression: () => expr | ||
if (this.exprICode(this.index) === GTHAN_CODE) { | ||
this.index++; | ||
return { | ||
type: ARROW_EXP, | ||
params: node ? [node] : null, | ||
body: this.gobbleExpression(), | ||
}; | ||
} else { | ||
this.throwError('Expected >', this.index); | ||
} | ||
}, | ||
}, | ||
// Get return the longest key length of any object | ||
getMaxKeyLen = function(obj) { | ||
var max_len = 0, len; | ||
|
@@ -139,35 +183,16 @@ | |
} | ||
}, | ||
|
||
// The main parsing function. Much of this code is dedicated to ternary expressions | ||
// The main parsing function | ||
gobbleExpression = function() { | ||
var test = gobbleBinaryExpression(), | ||
consequent, alternate; | ||
ch; | ||
gobbleSpaces(); | ||
|
||
if(exprICode(index) === QUMARK_CODE) { | ||
// Ternary expression: test ? consequent : alternate | ||
ch = exprI(index); | ||
if (expression_parsers.hasOwnProperty(ch)) { | ||
index++; | ||
consequent = gobbleExpression(); | ||
if(!consequent) { | ||
throwError('Expected expression', index); | ||
} | ||
gobbleSpaces(); | ||
if(exprICode(index) === COLON_CODE) { | ||
index++; | ||
alternate = gobbleExpression(); | ||
if(!alternate) { | ||
throwError('Expected expression', index); | ||
} | ||
return { | ||
type: CONDITIONAL_EXP, | ||
test: test, | ||
consequent: consequent, | ||
alternate: alternate | ||
}; | ||
} else { | ||
throwError('Expected :', index); | ||
} | ||
return expression_parsers[ch].bind(expression_parser_scope)(test, expression_parser_scope); | ||
} else { | ||
return test; | ||
} | ||
|
@@ -530,11 +555,22 @@ | |
// then the expression probably doesn't have a `)` | ||
gobbleGroup = function() { | ||
index++; | ||
var node = gobbleExpression(); | ||
var node = gobbleExpression(), params; | ||
gobbleSpaces(); | ||
if(exprICode(index) === CPAREN_CODE) { | ||
index++; | ||
return node; | ||
} else if (exprICode(index) === COMMA_CODE) { | ||
// arrow function arguments: | ||
index++; | ||
params = gobbleArguments(CPAREN_CODE); | ||
params.unshift(node); | ||
node = gobbleExpression(); | ||
if (!node || node.type !== ARROW_EXP) { | ||
throwError('Unclosed (', index); | ||
} | ||
node.params = params; | ||
return node; | ||
} else { | ||
throwError('Unclosed (', index); | ||
} | ||
|
@@ -551,6 +587,16 @@ | |
}; | ||
}, | ||
|
||
expression_parser_scope = { | ||
get index() { return index; }, | ||
set index(v) { index = v; }, | ||
exprI: exprI, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be useful to include all methods here? Too bad it feels like code duplication though... |
||
exprICode: exprICode, | ||
gobbleSpaces: gobbleSpaces, | ||
gobbleExpression: gobbleExpression, | ||
throwError: throwError, | ||
}, | ||
|
||
nodes = [], ch_i, node; | ||
|
||
while(index < length) { | ||
|
@@ -618,6 +664,24 @@ | |
additional_identifier_chars[char] = t; return this; | ||
}; | ||
|
||
/** | ||
* This callback is an expression parser | ||
* @callback ExpressionParser | ||
* @this {{ index: number, exprI: function, exprICode: function, gobbleSpaces: function, gobbleExpression: function, throwError: function}} | ||
* @param {*} node | ||
* @param {*} thisAsArg - same as @this | ||
* @returns {*} node | ||
*/ | ||
/** | ||
* @method jsep.addExpressionParser | ||
* @param {string} char The additional character to treat as the start of an expression | ||
* @param {ExpressionParser} fn | ||
* @return jsep | ||
*/ | ||
jsep.addExpressionParser = function(char, fn) { | ||
expression_parsers[char] = fn; return this; | ||
}; | ||
|
||
/** | ||
* @method jsep.addLiteral | ||
* @param {string} literal_name The name of the literal to add | ||
|
@@ -663,6 +727,24 @@ | |
return this; | ||
}; | ||
|
||
/** | ||
* @method jsep.removeExpressionParser | ||
* @param {string} char The character to stop treating as a valid part of an expression | ||
* @return jsep | ||
*/ | ||
jsep.removeExpressionParser = function(char) { | ||
delete expression_parsers[char]; | ||
return this; | ||
}; | ||
|
||
/** | ||
* @method jsep.removeAllExpressionParsers | ||
* @return jsep | ||
*/ | ||
jsep.removeAllExpressionParsers = function() { | ||
expression_parsers = {}; | ||
return this; | ||
}; | ||
|
||
/** | ||
* @method jsep.removeBinaryOp | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EQUAL_CODE
is unused.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I debated using a Map for the
expression_parsers
versus an object which has to be keyed by strings? And wasn't sure if the arrow function should be included by default or described in the readme to be added if desired?