tree-sitter/spec/runtime/languages/javascript/control_flow.txt
Max Brunsfeld ed11ef557a Fix expansion of repeat rules into recursive rules
Previously, the way repeat rules were expanded, the auxiliary
rule always needed to be reduced, even if the repeating content
was empty. This caused problems in parse states where some items
contained the repeat rule and some did not. To make those cases
work, the repeat rule had to explicitly be marked as optional.
With this change, that is no longer necessary.
2014-09-07 09:39:14 -07:00

164 lines
4.6 KiB
Text

==========================================
parses multiple statements
==========================================
var x = {}, z, i = 0;
firstFunction(x)
secondFunction(x);
---
(program
(var_declaration
(identifier) (object)
(identifier)
(identifier) (number))
(expression_statement (function_call (identifier) (identifier)))
(expression_statement (function_call (identifier) (identifier))))
==========================================
js parses if statements
==========================================
if (isReady()) {
console.log(theData)
}
---
(if_statement (function_call (identifier))
(statement_block (expression_statement (function_call (property_access (identifier) (identifier)) (identifier)))))
==========================================
parses 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)))))
==================================================
parses 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))))))
==========================================
parses for loops
==========================================
for (var i = 1; someCondition(i); i = next()) {
doSomething();
}
---
(for_statement
(var_declaration (identifier) (number))
(expression_statement (function_call (identifier) (identifier)))
(assignment (identifier) (function_call (identifier)))
(statement_block (expression_statement (function_call (identifier)))))
==========================================
parses 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)))))
==========================================
parses while loops
==========================================
while (someCondition(i)) {
doSomething();
}
---
(while_statement
(function_call (identifier) (identifier))
(statement_block (expression_statement (function_call (identifier)))))
==========================================
parses try/catch statements
==========================================
try {
doSomething();
} catch (e) {
logError(e);
}
try {
doSomething();
} finally {
logError();
}
---
(program
(try_statement
(statement_block (expression_statement (function_call (identifier))))
(catch_clause (identifier)
(statement_block (expression_statement (function_call (identifier) (identifier))))))
(try_statement
(statement_block (expression_statement (function_call (identifier))))
(finally_clause
(statement_block (expression_statement (function_call (identifier)))))))
===========================================
parses throw statements
===========================================
throw new Error("wtf");
---
(throw_statement (constructor_call (function_call (identifier) (string))))
===========================================
parses indented code after blocks
===========================================
function x() {}
return z;
---
(program
(expression_statement
(function_expression (identifier) (formal_parameters) (statement_block)))
(return_statement (identifier)))
===========================================
parses switch statements
===========================================
switch(x) {
case "hello":
print("one");
break;
case z():
print("two");
break;
default:
print("three");
}
---
(switch_statement (identifier)
(switch_case
(string)
(expression_statement (function_call (identifier) (string)))
(break_statement))
(switch_case
(function_call (identifier))
(expression_statement (function_call (identifier) (string)))
(break_statement))
(switch_case
(expression_statement (function_call (identifier) (string)))))