Add constructor calls, pre/postfix operators to js grammar
This commit is contained in:
parent
1bdd87535a
commit
bae32adc7b
5 changed files with 24216 additions and 14767 deletions
|
|
@ -40,6 +40,12 @@ namespace tree_sitter_examples {
|
|||
sym(rule_name) }));
|
||||
}
|
||||
|
||||
rule_ptr postfix_op(std::string op, std::string rule_name, int precedence) {
|
||||
return prec(precedence, seq({
|
||||
sym(rule_name),
|
||||
str(op) }));
|
||||
}
|
||||
|
||||
rule_ptr delimited(std::string delimiter) {
|
||||
return seq({
|
||||
str(delimiter),
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ namespace tree_sitter_examples {
|
|||
rule_ptr in_brackets(rule_ptr rule);
|
||||
rule_ptr infix_op(std::string op, std::string rule_name, int precedence);
|
||||
rule_ptr prefix_op(std::string op, std::string rule_name, int precedence);
|
||||
rule_ptr postfix_op(std::string op, std::string rule_name, int precedence);
|
||||
rule_ptr delimited(std::string delimiter);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -54,9 +54,11 @@ namespace tree_sitter_examples {
|
|||
sym("_terminator") }) },
|
||||
{ "var_declaration", seq({
|
||||
keyword("var"),
|
||||
choice({
|
||||
sym("assignment"),
|
||||
sym("identifier") }),
|
||||
comma_sep(seq({
|
||||
optional(sym("comment")),
|
||||
choice({
|
||||
sym("assignment"),
|
||||
sym("identifier") }) })),
|
||||
sym("_terminator") }) },
|
||||
{ "expression_statement", seq({
|
||||
err(sym("expression")),
|
||||
|
|
@ -74,6 +76,7 @@ namespace tree_sitter_examples {
|
|||
{ "expression", choice({
|
||||
sym("function_expression"),
|
||||
sym("function_call"),
|
||||
sym("constructor_call"),
|
||||
sym("property_access"),
|
||||
sym("assignment"),
|
||||
sym("ternary"),
|
||||
|
|
@ -90,10 +93,17 @@ namespace tree_sitter_examples {
|
|||
sym("identifier"),
|
||||
in_parens(sym("expression")) }) },
|
||||
{ "math_op", choice({
|
||||
prefix_op("++", "expression", 3),
|
||||
prefix_op("--", "expression", 3),
|
||||
postfix_op("++", "expression", 3),
|
||||
postfix_op("--", "expression", 3),
|
||||
prefix_op("+", "expression", 3),
|
||||
prefix_op("-", "expression", 3),
|
||||
infix_op("*", "expression", 2),
|
||||
infix_op("/", "expression", 2),
|
||||
infix_op("&", "expression", 2),
|
||||
infix_op("|", "expression", 2),
|
||||
infix_op("^", "expression", 2),
|
||||
infix_op("+", "expression", 1),
|
||||
infix_op("-", "expression", 1) }) },
|
||||
{ "bool_op", choice({
|
||||
|
|
@ -101,6 +111,8 @@ namespace tree_sitter_examples {
|
|||
infix_op("&&", "expression", 2),
|
||||
infix_op("===", "expression", 3),
|
||||
infix_op("==", "expression", 3),
|
||||
infix_op("!==", "expression", 3),
|
||||
infix_op("!=", "expression", 3),
|
||||
infix_op("<=", "expression", 3),
|
||||
infix_op("<", "expression", 3),
|
||||
infix_op(">=", "expression", 3),
|
||||
|
|
@ -126,6 +138,9 @@ namespace tree_sitter_examples {
|
|||
{ "function_call", seq({
|
||||
sym("expression"),
|
||||
in_parens(comma_sep(err(sym("expression")))) }) },
|
||||
{ "constructor_call", seq({
|
||||
keyword("new"),
|
||||
sym("function_call") }) },
|
||||
{ "property_access", seq({
|
||||
sym("expression"),
|
||||
choice({
|
||||
|
|
@ -136,12 +151,20 @@ namespace tree_sitter_examples {
|
|||
{ "formal_parameters", in_parens(comma_sep(sym("identifier"))) },
|
||||
|
||||
// Literals
|
||||
{ "comment", pattern("//[^\n]*") },
|
||||
{ "comment", token(choice({
|
||||
seq({
|
||||
str("/*"),
|
||||
repeat(pattern("[^*]|(*[^/])")),
|
||||
str("*/") }),
|
||||
pattern("//[^\n]*") })) },
|
||||
{ "object", in_braces(comma_sep(err(seq({
|
||||
choice({ sym("string"), sym("identifier") }),
|
||||
str(":"),
|
||||
sym("expression") })))) },
|
||||
{ "array", in_brackets(comma_sep(err(sym("expression")))) },
|
||||
optional(sym("comment")),
|
||||
choice({ sym("string"), sym("identifier") }),
|
||||
str(":"),
|
||||
sym("expression") })))) },
|
||||
{ "array", in_brackets(comma_sep(err(seq({
|
||||
optional(sym("comment")),
|
||||
sym("expression") })))) },
|
||||
{ "_terminator", pattern("[;\n]") },
|
||||
{ "regex", token(delimited("/")) },
|
||||
{ "string", token(choice({
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -17,15 +17,31 @@ theFunction(
|
|||
(string)
|
||||
(string))))
|
||||
|
||||
==========================================
|
||||
parses constructor calls
|
||||
==========================================
|
||||
var x = new Node(5, new Node(3, null));
|
||||
---
|
||||
(program (var_declaration
|
||||
(assignment (identifier)
|
||||
(constructor_call (function_call (identifier)
|
||||
(number)
|
||||
(constructor_call (function_call (identifier)
|
||||
(number)
|
||||
(null))))))))
|
||||
|
||||
==========================================
|
||||
parses multiple statements
|
||||
==========================================
|
||||
var x = {}
|
||||
var x = {}, z, i = 0;
|
||||
firstFunction(x)
|
||||
secondFunction(x);
|
||||
---
|
||||
(program
|
||||
(var_declaration (assignment (identifier) (object)))
|
||||
(var_declaration
|
||||
(assignment (identifier) (object))
|
||||
(identifier)
|
||||
(assignment (identifier) (number)))
|
||||
(expression_statement (function_call (identifier) (identifier)))
|
||||
(expression_statement (function_call (identifier) (identifier))))
|
||||
|
||||
|
|
@ -83,31 +99,30 @@ parses comments
|
|||
==========================================
|
||||
// this is another comment
|
||||
stuff(); // this is a comment
|
||||
/* this is a third comment */
|
||||
---
|
||||
(program
|
||||
(comment)
|
||||
(expression_statement (function_call (identifier)))
|
||||
(expression_statement (function_call (identifier))) (comment)
|
||||
(comment))
|
||||
|
||||
======================================
|
||||
parses real code
|
||||
=====================================
|
||||
_.each = _.forEach = function(obj, iterator, context) {
|
||||
if (obj == null) return obj;
|
||||
if (obj.length === obj.length) {
|
||||
for (var i = 0; i < length; i = i + 1) {
|
||||
if (iterator.call(context, obj[i], i, obj) === breaker) return;
|
||||
}
|
||||
} else {
|
||||
var keys = _.keys(obj);
|
||||
for (var i = 0; i < length; i = i + 1) {
|
||||
if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
|
||||
}
|
||||
_.each = _.forEach = function(obj, iterator, context) {
|
||||
if (obj == null) return obj;
|
||||
if (obj.length === obj.length) {
|
||||
for (var i = 0; i < length; i++) {
|
||||
if (iterator.call(context, obj[i], i, obj) === breaker) return;
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
|
||||
|
||||
} else {
|
||||
var keys = _.keys(obj);
|
||||
for (var i = 0; i < length; i++) {
|
||||
if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
---
|
||||
(program (expression_statement
|
||||
(assignment
|
||||
|
|
@ -123,7 +138,7 @@ parses real code
|
|||
(for_statement
|
||||
(var_declaration (assignment (identifier) (number)))
|
||||
(expression_statement (bool_op (identifier) (identifier)))
|
||||
(assignment (identifier) (math_op (identifier) (number)))
|
||||
(math_op (identifier))
|
||||
(statement_block
|
||||
(if_statement
|
||||
(bool_op (function_call (property_access (identifier) (identifier)) (identifier) (property_access (identifier) (identifier)) (identifier) (identifier)) (identifier))
|
||||
|
|
@ -133,7 +148,7 @@ parses real code
|
|||
(for_statement
|
||||
(var_declaration (assignment (identifier) (number)))
|
||||
(expression_statement (bool_op (identifier) (identifier)))
|
||||
(assignment (identifier) (math_op (identifier) (number)))
|
||||
(math_op (identifier))
|
||||
(statement_block
|
||||
(if_statement
|
||||
(bool_op (function_call
|
||||
|
|
@ -142,4 +157,4 @@ parses real code
|
|||
(property_access (identifier) (property_access (identifier) (identifier)))
|
||||
(property_access (identifier) (identifier)) (identifier)) (identifier))
|
||||
(return_statement))))))
|
||||
(return_statement (identifier))))))))
|
||||
(return_statement (identifier))))))))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue