Browse Source

make SourceFile use BBBParser

feature/dependency-graph
Martin Dørum 4 years ago
parent
commit
d8b8167187
4 changed files with 26 additions and 43 deletions
  1. 7
    8
      src/BBBParser.h
  2. 14
    30
      src/SourceFile.cc
  3. 4
    4
      src/SourceFile.h
  4. 1
    1
      src/main.cc

+ 7
- 8
src/BBBParser.h View File

BBBParser(std::istream &stream, int flags, int line = 1, int ch = 1): BBBParser(std::istream &stream, int flags, int line = 1, int ch = 1):
flags_(flags), line_(line), ch_(ch), stream_(stream) {} flags_(flags), line_(line), ch_(ch), stream_(stream) {}


void parseLine(const Variables &vars, std::vector<std::string> &values);
void parse(Variables &vars); void parse(Variables &vars);


int peek() { return stream_.peek(); }
int peek2() { stream_.get(); int ch = peek(); stream_.unget(); return ch; }
int get();
void skip(char);
void skip() { get(); }

int flags_; int flags_;
int line_; int line_;
int ch_; int ch_;
EQUALS_PLUS, EQUALS_PLUS,
NONE, NONE,
}; };

int peek() { return stream_.peek(); }
int peek2() { stream_.get(); int ch = peek(); stream_.unget(); return ch; }
int get();
Operator readOperator();
void skip(char);
void skip() { get(); }
[[noreturn]] void error(std::string); [[noreturn]] void error(std::string);


Operator readOperator();
void skipWhitespaceLine(); void skipWhitespaceLine();
void skipWhitespace(); void skipWhitespace();



+ 14
- 30
src/SourceFile.cc View File

#include <fstream> #include <fstream>
#include <sstream> #include <sstream>


static bool startsWith(const std::string &a, const std::string &b, std::string &after) {
if (a.size() < b.size())
return false;

for (size_t i = 0; i < b.size(); ++i) {
if (a[i] != b[i])
static bool startsWith(BBBParser &parser, const char *str) {
for (size_t i = 0; str[i] != '\0'; ++i) {
if (parser.get() != str[i])
return false; return false;
} }


after = a.substr(b.size());
return true; return true;
} }


static void parseParts(std::vector<std::string> &vec, const std::string &str) {
std::istringstream ss(str);
std::string part;
while (ss >> part) {
vec.push_back(part);
}
}

SourceFile::SourceFile(std::string dir, std::string name):
dir_(std::move(dir)), name_(std::move(name)) {
SourceFile::SourceFile(std::string dir, std::string name, BBBParser::Variables vars):
dir_(std::move(dir)), name_(std::move(name)), vars_(std::move(vars)) {


std::ifstream file(dir_ + "/" + name_); std::ifstream file(dir_ + "/" + name_);

std::string line;
std::string content;
while (std::getline(file, line)) {
if (line.size() < 4) continue;
if (line[0] != '/' || line[1] != '/' || line[2] != '#') continue;

if (startsWith(line, "//# pkgs:", content)) {
parseParts(pkgs_, content);
} else if (startsWith(line, "//# cflags:", content)) {
parseParts(pkgs_, content);
} else if (startsWith(line, "//# ldflags:", content)) {
parseParts(pkgs_, content);
BBBParser parser(file, BBBParser::FLAG_ONE_LINE);

bool freshLine = true;
while (file.good()) {
if (freshLine && startsWith(parser, "//#bb")) {
parser.parse(vars_);
} else {
while (file.good() && parser.get() != '\n');
freshLine = true;
} }
} }
} }

+ 4
- 4
src/SourceFile.h View File

#include <vector> #include <vector>
#include <string> #include <string>


#include "BBBParser.h"

class SourceFile { class SourceFile {
public: public:
SourceFile(std::string dir, std::string name);
SourceFile(std::string dir, std::string name, BBBParser::Variables vars);


private: private:
std::string dir_; std::string dir_;
std::string name_; std::string name_;
std::vector<std::string> pkgs_;
std::vector<std::string> cflags_;
std::vector<std::string> ldflags_;
BBBParser::Variables vars_;
}; };

+ 1
- 1
src/main.cc View File

if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
readDir(path, sources, vars); readDir(path, sources, vars);
} else { } else {
sources.emplace_back(dir, ent);
sources.emplace_back(dir, ent, vars);
} }
} }
} }

Loading…
Cancel
Save