Refactor grammar helper functions
This commit is contained in:
parent
2906125824
commit
b9393b5c1a
8 changed files with 2571 additions and 2597 deletions
|
|
@ -16,11 +16,11 @@ namespace tree_sitter_examples {
|
|||
sym("number"),
|
||||
sym("variable") }) },
|
||||
|
||||
{ "sum", infix(1, "+") },
|
||||
{ "difference", infix(1, "-") },
|
||||
{ "product", infix(2, "*") },
|
||||
{ "quotient", infix(2, "/") },
|
||||
{ "exponent", infix(3, "^") },
|
||||
{ "sum", infix_op("+", "expression", 1) },
|
||||
{ "difference", infix_op("-", "expression", 1) },
|
||||
{ "product", infix_op("*", "expression", 2) },
|
||||
{ "quotient", infix_op("/", "expression", 2) },
|
||||
{ "exponent", infix_op("^", "expression", 3) },
|
||||
{ "group", in_parens(err(sym("expression"))) },
|
||||
|
||||
{ "number", pattern("\\d+") },
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@ namespace tree_sitter_examples {
|
|||
repeat(sym("imports_block")),
|
||||
repeat(sym("declaration")) }) },
|
||||
{ "package_directive", seq({
|
||||
sym("_package"),
|
||||
keyword("package"),
|
||||
sym("package_name") }) },
|
||||
{ "imports_block", seq({
|
||||
sym("_import"),
|
||||
keyword("import"),
|
||||
choice({
|
||||
in_parens(err(repeat(sym("package_import")))),
|
||||
sym("package_import") }) }) },
|
||||
|
|
@ -28,16 +28,16 @@ namespace tree_sitter_examples {
|
|||
|
||||
// Declarations
|
||||
{ "type_declaration", seq({
|
||||
sym("_type"),
|
||||
keyword("type"),
|
||||
sym("type_name"),
|
||||
sym("type_expression") }) },
|
||||
{ "var_declaration", seq({
|
||||
sym("_var"),
|
||||
keyword("var"),
|
||||
sym("var_name"),
|
||||
str("="),
|
||||
sym("expression") }) },
|
||||
{ "func_declaration", seq({
|
||||
sym("_func"),
|
||||
keyword("func"),
|
||||
sym("var_name"),
|
||||
sym("_func_signature"),
|
||||
sym("statement_block") }) },
|
||||
|
|
@ -55,7 +55,7 @@ namespace tree_sitter_examples {
|
|||
str("*"),
|
||||
sym("type_expression") }) },
|
||||
{ "map_type", seq({
|
||||
sym("_map"),
|
||||
keyword("map"),
|
||||
str("["),
|
||||
sym("type_expression"),
|
||||
str("]"),
|
||||
|
|
@ -65,12 +65,12 @@ namespace tree_sitter_examples {
|
|||
str("]"),
|
||||
sym("type_expression") }) },
|
||||
{ "struct_type", seq({
|
||||
sym("_struct"),
|
||||
keyword("struct"),
|
||||
in_braces(repeat(seq({
|
||||
sym("var_name"),
|
||||
sym("type_expression") }))) }) },
|
||||
{ "interface_type", seq({
|
||||
sym("_interface"),
|
||||
keyword("interface"),
|
||||
in_braces(repeat(seq({
|
||||
sym("var_name"),
|
||||
sym("_func_signature") }))) }) },
|
||||
|
|
@ -82,20 +82,19 @@ namespace tree_sitter_examples {
|
|||
sym("number"),
|
||||
sym("var_name") }) },
|
||||
{ "math_op", choice({
|
||||
infix(2, "*"),
|
||||
infix(2, "/"),
|
||||
infix(1, "+"),
|
||||
infix(1, "-") }) },
|
||||
infix_op("*", "expression", 2),
|
||||
infix_op("/", "expression", 2),
|
||||
infix_op("+", "expression", 1),
|
||||
infix_op("-", "expression", 1) }) },
|
||||
{ "bool_op", choice({
|
||||
infix(3, "&&"),
|
||||
infix(2, "||"),
|
||||
infix(2, "==="),
|
||||
infix(2, "=="),
|
||||
infix(2, "<="),
|
||||
infix(4, "<"),
|
||||
infix(2, ">="),
|
||||
infix(2, ">"),
|
||||
prefix(4, "!") }) },
|
||||
infix_op("||", "expression", 1),
|
||||
infix_op("&&", "expression", 2),
|
||||
infix_op("==", "expression", 3),
|
||||
infix_op("<=", "expression", 3),
|
||||
infix_op("<", "expression", 3),
|
||||
infix_op(">=", "expression", 3),
|
||||
infix_op(">", "expression", 3),
|
||||
prefix_op("!", "expression", 4) }) },
|
||||
{ "_func_signature", seq({
|
||||
in_parens(comma_sep(seq({
|
||||
comma_sep1(sym("var_name")),
|
||||
|
|
@ -107,16 +106,6 @@ namespace tree_sitter_examples {
|
|||
sym("type_name"),
|
||||
blank() }) }) },
|
||||
|
||||
// Keywords
|
||||
{ "_map", str("map") },
|
||||
{ "_interface", str("interface") },
|
||||
{ "_struct", str("struct") },
|
||||
{ "_package", str("package") },
|
||||
{ "_import", str("import") },
|
||||
{ "_var", str("var") },
|
||||
{ "_func", str("func") },
|
||||
{ "_type", str("type") },
|
||||
|
||||
{ "string", pattern("\"([^\"]|\\\\\")+\"") },
|
||||
{ "package_name", sym("_identifier") },
|
||||
{ "var_name", sym("_identifier") },
|
||||
|
|
|
|||
|
|
@ -27,16 +27,24 @@ namespace tree_sitter_examples {
|
|||
return seq({ str("["), rule, str("]") });
|
||||
}
|
||||
|
||||
rule_ptr infix(int precedence, std::string op) {
|
||||
rule_ptr infix_op(std::string op, std::string rule_name, int precedence) {
|
||||
return prec(precedence, seq({
|
||||
sym("expression"),
|
||||
sym(rule_name),
|
||||
str(op),
|
||||
sym("expression") }));
|
||||
sym(rule_name) }));
|
||||
}
|
||||
|
||||
rule_ptr prefix(int precedence, std::string op) {
|
||||
rule_ptr prefix_op(std::string op, std::string rule_name, int precedence) {
|
||||
return prec(precedence, seq({
|
||||
str(op),
|
||||
sym("expression") }));
|
||||
sym(rule_name) }));
|
||||
}
|
||||
|
||||
rule_ptr delimited(std::string delimiter) {
|
||||
return seq({
|
||||
str(delimiter),
|
||||
pattern("([^" + delimiter + "]|\\\\" + delimiter + ")+"),
|
||||
str(delimiter)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,8 +12,9 @@ namespace tree_sitter_examples {
|
|||
rule_ptr in_parens(rule_ptr rule);
|
||||
rule_ptr in_braces(rule_ptr rule);
|
||||
rule_ptr in_brackets(rule_ptr rule);
|
||||
rule_ptr infix(int precedence, std::string op);
|
||||
rule_ptr prefix(int precedence, std::string op);
|
||||
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 delimited(std::string delimiter);
|
||||
}
|
||||
|
||||
#endif // TREESITTER_EXAMPLES_HELPERS_
|
||||
|
|
@ -5,14 +5,6 @@ namespace tree_sitter_examples {
|
|||
using tree_sitter::Grammar;
|
||||
using namespace tree_sitter::rules;
|
||||
|
||||
static rule_ptr delimited(std::string delimiter) {
|
||||
return seq({
|
||||
str(delimiter),
|
||||
pattern("([^" + delimiter + "]|\\\\" + delimiter + ")+"),
|
||||
str(delimiter)
|
||||
});
|
||||
}
|
||||
|
||||
extern const Grammar javascript({
|
||||
{ "program", repeat(sym("statement")) },
|
||||
|
||||
|
|
@ -98,20 +90,20 @@ namespace tree_sitter_examples {
|
|||
sym("identifier"),
|
||||
in_parens(sym("expression")) }) },
|
||||
{ "math_op", choice({
|
||||
infix(2, "*"),
|
||||
infix(2, "/"),
|
||||
infix(1, "+"),
|
||||
infix(1, "-") }) },
|
||||
infix_op("*", "expression", 2),
|
||||
infix_op("/", "expression", 2),
|
||||
infix_op("+", "expression", 1),
|
||||
infix_op("-", "expression", 1) }) },
|
||||
{ "bool_op", choice({
|
||||
infix(3, "&&"),
|
||||
infix(2, "||"),
|
||||
infix(2, "==="),
|
||||
infix(2, "=="),
|
||||
infix(2, "<="),
|
||||
infix(4, "<"),
|
||||
infix(2, ">="),
|
||||
infix(2, ">"),
|
||||
prefix(4, "!") }) },
|
||||
infix_op("||", "expression", 1),
|
||||
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),
|
||||
prefix_op("!", "expression", 4) }) },
|
||||
{ "ternary", seq({
|
||||
sym("expression"),
|
||||
str("?"),
|
||||
|
|
|
|||
|
|
@ -21,8 +21,8 @@ namespace tree_sitter_examples {
|
|||
{ "array", in_brackets(comma_sep(err(sym("value")))) },
|
||||
{ "string", pattern("\"([^\"]|\\\\\")+\"") },
|
||||
{ "number", pattern("\\d+(\\.\\d+)?") },
|
||||
{ "null", str("null") },
|
||||
{ "true", str("true") },
|
||||
{ "false", str("false") },
|
||||
{ "null", keyword("null") },
|
||||
{ "true", keyword("true") },
|
||||
{ "false", keyword("false") },
|
||||
});
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue