2021-10-21 14:22:06 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
2021-09-07 17:37:23 -04:00
|
|
|
//
|
2021-10-21 14:22:06 -04:00
|
|
|
// print, pprint, and tostring
|
2021-09-07 17:37:23 -04:00
|
|
|
//
|
2021-10-21 14:22:06 -04:00
|
|
|
// This module implements the heart of the lua 'print', lua 'pprint', and lua
|
|
|
|
|
// 'tostring' functions. Note that we have to override the lua builtins 'print'
|
|
|
|
|
// and 'tostring' for two reasons:
|
2021-09-07 17:37:23 -04:00
|
|
|
//
|
2021-10-21 14:22:06 -04:00
|
|
|
// * We need to suppress the printing of table addresses, for determinism.
|
|
|
|
|
// * We need to channel the output to a PrintBuffer in the world model.
|
|
|
|
|
//
|
|
|
|
|
// Note that the actual lua 'print' and 'pprint' routines aren't defined in this
|
|
|
|
|
// module, they're in the World module, because they send their output into the
|
|
|
|
|
// PrintBuffer of the world model. But all the tricky code to implement 'print'
|
|
|
|
|
// and 'pprint' are in this module.
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
2021-09-07 17:37:23 -04:00
|
|
|
|
2021-10-21 13:15:04 -04:00
|
|
|
#ifndef PPRINT_HPP
|
|
|
|
|
#define PPRINT_HPP
|
2021-09-07 17:37:23 -04:00
|
|
|
|
|
|
|
|
#include "luastack.hpp"
|
2022-02-25 19:57:23 -05:00
|
|
|
#include <ostream>
|
2022-02-24 02:17:41 -05:00
|
|
|
|
2023-03-05 01:51:25 -05:00
|
|
|
struct PrettyPrintOptions {
|
|
|
|
|
bool indent;
|
|
|
|
|
int level;
|
|
|
|
|
bool expand;
|
|
|
|
|
PrettyPrintOptions() : indent(true), level(0), expand(true) {}
|
2026-02-17 19:57:58 -05:00
|
|
|
PrettyPrintOptions(bool indent, bool expand) : indent(indent), level(0), expand(expand) {}
|
2023-03-05 01:51:25 -05:00
|
|
|
void parse(LuaKeywordParser &kp);
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-21 14:22:06 -04:00
|
|
|
// Atomic print to a stream.
|
2021-09-08 01:32:08 -04:00
|
|
|
//
|
2021-10-21 14:22:06 -04:00
|
|
|
// This prints an atomic value to a stream. If you give it a table,
|
2021-12-15 23:03:43 -05:00
|
|
|
// it just prints "<table>". This routine is the heart of the lua
|
2021-10-21 14:22:06 -04:00
|
|
|
// primitives 'print' and 'tostring'.
|
2021-09-08 01:32:08 -04:00
|
|
|
//
|
2023-04-06 20:12:03 -04:00
|
|
|
void atomic_print(LuaCoreStack &LS, LuaSlot val, bool quote, std::ostream *os);
|
2021-09-08 01:32:08 -04:00
|
|
|
|
2021-09-07 17:37:23 -04:00
|
|
|
// Pretty print to a stream.
|
|
|
|
|
//
|
2023-04-06 20:12:03 -04:00
|
|
|
void pprint(LuaCoreStack &LS, LuaSlot val, const PrettyPrintOptions &opts, std::ostream *os);
|
2021-09-07 17:37:23 -04:00
|
|
|
|
2026-02-17 19:57:58 -05:00
|
|
|
// Format a string using printf-style format specifiers, consuming
|
|
|
|
|
// arguments from a LuaExtraArgs. Returns an empty string on success,
|
|
|
|
|
// or an error message on failure.
|
|
|
|
|
//
|
|
|
|
|
// Numeric types (argument must be a number). All of the following
|
|
|
|
|
// use the same formatting parameters as 'printf'.
|
|
|
|
|
//
|
|
|
|
|
// %d, %i — signed decimal integer
|
|
|
|
|
// %o — unsigned octal
|
|
|
|
|
// %u — unsigned decimal
|
|
|
|
|
// %x, %X — unsigned hexadecimal (lower/upper)
|
|
|
|
|
// %e, %E — scientific notation (lower/upper)
|
|
|
|
|
// %f — decimal floating point
|
|
|
|
|
// %g, %G — shortest of %e/%f (lower/upper)
|
|
|
|
|
// %c — character (integer converted to character)
|
|
|
|
|
//
|
|
|
|
|
// Non-numeric types:
|
|
|
|
|
// %s — prints any lua value using atomic_print (unquoted)
|
|
|
|
|
// %q — prints any lua value using atomic_print (quoted)
|
|
|
|
|
//
|
|
|
|
|
// Pretty-printing:
|
|
|
|
|
//
|
|
|
|
|
// %p - pretty print
|
|
|
|
|
// %lp - pretty print, but all on one line
|
|
|
|
|
// %P - pretty print, force table expansion
|
|
|
|
|
// %lP - pretty print, force table expansion, all on one line
|
|
|
|
|
//
|
|
|
|
|
// Special:
|
|
|
|
|
// %% — literal percent sign (consumes no argument)
|
|
|
|
|
//
|
|
|
|
|
eng::string format(LuaCoreStack &LS, std::string_view fmt, LuaExtraArgs args, std::ostream *os);
|
|
|
|
|
|
2021-10-21 13:15:04 -04:00
|
|
|
#endif // PPRINT_HPP
|