From 7bcae8f6a80a917e5295548ae1ddf652b2303ca1 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 9 Nov 2016 20:25:20 -0800 Subject: [PATCH] :art: flatten_grammar --- .../prepare_grammar/flatten_grammar_spec.cc | 56 +++++++++---------- .../prepare_grammar/flatten_grammar.cc | 6 +- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/spec/compiler/prepare_grammar/flatten_grammar_spec.cc b/spec/compiler/prepare_grammar/flatten_grammar_spec.cc index 80fe1332..eab2b61c 100644 --- a/spec/compiler/prepare_grammar/flatten_grammar_spec.cc +++ b/spec/compiler/prepare_grammar/flatten_grammar_spec.cc @@ -6,9 +6,8 @@ #include "helpers/rule_helpers.h" template -std::vector::type> -collect(const std::vector &v, Func f) { - vector::type> result; +vector::type> collect(const vector &v, Func f) { + vector::type> result; for (const T &item : v) result.push_back(f(item)); return result; @@ -21,6 +20,30 @@ using prepare_grammar::flatten_grammar; using prepare_grammar::InitialSyntaxGrammar; describe("flatten_grammar", []() { + auto get_symbol_sequences = [&](vector productions) { + return collect(productions, [](Production p) { + return collect(p, [](ProductionStep e) { + return e.symbol; + }); + }); + }; + + auto get_precedence_sequences = [&](vector productions) { + return collect(productions, [](Production p) { + return collect(p, [](ProductionStep e) { + return e.precedence; + }); + }); + }; + + auto get_associativity_sequences = [&](vector productions) { + return collect(productions, [](Production p) { + return collect(p, [](ProductionStep e) { + return e.associativity; + }); + }); + }; + InitialSyntaxGrammar input_grammar{{ // Choices within rules are extracted, resulting in multiple productions. @@ -59,35 +82,10 @@ describe("flatten_grammar", []() { i_sym(4), })), })) - }, {}, {}}; SyntaxGrammar grammar = flatten_grammar(input_grammar); - auto get_symbol_sequences = [&](vector productions) { - return collect(productions, [](Production p) { - return collect(p, [](ProductionStep e) { - return e.symbol; - }); - }); - }; - - auto get_precedence_sequences = [&](vector productions) { - return collect(productions, [](Production p) { - return collect(p, [](ProductionStep e) { - return e.precedence; - }); - }); - }; - - auto get_associativity_sequences = [&](vector productions) { - return collect(productions, [](Production p) { - return collect(p, [](ProductionStep e) { - return e.associativity; - }); - }); - }; - it("preserves the names and types of the grammar's variables", [&]() { AssertThat(grammar.variables[0].name, Equals("variable0")); AssertThat(grammar.variables[1].name, Equals("variable1")); @@ -142,7 +140,7 @@ describe("flatten_grammar", []() { }))); }); - it("associates each symbol with the correct associativity annotation", [&]() { + it("associates each symbol with the correct associativity", [&]() { Associativity none = AssociativityNone; AssertThat( diff --git a/src/compiler/prepare_grammar/flatten_grammar.cc b/src/compiler/prepare_grammar/flatten_grammar.cc index a84cd43e..76b6e769 100644 --- a/src/compiler/prepare_grammar/flatten_grammar.cc +++ b/src/compiler/prepare_grammar/flatten_grammar.cc @@ -1,5 +1,4 @@ #include "compiler/prepare_grammar/flatten_grammar.h" -#include #include #include #include "compiler/prepare_grammar/extract_choices.h" @@ -14,7 +13,6 @@ namespace tree_sitter { namespace prepare_grammar { using std::find; -using std::string; using std::vector; class FlattenRule : public rules::RuleFn { @@ -81,8 +79,8 @@ SyntaxGrammar flatten_grammar(const InitialSyntaxGrammar &grammar) { vector productions; for (const rule_ptr &rule_component : extract_choices(variable.rule)) { Production production = FlattenRule().flatten(rule_component); - if (std::find(productions.begin(), productions.end(), production) == - productions.end()) + auto end = productions.end(); + if (find(productions.begin(), end, production) == end) productions.push_back(production); } result.variables.push_back(