Skip to content

Commit

Permalink
Don’t guess what’s a file name.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Apr 12, 2024
1 parent 01043ca commit 9c340ec
Show file tree
Hide file tree
Showing 45 changed files with 2,041 additions and 369 deletions.
71 changes: 71 additions & 0 deletions Grammar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
file:
< statement NEWLINE > +

statement:
variable-assignment
| rule
| build
| default
| pool
| include
| subninja

build:
BUILD filename-list ':' IDENTIFIER filename-list NEWLINE [ BEGIN_SCOPE variable-assignments END_SCOPE ]

rule:
RULE IDENTIFIER NEWLINE BEGIN_SCOPE variable-assignments END_SCOPE

pool:
POOL IDENTIFIER NEWLINE BEGIN_SCOPE variable-assignments END_SCOPE

default:
DEFAULT filename-list

include:
INCLUDE filename-list

subninja:
SUBNINJA filename-list

variable-assignments:
< variable-assignment NEWLINE > +

variable-assignment:
variable '=' variable-text
| single-filename-variable ':=' filename
| multi-filename-variable ':=' [ filename-list ] BEGIN_SCOPE multi-line-filename-list END_SCOPE

filename-list:
filename
| multi-file-variable
| filename-list SPACE filename
| filename-list SPACE multi-file-variable

multi-line-filename-list:
filename
| multi-file-variable
| filename-list [SPACE | NEWLINE] filename
| filename-list [SPACE | NEWLINE] multi-file-variable

filename:
literal-filename
explicit-filename
| single-file-variable

literal-filename:
< WORD | variable > +

variable-text:
< variable-text-component > *

variable-text-component:
WORD
| explicit-filename
| SPACE
| variable
| single-filename-variable
| multi-filename-variable

explicit-filename:
'{{' < WORD SPACE > + '}}'
28 changes: 14 additions & 14 deletions src/Bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "Bindings.h"


#include "FilenameVariable.h"
#include "TextVariable.h"
#include <Exception.h>

Bindings::Bindings(Tokenizer& tokenizer) {
Expand All @@ -47,31 +48,30 @@ Bindings::Bindings(Tokenizer& tokenizer) {
}
auto name = token.string();
token = tokenizer.next(Tokenizer::Skip::SPACE);
if (token.type != Tokenizer::TokenType::ASSIGN && token.type != Tokenizer::TokenType::ASSIGN_LIST) {
if (token.type == Tokenizer::TokenType::ASSIGN) {
variables[name] = std::unique_ptr<Variable>(new TextVariable{name, tokenizer});
}
else if (token.type == Tokenizer::TokenType::ASSIGN_LIST) {
variables[name] = std::unique_ptr<Variable>(new FilenameVariable{name, tokenizer});
}
else {
throw Exception("assignment expected");
}
variables[name] = Variable(name, token.type == Tokenizer::TokenType::ASSIGN_LIST, tokenizer);
}
}

Bindings::Bindings(const std::vector<Variable>& variable_list) {
for (auto& variable: variable_list) {
variables[variable.name] = variable;
}
}

void Bindings::print(std::ostream& stream, const std::string& indent) const {
for (auto& pair : *this) {
if (!pair.second.is_list) {
if (!pair.second->is_filename()) {
stream << indent;
pair.second.print_definition(stream);
stream << std::endl;
pair.second->print_definition(stream);
}
}
}

void Bindings::process(const File& file) {
void Bindings::resolve(const Scope& scope) {
auto context = ResolveContext{scope};
for (auto& pair : variables) {
pair.second.process(file);
pair.second->resolve(context);
}
}
15 changes: 7 additions & 8 deletions src/Bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,30 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <map>

#include "Tokenizer.h"
#include "Variable.h"

class Scope;

class Bindings {
public:
Bindings() = default;
explicit Bindings(Tokenizer& tokenizer);
explicit Bindings(const std::vector<Variable>& variable_list);

void print(std::ostream& stream, const std::string& indent) const;
void process(const File& file);
void add(const std::string& name, Variable variable) {variables[name] = std::move(variable);}
void resolve(const Scope& scope);
void add(std::shared_ptr<Variable> variable) {variables[variable->name] = std::move(variable);}

[[nodiscard]] auto empty() const {return variables.empty();}
auto begin() { return variables.begin(); }

auto end() { return variables.end(); }

[[nodiscard]] auto begin() const { return variables.begin(); }

[[nodiscard]] auto end() const { return variables.end(); }
auto find(const std::string& name) {return variables.find(name);}
auto find(const std::string& name) const {return variables.find(name);}
[[nodiscard]] auto find(const std::string& name) const {return variables.find(name);}

private:
std::map<std::string, Variable> variables;
std::map<std::string, std::shared_ptr<Variable>> variables;
};

#endif // BINDINGS_H
28 changes: 10 additions & 18 deletions src/Build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,26 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "File.h"

Build::Build(Tokenizer& tokenizer) {
outputs = Text{ tokenizer, Tokenizer::TokenType::COLON };
for (auto& element: outputs) {
if (element.type == Text::ElementType::WORD) {
element.type = Text::ElementType::BUILD_FILE;
}
}
Build::Build(const File* file, Tokenizer& tokenizer): ScopedDirective(file) {
outputs = Dependencies{tokenizer, true};
tokenizer.expect(Tokenizer::TokenType::COLON, Tokenizer::Skip::SPACE);
rule_name = tokenizer.expect(Tokenizer::TokenType::WORD, Tokenizer::Skip::SPACE).string();
inputs = Text{tokenizer, Tokenizer::TokenType::NEWLINE};
bindings = Bindings{ tokenizer };
inputs = Dependencies{tokenizer, false};
bindings = Bindings{tokenizer};
}

Build::Build(const File* file, std::string rule_name, Dependencies outputs, Dependencies inputs, Bindings bindings): ScopedDirective{file, std::move(bindings)}, rule_name{std::move(rule_name)}, outputs{std::move(outputs)}, inputs{std::move(inputs)} {}

void Build::process(const File& file) {
inputs.process(file);
bindings.process(file);
inputs.resolve(file);
bindings.resolve(file);
}

void Build::process_outputs(const File& file) {
outputs.process(file);
outputs.resolve(file);
}

void Build::print(std::ostream& stream) const {
stream << std::endl << "build " << outputs << " : " << rule_name << " " << inputs << std::endl;
bindings.print(stream, " ");
}

std::unordered_set<std::string> Build::get_outputs() const {
auto result = std::unordered_set<std::string>{};
outputs.collect_words(result);
return result;
}
17 changes: 8 additions & 9 deletions src/Build.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,28 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define BUILD_H

#include <unordered_set>
#include <vector>

#include "Bindings.h"
#include "Dependencies.h"
#include "Rule.h"
#include "ScopedDirective.h"

class Build {
class Build: public ScopedDirective {
public:
Build() = default;
explicit Build(Tokenizer& tokenizer);
Build(std::string rule_name, Text outputs, Text inputs, Bindings bindings): rule_name{std::move(rule_name)}, outputs{std::move(outputs)}, inputs{std::move(inputs)}, bindings{std::move(bindings)} {}
explicit Build(const File* file, Tokenizer& tokenizer);
Build(const File* file, std::string rule_name, Dependencies outputs, Dependencies inputs, Bindings bindings);

void process(const File& file);
void process_outputs(const File& file);
void print(std::ostream& stream) const;

[[nodiscard]] std::unordered_set<std::string> get_outputs() const;
void collect_output_files(std::unordered_set<std::filesystem::path>& output_files) const {outputs.collect_output_files(output_files);}

private:
const Rule* rule{};
std::string rule_name;
Text outputs;
Text inputs;
Bindings bindings;
Dependencies outputs;
Dependencies inputs;
};

#endif // BUILD_H
10 changes: 10 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ ADD_EXECUTABLE(fast-ninja
fast-ninja.cc
Bindings.cc
Build.cc
Dependencies.cc
File.cc
Filename.cc
FilenameList.cc
FilenameVariable.cc
FilenameWord.cc
Pool.cc
ResolveContext.cc
Rule.cc
Scope.cc
ScopedDirective.cc
Text.cc
TextVariable.cc
Tokenizer.cc
Variable.cc
VariableReference.cc
Word.cc
)
target_include_directories(fast-ninja PRIVATE ${CMAKE_SOURCE_DIR}/foundation/lib ${PROJECT_BINARY_DIR})
target_link_libraries(fast-ninja foundation)
Expand Down
124 changes: 124 additions & 0 deletions src/Dependencies.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Dependencies.cc --
Copyright (C) Dieter Baron
The authors can be contacted at <accelerate@tpau.group>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "Dependencies.h"


#include <Exception.h>

Dependencies::Dependencies(Tokenizer& tokenizer, bool is_build) {
auto type = is_build ? FilenameList::BUILD : FilenameList::INLINE;

direct = FilenameList{tokenizer, type};

while (true) {
tokenizer.skip_space();
const auto token = tokenizer.next();
switch (token.type) {
case Tokenizer::TokenType::END:
case Tokenizer::TokenType::NEWLINE:
case Tokenizer::TokenType::COLON:
tokenizer.unget(token);
return;

case Tokenizer::TokenType::IMPLICIT_DEPENDENCY:
implicit = FilenameList(tokenizer, type);
break;

case Tokenizer::TokenType::ORDER_DEPENDENCY:
order = FilenameList(tokenizer, type);
break;

case Tokenizer::TokenType::VALIDATION_DEPENDENCY:
validation = FilenameList(tokenizer, type);
break;

default:
throw Exception("internal error: %s not included in filename", token.type_name().c_str());
}
}
}


void Dependencies::resolve(const Scope& scope) {
auto context = ResolveContext{scope};
direct.resolve(context);
implicit.resolve(context);
order.resolve(context);
validation.resolve(context);
}

void Dependencies::serialize(std::ostream& stream) const {
auto first = true;
if (!direct.empty()) {
stream << direct;
first = false;
}
if (!implicit.empty()) {
if (first) {
first = false;
}
else {
stream << " ";
}
stream << "| " << implicit;
}
if (!order.empty()) {
if (first) {
first = false;
}
else {
stream << " ";
}
stream << "|| " << order;
}
if (!validation.empty()) {
if (first) {
first = false;
}
else {
stream << " ";
}
stream << "|@ " << validation;
}
}

void Dependencies::collect_output_files(std::unordered_set<std::filesystem::path>& output_files) const {
direct.collect_output_files(output_files);
implicit.collect_output_files(output_files);
order.collect_output_files(output_files);
validation.collect_output_files(output_files);
}

std::ostream& operator<<(std::ostream& stream, const Dependencies& dependencies) {
dependencies.serialize(stream);
return stream;
}
Loading

0 comments on commit 9c340ec

Please sign in to comment.