1 // Copyright 2016-2021 Doug Moen 2 // Licensed under the Apache License, version 2.0 3 // See accompanying file LICENSE or https://www.apache.org/licenses/LICENSE-2.0 4 5 #ifndef LIBCURV_SOURCE_H 6 #define LIBCURV_SOURCE_H 7 8 #include <libcurv/range.h> 9 #include <libcurv/shared.h> 10 #include <libcurv/string.h> 11 12 namespace curv { 13 14 struct Context; 15 16 /// Abstract base class: the name and contents of a source file. 17 /// Either the name or contents can be missing (but not both). 18 /// 19 /// The name is just an uninterpreted utf8 string for now, will later be 20 /// a filename or uri. If there is no filename, then the name is a zero-length 21 /// string, which won't be reported in error messages. 22 /// 23 /// The contents are a const utf-8 character array. 24 /// Subclasses provide storage management for the contents. 25 /// The contents can be null (different from zero length), 26 /// in which case error messages only report the file name. 27 struct Source : public Shared_Base, public Range<const char*> 28 { 29 enum class Type { curv, gpu, directory }; 30 31 Shared<const String> name_; 32 Type type_ = Type::curv; 33 protected: SourceSource34 Source(String_Ref name, const char*f, const char*l) 35 : 36 Range(f,l), name_(move(name)) 37 {} 38 public: 39 Source(String_Ref name, Type type = Type::curv) 40 : RangeSource41 Range(nullptr,nullptr), name_(move(name)), type_(type) 42 {} no_nameSource43 bool no_name() const { return name_->empty(); } no_contentsSource44 bool no_contents() const { return first == nullptr; } ~SourceSource45 virtual ~Source() {} 46 }; 47 48 /// A Source subclass where the program text is represented as a String. 49 struct String_Source : public Source 50 { 51 Shared<const String> text_; 52 String_SourceString_Source53 String_Source(String_Ref name, String_Ref text) 54 : 55 Source(move(name), text->data(), text->data() + text->size()), 56 text_(move(text)) 57 {} 58 }; 59 60 /// A Source subclass that represents a file. 61 struct File_Source : public String_Source 62 { 63 File_Source(String_Ref filename, const Context&); 64 }; 65 66 } // namespace curv 67 #endif // header guard 68