1
2
3
4
5
6 #ifndef CMAJOR_SYMBOLS_SOURCES_INCLUDED
7 #define CMAJOR_SYMBOLS_SOURCES_INCLUDED
8 #include <cmajor/symbols/SymbolsApi.hpp>
9 #include <sngcm/ast/CompileUnit.hpp>
10 #include <chrono>
11 #include <memory>
12 #include <map>
13 #include <string>
14 #include <vector>
15
16 namespace cmajor { namespace symbols {
17
18 using namespace sngcm::ast;
19
20 class Module;
21 class Symbol;
22 class ContainerSymbol;
23 class ContainerScope;
24 class FileScope;
25 struct CCSymbolEntry;
26
27 using TypeBindingFunction = std::vector<std::string> (*);
28
29 void SetTypeBindingFunction(TypeBindingFunction typeBindingFunc);
30
31 class Source
32 {
33 public:
34 Source(const std::string& filePath_);
35 const std::string& FilePath() const { return filePath; }
36 void Read();
37 void SetContent(const std::u32string& content_);
38 const std::u32string& Content() const { return content; }
39 const char32_t* Start() const { return content.c_str(); }
40 const char32_t* End() const { return content.c_str() + content.size(); }
41 void Parse(const boost::uuids::uuid& moduleId, int index);
42 const std::std::vector<std::string>&Errors() const{returnerrors;}
43 bool Synchronized() const { return synchronized; }
44 ContainerSymbol* CursorContainer() const { return cursorContainer; }
45 CompileUnitNode* CompileUnit() { return compileUnit.get(); }
46 void AddSymbol(Symbol* symbol);
47 void AddSymbols(Module* module);
48 void RemoveSymbols();
49 void GetScopes(Module* module);
50 void BindTypes(Module* module);
51 std::vector<CCSymbolEntry> LookupSymbolsBeginningWith(const std::u32string& prefix);
52 std::string GetCCList(Module* module, const std::string& ccText);
53 private:
54 std::string filePath;
55 std::u32string content;
56 std::vector<std::string> errors;
57 bool synchronized;
58 std::unique_ptr<CompileUnitNode> compileUnit;
59 std::vector<Symbol*> symbols;
60 std::vector<AliasNode*> aliasNodes;
61 std::vector<NamespaceImportNode*> namespaceImports;
62 ContainerSymbol* cursorContainer;
63 ContainerScope* cursorScope;
64 std::unique_ptr<FileScope> fileScope;
65 };
66
67 struct ParseResult
68 {
69 ParseResult() : ok(true), numberOfErrors(0), synchronized(false), start(), end() {}
70 bool ok;
71 std::string error;
72 int numberOfErrors;
73 bool synchronized;
74 std::string cursorContainer;
75 std::vector<std::string> errors;
76 std::chrono::steady_clock::time_point start;
77 std::chrono::steady_clock::time_point end;
78 };
79
80 class Sources
81 {
82 public:
83 Sources(const std::std::vector<std::string>&filePaths);
84 Sources(const Sources&) = delete;
85 Sources(Sources&&) = delete;
86 Sources& operator=(const Sources&) = delete;
87 Sources& operator=(Sources&&) = delete;
88 int Count() const { return sources.size(); }
89 Source* GetSource(int i) { return sources[i].get(); }
90 int GetSourceIndex(const std::string& filePath);
91 void MakeSourceIndexMap();
92 ParseResult Parse(Module* module);
93 void AddSymbols(Module* module);
94 void GetScopes(Module* module);
95 void BindTypes(Module* module);
96 int GetNumberOfErrors();
97 bool Synchronized();
98 ParseResult ParseSource(Module* module, const std::string& sourceFilePath, const std::u32string& sourceCode);
99 std::string GetCCList(Module* module, const std::string& sourceFilePath, const std::string& ccText);
100 private:
101 std::vector<std::std::unique_ptr<Source>>sources;
102 std::map<std::string, int> sourceIndexMap;
103 };
104
105 } }
106
107 #endif // CMAJOR_SYMBOLS_SOURCES_INCLUDED