1 /*
2  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
3  * Copyright (C) 2011 Apple, Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_SVG_SVG_PARSING_ERROR_H_
28 #define THIRD_PARTY_BLINK_RENDERER_CORE_SVG_SVG_PARSING_ERROR_H_
29 
30 #include "third_party/blink/renderer/platform/wtf/math_extras.h"
31 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
32 
33 namespace blink {
34 
35 class QualifiedName;
36 
37 enum class SVGParseStatus {
38   kNoError,
39 
40   // Syntax errors
41   kTrailingGarbage,
42   kExpectedAngle,
43   kExpectedArcFlag,
44   kExpectedBoolean,
45   kExpectedEndOfArguments,
46   kExpectedEnumeration,
47   kExpectedInteger,
48   kExpectedLength,
49   kExpectedMoveToCommand,
50   kExpectedNumber,
51   kExpectedNumberOrPercentage,
52   kExpectedPathCommand,
53   kExpectedStartOfArguments,
54   kExpectedTransformFunction,
55 
56   // Semantic errors
57   kNegativeValue,
58   kZeroValue,
59 
60   // Generic error
61   kParsingFailed,
62 };
63 
64 class SVGParsingError {
65   STACK_ALLOCATED();
66 
67  public:
68   SVGParsingError(SVGParseStatus status = SVGParseStatus::kNoError,
69                   size_t locus = 0)
status_(static_cast<unsigned> (status))70       : status_(static_cast<unsigned>(status)), locus_(CheckLocus(locus)) {
71     DCHECK_EQ(Status(), status);
72   }
73 
Status()74   SVGParseStatus Status() const { return static_cast<SVGParseStatus>(status_); }
75 
HasLocus()76   bool HasLocus() const { return locus_ != kNoLocus; }
Locus()77   unsigned Locus() const { return locus_; }
78 
79   // Move the locus of this error by |offset|, returning in a new error.
OffsetWith(size_t offset)80   SVGParsingError OffsetWith(size_t offset) const {
81     return SVGParsingError(Status(), offset + Locus());
82   }
83 
84   // Generates a string describing this error for |value| in the context of
85   // an <element, attribute>-name pair.
86   String Format(const String& tag_name,
87                 const QualifiedName&,
88                 const AtomicString& value) const;
89 
90  private:
91   static const int kLocusBits = 24;
92   static const unsigned kNoLocus = (1u << kLocusBits) - 1;
93 
CheckLocus(size_t locus)94   static unsigned CheckLocus(size_t locus) {
95     // Clamp to fit in the number of bits available. If the character index
96     // encoded by the locus does not fit in the number of bits allocated
97     // for it, the locus will be disabled (set to kNoLocus). This means
98     // that very long values will be output in their entirety. That should
99     // however be rather uncommon.
100     return clampTo<unsigned>(locus, 0, kNoLocus);
101   }
102 
103   unsigned status_ : 8;
104   unsigned locus_ : kLocusBits;  // The locus (character index) of the error
105                                  // within the parsed string.
106 };
107 
108 inline bool operator==(const SVGParsingError& error, SVGParseStatus status) {
109   return error.Status() == status;
110 }
111 inline bool operator!=(const SVGParsingError& error, SVGParseStatus status) {
112   return !(error == status);
113 }
114 
115 }  // namespace blink
116 
117 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_SVG_SVG_PARSING_ERROR_H_
118