1 /**
2  * Exception definitions specific to the JSON processing functions.
3  *
4  * Copyright: Copyright 2012 - 2014, Sönke Ludwig.
5  * License:   $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6  * Authors:   Sönke Ludwig
7  * Source:    $(PHOBOSSRC std/data/json/foundation.d)
8  */
9 module funkwerk.stdx.data.json.foundation;
10 @safe:
11 
12 import funkwerk.stdx.data.json.lexer;
13 
14 /**
15  * Represents a location in an input range/file.
16  *
17  * The indices are zero based and the column is represented in code units of
18  * the input (i.e. in bytes in case of a UTF-8 input string).
19  */
20 struct Location
21 {
22     /// Optional file name.
23     string file;
24     /// The zero based line of the input file.
25     size_t line = 0;
26     /// The zero based code unit index of the referenced line.
27     size_t column = 0;
28 
29     /// Returns a string representation of the location.
30     string toString() const
31     {
32         import std..string;
33         return format("%s(%s:%s)", this.file, this.line, this.column);
34     }
35 }
36 
37 
38 /**
39  * JSON specific exception.
40  *
41  * This exception is thrown during the lexing and parsing stages.
42 */
43 class JSONException : Exception
44 {
45     /// The bare error message
46     string message;
47 
48     /// The location where the error occured
49     Location location;
50 
51     /// Constructs a new exception from the given message and location
52     this(string message, Location loc, string file = __FILE__, size_t line = __LINE__)
53     {
54         import std..string;
55         this.message = message;
56         this.location = loc;
57         super(format("%s(%s:%s) %s", loc.file, loc.line, loc.column, message), file, line);
58     }
59 }
60 
61 package void enforceJson(string file = __FILE__, size_t line = __LINE__)(bool cond, lazy string message, lazy Location loc)
62 {
63     if (!cond) throw new JSONException(message, loc, file, line);
64 }
65