2023-04-09 23:42:20 +03:00
|
|
|
#pragma once
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Common printing functions for the Nix language
|
|
|
|
*
|
|
|
|
* While most types come with their own methods for printing, they share some
|
|
|
|
* functions that are placed here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
#include "print-options.hh"
|
|
|
|
|
2023-04-09 23:42:20 +03:00
|
|
|
namespace nix {
|
2023-12-18 20:34:19 +02:00
|
|
|
|
2023-12-07 02:03:01 +02:00
|
|
|
class EvalState;
|
|
|
|
struct Value;
|
|
|
|
|
2023-12-18 20:34:19 +02:00
|
|
|
/**
|
|
|
|
* Print a string as a Nix string literal.
|
|
|
|
*
|
|
|
|
* Quotes and fairly minimal escaping are added.
|
|
|
|
*
|
2023-12-12 23:57:36 +02:00
|
|
|
* @param o The output stream to print to
|
2023-12-18 20:34:19 +02:00
|
|
|
* @param s The logical string
|
|
|
|
*/
|
|
|
|
std::ostream & printLiteralString(std::ostream & o, std::string_view s);
|
|
|
|
inline std::ostream & printLiteralString(std::ostream & o, const char * s) {
|
|
|
|
return printLiteralString(o, std::string_view(s));
|
|
|
|
}
|
|
|
|
inline std::ostream & printLiteralString(std::ostream & o, const std::string & s) {
|
|
|
|
return printLiteralString(o, std::string_view(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Print `true` or `false`. */
|
|
|
|
std::ostream & printLiteralBool(std::ostream & o, bool b);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print a string as an attribute name in the Nix expression language syntax.
|
|
|
|
*
|
|
|
|
* Prints a quoted string if necessary.
|
|
|
|
*/
|
|
|
|
std::ostream & printAttributeName(std::ostream & o, std::string_view s);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns `true' is a string is a reserved keyword which requires quotation
|
|
|
|
* when printing attribute set field names.
|
|
|
|
*/
|
|
|
|
bool isReservedKeyword(const std::string_view str);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print a string as an identifier in the Nix expression language syntax.
|
|
|
|
*
|
|
|
|
* FIXME: "identifier" is ambiguous. Identifiers do not have a single
|
|
|
|
* textual representation. They can be used in variable references,
|
|
|
|
* let bindings, left-hand sides or attribute names in a select
|
|
|
|
* expression, or something else entirely, like JSON. Use one of the
|
|
|
|
* `print*` functions instead.
|
|
|
|
*/
|
|
|
|
std::ostream & printIdentifier(std::ostream & o, std::string_view s);
|
|
|
|
|
2023-12-12 23:57:36 +02:00
|
|
|
void printValue(EvalState & state, std::ostream & str, Value & v, PrintOptions options = PrintOptions {});
|
|
|
|
|
2023-12-07 02:03:01 +02:00
|
|
|
/**
|
|
|
|
* A partially-applied form of `printValue` which can be formatted using `<<`
|
|
|
|
* without allocating an intermediate string.
|
|
|
|
*/
|
|
|
|
class ValuePrinter {
|
|
|
|
friend std::ostream & operator << (std::ostream & output, const ValuePrinter & printer);
|
|
|
|
private:
|
|
|
|
EvalState & state;
|
|
|
|
Value & value;
|
|
|
|
PrintOptions options;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ValuePrinter(EvalState & state, Value & value, PrintOptions options = PrintOptions {})
|
|
|
|
: state(state), value(value), options(options) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream & operator<<(std::ostream & output, const ValuePrinter & printer);
|
2023-04-09 23:42:20 +03:00
|
|
|
}
|