From e4071ff5894d13e95f72457aac68899610e5d7eb Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Mon, 2 Feb 2015 00:34:04 +0100 Subject: Allowing to store SourceLocation from which a Variant was parsed along with the Variant --- src/core/common/Variant.cpp | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'src/core/common/Variant.cpp') diff --git a/src/core/common/Variant.cpp b/src/core/common/Variant.cpp index 9b22b71..0c7fceb 100644 --- a/src/core/common/Variant.cpp +++ b/src/core/common/Variant.cpp @@ -20,6 +20,7 @@ #include +#include "Location.hpp" #include "Logger.hpp" #include "Utils.hpp" #include "Variant.hpp" @@ -29,6 +30,42 @@ namespace ousia { +/* Struct VariantMetadata */ + +bool VariantMetadata::hasLocation() const +{ + return locationSourceId != InvalidSourceId; +} + +SourceLocation VariantMetadata::getLocation() const +{ + if (locationOffset == InvalidLocationOffset) { + return SourceLocation(locationSourceId); + } + if (locationLength == InvalidLocationLength) { + return SourceLocation(locationSourceId, locationOffset); + } + return SourceLocation(locationSourceId, locationOffset, + static_cast(locationOffset) + + static_cast(locationLength)); +} + +void VariantMetadata::setLocation(const SourceLocation &location) +{ + // Copy the location members + const SourceId sourceId = location.getSourceId(); + const size_t offset = location.getStart(); + const size_t length = location.getLength(); + + // Copy the location, mark values that cannot be stored as invalid + locationSourceId = + sourceId < InvalidLocationSourceId ? sourceId : InvalidLocationSourceId; + locationOffset = + offset < InvalidLocationOffset ? offset : InvalidLocationOffset; + locationLength = + length < InvalidLocationLength ? length : InvalidLocationLength; +} + /* Class Variant::TypeException */ Variant::TypeException::TypeException(VariantType actualType, @@ -155,7 +192,7 @@ Variant::cardinalityType Variant::toCardinality() const const Rtti &Variant::getRtti() const { - switch (type) { + switch (meta.getType()) { case VariantType::NULLPTR: return RttiTypes::Nullptr; case VariantType::BOOL: -- cgit v1.2.3