lean2/src/util/format.h

102 lines
3 KiB
C
Raw Normal View History

2013-07-23 01:30:25 +00:00
/*
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Soonho Kong
*/
#pragma once
#include "sexpr.h"
2013-07-23 01:30:25 +00:00
#include <iostream>
namespace lean {
/**
\brief Format
uses `sexpr` as an internal representation.
2013-07-23 01:30:25 +00:00
nil = nil sexpr
text s = ("TEXT" s )
nest f = ("NEST" f )
choice f1 f2 = ("CHOICE" f1 f2)
compose f1 f2 = ("COMPOSE" f1 f2)
line = ("LINE")
nest n f = ("NEST" n f)
2013-07-23 01:30:25 +00:00
*/
2013-07-23 01:30:25 +00:00
class format {
sexpr m_value;
2013-07-23 01:30:25 +00:00
public:
enum format_kind { NIL, NEST, COMPOSE, CHOICE, LINE, TEXT };
format():m_value(sexpr()) {}
explicit format(sexpr const & v):m_value(v) {}
explicit format(char const * v):m_value(v) {}
explicit format(std::string const & v):m_value(v) {}
explicit format(int v):m_value(v) {}
explicit format(double v):m_value(v) {}
explicit format(name const & v):m_value(v) {}
explicit format(mpz const & v):m_value(v) {}
explicit format(mpq const & v):m_value(v) {}
format(format const & f1, format const & f2) {
m_value = sexpr(sexpr(COMPOSE), sexpr(f1.m_value, f2.m_value));
2013-07-23 01:30:25 +00:00
}
format(format const & f):m_value(f.m_value) {}
2013-07-23 01:30:25 +00:00
format_kind kind() const {
if(is_nil(m_value)) {
return NIL;
}
if(is_cons(m_value)) {
/* CHOICE, COMPOSE, LINE, NEST */
return static_cast<format_kind>(to_int(car(m_value)));
}
return TEXT;
}
2013-07-23 01:30:25 +00:00
unsigned hash() const { return m_value.hash(); }
2013-07-23 01:30:25 +00:00
format & operator=(format const & s);
format & operator=(format&& s);
template<typename T>
format & operator=(T const & v) { return operator=(format(v)); }
std::ostream & display(std::ostream & out, sexpr const & s);
friend std::ostream & operator<<(std::ostream & out, format const & f);
2013-07-23 01:30:25 +00:00
friend format compose(format const & f1, format const & f2) {
return format(f1, f2);
}
friend format choice(format const & f1, format const & f2) {
return format(sexpr(sexpr(CHOICE), sexpr(f1.m_value, f2.m_value)));
}
friend format nest(int i, format const & f) {
return format(sexpr(sexpr(NEST), sexpr(sexpr(i), f.m_value)));
}
2013-07-23 01:30:25 +00:00
friend bool is_compose(format const & f) {
return is_cons(f.m_value) && to_int(car(f.m_value)) == format_kind::COMPOSE;
}
friend bool is_nest(format const & f) {
return is_cons(f.m_value) && to_int(car(f.m_value)) == format_kind::NEST;
}
friend bool is_text(format const & f) {
return !is_cons(f.m_value);
}
friend bool is_line(format const & f) {
return is_cons(f.m_value) && to_int(car(f.m_value)) == format_kind::LINE;
}
friend bool is_choice(format const & f) {
return is_cons(f.m_value) && to_int(car(f.m_value)) == format_kind::CHOICE;
}
2013-07-23 01:30:25 +00:00
friend format flatten(format const & f);
2013-07-23 01:30:25 +00:00
};
inline format line() {
return format(sexpr(format::format_kind::LINE));
}
2013-07-23 01:30:25 +00:00
}