Put rule_can_be_blank function in its own file

This commit is contained in:
Max Brunsfeld 2014-02-11 13:21:45 -08:00
parent 1962c17f45
commit 27f305d556
11 changed files with 89 additions and 49 deletions

View file

@ -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

View file

@ -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

View file

@ -1,5 +1,4 @@
#include "spec_helper.h"
#include "rule_transitions.h"
using namespace rules;

View file

@ -1,5 +1,5 @@
#include "first_set.h"
#include "rule_transitions.h"
#include "rule_can_be_blank.h"
#include "grammar.h"
#include <vector>

View file

@ -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;

View file

@ -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;

View file

@ -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;
}
}
}

View file

@ -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

View file

@ -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<Symbol, rule_ptr> sym_transitions(const rule_ptr &rule) {
return TransitionsVisitor<Symbol>::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;
}
}
}

View file

@ -7,7 +7,6 @@
namespace tree_sitter {
namespace build_tables {
bool rule_can_be_blank(const rules::rule_ptr &rule);
std::map<rules::CharacterSet, rules::rule_ptr> char_transitions(const rules::rule_ptr &rule);
std::map<rules::Symbol, rules::rule_ptr> sym_transitions(const rules::rule_ptr &rule);
}

View file

@ -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 = "<group>"; };
127528B118AACAAA006B682B /* rule_can_be_blank.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule_can_be_blank.cpp; sourceTree = "<group>"; };
127528B218AACAAA006B682B /* rule_can_be_blank.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rule_can_be_blank.h; sourceTree = "<group>"; };
127528B418AACB70006B682B /* rule_can_be_blank_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rule_can_be_blank_spec.cpp; sourceTree = "<group>"; };
12AB465D188BD03E00DE79DF /* follow_sets.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = follow_sets.cpp; sourceTree = "<group>"; };
12AB465E188BD03E00DE79DF /* follow_sets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = follow_sets.h; sourceTree = "<group>"; };
12AB4660188CB3A300DE79DF /* item_set_closure_spec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = item_set_closure_spec.cpp; sourceTree = "<group>"; };
@ -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 = "<group>";
@ -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 = "<group>";
@ -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 */,
);