mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Marko v3: parse init/test/update parts of for loop
This commit is contained in:
parent
0c87b4a91a
commit
a574a2be82
@ -82,6 +82,14 @@ function parseExpression(str, errorMessage) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseStatement(str, errorMessage) {
|
||||||
|
try {
|
||||||
|
return compiler.builder.parseStatement(str);
|
||||||
|
} catch(e) {
|
||||||
|
throwError(errorMessage + ': ' + e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function createNumberExpression(str, errorMessage) {
|
function createNumberExpression(str, errorMessage) {
|
||||||
if (str == null) {
|
if (str == null) {
|
||||||
return null;
|
return null;
|
||||||
@ -294,6 +302,18 @@ module.exports = function(str) {
|
|||||||
statusVarName = buildIdentifier(statusVarName, 'Invalid status-var option');
|
statusVarName = buildIdentifier(statusVarName, 'Invalid status-var option');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (forInit) {
|
||||||
|
forInit = parseStatement(forInit, 'Invalid for loop init');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (forTest) {
|
||||||
|
forTest = parseExpression(forTest, 'Invalid for loop test');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (forUpdate) {
|
||||||
|
forUpdate = parseExpression(forUpdate, 'Invalid for loop update');
|
||||||
|
}
|
||||||
|
|
||||||
// No more tokens... now we need to sort out what happened
|
// No more tokens... now we need to sort out what happened
|
||||||
if (loopType === 'ForEach') {
|
if (loopType === 'ForEach') {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -1,6 +1,33 @@
|
|||||||
{
|
{
|
||||||
"loopType": "For",
|
"loopType": "For",
|
||||||
"init": "",
|
"init": "",
|
||||||
"test": " i<foo.length",
|
"test": {
|
||||||
"update": " i++"
|
"type": "BinaryExpression",
|
||||||
|
"left": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"name": "i"
|
||||||
|
},
|
||||||
|
"operator": "<",
|
||||||
|
"right": {
|
||||||
|
"type": "MemberExpression",
|
||||||
|
"object": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"name": "foo"
|
||||||
|
},
|
||||||
|
"property": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"name": "length"
|
||||||
|
},
|
||||||
|
"computed": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"update": {
|
||||||
|
"type": "UpdateExpression",
|
||||||
|
"argument": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"name": "i"
|
||||||
|
},
|
||||||
|
"operator": "++",
|
||||||
|
"prefix": false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,6 +1,36 @@
|
|||||||
{
|
{
|
||||||
"loopType": "For",
|
"loopType": "For",
|
||||||
"init": "var i=0",
|
"init": {
|
||||||
"test": " i<foo.length",
|
"type": "Expression",
|
||||||
"update": " i++"
|
"value": "var i=0"
|
||||||
|
},
|
||||||
|
"test": {
|
||||||
|
"type": "BinaryExpression",
|
||||||
|
"left": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"name": "i"
|
||||||
|
},
|
||||||
|
"operator": "<",
|
||||||
|
"right": {
|
||||||
|
"type": "MemberExpression",
|
||||||
|
"object": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"name": "foo"
|
||||||
|
},
|
||||||
|
"property": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"name": "length"
|
||||||
|
},
|
||||||
|
"computed": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"update": {
|
||||||
|
"type": "UpdateExpression",
|
||||||
|
"argument": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"name": "i"
|
||||||
|
},
|
||||||
|
"operator": "++",
|
||||||
|
"prefix": false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user