Files

47 lines
1.7 KiB
C++

#pragma once
#include <string>
#include <unordered_map>
#include <vector>
#include "sedpp/Command.h"
#include "sedpp/Options.h"
namespace sedpp {
// Converts raw -e/-f script text into a flat Program. The compiler is deliberately
// syntax-oriented; command range state and "last regex" behavior are resolved by
// Runner because they depend on input cycles.
class Compiler {
public:
explicit Compiler(const Options &options);
[[nodiscard]] Program compile(const std::vector<std::string> &scripts);
[[nodiscard]] const std::unordered_map<std::string, std::size_t> &labels() const {
return labels_;
}
private:
// Script parsing is a small cursor parser because sed syntax is compact and
// delimiter-driven; tokenizing first tends to lose useful byte positions.
void compileOne(const std::string &script);
[[nodiscard]] std::optional<Address> parseAddress(const std::string &script,
std::size_t &pos,
bool secondAddress);
[[nodiscard]] std::string parseDelimited(const std::string &script,
std::size_t &pos, char delimiter,
bool regexMode = false,
bool unescapeDelimiter = false);
[[nodiscard]] std::string readUntilCommandEnd(const std::string &script,
std::size_t &pos);
static void skipBlanks(const std::string &script, std::size_t &pos);
static void skipSeparators(const std::string &script, std::size_t &pos);
const Options &options_;
Program program_;
std::unordered_map<std::string, std::size_t> labels_;
int braceDepth_ = 0;
};
} // namespace sedpp