diff --git a/spec/compiler/build_tables/rule_can_be_blank_spec.cpp b/spec/compiler/build_tables/rule_can_be_blank_spec.cpp new file mode 100644 index 00000000..312abf39 --- /dev/null +++ b/spec/compiler/build_tables/rule_can_be_blank_spec.cpp @@ -0,0 +1,23 @@ +#include "spec_helper.h" +#include "rule_can_be_blank.h" + +using namespace rules; +using build_tables::rule_can_be_blank; + +START_TEST + +describe("checking if rules can be blank", [&]() { + it("handles sequences", [&]() { + rule_ptr rule = seq({ + choice({ + str("x"), + blank(), + }), + str("y"), + }); + + AssertThat(rule_can_be_blank(rule), Equals(false)); + }); +}); + +END_TEST \ No newline at end of file diff --git a/spec/compiler/build_tables/rule_transitions_spec.cpp b/spec/compiler/build_tables/rule_transitions_spec.cpp index bc1511d4..16c6acfe 100644 --- a/spec/compiler/build_tables/rule_transitions_spec.cpp +++ b/spec/compiler/build_tables/rule_transitions_spec.cpp @@ -182,18 +182,5 @@ describe("rule transitions", []() { }); }); -describe("checking if rules can be blank", [&]() { - it("handles sequences", [&]() { - rule_ptr rule = seq({ - choice({ - str("x"), - blank(), - }), - str("y"), - }); - - AssertThat(rule_can_be_blank(rule), Equals(false)); - }); -}); END_TEST diff --git a/spec/compiler/rules/rules_spec.cpp b/spec/compiler/rules/rules_spec.cpp index d48477b6..2d497a55 100644 --- a/spec/compiler/rules/rules_spec.cpp +++ b/spec/compiler/rules/rules_spec.cpp @@ -1,5 +1,4 @@ #include "spec_helper.h" -#include "rule_transitions.h" using namespace rules; diff --git a/src/compiler/build_tables/first_set.cpp b/src/compiler/build_tables/first_set.cpp index d94fb2ec..474edbea 100644 --- a/src/compiler/build_tables/first_set.cpp +++ b/src/compiler/build_tables/first_set.cpp @@ -1,5 +1,5 @@ #include "first_set.h" -#include "rule_transitions.h" +#include "rule_can_be_blank.h" #include "grammar.h" #include diff --git a/src/compiler/build_tables/follow_sets.cpp b/src/compiler/build_tables/follow_sets.cpp index 16f2ed9c..f209ccb6 100644 --- a/src/compiler/build_tables/follow_sets.cpp +++ b/src/compiler/build_tables/follow_sets.cpp @@ -1,6 +1,7 @@ #include "follow_sets.h" #include "first_set.h" #include "rule_transitions.h" +#include "rule_can_be_blank.h" #include "grammar.h" using std::map; diff --git a/src/compiler/build_tables/item.cpp b/src/compiler/build_tables/item.cpp index d6619053..7801da5a 100644 --- a/src/compiler/build_tables/item.cpp +++ b/src/compiler/build_tables/item.cpp @@ -1,6 +1,6 @@ #include "item.h" #include "grammar.h" -#include "rule_transitions.h" +#include "rule_can_be_blank.h" using std::string; using std::to_string; diff --git a/src/compiler/build_tables/rule_can_be_blank.cpp b/src/compiler/build_tables/rule_can_be_blank.cpp new file mode 100644 index 00000000..271b9f5f --- /dev/null +++ b/src/compiler/build_tables/rule_can_be_blank.cpp @@ -0,0 +1,39 @@ +#include "rule_can_be_blank.h" +#include "rules.h" + +namespace tree_sitter { + using namespace rules; + + namespace build_tables { + class EpsilonVisitor : public rules::Visitor { + public: + bool value; + + void default_visit(const Rule *) { + value = false; + } + + void visit(const Blank *) { + value = true; + } + + void visit(const Choice *rule) { + value = rule_can_be_blank(rule->left) || rule_can_be_blank(rule->right); + } + + void visit(const Seq *rule) { + value = rule_can_be_blank(rule->left) && rule_can_be_blank(rule->right); + } + + void visit(const Repeat *rule) { + value = rule_can_be_blank(rule->content); + } + }; + + bool rule_can_be_blank(const rule_ptr &rule) { + EpsilonVisitor visitor; + rule->accept(visitor); + return visitor.value; + } + } +} diff --git a/src/compiler/build_tables/rule_can_be_blank.h b/src/compiler/build_tables/rule_can_be_blank.h new file mode 100644 index 00000000..5d235ee9 --- /dev/null +++ b/src/compiler/build_tables/rule_can_be_blank.h @@ -0,0 +1,12 @@ +#ifndef __tree_sitter__rule_can_be_blank__ +#define __tree_sitter__rule_can_be_blank__ + +#include "rule.h" + +namespace tree_sitter { + namespace build_tables { + bool rule_can_be_blank(const rules::rule_ptr &rule); + } +} + +#endif diff --git a/src/compiler/build_tables/rule_transitions.cpp b/src/compiler/build_tables/rule_transitions.cpp index 27048fdb..bd72421a 100644 --- a/src/compiler/build_tables/rule_transitions.cpp +++ b/src/compiler/build_tables/rule_transitions.cpp @@ -1,5 +1,6 @@ -#include "rule_transitions.h" #include "rules.h" +#include "rule_transitions.h" +#include "rule_can_be_blank.h" #include "merge_transitions.h" using namespace tree_sitter::rules; @@ -107,36 +108,5 @@ namespace tree_sitter { map sym_transitions(const rule_ptr &rule) { return TransitionsVisitor::transitions(rule); } - - class EpsilonVisitor : public rules::Visitor { - public: - bool value; - - void default_visit(const Rule *) { - value = false; - } - - void visit(const Blank *) { - value = true; - } - - void visit(const Choice *rule) { - value = rule_can_be_blank(rule->left) || rule_can_be_blank(rule->right); - } - - void visit(const Seq *rule) { - value = rule_can_be_blank(rule->left) && rule_can_be_blank(rule->right); - } - - void visit(const Repeat *rule) { - value = rule_can_be_blank(rule->content); - } - }; - - bool rule_can_be_blank(const rule_ptr &rule) { - EpsilonVisitor visitor; - rule->accept(visitor); - return visitor.value; - } } } diff --git a/src/compiler/build_tables/rule_transitions.h b/src/compiler/build_tables/rule_transitions.h index 287f431e..2f810f38 100644 --- a/src/compiler/build_tables/rule_transitions.h +++ b/src/compiler/build_tables/rule_transitions.h @@ -7,7 +7,6 @@ namespace tree_sitter { namespace build_tables { - bool rule_can_be_blank(const rules::rule_ptr &rule); std::map char_transitions(const rules::rule_ptr &rule); std::map sym_transitions(const rules::rule_ptr &rule); } diff --git a/tree_sitter.xcodeproj/project.pbxproj b/tree_sitter.xcodeproj/project.pbxproj index b22b737c..7bde2567 100644 --- a/tree_sitter.xcodeproj/project.pbxproj +++ b/tree_sitter.xcodeproj/project.pbxproj @@ -18,6 +18,8 @@ 1251209B1830145300C9B56A /* rule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1251209A1830145300C9B56A /* rule.cpp */; }; 125120A4183083BD00C9B56A /* arithmetic.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 125120A3183083BD00C9B56A /* arithmetic.cpp */; }; 12661BF418A1505A00A259FB /* character_set_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12661BF318A1505A00A259FB /* character_set_spec.cpp */; }; + 127528B318AACAAA006B682B /* rule_can_be_blank.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 127528B118AACAAA006B682B /* rule_can_be_blank.cpp */; }; + 127528B518AACB70006B682B /* rule_can_be_blank_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 127528B418AACB70006B682B /* rule_can_be_blank_spec.cpp */; }; 12AB465F188BD03E00DE79DF /* follow_sets.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12AB465D188BD03E00DE79DF /* follow_sets.cpp */; }; 12AB4661188CB3A300DE79DF /* item_set_closure_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12AB4660188CB3A300DE79DF /* item_set_closure_spec.cpp */; }; 12BC470518822B27005AC502 /* parse_config.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 12BC470318822A17005AC502 /* parse_config.cpp */; }; @@ -102,6 +104,9 @@ 125120A3183083BD00C9B56A /* arithmetic.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = arithmetic.cpp; path = spec/fixtures/grammars/arithmetic.cpp; sourceTree = SOURCE_ROOT; }; 12661BF318A1505A00A259FB /* character_set_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = character_set_spec.cpp; sourceTree = SOURCE_ROOT; }; 127528AF18A6F9C6006B682B /* merge_transitions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = merge_transitions.h; sourceTree = ""; }; + 127528B118AACAAA006B682B /* rule_can_be_blank.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule_can_be_blank.cpp; sourceTree = ""; }; + 127528B218AACAAA006B682B /* rule_can_be_blank.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rule_can_be_blank.h; sourceTree = ""; }; + 127528B418AACB70006B682B /* rule_can_be_blank_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule_can_be_blank_spec.cpp; sourceTree = ""; }; 12AB465D188BD03E00DE79DF /* follow_sets.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = follow_sets.cpp; sourceTree = ""; }; 12AB465E188BD03E00DE79DF /* follow_sets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = follow_sets.h; sourceTree = ""; }; 12AB4660188CB3A300DE79DF /* item_set_closure_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = item_set_closure_spec.cpp; sourceTree = ""; }; @@ -233,6 +238,8 @@ 12EDCFA518820137005A7A07 /* perform.h */, 12EDCFA618820137005A7A07 /* rule_transitions.cpp */, 12EDCFA718820137005A7A07 /* rule_transitions.h */, + 127528B118AACAAA006B682B /* rule_can_be_blank.cpp */, + 127528B218AACAAA006B682B /* rule_can_be_blank.h */, ); path = build_tables; sourceTree = ""; @@ -244,6 +251,7 @@ 12EDCFB7188205BA005A7A07 /* perform_spec.cpp */, 12BC470618830BC5005AC502 /* first_set_spec.cpp */, 12AB4660188CB3A300DE79DF /* item_set_closure_spec.cpp */, + 127528B418AACB70006B682B /* rule_can_be_blank_spec.cpp */, ); path = build_tables; sourceTree = ""; @@ -504,6 +512,7 @@ 12130611182C3A1100FCF928 /* blank.cpp in Sources */, 12AB465F188BD03E00DE79DF /* follow_sets.cpp in Sources */, 1213060E182C398300FCF928 /* choice.cpp in Sources */, + 127528B318AACAAA006B682B /* rule_can_be_blank.cpp in Sources */, 12F9A64E182DD5FD00FAF50C /* spec_helper.cpp in Sources */, 12EDCFB018820392005A7A07 /* item.cpp in Sources */, 12FD40F7186A16020041A84E /* lex_table.cpp in Sources */, @@ -530,6 +539,7 @@ 12EDCFB418820519005A7A07 /* compile.cpp in Sources */, 12BC470718830BC5005AC502 /* first_set_spec.cpp in Sources */, 1213060B182C389100FCF928 /* symbol.cpp in Sources */, + 127528B518AACB70006B682B /* rule_can_be_blank_spec.cpp in Sources */, 1251209B1830145300C9B56A /* rule.cpp in Sources */, 27A343CA69E17E0F9EBEDF1C /* pattern.cpp in Sources */, );