summaryrefslogtreecommitdiff
path: root/src/core/resource
diff options
context:
space:
mode:
authorAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-24 03:08:16 +0100
committerAndreas Stöckel <astoecke@techfak.uni-bielefeld.de>2015-01-24 03:08:16 +0100
commit67d36e699a2852ce471c4d1b8dab5992d6c01a98 (patch)
tree0ef23befe3fa5af9c5d83b3b8934e444366a8575 /src/core/resource
parentf819b42057b2baea205569dd808c4fcf2bc4d630 (diff)
Implemented SourceContextReader, added unit tests, implemented SourceContextReader interface in ResourceManager, added LoggerTest
Diffstat (limited to 'src/core/resource')
-rw-r--r--src/core/resource/ResourceManager.cpp29
-rw-r--r--src/core/resource/ResourceManager.hpp16
2 files changed, 29 insertions, 16 deletions
diff --git a/src/core/resource/ResourceManager.cpp b/src/core/resource/ResourceManager.cpp
index f154c9c..a5e76b0 100644
--- a/src/core/resource/ResourceManager.cpp
+++ b/src/core/resource/ResourceManager.cpp
@@ -35,7 +35,8 @@ namespace ousia {
/* Static helper functions */
-static void logUnsopportedType(Logger &logger, Resource &resource, const RttiSet &supportedTypes)
+static void logUnsopportedType(Logger &logger, Resource &resource,
+ const RttiSet &supportedTypes)
{
// Build a list containing the expected type names
std::vector<std::string> expected;
@@ -81,7 +82,7 @@ void ResourceManager::purgeResource(SourceId sourceId)
}
resources.erase(sourceId);
nodes.erase(sourceId);
- lineNumberCache.erase(sourceId);
+ contextReaders.erase(sourceId);
}
Rooted<Node> ResourceManager::parse(ParserContext &ctx, Resource &resource,
@@ -93,7 +94,8 @@ Rooted<Node> ResourceManager::parse(ParserContext &ctx, Resource &resource,
if (mime.empty()) {
mime = ctx.registry.getMimetypeForFilename(resource.getLocation());
if (mime.empty()) {
- ctx.logger.error(std::string("Filename \"") + resource.getLocation() +
+ ctx.logger.error(std::string("Filename \"") +
+ resource.getLocation() +
std::string(
"\" has an unknown file extension. Explicitly "
"specify a mimetype."));
@@ -137,7 +139,8 @@ Rooted<Node> ResourceManager::parse(ParserContext &ctx, Resource &resource,
if (node == nullptr) {
throw LoggableException{"Internal error: Parser returned null."};
}
- } catch (LoggableException ex) {
+ }
+ catch (LoggableException ex) {
// Remove all data associated with the allocated source id
purgeResource(sourceId);
@@ -262,14 +265,20 @@ Rooted<Node> ResourceManager::link(ParserContext &ctx, const std::string &path,
return link(ctx, path, mimetype, rel, supportedTypes, relativeResource);
}
-SourceContext ResourceManager::buildContext(const SourceLocation &location)
+SourceContext ResourceManager::readContext(const SourceLocation &location,
+ size_t maxContextLength)
{
- SourceContext res;
-
- // TODO
+ const Resource &resource = getResource(location.getSourceId());
+ if (resource.isValid()) {
+ // Fetch a char reader for the resource
+ std::unique_ptr<std::istream> is = resource.stream();
+ CharReader reader{*is, location.getSourceId()};
- return res;
+ // Return the context
+ return contextReaders[location.getSourceId()].readContext(
+ reader, location, maxContextLength, resource.getLocation());
+ }
+ return SourceContext{};
}
-
}
diff --git a/src/core/resource/ResourceManager.hpp b/src/core/resource/ResourceManager.hpp
index 51c00e3..d5381b9 100644
--- a/src/core/resource/ResourceManager.hpp
+++ b/src/core/resource/ResourceManager.hpp
@@ -34,6 +34,7 @@
#include <core/common/Location.hpp>
#include <core/common/Rtti.hpp>
+#include <core/common/SourceContextReader.hpp>
#include <core/managed/Managed.hpp>
#include "Resource.hpp"
@@ -74,11 +75,11 @@ private:
std::unordered_map<SourceId, ManagedUid> nodes;
/**
- * Cache used for translating byte offsets to line numbers. Maps from a
- * SourceId onto a list of (sorted) SourceOffsets. The index in the list
- * corresponds to the line number.
+ * Map containing SourceContextReader instances which are -- as their name
+ * suggests -- used to produce SourceContext structures describing the
+ * source code at a given SourceLocation.
*/
- std::unordered_map<SourceId, std::vector<SourceOffset>> lineNumberCache;
+ std::unordered_map<SourceId, SourceContextReader> contextReaders;
/**
* Allocates a new SourceId for the given resource.
@@ -224,11 +225,14 @@ public:
* @param location is the SourceLocation for which context information
* should be retrieved. This method is used by the Logger class to print
* pretty messages.
+ * @param maxContextLength is the maximum length in character of context
+ * that should be extracted.
* @return a valid SourceContext if a valid SourceLocation was given or an
* invalid SourceContext if the location is invalid.
*/
- SourceContext buildContext(const SourceLocation &location);
-
+ SourceContext readContext(
+ const SourceLocation &location,
+ size_t maxContextLength = SourceContextReader::MAX_MAX_CONTEXT_LENGTH);
};
}