tree-sitter/spec/runtime/languages/javascript/control_flow.txt

197 lines
4.5 KiB
Text
Raw Normal View History

==========================================
multiple statements
==========================================
var x = {}, z, i = 0;
firstFunction(x)
secondFunction(x);
---
(program
(var_declaration
(var_assignment (identifier) (object))
(identifier)
(var_assignment (identifier) (number)))
(expression_statement (function_call (identifier) (identifier)))
(expression_statement (function_call (identifier) (identifier))))
==========================================
if statements
==========================================
if (isReady()) {
console.log(theData)
}
---
(if_statement (function_call (identifier))
2014-10-21 08:49:16 -07:00
(statement_block (expression_statement (function_call (member_access (identifier) (identifier)) (identifier)))))
==========================================
if-else statements
==========================================
if (theCondition) {
firstFunction();
} else {
secondFunction();
}
---
(if_statement
(identifier)
(statement_block (expression_statement (function_call (identifier))))
(statement_block (expression_statement (function_call (identifier)))))
==================================================
if-else statements with multiple conditions
==================================================
if (firstValue) {
firstFunction();
} else if (secondValue)
secondFunction();
else {
thirdFunction();
}
---
(if_statement (identifier)
(statement_block (expression_statement (function_call (identifier))))
(if_statement (identifier)
(expression_statement (function_call (identifier)))
(statement_block (expression_statement (function_call (identifier))))))
==========================================
for loops
==========================================
for (var i = 1; someCondition(i); i = next()) {
doSomething();
}
---
(for_statement
(var_declaration (var_assignment (identifier) (number)))
2014-10-21 08:49:16 -07:00
(function_call (identifier) (identifier))
(assignment (identifier) (function_call (identifier)))
(statement_block (expression_statement (function_call (identifier)))))
==========================================
for-in loops
==========================================
for (var key in someObject)
doSomething();
for (key in someObject)
doSomethingElse();
---
(program
(for_in_statement
(identifier) (identifier)
(expression_statement (function_call (identifier))))
(for_in_statement
(identifier) (identifier)
(expression_statement (function_call (identifier)))))
==========================================
while loops
==========================================
while (someCondition(i)) {
doSomething();
}
---
(while_statement
(function_call (identifier) (identifier))
(statement_block (expression_statement (function_call (identifier)))))
==========================================
try/catch statements
==========================================
try {
doSomething();
} catch (e) {
logError(e);
}
try {
doSomething();
} finally {
logError();
}
---
(program
(try_statement
(statement_block (expression_statement (function_call (identifier))))
2014-10-21 08:49:16 -07:00
(catch (identifier)
(statement_block (expression_statement (function_call (identifier) (identifier))))))
(try_statement
(statement_block (expression_statement (function_call (identifier))))
2014-10-21 08:49:16 -07:00
(finally
(statement_block (expression_statement (function_call (identifier)))))))
===========================================
throw statements
2014-07-03 08:20:43 -07:00
===========================================
2014-07-03 08:20:43 -07:00
throw new Error("wtf");
2014-07-03 08:20:43 -07:00
---
2014-10-21 08:49:16 -07:00
(throw_statement (constructor_call (identifier) (string)))
2014-07-03 08:20:43 -07:00
===========================================
indented code after blocks
===========================================
function x() {}
return z;
---
(program
(expression_statement
2014-10-21 08:49:16 -07:00
(function_expression (identifier) (statement_block)))
(return_statement (identifier)))
===========================================
switch statements
===========================================
switch(x) {
case "hello":
print("one");
break;
case z():
print("two");
break;
default:
print("three");
}
---
(switch_statement (identifier)
2014-10-21 08:49:16 -07:00
(case
(string)
(expression_statement (function_call (identifier) (string)))
(break_statement))
2014-10-21 08:49:16 -07:00
(case
(function_call (identifier))
(expression_statement (function_call (identifier) (string)))
(break_statement))
2014-10-21 08:49:16 -07:00
(default
(expression_statement (function_call (identifier) (string)))))