Make expression and statement rules hidden in javascript grammar

This commit is contained in:
Max Brunsfeld 2015-09-02 13:05:31 -07:00
parent 76e2067ee0
commit acf9280eda
7 changed files with 20060 additions and 19562 deletions

View file

@ -14,32 +14,33 @@ static rule_ptr terminated(rule_ptr rule) {
enum {
PREC_COMMA = -1,
PREC_ASSIGN,
PREC_BLOCK,
PREC_TERNARY,
PREC_OR,
PREC_AND,
PREC_REL,
PREC_ADD,
PREC_MULT,
PREC_TYPE,
PREC_NOT,
PREC_SIGN,
PREC_INC,
PREC_CALL,
PREC_SHORT_NEW,
PREC_FULL_NEW,
PREC_MEMBER,
PREC_ASSIGN = 0,
PREC_BLOCK = 1,
PREC_TERNARY = 2,
PREC_OR = 3,
PREC_AND = 4,
PREC_REL = 5,
PREC_ADD = 6,
PREC_MULT = 7,
PREC_TYPE = 8,
PREC_NOT = 9,
PREC_SIGN = 10,
PREC_INC = 11,
PREC_SHORT_NEW = 12,
PREC_CALL = 13,
PREC_FULL_NEW = 14,
PREC_MEMBER = 15,
PREC_ARGS = 16,
};
extern const Grammar javascript = Grammar({
{ "program", repeat(sym("statement")) },
{ "program", repeat(sym("_statement")) },
/*
* Statements
*/
{ "statement", choice({
{ "_statement", choice({
sym("expression_statement"),
sym("var_declaration"),
sym("statement_block"),
@ -57,7 +58,7 @@ extern const Grammar javascript = Grammar({
sym("throw_statement"),
sym("delete_statement") }) },
{ "expression_statement", terminated(err(sym("expression"))) },
{ "expression_statement", terminated(err(sym("_expression"))) },
{ "var_declaration", terminated(seq({
str("var"),
@ -66,15 +67,15 @@ extern const Grammar javascript = Grammar({
sym("var_assignment") }))) })) },
{ "statement_block", prec(PREC_BLOCK,
in_braces(err(repeat(sym("statement"))))) },
in_braces(err(repeat(sym("_statement"))))) },
{ "if_statement", prec(0, seq({
str("if"),
sym("_paren_expression"),
sym("statement"),
sym("_statement"),
optional(seq({
str("else"),
sym("statement") })) }), AssociativityRight) },
sym("_statement") })) }), AssociativityRight) },
{ "switch_statement", seq({
str("switch"),
@ -88,12 +89,12 @@ extern const Grammar javascript = Grammar({
str("("),
choice({
sym("var_declaration"),
seq({ sym("expression"), str(";") }),
seq({ sym("_expression"), str(";") }),
str(";") }),
optional(err(sym("expression"))), str(";"),
optional(err(sym("expression"))),
optional(err(sym("_expression"))), str(";"),
optional(err(sym("_expression"))),
str(")"),
sym("statement") }) },
sym("_statement") }) },
{ "for_in_statement", seq({
str("for"),
@ -102,18 +103,18 @@ extern const Grammar javascript = Grammar({
prec(PREC_REL, seq({
sym("identifier"),
str("in"),
sym("expression") })),
sym("_expression") })),
str(")"),
sym("statement") }) },
sym("_statement") }) },
{ "while_statement", seq({
str("while"),
sym("_paren_expression"),
sym("statement") }) },
sym("_statement") }) },
// { "do_statement", seq({
// str("do"),
// sym("statement"),
// sym("_statement"),
// str("while"),
// sym("_paren_expression") })},
@ -125,11 +126,11 @@ extern const Grammar javascript = Grammar({
{ "return_statement", terminated(seq({
str("return"),
optional(sym("expression")) })) },
optional(sym("_expression")) })) },
{ "throw_statement", terminated(seq({
str("throw"),
sym("expression") })) },
sym("_expression") })) },
{ "break_statement", terminated(str("break")) },
@ -143,14 +144,14 @@ extern const Grammar javascript = Grammar({
{ "case", seq({
str("case"),
sym("expression"),
sym("_expression"),
str(":"),
repeat(sym("statement")) }) },
repeat(sym("_statement")) }) },
{ "default", seq({
str("default"),
str(":"),
repeat(sym("statement")) }) },
repeat(sym("_statement")) }) },
{ "catch", seq({
str("catch"),
@ -166,15 +167,15 @@ extern const Grammar javascript = Grammar({
{ "var_assignment", seq({
sym("identifier"),
str("="),
sym("expression") }) },
sym("_expression") }) },
{ "_paren_expression", in_parens(err(sym("expression"))) },
{ "_paren_expression", in_parens(err(sym("_expression"))) },
/*
* Expressions
*/
{ "expression", choice({
{ "_expression", choice({
sym("object"),
sym("array"),
sym("function_expression"),
@ -205,7 +206,7 @@ extern const Grammar javascript = Grammar({
{ "object", in_braces(comma_sep(err(sym("pair")))) },
{ "array", in_brackets(comma_sep(err(sym("expression")))) },
{ "array", in_brackets(comma_sep(err(sym("_expression")))) },
{ "function_expression", seq({
str("function"),
@ -216,31 +217,27 @@ extern const Grammar javascript = Grammar({
sym("statement_block") }) },
{ "function_call", prec(PREC_CALL, seq({
sym("expression"),
str("("),
optional(err(sym("arguments"))),
str(")") })) },
sym("_expression"),
sym("arguments") })) },
{ "constructor_call", choice({
prec(PREC_SHORT_NEW, seq({
str("new"),
sym("expression") })),
prec(PREC_FULL_NEW, seq({
sym("_expression") }), AssociativityRight),
prec(PREC_MEMBER, seq({
str("new"),
sym("expression"),
str("("),
err(optional(sym("arguments"))),
str(")") })) }) },
sym("_expression"),
sym("arguments") }), AssociativityRight) }) },
{ "member_access", prec(PREC_MEMBER, seq({
sym("expression"),
sym("_expression"),
str("."),
sym("identifier") })) },
{ "subscript_access", prec(PREC_MEMBER, seq({
sym("expression"),
sym("_expression"),
str("["),
err(sym("expression")),
err(sym("_expression")),
str("]") })) },
{ "assignment", prec(PREC_ASSIGN, seq({
@ -249,7 +246,7 @@ extern const Grammar javascript = Grammar({
sym("member_access"),
sym("subscript_access") }),
str("="),
sym("expression") }), AssociativityRight) },
sym("_expression") }), AssociativityRight) },
{ "math_assignment", prec(PREC_ASSIGN, seq({
choice({
@ -257,59 +254,59 @@ extern const Grammar javascript = Grammar({
sym("member_access"),
sym("subscript_access") }),
choice({ str("+="), str("-="), str("*="), str("/=") }),
sym("expression") }), AssociativityRight) },
sym("_expression") }), AssociativityRight) },
{ "ternary", prec(PREC_TERNARY, seq({
sym("expression"),
sym("_expression"),
str("?"),
sym("expression"),
sym("_expression"),
str(":"),
sym("expression") }), AssociativityRight) },
sym("_expression") }), AssociativityRight) },
{ "bool_op", choice({
infix_op("||", "expression", PREC_OR),
infix_op("&&", "expression", PREC_AND),
prefix_op("!", "expression", PREC_NOT) }) },
infix_op("||", "_expression", PREC_OR),
infix_op("&&", "_expression", PREC_AND),
prefix_op("!", "_expression", PREC_NOT) }) },
{ "comma_op", infix_op(",", "expression", PREC_COMMA) },
{ "comma_op", infix_op(",", "_expression", PREC_COMMA) },
{ "math_op", choice({
// prefix_op("+", "expression", PREC_SIGN),
// prefix_op("-", "expression", PREC_SIGN),
// prefix_op("+", "_expression", PREC_SIGN),
// prefix_op("-", "_expression", PREC_SIGN),
postfix_op("++", "expression", PREC_INC),
postfix_op("--", "expression", PREC_INC),
infix_op("*", "expression", PREC_MULT),
infix_op("/", "expression", PREC_MULT),
infix_op("+", "expression", PREC_ADD),
infix_op("-", "expression", PREC_ADD) }) },
postfix_op("++", "_expression", PREC_INC),
postfix_op("--", "_expression", PREC_INC),
infix_op("*", "_expression", PREC_MULT),
infix_op("/", "_expression", PREC_MULT),
infix_op("+", "_expression", PREC_ADD),
infix_op("-", "_expression", PREC_ADD) }) },
// { "bitwise_op", choice({
// infix_op("&", "expression", PREC_MULT),
// infix_op("|", "expression", PREC_MULT),
// infix_op("<<", "expression", PREC_MULT),
// infix_op(">>", "expression", PREC_MULT) }) },
// infix_op("&", "_expression", PREC_MULT),
// infix_op("|", "_expression", PREC_MULT),
// infix_op("<<", "_expression", PREC_MULT),
// infix_op(">>", "_expression", PREC_MULT) }) },
{ "rel_op", choice({
// infix_op("==", "expression", PREC_REL),
// infix_op("!=", "expression", PREC_REL),
// infix_op("<=", "expression", PREC_REL),
// infix_op(">=", "expression", PREC_REL),
// infix_op("==", "_expression", PREC_REL),
// infix_op("!=", "_expression", PREC_REL),
// infix_op("<=", "_expression", PREC_REL),
// infix_op(">=", "_expression", PREC_REL),
infix_op("===", "expression", PREC_REL),
infix_op("!==", "expression", PREC_REL),
infix_op("<", "expression", PREC_REL),
infix_op(">", "expression", PREC_REL) }) },
infix_op("===", "_expression", PREC_REL),
infix_op("!==", "_expression", PREC_REL),
infix_op("<", "_expression", PREC_REL),
infix_op(">", "_expression", PREC_REL) }) },
{ "type_op", choice({
prec(PREC_REL, seq({
choice({ sym("expression"), sym("identifier") }),
choice({ sym("_expression"), sym("identifier") }),
str("in"),
sym("expression") })),
infix_op("instanceof", "expression", PREC_REL),
prefix_op("typeof", "expression", PREC_TYPE) }) },
sym("_expression") })),
infix_op("instanceof", "_expression", PREC_REL),
prefix_op("typeof", "_expression", PREC_TYPE) }) },
/*
* Primitives
@ -344,12 +341,15 @@ extern const Grammar javascript = Grammar({
{ "formal_parameters", comma_sep1(sym("identifier")) },
{ "arguments", prec(-5, comma_sep1(err(sym("expression")))) },
{ "arguments", prec(PREC_ARGS, seq({
str("("),
comma_sep(err(sym("_expression"))),
str(")") })) },
{ "pair", seq({
choice({ sym("string"), sym("identifier") }),
str(":"),
sym("expression") }) },
sym("_expression") }) },
}).ubiquitous_tokens({
sym("comment"),
sym("_line_break"),

File diff suppressed because it is too large Load diff

View file

@ -13,8 +13,8 @@ secondFunction(x);
(var_assignment (identifier) (object))
(identifier)
(var_assignment (identifier) (number)))
(expression_statement (function_call (identifier) (identifier)))
(expression_statement (function_call (identifier) (identifier))))
(expression_statement (function_call (identifier) (arguments (identifier))))
(expression_statement (function_call (identifier) (arguments (identifier)))))
==========================================
if statements
@ -26,9 +26,9 @@ if (isReady()) {
---
(program (if_statement (function_call (identifier))
(program (if_statement (function_call (identifier) (arguments))
(statement_block (expression_statement
(function_call (member_access (identifier) (identifier)) (identifier))))))
(function_call (member_access (identifier) (identifier)) (arguments (identifier)))))))
==========================================
if-else statements
@ -44,8 +44,8 @@ if (theCondition) {
(program (if_statement
(identifier)
(statement_block (expression_statement (function_call (identifier))))
(statement_block (expression_statement (function_call (identifier))))))
(statement_block (expression_statement (function_call (identifier) (arguments))))
(statement_block (expression_statement (function_call (identifier) (arguments))))))
==================================================
if-else statements with multiple conditions
@ -62,10 +62,10 @@ else {
---
(program (if_statement (identifier)
(statement_block (expression_statement (function_call (identifier))))
(statement_block (expression_statement (function_call (identifier) (arguments))))
(if_statement (identifier)
(expression_statement (function_call (identifier)))
(statement_block (expression_statement (function_call (identifier)))))))
(expression_statement (function_call (identifier) (arguments)))
(statement_block (expression_statement (function_call (identifier) (arguments)))))))
==========================================
for loops
@ -79,9 +79,9 @@ for (var i = 1; someCondition(i); i = next()) {
(program (for_statement
(var_declaration (var_assignment (identifier) (number)))
(function_call (identifier) (identifier))
(assignment (identifier) (function_call (identifier)))
(statement_block (expression_statement (function_call (identifier))))))
(function_call (identifier) (arguments (identifier)))
(assignment (identifier) (function_call (identifier) (arguments)))
(statement_block (expression_statement (function_call (identifier) (arguments))))))
==========================================
for-in loops
@ -97,10 +97,10 @@ for (key in someObject)
(program
(for_in_statement
(identifier) (identifier)
(expression_statement (function_call (identifier))))
(expression_statement (function_call (identifier) (arguments))))
(for_in_statement
(identifier) (identifier)
(expression_statement (function_call (identifier)))))
(expression_statement (function_call (identifier) (arguments)))))
==========================================
while loops
@ -113,8 +113,8 @@ while (someCondition(i)) {
---
(program (while_statement
(function_call (identifier) (identifier))
(statement_block (expression_statement (function_call (identifier))))))
(function_call (identifier) (arguments (identifier)))
(statement_block (expression_statement (function_call (identifier) (arguments))))))
==========================================
try/catch statements
@ -136,13 +136,13 @@ try {
(program
(try_statement
(statement_block (expression_statement (function_call (identifier))))
(statement_block (expression_statement (function_call (identifier) (arguments))))
(catch (identifier)
(statement_block (expression_statement (function_call (identifier) (identifier))))))
(statement_block (expression_statement (function_call (identifier) (arguments (identifier)))))))
(try_statement
(statement_block (expression_statement (function_call (identifier))))
(statement_block (expression_statement (function_call (identifier) (arguments))))
(finally
(statement_block (expression_statement (function_call (identifier)))))))
(statement_block (expression_statement (function_call (identifier) (arguments)))))))
===========================================
throw statements
@ -153,7 +153,7 @@ throw new Error("wtf");
---
(program
(throw_statement (constructor_call (identifier) (string))))
(throw_statement (constructor_call (identifier) (arguments (string)))))
===========================================
indented code after blocks
@ -189,11 +189,11 @@ switch(x) {
(program (switch_statement (identifier)
(case
(string)
(expression_statement (function_call (identifier) (string)))
(expression_statement (function_call (identifier) (arguments (string))))
(break_statement))
(case
(function_call (identifier))
(expression_statement (function_call (identifier) (string)))
(function_call (identifier) (arguments))
(expression_statement (function_call (identifier) (arguments (string))))
(break_statement))
(default
(expression_statement (function_call (identifier) (string))))))
(expression_statement (function_call (identifier) (arguments (string)))))))

View file

@ -7,7 +7,7 @@ stuff(|||);
---
(program
(expression_statement (function_call (identifier) (ERROR (UNEXPECTED '|')))))
(expression_statement (function_call (identifier) (arguments (ERROR (UNEXPECTED '|'))))))
==========================================
errors in if statements
@ -22,9 +22,9 @@ moreStuff();
---
(program
(expression_statement (function_call (identifier)))
(expression_statement (function_call (identifier) (arguments)))
(if_statement
(ERROR (UNEXPECTED '*') (identifier))
(statement_block
(expression_statement (ERROR (UNEXPECTED '*') (identifier) (identifier)))))
(expression_statement (function_call (identifier))))
(expression_statement (function_call (identifier) (arguments))))

View file

@ -83,7 +83,7 @@ var thing = {
(formal_parameters (identifier) (comment))
(statement_block
(comment)
(expression_statement (function_call (identifier))))))))))
(expression_statement (function_call (identifier) (arguments))))))))))
==========================================
comments within expressions
@ -95,4 +95,4 @@ y // comment
---
(program (expression_statement
(math_op (expression (identifier) (comment)) (identifier))))
(math_op (identifier) (comment) (identifier))))

View file

@ -16,33 +16,34 @@ constructor calls
==========================================
var x = new Node(5, new Node(3, null));
new Thing;
---
(program (var_declaration (var_assignment
(identifier)
(constructor_call (identifier) (arguments
(number)
(program
(var_declaration (var_assignment
(identifier)
(constructor_call (identifier) (arguments
(number)
(null))))))))
(constructor_call (identifier) (arguments
(number)
(null)))))))
(expression_statement (constructor_call (identifier))))
==========================================
property access with dot notation
==========================================
object.property = "the-value";
print(object.property);
object.property;
---
(program
(expression_statement (assignment
(member_access (identifier) (identifier))
(string)))
(expression_statement (function_call
(identifier)
(member_access (identifier) (identifier)))))
(expression_statement
(assignment (member_access (identifier) (identifier)) (string)))
(expression_statement
(member_access (identifier) (identifier))))
==========================================
property access across lines
@ -64,30 +65,28 @@ dynamic property access
==========================================
object[propertName()] = propertyValue();
print(object[propertyName()]);
object[propertyName()];
---
(program
(expression_statement (assignment
(subscript_access (identifier) (function_call (identifier)))
(function_call (identifier))))
(expression_statement (function_call
(identifier)
(subscript_access (identifier) (function_call (identifier))))))
(expression_statement
(assignment
(subscript_access (identifier) (function_call (identifier) (arguments)))
(function_call (identifier) (arguments))))
(expression_statement
(subscript_access (identifier) (function_call (identifier) (arguments)))))
==========================================
ternary expressions
==========================================
print(isDone() ? stuff : otherStuff);
isDone() ? stuff : otherStuff;
---
(program (expression_statement
(function_call
(identifier)
(ternary (function_call (identifier)) (identifier) (identifier)))))
(ternary (function_call (identifier) (arguments)) (identifier) (identifier))))
==========================================
mathematical operators
@ -118,21 +117,20 @@ boolean operators
(bool_op
(bool_op (identifier))
(bool_op
(expression (bool_op (identifier) (identifier)))))))
(bool_op (identifier) (identifier))))))
===========================================
type operators
===========================================
print((x instanceof Array) || (typeof x === "string"))
(x instanceof Array) || (typeof x === "string")
---
(program (expression_statement
(function_call (identifier)
(bool_op
(expression (type_op (identifier) (identifier)))
(expression (rel_op (type_op (identifier)) (string)))))))
(bool_op
(type_op (identifier) (identifier))
(rel_op (type_op (identifier)) (string)))))
============================================
the 'in' operator
@ -145,7 +143,7 @@ print(x in y)
(program (expression_statement
(function_call
(identifier)
(type_op (identifier) (identifier)))))
(arguments (type_op (identifier) (identifier))))))
============================================
assignment operators
@ -174,6 +172,6 @@ print(x.y.z && a.b.c)
(program (expression_statement
(function_call (identifier)
(bool_op
(arguments (bool_op
(member_access (member_access (identifier) (identifier)) (identifier))
(member_access (member_access (identifier) (identifier)) (identifier))))))
(member_access (member_access (identifier) (identifier)) (identifier)))))))

View file

@ -160,7 +160,7 @@ describe("Parser", [&]() {
set_text("fn()\n");
AssertThat(ts_node_string(root, doc), Equals(
"(expression_statement (function_call (identifier)))"));
"(expression_statement (function_call (identifier) (arguments)))"));
});
});
@ -172,7 +172,8 @@ describe("Parser", [&]() {
AssertThat(ts_node_string(root, doc), Equals(
"(expression_statement (function_call "
"(member_access (function_call (identifier)) (identifier))))"));
"(member_access (function_call (identifier) (arguments)) (identifier)) "
"(arguments)))"));
});
});
@ -186,9 +187,10 @@ describe("Parser", [&]() {
AssertThat(ts_node_string(root, doc), Equals(
"(expression_statement (function_call "
"(member_access (function_call (identifier)) "
"(member_access (function_call (identifier) (arguments)) "
"(comment) "
"(identifier))))"));
"(identifier)) "
"(arguments)))"));
});
});
});
@ -381,7 +383,7 @@ describe("Parser", [&]() {
AssertThat(ts_node_string(root, doc), Equals(
"(expression_statement (object (pair "
"(identifier) (expression (member_access (identifier) (identifier))))))"));
"(identifier) (member_access (identifier) (identifier)))))"));
replace_text(strlen("{ x: "), strlen("(b.c)"), "b.c");