compilation
This commit is contained in:
23
src/parser.h
23
src/parser.h
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <istream>
|
||||
#include <vector>
|
||||
|
||||
struct Expr {
|
||||
@@ -20,19 +19,19 @@ public:
|
||||
if (child.type == Type::List && child.children.size() >= 2 &&
|
||||
child.children[0].type == Type::Atom &&
|
||||
child.children[0].value == key) {
|
||||
return child.children[1];
|
||||
Expr result = {.type = Type::List};
|
||||
result.children.assign(child.children.begin() + 1,
|
||||
child.children.end());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
throw std::runtime_error("key not found: " + key);
|
||||
}
|
||||
|
||||
static Expr parse(const std::string &input) {
|
||||
std::istringstream in(input);
|
||||
return parse_expr(in);
|
||||
}
|
||||
static Expr parse(std::istream &in) { return parse_expr(in); }
|
||||
|
||||
private:
|
||||
static std::string read_atom(std::istringstream &in) {
|
||||
static std::string read_atom(std::istream &in) {
|
||||
std::string atom;
|
||||
while (true) {
|
||||
int next = in.peek();
|
||||
@@ -48,7 +47,7 @@ private:
|
||||
return atom;
|
||||
}
|
||||
|
||||
static Expr parse_list(std::istringstream &in) {
|
||||
static Expr parse_list(std::istream &in) {
|
||||
Expr result = {.type = Expr::Type::List};
|
||||
|
||||
in.get(); // consume bracket
|
||||
@@ -72,7 +71,7 @@ private:
|
||||
return result;
|
||||
}
|
||||
|
||||
static std::string read_string(std::istringstream &in) {
|
||||
static std::string read_string(std::istream &in) {
|
||||
std::string result;
|
||||
|
||||
in.get(); // consume the quote
|
||||
@@ -91,7 +90,7 @@ private:
|
||||
return result;
|
||||
}
|
||||
|
||||
static Expr parse_expr(std::istringstream &in) {
|
||||
static Expr parse_expr(std::istream &in) {
|
||||
while (is_space(static_cast<char>(in.peek()))) {
|
||||
in.get();
|
||||
}
|
||||
@@ -138,4 +137,4 @@ inline std::ostream &operator<<(std::ostream &out, const Expr &e) {
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user