1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#pragma once
#include "Core/Array.h"
#include "Core/Fn.h"
#include "Core/Io/Url.h"
#include "NamedThread.h"
#include "Package.h"
#include "Syntax/Parser.h"
namespace storm {
STORM_PKG(lang);
class FileReader;
/**
* Wrapper of an array of URL:s, since we need to be able to express a map of those. This is not
* allowed by the preprocessor unless we wrap it inside a class.
*/
class PkgFiles : public Object {
STORM_CLASS;
public:
STORM_CTOR PkgFiles(SimpleName *name);
virtual void STORM_FN deepCopy(CloneEnv *e);
SimpleName *name;
Array<Url *> *files;
};
// Get the file extension that determines which package to use when parsing/compiling a
// file. Usually, the last file extension is returned, but if a file is named like foo.so.2,
// 'so' is used instead, since the last part is a number and there is a previous file
// extension. If no dots are found at all, 'null' is returned.
MAYBE(Str *) STORM_FN codeFileType(Url *file);
// Get the package containing syntax when parsing 'file'. This is the default package, and
// languages may choose to ignore this.
MAYBE(SimpleName *) STORM_FN syntaxPkgName(Url *file);
// Get the name of the 'reader' function for use when reading 'file'. Returns 'null' for files without an extension.
MAYBE(SimpleName *) STORM_FN readerName(Url *file);
MAYBE(SimpleName *) STORM_FN readerName(Str *ext);
// Group files together by which reader they should use. Storm will have a custom implementation
// of this which does not use a 'PkgFiles' object.
Map<Str *, PkgFiles *> *readerName(Array<Url *> *files);
// Create a reader for a given file type.
MAYBE(PkgReader *) STORM_FN createReader(Array<Url *> *files, Package *pkg);
MAYBE(PkgReader *) STORM_FN createReader(SimpleName *name, Array<Url *> *files, Package *pkg);
/**
* Load a specific file type from a package. This is the abstract base class that does not do
* very much at all. Create a function 'lang.ext.reader(Array<Url>, Package)' which creates an
* instance of a Reader, and you are ready to parse a language!
*
* The process is designed to be implemented as follows:
* 1: get syntax rules
* 2: get syntax options
* 3: get types (just information, should be lazily-loaded!)
* 4: resolve types (ie. set up inheritance, figure out how structs look...)
* 5: get functions
* 6: resolve functions (any other work needed to finalize functions etc.)
*
* This is to ensure that inter-dependencies are resolved correctly. Within each language, these
* rules may be implemented differently.
*/
class PkgReader : public ObjectOn<Compiler> {
STORM_CLASS;
public:
// Create a PkgReader.
STORM_CTOR PkgReader(Array<Url *> *files, Package *pkg);
// Owning package.
Package *pkg;
// Files to parse.
Array<Url *> *files;
// Get the syntax rules.
virtual void STORM_FN readSyntaxRules();
// Get the syntax options.
virtual void STORM_FN readSyntaxProductions();
// Get the types.
virtual void STORM_FN readTypes();
// Resolve types.
virtual void STORM_FN resolveTypes();
// Get all functions.
virtual void STORM_FN readFunctions();
// Resolve functions.
virtual void STORM_FN resolveFunctions();
/**
* For language server integration.
*/
// Get a file reader for the given Url and the contents of the file.
virtual MAYBE(FileReader *) STORM_FN readFile(Url *url, Str *src);
};
class FileInfo;
class FileReader;
/**
* Specialization of PkgReader which uses a set of FileReaders. Provide a function which creates
* instances of a 'FileReader' to use.
*/
class FilePkgReader : public PkgReader {
STORM_CLASS;
public:
// Create.
STORM_CTOR FilePkgReader(Array<Url *> *files, Package *pkg, Fn<FileReader *, FileInfo *> *create);
// Get the syntax rules.
virtual void STORM_FN readSyntaxRules();
// Get the syntax productions.
virtual void STORM_FN readSyntaxProductions();
// Get the types.
virtual void STORM_FN readTypes();
// Resolve types.
virtual void STORM_FN resolveTypes();
// Get all functions.
virtual void STORM_FN readFunctions();
// Resolve functions.
virtual void STORM_FN resolveFunctions();
// Get a file reader for the given Url.
virtual MAYBE(FileReader *) STORM_FN readFile(Url *url, Str *src);
private:
// Store all files in use.
Array<FileReader *> *readers;
// Create FileReaders.
Fn<FileReader *, FileInfo *> *create;
// Populate 'readers' if it is not already done.
void loadReaders();
};
// Load everything from a number of readers in the proper order.
// Calls 'readSyntaxRules', 'readSyntaxProductions', ...
void STORM_FN read(Array<PkgReader *> *readers);
}
|