Skip to content

Commit

Permalink
Mostly complete parsing, list variable expansion and output generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Dec 20, 2023
1 parent 8f74ecd commit 52fbe18
Show file tree
Hide file tree
Showing 22 changed files with 1,258 additions and 54 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ ReferenceAlignment: Left
SeparateDefinitionBlocks : Always
UseTab: Never
#PPDirectiveIndentStyle: AfterHash
Cpp11BracedListStyle: false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/cmake-build-debug
.DS_Store
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- detect circular list variables
2 changes: 1 addition & 1 deletion foundation
Submodule foundation updated 2 files
+2 −2 lib/Util.cc
+2 −1 lib/Util.h
72 changes: 72 additions & 0 deletions src/Bindings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Bindings.cc --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
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 "Bindings.h"


#include <Exception.h>

Bindings::Bindings(Tokenizer& tokenizer) {
auto token = tokenizer.next(Tokenizer::Skip::WHITESPACE);
if (token.type != Tokenizer::TokenType::BEGIN_SCOPE) {
tokenizer.unget(token);
return;
}

while (((token = tokenizer.next(Tokenizer::Skip::SPACE))) && token.type != Tokenizer::TokenType::END_SCOPE) {
if (token.type != Tokenizer::TokenType::WORD) {
throw Exception("invalid variable name");
}
auto name = token.string();
token = tokenizer.next(Tokenizer::Skip::SPACE);
if (token.type != Tokenizer::TokenType::ASSIGN && token.type != Tokenizer::TokenType::ASSIGN_LIST) {
throw Exception("assignment expected");
}
variables[name] = Variable(name, token.type == Tokenizer::TokenType::ASSIGN_LIST, tokenizer);
}
}


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

void Bindings::process(const File& file) {
for (auto& pair : variables) {
pair.second.process(file);
}
}
62 changes: 62 additions & 0 deletions src/Bindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Bindings.h --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
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.
*/

#ifndef BINDINGS_H
#define BINDINGS_H

#include <unordered_map>

#include "Variable.h"

class Bindings {
public:
Bindings() = default;
explicit Bindings(Tokenizer& tokenizer);

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

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

private:
std::unordered_map<std::string, Variable> variables;
};

#endif // BINDINGS_H
61 changes: 61 additions & 0 deletions src/Build.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Build.cc --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
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 "Build.h"


#include "File.h"

Build::Build(Tokenizer& tokenizer) {
outputs = Text{ tokenizer, Tokenizer::TokenType::COLON };
inputs = Text{tokenizer, Tokenizer::TokenType::NEWLINE};
bindings = Bindings{ tokenizer };
}

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

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

void Build::print(std::ostream& stream) const {
stream << std::endl << "build " << outputs << " : " << 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;
}
60 changes: 60 additions & 0 deletions src/Build.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Build.h --
Copyright (C) Dieter Baron
The authors can be contacted at <[email protected]>
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.
*/

#ifndef BUILD_H
#define BUILD_H

#include <unordered_set>
#include <vector>

#include "Bindings.h"
#include "Rule.h"

class Build {
public:
Build() = default;
explicit Build(Tokenizer& tokenizer);

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;

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

#endif // BUILD_H
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ set(PROGRAM fast-ninja)

ADD_EXECUTABLE(fast-ninja
fast-ninja.cc
Bindings.cc
Build.cc
File.cc
Rule.cc
ScopedDirective.cc
Text.cc
Tokenizer.cc
Variable.cc
)
target_include_directories(fast-ninja PRIVATE ${CMAKE_SOURCE_DIR}/foundation/lib ${PROJECT_BINARY_DIR})
target_link_libraries(fast-ninja foundation)
Expand Down
Loading

0 comments on commit 52fbe18

Please sign in to comment.