File src/tooling/common.cpp changed (mode: 100644) (index 4bdfb7f..a31a51b) |
... |
... |
static optional_t<Tree> buildTreeFromFile(const CommonArgs &args, |
218 |
218 |
Tree t(mr); |
Tree t(mr); |
219 |
219 |
|
|
220 |
220 |
if (args.fine) { |
if (args.fine) { |
221 |
|
t = Tree(std::move(lang), contents, tb.getRoot(), mr); |
|
|
221 |
|
t = Tree(std::move(lang), attrs.tabWidth, contents, tb.getRoot(), mr); |
222 |
222 |
} else { |
} else { |
223 |
223 |
STree stree(std::move(tb), contents, args.dumpSTree, args.sdebug, |
STree stree(std::move(tb), contents, args.dumpSTree, args.sdebug, |
224 |
224 |
*lang, localMR); |
*lang, localMR); |
225 |
|
t = Tree(std::move(lang), contents, stree.getRoot(), mr); |
|
|
225 |
|
t = Tree(std::move(lang), attrs.tabWidth, contents, stree.getRoot(), |
|
226 |
|
mr); |
226 |
227 |
} |
} |
227 |
228 |
|
|
228 |
229 |
return optional_t<Tree>(std::move(t)); |
return optional_t<Tree>(std::move(t)); |
File src/tree.cpp changed (mode: 100644) (index b47c098..93ba027) |
... |
... |
static void dumpTree(std::ostream &os, const Node *node, const Language *lang, |
67 |
67 |
std::vector<bool> &trace, int depth); |
std::vector<bool> &trace, int depth); |
68 |
68 |
static void dumpNode(std::ostream &os, const Node *node, const Language *lang); |
static void dumpNode(std::ostream &os, const Node *node, const Language *lang); |
69 |
69 |
|
|
70 |
|
Tree::Tree(std::unique_ptr<Language> lang, const std::string &contents, |
|
71 |
|
const PNode *node, allocator_type al) |
|
72 |
|
: lang(std::move(lang)), nodes(al), stringified(al), internPool(al) |
|
|
70 |
|
Tree::Tree(std::unique_ptr<Language> lang, int tabWidth, |
|
71 |
|
const std::string &contents, const PNode *node, allocator_type al) |
|
72 |
|
: lang(std::move(lang)), nodes(al), stringified(al), internPool(al), |
|
73 |
|
tabWidth(tabWidth) |
73 |
74 |
{ |
{ |
74 |
75 |
stringified.reserve(maxStringifiedSize(contents, tabWidth)); |
stringified.reserve(maxStringifiedSize(contents, tabWidth)); |
75 |
76 |
const char *buf = stringified.data(); |
const char *buf = stringified.data(); |
|
... |
... |
Tree::Tree(std::unique_ptr<Language> lang, const std::string &contents, |
82 |
83 |
(void)buf; |
(void)buf; |
83 |
84 |
} |
} |
84 |
85 |
|
|
85 |
|
Tree::Tree(std::unique_ptr<Language> lang, const std::string &contents, |
|
86 |
|
const SNode *node, allocator_type al) |
|
87 |
|
: lang(std::move(lang)), nodes(al), stringified(al), internPool(al) |
|
|
86 |
|
Tree::Tree(std::unique_ptr<Language> lang, int tabWidth, |
|
87 |
|
const std::string &contents, const SNode *node, allocator_type al) |
|
88 |
|
: lang(std::move(lang)), nodes(al), stringified(al), internPool(al), |
|
89 |
|
tabWidth(tabWidth) |
88 |
90 |
{ |
{ |
89 |
91 |
stringified.reserve(maxStringifiedSize(contents, tabWidth)); |
stringified.reserve(maxStringifiedSize(contents, tabWidth)); |
90 |
92 |
const char *buf = stringified.data(); |
const char *buf = stringified.data(); |
File src/tree.hpp changed (mode: 100644) (index 1d501b7..12175ed) |
... |
... |
class Tree |
123 |
123 |
using allocator_type = cpp17::pmr::polymorphic_allocator<cpp17::byte>; |
using allocator_type = cpp17::pmr::polymorphic_allocator<cpp17::byte>; |
124 |
124 |
|
|
125 |
125 |
public: |
public: |
126 |
|
Tree(allocator_type al = {}) : nodes(al), stringified(al), internPool(al) |
|
|
126 |
|
Tree(allocator_type al = {}) |
|
127 |
|
: nodes(al), stringified(al), internPool(al), tabWidth(0) |
127 |
128 |
{ } |
{ } |
128 |
129 |
Tree(const Tree &rhs) = delete; |
Tree(const Tree &rhs) = delete; |
129 |
130 |
Tree(Tree &&rhs) = default; |
Tree(Tree &&rhs) = default; |
130 |
|
Tree(std::unique_ptr<Language> lang, const std::string &contents, |
|
131 |
|
const PNode *node, allocator_type al = {}); |
|
132 |
|
Tree(std::unique_ptr<Language> lang, const std::string &contents, |
|
133 |
|
const SNode *node, allocator_type al = {}); |
|
|
131 |
|
Tree(std::unique_ptr<Language> lang, int tabWidth, |
|
132 |
|
const std::string &contents, const PNode *node, |
|
133 |
|
allocator_type al = {}); |
|
134 |
|
Tree(std::unique_ptr<Language> lang, int tabWidth, |
|
135 |
|
const std::string &contents, const SNode *node, |
|
136 |
|
allocator_type al = {}); |
134 |
137 |
|
|
135 |
138 |
Tree & operator=(const Tree &rhs) = delete; |
Tree & operator=(const Tree &rhs) = delete; |
136 |
139 |
Tree & operator=(Tree &&rhs) = default; |
Tree & operator=(Tree &&rhs) = default; |
|
... |
... |
private: |
196 |
199 |
cpp17::pmr::vector<char> stringified; |
cpp17::pmr::vector<char> stringified; |
197 |
200 |
// Storage for interned strings. |
// Storage for interned strings. |
198 |
201 |
cpp17::pmr::deque<std::string> internPool; |
cpp17::pmr::deque<std::string> internPool; |
199 |
|
// XXX: hard-coded width of a tabulation character. |
|
200 |
|
int tabWidth = 4; |
|
|
202 |
|
// Width of a single tabulation character. |
|
203 |
|
int tabWidth; |
201 |
204 |
}; |
}; |
202 |
205 |
|
|
203 |
206 |
std::vector<Node *> postOrder(Node &root); |
std::vector<Node *> postOrder(Node &root); |
File tests/c/c-parser.cpp changed (mode: 100644) (index 2d941ab..384fd0f) |
... |
... |
TEST_CASE("EOL continuation is identified in C", "[parser]") |
560 |
560 |
|
|
561 |
561 |
TEST_CASE("Tabulation of size 1 is allowed", "[parser]") |
TEST_CASE("Tabulation of size 1 is allowed", "[parser]") |
562 |
562 |
{ |
{ |
|
563 |
|
int tabWidth = 1; |
|
564 |
|
|
563 |
565 |
const char *const str = "" |
const char *const str = "" |
564 |
566 |
"// 0\n" |
"// 0\n" |
565 |
567 |
"\t// 1\n" |
"\t// 1\n" |
|
... |
... |
TEST_CASE("Tabulation of size 1 is allowed", "[parser]") |
569 |
571 |
cpp17::pmr::monolithic mr; |
cpp17::pmr::monolithic mr; |
570 |
572 |
std::unique_ptr<Language> lang = Language::create("file.c"); |
std::unique_ptr<Language> lang = Language::create("file.c"); |
571 |
573 |
|
|
572 |
|
TreeBuilder tb = lang->parse(str, "<input>", /*tabWidth=*/1, |
|
573 |
|
/*debug=*/false, mr); |
|
|
574 |
|
TreeBuilder tb = lang->parse(str, "<input>", tabWidth, /*debug=*/false, mr); |
574 |
575 |
REQUIRE_FALSE(tb.hasFailed()); |
REQUIRE_FALSE(tb.hasFailed()); |
575 |
576 |
|
|
576 |
577 |
STree stree(std::move(tb), str, false, false, *lang, mr); |
STree stree(std::move(tb), str, false, false, *lang, mr); |
577 |
|
Tree tree(std::move(lang), str, stree.getRoot()); |
|
|
578 |
|
Tree tree(std::move(lang), tabWidth, str, stree.getRoot()); |
578 |
579 |
|
|
579 |
580 |
const Node *node; |
const Node *node; |
580 |
581 |
|
|
File tests/make/make-parser.cpp changed (mode: 100644) (index e06e903..e0fdc82) |
... |
... |
TEST_CASE("New line node does not appear in the tree", "[make][parser]") |
780 |
780 |
|
|
781 |
781 |
TEST_CASE("Tabulation of size 1 is allowed in Makefiles", "[make][parser]") |
TEST_CASE("Tabulation of size 1 is allowed in Makefiles", "[make][parser]") |
782 |
782 |
{ |
{ |
|
783 |
|
int tabWidth = 1; |
|
784 |
|
|
783 |
785 |
const char *const str = "" |
const char *const str = "" |
784 |
786 |
"# 0\n" |
"# 0\n" |
785 |
787 |
"\t# 1\n" |
"\t# 1\n" |
|
... |
... |
TEST_CASE("Tabulation of size 1 is allowed in Makefiles", "[make][parser]") |
789 |
791 |
cpp17::pmr::monolithic mr; |
cpp17::pmr::monolithic mr; |
790 |
792 |
std::unique_ptr<Language> lang = Language::create("Makefile"); |
std::unique_ptr<Language> lang = Language::create("Makefile"); |
791 |
793 |
|
|
792 |
|
TreeBuilder tb = lang->parse(str, "<input>", /*tabWidth=*/1, |
|
793 |
|
/*debug=*/false, mr); |
|
|
794 |
|
TreeBuilder tb = lang->parse(str, "<input>", tabWidth, /*debug=*/false, mr); |
794 |
795 |
REQUIRE_FALSE(tb.hasFailed()); |
REQUIRE_FALSE(tb.hasFailed()); |
795 |
796 |
|
|
796 |
797 |
STree stree(std::move(tb), str, false, false, *lang, mr); |
STree stree(std::move(tb), str, false, false, *lang, mr); |
797 |
|
Tree tree(std::move(lang), str, stree.getRoot()); |
|
|
798 |
|
Tree tree(std::move(lang), tabWidth, str, stree.getRoot()); |
798 |
799 |
|
|
799 |
800 |
const Node *node; |
const Node *node; |
800 |
801 |
|
|
File tests/tests.cpp changed (mode: 100644) (index 8db73c1..34ee087) |
... |
... |
parseLua(const std::string &str) |
138 |
138 |
static Tree |
static Tree |
139 |
139 |
parse(const std::string &fileName, const std::string &str, bool coarse) |
parse(const std::string &fileName, const std::string &str, bool coarse) |
140 |
140 |
{ |
{ |
|
141 |
|
const int tabWidth = 4; |
|
142 |
|
|
141 |
143 |
cpp17::pmr::monolithic mr; |
cpp17::pmr::monolithic mr; |
142 |
144 |
std::unique_ptr<Language> lang = Language::create(fileName); |
std::unique_ptr<Language> lang = Language::create(fileName); |
143 |
145 |
|
|
144 |
|
TreeBuilder tb = lang->parse(str, "<input>", /*tabWidth=*/4, false, mr); |
|
|
146 |
|
TreeBuilder tb = lang->parse(str, "<input>", tabWidth, false, mr); |
145 |
147 |
REQUIRE_FALSE(tb.hasFailed()); |
REQUIRE_FALSE(tb.hasFailed()); |
146 |
148 |
|
|
147 |
149 |
if (!coarse) { |
if (!coarse) { |
148 |
|
return Tree(std::move(lang), str, tb.getRoot()); |
|
|
150 |
|
return Tree(std::move(lang), tabWidth, str, tb.getRoot()); |
149 |
151 |
} |
} |
150 |
152 |
|
|
151 |
153 |
STree stree(std::move(tb), str, false, false, *lang, mr); |
STree stree(std::move(tb), str, false, false, *lang, mr); |
152 |
|
return Tree(std::move(lang), str, stree.getRoot()); |
|
|
154 |
|
return Tree(std::move(lang), tabWidth, str, stree.getRoot()); |
153 |
155 |
} |
} |
154 |
156 |
|
|
155 |
157 |
const Node * |
const Node * |
File tests/tree.cpp changed (mode: 100644) (index 07c094f..f8c1b76) |
16 |
16 |
|
|
17 |
17 |
#include "Catch/catch.hpp" |
#include "Catch/catch.hpp" |
18 |
18 |
|
|
|
19 |
|
#include <boost/algorithm/string/replace.hpp> |
|
20 |
|
|
19 |
21 |
#include "c/C11SType.hpp" |
#include "c/C11SType.hpp" |
|
22 |
|
#include "STree.hpp" |
20 |
23 |
#include "tree.hpp" |
#include "tree.hpp" |
21 |
24 |
|
|
22 |
25 |
#include "tests.hpp" |
#include "tests.hpp" |
|
... |
... |
TEST_CASE("Coarse reduction links matched nodes", "[tree]") |
64 |
67 |
CHECK(findNode(oldTree, test, true) == nullptr); |
CHECK(findNode(oldTree, test, true) == nullptr); |
65 |
68 |
CHECK(findNode(newTree, test, true) == nullptr); |
CHECK(findNode(newTree, test, true) == nullptr); |
66 |
69 |
} |
} |
|
70 |
|
|
|
71 |
|
TEST_CASE("Tabulation size in comments is variable", "[tree]") |
|
72 |
|
{ |
|
73 |
|
int tabWidth = 8; |
|
74 |
|
|
|
75 |
|
const char *const str = "" |
|
76 |
|
"/*\n" |
|
77 |
|
"\t * comment\n" |
|
78 |
|
"\t */" |
|
79 |
|
; |
|
80 |
|
|
|
81 |
|
std::string expanded = str; |
|
82 |
|
boost::replace_all(expanded, "\t", std::string(8, ' ')); |
|
83 |
|
|
|
84 |
|
cpp17::pmr::monolithic mr; |
|
85 |
|
std::unique_ptr<Language> lang = Language::create("file.c"); |
|
86 |
|
|
|
87 |
|
TreeBuilder tb = lang->parse(str, "<input>", tabWidth, /*debug=*/false, mr); |
|
88 |
|
REQUIRE_FALSE(tb.hasFailed()); |
|
89 |
|
|
|
90 |
|
STree stree(std::move(tb), str, false, false, *lang, mr); |
|
91 |
|
Tree tree(std::move(lang), tabWidth, str, stree.getRoot()); |
|
92 |
|
|
|
93 |
|
const Node *node = findNode(tree, |
|
94 |
|
[&](const Node *node) { |
|
95 |
|
if (node->type == Type::Comments) { |
|
96 |
|
if (!node->spelling.empty()) { |
|
97 |
|
return true; |
|
98 |
|
} |
|
99 |
|
} |
|
100 |
|
return false; |
|
101 |
|
}, |
|
102 |
|
/*skipLastLayer=*/false); |
|
103 |
|
|
|
104 |
|
REQUIRE(node != nullptr); |
|
105 |
|
CHECK(node->spelling == expanded); |
|
106 |
|
} |