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
|
2013-07-25 00:50:51 +00:00
|
|
|
#include <algorithm>
|
2013-08-01 22:41:45 +00:00
|
|
|
#include <iostream>
|
2013-08-12 17:47:01 +00:00
|
|
|
#include <numeric>
|
2013-08-01 22:41:45 +00:00
|
|
|
#include <sstream>
|
2013-09-13 10:35:29 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <string>
|
2013-12-09 05:06:24 +00:00
|
|
|
#include <vector>
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/pair.h"
|
|
|
|
#include "util/debug.h"
|
2013-11-27 03:15:49 +00:00
|
|
|
#include "util/lua.h"
|
2013-09-13 03:04:10 +00:00
|
|
|
#include "util/numerics/mpz.h"
|
|
|
|
#include "util/sexpr/sexpr.h"
|
2013-07-23 01:30:25 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2013-08-13 23:19:30 +00:00
|
|
|
class options;
|
2013-07-23 01:30:25 +00:00
|
|
|
/**
|
2013-07-23 23:21:04 +00:00
|
|
|
\brief Format
|
|
|
|
|
|
|
|
uses `sexpr` as an internal representation.
|
2013-07-23 01:30:25 +00:00
|
|
|
|
2013-07-25 00:50:51 +00:00
|
|
|
nil = ["NIL"]
|
|
|
|
text s = ("TEXT" . s)
|
|
|
|
choice f1 f2 = ("CHOICE" f1 . f2)
|
|
|
|
compose f1 ... fn = ["COMPOSE" f1 ... fn]
|
|
|
|
line = ["LINE"]
|
|
|
|
nest n f = ("NEST" n . f)
|
|
|
|
highlight c f = ("HIGHLIGHT" c . f)
|
2013-07-23 01:30:25 +00:00
|
|
|
*/
|
2013-07-23 23:21:04 +00:00
|
|
|
|
2013-07-23 01:30:25 +00:00
|
|
|
class format {
|
|
|
|
public:
|
2013-08-22 05:45:48 +00:00
|
|
|
enum format_kind { NIL, NEST, COMPOSE, FLAT_COMPOSE, CHOICE, LINE, TEXT, COLOR_BEGIN, COLOR_END};
|
2013-07-25 00:50:51 +00:00
|
|
|
enum format_color {RED, GREEN, ORANGE, BLUE, PINK, CYAN, GREY};
|
|
|
|
private:
|
|
|
|
sexpr m_value;
|
|
|
|
static sexpr flatten(sexpr const & s);
|
|
|
|
static format flatten(format const & f);
|
2013-07-23 23:21:04 +00:00
|
|
|
|
2013-07-25 00:50:51 +00:00
|
|
|
// Functions for the internal sexpr representation
|
|
|
|
static inline format_kind sexpr_kind(sexpr const & s) {
|
|
|
|
lean_assert(is_cons(s));
|
|
|
|
return static_cast<format_kind>(to_int(car(s)));
|
2013-07-23 01:30:25 +00:00
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
static inline sexpr sexpr_compose(sexpr const & l) {
|
|
|
|
lean_assert(is_list(l));
|
|
|
|
return sexpr(sexpr(format_kind::COMPOSE), l);
|
|
|
|
}
|
2013-08-22 05:45:48 +00:00
|
|
|
static inline sexpr sexpr_flat_compose(sexpr const & l) {
|
|
|
|
lean_assert(is_list(l));
|
|
|
|
return sexpr(sexpr(format_kind::FLAT_COMPOSE), l);
|
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
static inline sexpr sexpr_compose(std::initializer_list<sexpr> const & l) {
|
|
|
|
return sexpr_compose(sexpr(l));
|
|
|
|
}
|
|
|
|
static inline sexpr const & sexpr_compose_list(sexpr const & s) {
|
2013-08-22 05:45:48 +00:00
|
|
|
lean_assert(sexpr_kind(s) == format_kind::COMPOSE || sexpr_kind(s) == format_kind::FLAT_COMPOSE);
|
2013-07-25 00:50:51 +00:00
|
|
|
return cdr(s);
|
|
|
|
}
|
|
|
|
static inline sexpr sexpr_choice(sexpr const & s1, sexpr const & s2) {
|
|
|
|
return sexpr(sexpr(format_kind::CHOICE), sexpr(s1, s2));
|
|
|
|
}
|
|
|
|
static inline sexpr const & sexpr_choice_1(sexpr const & s) {
|
|
|
|
return car(cdr(s));
|
|
|
|
}
|
|
|
|
static inline sexpr const & sexpr_choice_2(sexpr const & s) {
|
|
|
|
return cdr(cdr(s));
|
|
|
|
}
|
|
|
|
static inline sexpr sexpr_nest(int i, sexpr const & s) {
|
|
|
|
return sexpr(sexpr(format_kind::NEST), sexpr(i, s));
|
|
|
|
}
|
|
|
|
static inline int sexpr_nest_i(sexpr const & s) {
|
|
|
|
lean_assert(sexpr_kind(s) == format_kind::NEST);
|
|
|
|
return to_int(car(cdr(s)));
|
|
|
|
}
|
|
|
|
static inline sexpr const & sexpr_nest_s(sexpr const & s) {
|
|
|
|
lean_assert(sexpr_kind(s) == format_kind::NEST);
|
|
|
|
return cdr(cdr(s));
|
|
|
|
}
|
|
|
|
static inline sexpr sexpr_text(sexpr const & s) {
|
|
|
|
return sexpr(sexpr(format_kind::TEXT), s);
|
|
|
|
}
|
|
|
|
static inline sexpr const & sexpr_text_t(sexpr const & s) {
|
|
|
|
lean_assert(sexpr_kind(s) == format_kind::TEXT);
|
|
|
|
return cdr(s);
|
|
|
|
}
|
2013-09-20 05:52:52 +00:00
|
|
|
static inline size_t sexpr_text_length(sexpr const & s) {
|
2013-08-01 22:41:45 +00:00
|
|
|
lean_assert(sexpr_kind(s) == format_kind::TEXT);
|
2013-08-08 19:23:42 +00:00
|
|
|
sexpr const & content = cdr(s);
|
2013-09-13 23:14:24 +00:00
|
|
|
if (is_string(content)) {
|
2013-12-09 05:06:24 +00:00
|
|
|
return to_string(content).length();
|
2013-08-08 19:23:42 +00:00
|
|
|
} else {
|
2013-12-09 05:06:24 +00:00
|
|
|
std::stringstream ss;
|
2013-08-08 19:23:42 +00:00
|
|
|
ss << content;
|
2013-12-09 05:06:24 +00:00
|
|
|
return ss.str().length();
|
2013-08-08 19:23:42 +00:00
|
|
|
}
|
2013-08-01 22:41:45 +00:00
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
static inline sexpr sexpr_text(std::string const & s) {
|
|
|
|
return sexpr(sexpr(format_kind::TEXT), sexpr(s));
|
|
|
|
}
|
|
|
|
static inline sexpr sexpr_color_begin(format_color c) {
|
|
|
|
return sexpr(sexpr(format_kind::COLOR_BEGIN), sexpr(c));
|
|
|
|
}
|
|
|
|
static inline format_color sexpr_color_begin(sexpr const & s) {
|
|
|
|
lean_assert(sexpr_kind(s) == format_kind::TEXT);
|
|
|
|
return static_cast<format_color>(to_int(cdr(s)));
|
|
|
|
}
|
|
|
|
static inline sexpr sexpr_color_end() {
|
|
|
|
return sexpr{sexpr(format_kind::COLOR_END)};
|
|
|
|
}
|
|
|
|
static inline sexpr sexpr_highlight(sexpr const & s, format_color c) {
|
|
|
|
return sexpr_compose({sexpr_color_begin(c), s, sexpr_color_end()});
|
|
|
|
}
|
|
|
|
static inline sexpr sexpr_nil() {
|
|
|
|
return sexpr{sexpr(format::format_kind::NIL)};
|
|
|
|
}
|
|
|
|
static inline sexpr sexpr_line() {
|
|
|
|
return sexpr{sexpr(format::format_kind::LINE)};
|
2013-07-23 23:21:04 +00:00
|
|
|
}
|
2013-07-23 01:30:25 +00:00
|
|
|
|
2013-07-25 00:50:51 +00:00
|
|
|
// Functions used inside of pretty printing
|
2013-12-09 05:06:24 +00:00
|
|
|
static bool space_upto_line_break_list_exceeded(sexpr const & s, int available, std::vector<std::pair<sexpr, unsigned>> const & todo);
|
2013-08-22 05:45:48 +00:00
|
|
|
static int space_upto_line_break(sexpr const & s, int available, bool & found);
|
2013-07-23 01:30:25 +00:00
|
|
|
|
2013-07-25 00:50:51 +00:00
|
|
|
static bool is_fnil(format const & f) {
|
|
|
|
return to_int(car(f.m_value)) == format_kind::NIL;
|
2013-07-23 23:21:04 +00:00
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
static bool is_compose(format const & f) {
|
|
|
|
return to_int(car(f.m_value)) == format_kind::COMPOSE;
|
2013-07-23 23:21:04 +00:00
|
|
|
}
|
2013-08-22 05:45:48 +00:00
|
|
|
static bool is_flat_compose(format const & f) {
|
|
|
|
return to_int(car(f.m_value)) == format_kind::FLAT_COMPOSE;
|
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
static bool is_nest(format const & f) {
|
|
|
|
return to_int(car(f.m_value)) == format_kind::NEST;
|
2013-07-23 23:21:04 +00:00
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
static bool is_text(format const & f) {
|
|
|
|
return to_int(car(f.m_value)) == format_kind::TEXT;
|
|
|
|
}
|
|
|
|
static bool is_line(format const & f) {
|
|
|
|
return to_int(car(f.m_value)) == format_kind::LINE;
|
2013-07-23 23:21:04 +00:00
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
static bool is_choice(format const & f) {
|
|
|
|
return to_int(car(f.m_value)) == format_kind::CHOICE;
|
2013-07-23 23:21:04 +00:00
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
friend format choice(format const & f1, format const & f2) {
|
|
|
|
return format(sexpr_choice(f1.m_value, f2.m_value));
|
2013-07-23 23:21:04 +00:00
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Constructors
|
|
|
|
format():m_value(sexpr_nil()) {}
|
|
|
|
explicit format(sexpr const & v):m_value(v) {}
|
|
|
|
explicit format(char const * v):m_value(sexpr_text(sexpr(v))) {}
|
|
|
|
explicit format(std::string const & v):m_value(sexpr_text(sexpr(v))) {}
|
2013-08-01 22:41:45 +00:00
|
|
|
explicit format(int v):m_value(sexpr_text(sexpr(v))) {}
|
|
|
|
explicit format(double v):m_value(sexpr_text(sexpr(v))) {}
|
2013-08-08 02:10:12 +00:00
|
|
|
explicit format(unsigned v):m_value(sexpr_text(sexpr(mpz(v)))) {}
|
2013-07-25 00:50:51 +00:00
|
|
|
explicit format(name const & v):m_value(sexpr_text(sexpr(v))) {}
|
|
|
|
explicit format(mpz const & v):m_value(sexpr_text(sexpr(v))) {}
|
|
|
|
explicit format(mpq const & v):m_value(sexpr_text(sexpr(v))) {}
|
|
|
|
format(format const & f1, format const & f2):m_value(sexpr_compose({f1.m_value, f2.m_value})) {}
|
|
|
|
format(format const & f):m_value(f.m_value) {}
|
|
|
|
format(std::initializer_list<format> const & l):format() {
|
|
|
|
lean_assert(l.size() >= 2);
|
|
|
|
auto it = l.begin();
|
|
|
|
sexpr const & s1 = (it++)->m_value;
|
|
|
|
sexpr const & s2 = (it++)->m_value;
|
|
|
|
m_value = sexpr_compose({s1, s2});
|
|
|
|
|
|
|
|
m_value = std::accumulate(it, l.end(), m_value,
|
|
|
|
[](sexpr const & result, const format f) {
|
|
|
|
return sexpr_compose({result, f.m_value});
|
2013-09-13 23:14:24 +00:00
|
|
|
});
|
2013-07-23 23:21:04 +00:00
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
|
|
|
|
format_kind kind() const {
|
|
|
|
return sexpr_kind(m_value);
|
2013-07-23 23:21:04 +00:00
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
unsigned hash() const { return m_value.hash(); }
|
|
|
|
|
2013-12-03 07:02:39 +00:00
|
|
|
explicit operator bool() const { return static_cast<bool>(m_value); }
|
2013-10-14 21:37:35 +00:00
|
|
|
|
2013-07-25 00:50:51 +00:00
|
|
|
friend format compose(format const & f1, format const & f2);
|
|
|
|
friend format nest(int i, format const & f);
|
2013-09-25 04:30:56 +00:00
|
|
|
friend format highlight(format const & f, format::format_color const c);
|
2013-08-09 01:35:49 +00:00
|
|
|
friend format mk_line();
|
2013-07-25 00:50:51 +00:00
|
|
|
|
|
|
|
friend format group(format const & f);
|
|
|
|
friend format above(format const & f1, format const & f2);
|
2013-10-01 04:36:56 +00:00
|
|
|
friend format bracket(std::string const & l, format const & x, std::string const & r);
|
2013-07-25 00:50:51 +00:00
|
|
|
friend format wrap(format const & f1, format const & f2);
|
2013-07-23 01:30:25 +00:00
|
|
|
|
2013-08-03 02:17:29 +00:00
|
|
|
// x + y = x <> y
|
|
|
|
friend format operator+(format const & f1, format const & f2);
|
2013-07-25 00:50:51 +00:00
|
|
|
format & operator+=(format const & f) {
|
2013-08-01 22:41:45 +00:00
|
|
|
*this = *this + f;
|
2013-07-25 00:50:51 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2013-08-03 02:17:29 +00:00
|
|
|
|
|
|
|
// x ^ y = x <> " " <> y
|
|
|
|
friend format operator^(format const & f1, format const & f2);
|
|
|
|
format & operator^=(format const & f) {
|
|
|
|
*this = *this ^ f;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-12-09 05:06:24 +00:00
|
|
|
static std::ostream & pretty(std::ostream & out, unsigned w, bool colors, format const & f);
|
2013-08-13 23:19:30 +00:00
|
|
|
friend std::ostream & pretty(std::ostream & out, unsigned w, bool colors, format const & f);
|
2013-07-25 00:50:51 +00:00
|
|
|
friend std::ostream & pretty(std::ostream & out, unsigned w, format const & f);
|
2013-08-13 23:19:30 +00:00
|
|
|
friend std::ostream & pretty(std::ostream & out, options const & o, format const & f);
|
|
|
|
|
|
|
|
friend std::ostream & operator<<(std::ostream & out, format const & f);
|
|
|
|
friend std::ostream & operator<<(std::ostream & out, std::pair<format const &, options const &> const & p);
|
2013-08-19 19:04:40 +00:00
|
|
|
|
|
|
|
/** \brief Return true iff f is just a name */
|
|
|
|
friend bool is_name(format const & f) { return format::is_text(f) && ::lean::is_name(cdr(f.m_value)); }
|
2013-07-23 01:30:25 +00:00
|
|
|
};
|
|
|
|
|
2013-07-25 00:50:51 +00:00
|
|
|
format wrap(format const & f1, format const & f2);
|
|
|
|
format compose(format const & f1, format const & f2);
|
|
|
|
format nest(int i, format const & f);
|
2013-09-25 04:30:56 +00:00
|
|
|
format highlight(format const & f, format::format_color const c = format::RED);
|
2013-08-15 15:32:13 +00:00
|
|
|
format highlight_keyword(format const & f);
|
|
|
|
format highlight_builtin(format const & f);
|
|
|
|
format highlight_command(format const & f);
|
2013-08-09 01:35:49 +00:00
|
|
|
format const & line();
|
|
|
|
format const & space();
|
|
|
|
format const & lp();
|
|
|
|
format const & rp();
|
2013-09-13 01:25:38 +00:00
|
|
|
format const & lsb();
|
|
|
|
format const & rsb();
|
2013-08-27 01:46:16 +00:00
|
|
|
format const & lcurly();
|
|
|
|
format const & rcurly();
|
2013-08-09 01:35:49 +00:00
|
|
|
format const & comma();
|
2013-08-14 02:12:23 +00:00
|
|
|
format const & colon();
|
2013-08-09 01:35:49 +00:00
|
|
|
format const & dot();
|
2013-07-25 00:50:51 +00:00
|
|
|
format group(format const & f);
|
|
|
|
format above(format const & f1, format const & f2);
|
2013-10-01 04:36:56 +00:00
|
|
|
format bracket(std::string const & l, format const & x, std::string const & r);
|
2013-08-02 01:03:51 +00:00
|
|
|
format paren(format const & x);
|
2013-07-25 00:50:51 +00:00
|
|
|
format wrap(format const & f1, format const & f2);
|
|
|
|
|
2013-08-03 02:48:11 +00:00
|
|
|
// is_iterator
|
|
|
|
template<typename T, typename = void>
|
2013-09-13 23:14:24 +00:00
|
|
|
struct is_iterator {
|
|
|
|
static constexpr bool value = false;
|
2013-08-03 02:48:11 +00:00
|
|
|
};
|
|
|
|
template<typename T>
|
2013-09-13 23:14:24 +00:00
|
|
|
struct is_iterator<T, typename std::enable_if<!std::is_same<typename std::iterator_traits<T>::value_type, void>::value>::type> {
|
|
|
|
static constexpr bool value = true;
|
2013-08-03 02:48:11 +00:00
|
|
|
};
|
|
|
|
|
2013-07-25 00:50:51 +00:00
|
|
|
template <class InputIterator, typename F>
|
|
|
|
format folddoc(InputIterator first, InputIterator last, F f) {
|
2013-08-03 02:48:11 +00:00
|
|
|
// InputIterator : iterator<T>
|
|
|
|
static_assert(is_iterator<InputIterator>::value, "folddoc takes non-iterator type arguments");
|
|
|
|
// F : T x format -> format
|
|
|
|
static_assert(std::is_same<typename std::result_of<F(typename std::iterator_traits<InputIterator>::value_type,
|
|
|
|
format)>::type, format>::value,
|
|
|
|
"folddoc: return type of f is not format");
|
2013-09-13 23:14:24 +00:00
|
|
|
if (first == last) { return format(); }
|
2013-08-03 02:48:11 +00:00
|
|
|
return f(*first, folddoc(first + 1, last, f));
|
2013-07-25 00:50:51 +00:00
|
|
|
}
|
|
|
|
template <class InputIterator>
|
|
|
|
format spread(InputIterator first, InputIterator last) {
|
2013-08-03 02:48:11 +00:00
|
|
|
static_assert(std::is_same<typename std::iterator_traits<InputIterator>::value_type, format>::value,
|
2013-08-08 23:12:51 +00:00
|
|
|
"stack takes an argument which is not an iterator containing format.");
|
2013-07-25 00:50:51 +00:00
|
|
|
return folddoc(first, last, compose);
|
|
|
|
}
|
2013-08-03 02:48:11 +00:00
|
|
|
inline format spread(std::initializer_list<format> const & l) {
|
|
|
|
return spread(l.begin(), l.end());
|
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
template <class InputIterator>
|
|
|
|
format stack(InputIterator first, InputIterator last) {
|
2013-08-03 02:48:11 +00:00
|
|
|
static_assert(std::is_same<typename std::iterator_traits<InputIterator>::value_type, format>::value,
|
2013-08-08 23:12:51 +00:00
|
|
|
"stack takes an argument which is not an iterator containing format.");
|
2013-07-25 00:50:51 +00:00
|
|
|
return folddoc(first, last, above);
|
|
|
|
}
|
2013-08-03 02:48:11 +00:00
|
|
|
inline format stack(std::initializer_list<format> const & l) {
|
|
|
|
return stack(l.begin(), l.end());
|
|
|
|
}
|
2013-07-25 00:50:51 +00:00
|
|
|
template <typename InputIterator>
|
|
|
|
format fill(InputIterator first, InputIterator last) {
|
2013-08-03 02:48:11 +00:00
|
|
|
static_assert(std::is_same<typename std::iterator_traits<InputIterator>::value_type, format>::value,
|
2013-08-08 23:12:51 +00:00
|
|
|
"fill takes an argument which is not an iterator containing format.");
|
2013-07-25 00:50:51 +00:00
|
|
|
return folddoc(first, last, wrap);
|
2013-07-23 23:21:04 +00:00
|
|
|
}
|
2013-08-03 02:48:11 +00:00
|
|
|
inline format fill(std::initializer_list<format> const & l) {
|
|
|
|
return fill(l.begin(), l.end());
|
|
|
|
}
|
|
|
|
template <typename InputIterator>
|
|
|
|
format fillwords(InputIterator first, InputIterator last) {
|
|
|
|
static_assert(std::is_same<typename std::iterator_traits<InputIterator>::value_type,
|
|
|
|
typename std::string>::value,
|
2013-08-08 23:12:51 +00:00
|
|
|
"fillwords takes an argument which is not an iterator containing std::string.");
|
2013-08-03 02:48:11 +00:00
|
|
|
return folddoc(first, last, [](std::string const & s, format const & r) { return wrap(format(s), r); } );
|
|
|
|
}
|
|
|
|
inline format fillwords(std::initializer_list<std::string> const & l) {
|
|
|
|
return fillwords(l.begin(), l.end());
|
|
|
|
}
|
2013-08-09 01:35:49 +00:00
|
|
|
class options;
|
|
|
|
/** \brief Extract indentation from options */
|
|
|
|
unsigned get_pp_indent(options const & o);
|
2013-09-03 17:09:19 +00:00
|
|
|
/** \brief Return unicode characters flag */
|
|
|
|
bool get_pp_unicode(options const & o);
|
2013-08-09 01:35:49 +00:00
|
|
|
|
|
|
|
/** \brief Format a hierarchical name */
|
|
|
|
format pp(name const & n);
|
|
|
|
|
|
|
|
/** \brief Format a S-expression */
|
|
|
|
format pp(sexpr const & s, options const & o);
|
|
|
|
format pp(sexpr const & s);
|
2013-11-27 03:15:49 +00:00
|
|
|
|
|
|
|
UDATA_DEFS(format)
|
|
|
|
void open_format(lua_State * L);
|
2013-07-23 01:30:25 +00:00
|
|
|
}
|