Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
json-api: add location array to error and warning objects
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-moen committed Jan 4, 2019
1 parent deb0272 commit 2b84902
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 27 deletions.
17 changes: 4 additions & 13 deletions curv/export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <libcurv/exception.h>
#include <libcurv/filesystem.h>
#include <libcurv/format.h>
#include <libcurv/json.h>
#include <libcurv/program.h>
#include <libcurv/range.h>
#include <libcurv/source.h>
Expand Down Expand Up @@ -208,16 +209,6 @@ bool is_json_data(Value val)
return true; // null, bool or num
}
}
void export_json_string(const char* str, std::ostream& out)
{
out << '"';
for (const char* p = str; *p != '\0'; ++p) {
if (*p == '\\' || *p == '"')
out << '\\';
out << *p;
}
out << '"';
}
bool export_json_value(Value val, std::ostream& out, const Context& cx)
{
if (val.is_null()) {
Expand All @@ -238,7 +229,7 @@ bool export_json_value(Value val, std::ostream& out, const Context& cx)
case Ref_Value::ty_string:
{
auto& str = (String&)ref;
export_json_string(str.c_str(), out);
write_json_string(str.c_str(), out);
return true;
}
case Ref_Value::ty_list:
Expand Down Expand Up @@ -267,7 +258,7 @@ bool export_json_value(Value val, std::ostream& out, const Context& cx)
if (is_json_data(val)) {
if (!first) out << ",";
first = false;
export_json_string(id.c_str(), out);
write_json_string(id.c_str(), out);
out << ":";
export_json_value(val, out, At_Field(id.c_str(), cx));
}
Expand Down Expand Up @@ -325,7 +316,7 @@ void export_json_api(Value value,
<< "," << dfmt(shape.bbox_.ymax, dfmt::JSON)
<< "," << dfmt(shape.bbox_.zmax, dfmt::JSON)
<< "]],\"shader\":";
export_json_string(vshape.frag_.c_str(), ofile.ostream());
write_json_string(vshape.frag_.c_str(), ofile.ostream());
ofile.ostream() << "}}\n";
}
}
Expand Down
20 changes: 20 additions & 0 deletions libcurv/json.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2016-2018 Doug Moen
// Licensed under the Apache License, version 2.0
// See accompanying file LICENSE or https://www.apache.org/licenses/LICENSE-2.0

#include <libcurv/json.h>

namespace curv {

void write_json_string(const char* str, std::ostream& out)
{
out << '"';
for (const char* p = str; *p != '\0'; ++p) {
if (*p == '\\' || *p == '"')
out << '\\';
out << *p;
}
out << '"';
}

} // namespace curv
15 changes: 15 additions & 0 deletions libcurv/json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2016-2018 Doug Moen
// Licensed under the Apache License, version 2.0
// See accompanying file LICENSE or https://www.apache.org/licenses/LICENSE-2.0

#ifndef LIBCURV_JSON_H
#define LIBCURV_JSON_H

#include <ostream>

namespace curv {

void write_json_string(const char*, std::ostream&);

} // namespace curv
#endif // header guard
13 changes: 13 additions & 0 deletions libcurv/location.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <libcurv/ansi_colour.h>
#include <libcurv/format.h>
#include <libcurv/json.h>

namespace curv {

Expand Down Expand Up @@ -181,6 +182,18 @@ Location::write(std::ostream& out, bool colour, bool many) const
}
}

void
Location::write_json(std::ostream& out) const
{
out << "{\"byte_range\":["
<< token().first_ << "," << token().last_ << "]";
if (!filename().empty()) {
out << ",\"filename\":";
write_json_string(filename().c_str(), out);
}
out << "}";
}

Range<const char*>
Location::range() const
{
Expand Down
2 changes: 2 additions & 0 deletions libcurv/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ struct Location
/// The `colour` flag enables colour text using ANSI ESC sequences.
void write(std::ostream&, bool colour, bool many) const;

void write_json(std::ostream&) const;

/// Line and column information for a Location
///
/// Line and column numbers begin at 0.
Expand Down
26 changes: 12 additions & 14 deletions libcurv/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <libcurv/context.h>
#include <libcurv/exception.h>
#include <libcurv/import.h>
#include <libcurv/json.h>
#include <libcurv/program.h>
#include <libcurv/source.h>

Expand All @@ -31,26 +32,23 @@ void System::print_exception(
out << std::endl;
}

static void export_json_string(const char* str, std::ostream& out)
{
out << '"';
for (const char* p = str; *p != '\0'; ++p) {
if (*p == '\\' || *p == '"')
out << '\\';
out << *p;
}
out << '"';
}

void System::print_json_exception(
const char* type, const std::exception& exc, std::ostream& out)
{
out << "{\"" << type << "\":{";
out << "\"message\":";
export_json_string(exc.what(), out);
write_json_string(exc.what(), out);
const Exception *e = dynamic_cast<const Exception*>(&exc);
if (e) {
if (e && !e->loc_.empty()) {
// print location array
out << ",\"location\":[";
bool first = true;
for (auto L : e->loc_) {
if (!first) out << ",";
first = false;
L.write_json(out);
}
out << "]";
}
out << "}}" << std::endl;
}
Expand All @@ -75,7 +73,7 @@ void System::print(const char* str)
{
if (use_json_api_) {
console() << "{\"print\":";
export_json_string(str, console());
write_json_string(str, console());
console() << "}";
} else {
console() << str;
Expand Down

0 comments on commit 2b84902

Please sign in to comment.