show field names on output

This commit is contained in:
David Rose 2005-05-25 22:10:58 +00:00
parent 7c3dff4f02
commit 01ecc7124e
8 changed files with 477 additions and 375 deletions

View File

@ -331,7 +331,7 @@ output_element(ostream &out, bool brief, DCParameter *element) const {
DCPacker packer;
packer.set_unpack_data(element->get_default_value());
packer.begin_unpack(element);
packer.unpack_and_format(out);
packer.unpack_and_format(out, false);
packer.end_unpack();
}
}

View File

@ -1035,7 +1035,7 @@ write(ostream &out, bool brief, int indent_level) const {
DCPacker packer;
packer.set_unpack_data((*fi)->get_default_value());
packer.begin_unpack(*fi);
packer.unpack_and_format(out);
packer.unpack_and_format(out, false);
if (!packer.end_unpack()) {
out << "<error>";
}

View File

@ -199,7 +199,7 @@ format_data(const string &packed_data) {
DCPacker packer;
packer.set_unpack_data(packed_data);
packer.begin_unpack(this);
string result = packer.unpack_and_format();
string result = packer.unpack_and_format(true);
if (!packer.end_unpack()) {
return string();
}

View File

@ -154,6 +154,22 @@ get_pack_type() const {
}
}
////////////////////////////////////////////////////////////////////
// Function: DCPacker::get_current_field_name
// Access: Published
// Description: Returns the name of the current field, if it has a
// name, or the empty string if the field does not have
// a name or there is no current field.
////////////////////////////////////////////////////////////////////
INLINE string DCPacker::
get_current_field_name() const {
if (_current_field == NULL) {
return string();
} else {
return _current_field->get_name();
}
}
////////////////////////////////////////////////////////////////////
// Function: DCPacker::pack_double
// Access: Published
@ -550,6 +566,18 @@ unpack_literal_value(string &value) {
value.assign(_unpack_data + start, _unpack_p - start);
}
////////////////////////////////////////////////////////////////////
// Function: DCPacker::had_parse_error
// Access: Published
// Description: Returns true if there has been an parse error
// since the most recent call to begin(); this can only
// happen if you call parse_and_pack().
////////////////////////////////////////////////////////////////////
INLINE bool DCPacker::
had_parse_error() const {
return _parse_error;
}
////////////////////////////////////////////////////////////////////
// Function: DCPacker::had_pack_error
// Access: Published
@ -599,7 +627,7 @@ had_range_error() const {
////////////////////////////////////////////////////////////////////
INLINE bool DCPacker::
had_error() const {
return _range_error || _pack_error;
return _range_error || _pack_error || _parse_error;
}
////////////////////////////////////////////////////////////////////

View File

@ -40,6 +40,7 @@ DCPacker() {
_owns_unpack_data = false;
_unpack_p = 0;
_live_catalog = NULL;
_parse_error = false;
_pack_error = false;
_range_error = false;
_stack = NULL;
@ -76,6 +77,7 @@ begin_pack(const DCPackerInterface *root) {
nassertv(_mode == M_idle);
_mode = M_pack;
_parse_error = false;
_pack_error = false;
_range_error = false;
@ -168,6 +170,7 @@ begin_unpack(const DCPackerInterface *root) {
nassertv(_unpack_data != NULL);
_mode = M_unpack;
_parse_error = false;
_pack_error = false;
_range_error = false;
@ -238,6 +241,7 @@ begin_repack(const DCPackerInterface *root) {
nassertv(_unpack_p == 0);
_mode = M_repack;
_parse_error = false;
_pack_error = false;
_range_error = false;
_pack_data.clear();
@ -914,7 +918,12 @@ parse_and_pack(istream &in) {
dcyyparse();
dc_cleanup_parser();
return (dc_error_count() == 0);
bool parse_error = (dc_error_count() != 0);
if (parse_error) {
_parse_error = true;
}
return !parse_error;
}
////////////////////////////////////////////////////////////////////
@ -925,9 +934,9 @@ parse_and_pack(istream &in) {
// default value), or as an input to parse_object.
////////////////////////////////////////////////////////////////////
string DCPacker::
unpack_and_format() {
unpack_and_format(bool show_field_names) {
ostringstream strm;
unpack_and_format(strm);
unpack_and_format(strm, show_field_names);
return strm.str();
}
@ -939,9 +948,18 @@ unpack_and_format() {
// default value), or as an input to parse_object.
////////////////////////////////////////////////////////////////////
void DCPacker::
unpack_and_format(ostream &out) {
unpack_and_format(ostream &out, bool show_field_names) {
DCPackType pack_type = get_pack_type();
if (show_field_names && !get_current_field_name().empty()) {
nassertv(_current_field != (DCPackerInterface *)NULL);
const DCField *field = _current_field->as_field();
if (field != (DCField *)NULL &&
field->as_parameter() != (DCParameter *)NULL) {
out << field->get_name() << " = ";
}
}
switch (pack_type) {
case PT_invalid:
out << "<invalid>";
@ -995,7 +1013,7 @@ unpack_and_format(ostream &out) {
push();
while (more_nested_fields() && !had_pack_error()) {
unpack_and_format(out);
unpack_and_format(out, show_field_names);
if (more_nested_fields()) {
out << ", ";

View File

@ -72,6 +72,7 @@ PUBLISHED:
INLINE const DCPackerInterface *get_current_field() const;
INLINE const DCSwitchParameter *get_last_switch() const;
INLINE DCPackType get_pack_type() const;
INLINE string get_current_field_name() const;
void push();
void pop();
@ -115,9 +116,10 @@ PUBLISHED:
bool parse_and_pack(const string &formatted_object);
bool parse_and_pack(istream &in);
string unpack_and_format();
void unpack_and_format(ostream &out);
string unpack_and_format(bool show_field_names = false);
void unpack_and_format(ostream &out, bool show_field_names = false);
INLINE bool had_parse_error() const;
INLINE bool had_pack_error() const;
INLINE bool had_range_error() const;
INLINE bool had_error() const;
@ -252,6 +254,7 @@ private:
int _num_nested_fields;
const DCSwitchParameter *_last_switch;
bool _parse_error;
bool _pack_error;
bool _range_error;
};

File diff suppressed because it is too large Load Diff

View File

@ -952,6 +952,26 @@ char_or_number:
parameter_value:
parameter_actual_value
{
$$ = $1;
}
| IDENTIFIER '='
{
if ($1 != current_packer->get_current_field_name()) {
ostringstream strm;
strm << "Got '" << $1 << "', expected '"
<< current_packer->get_current_field_name() << "'";
yyerror(strm.str());
}
}
parameter_actual_value
{
$$ = $4;
}
;
parameter_actual_value:
signed_integer
{
current_packer->pack_int64($1);