An opaque type representing the source code being parsed, regardless of origin (constant memory, file, memory-mapped file, or stream).
More...
#include "prism/compiler/exported.h"
#include "prism/compiler/filesystem.h"
#include "prism/compiler/nodiscard.h"
#include "prism/compiler/nonnull.h"
#include <stddef.h>
#include <stdint.h>
Go to the source code of this file.
|
|
typedef struct pm_source_t | pm_source_t |
| | An opaque type representing source code being parsed.
|
| |
| typedef char *() | pm_source_stream_fgets_t(char *string, int size, void *stream) |
| | This function is used to retrieve a line of input from a stream.
|
| |
| typedef int() | pm_source_stream_feof_t(void *stream) |
| | This function is used to check whether a stream is at EOF.
|
| |
An opaque type representing the source code being parsed, regardless of origin (constant memory, file, memory-mapped file, or stream).
◆ pm_source_stream_fgets_t
| typedef char *() pm_source_stream_fgets_t(char *string, int size, void *stream) |
This function is used to retrieve a line of input from a stream.
It closely mirrors that of fgets so that fgets can be used as the default implementation.
◆ pm_source_stream_feof_t
| typedef int() pm_source_stream_feof_t(void *stream) |
This function is used to check whether a stream is at EOF.
It closely mirrors that of feof so that feof can be used as the default implementation.
◆ pm_source_init_result_t
Represents the result of initializing a source from a file.
| Enumerator |
|---|
| PM_SOURCE_INIT_SUCCESS | Indicates that the source was successfully initialized.
|
| PM_SOURCE_INIT_ERROR_GENERIC | Indicates a generic error from a source init function, where the type of error should be read from errno or GetLastError().
|
| PM_SOURCE_INIT_ERROR_DIRECTORY | Indicates that the file that was attempted to be opened was a directory.
|
| PM_SOURCE_INIT_ERROR_NON_REGULAR | Indicates that the file is not a regular file (e.g.
a pipe or character device) and the caller should handle reading it.
|
◆ pm_source_constant_new()
| PRISM_EXPORTED_FUNCTION PRISM_NODISCARD pm_source_t * pm_source_constant_new |
( |
const uint8_t * |
data, |
|
|
size_t |
length |
|
) |
| |
Create a new source that wraps existing constant memory.
The memory is not owned and will not be freed.
- Parameters
-
| data | The pointer to the source data. |
| length | The length of the source data in bytes. |
- Returns
- A new source. Aborts on allocation failure.
◆ pm_source_shared_new()
| PRISM_EXPORTED_FUNCTION PRISM_NODISCARD pm_source_t * pm_source_shared_new |
( |
const uint8_t * |
data, |
|
|
size_t |
length |
|
) |
| |
Create a new source that wraps existing shared memory.
The memory is not owned and will not be freed. Semantically a "slice" of another source.
- Parameters
-
| data | The pointer to the source data. |
| length | The length of the source data in bytes. |
- Returns
- A new source. Aborts on allocation failure.
◆ pm_source_owned_new()
| PRISM_EXPORTED_FUNCTION PRISM_NODISCARD pm_source_t * pm_source_owned_new |
( |
uint8_t * |
data, |
|
|
size_t |
length |
|
) |
| |
Create a new source that owns its memory.
The memory will be freed with xfree when the source is freed.
- Parameters
-
| data | The pointer to the heap-allocated source data. |
| length | The length of the source data in bytes. |
- Returns
- A new source. Aborts on allocation failure.
◆ pm_source_file_new()
Create a new source by reading a file into a heap-allocated buffer.
- Parameters
-
| filepath | The path to the file to read. |
| result | Out parameter for the result of the initialization. |
- Returns
- A new source, or NULL on error (with result written to out param).
◆ pm_source_mapped_new()
Create a new source by memory-mapping a file.
Falls back to file reading on platforms without mmap support.
If the file is a non-regular file (e.g. a pipe or character device), PM_SOURCE_INIT_ERROR_NON_REGULAR is returned, allowing the caller to handle it appropriately (e.g. by reading it through their own I/O layer).
- Parameters
-
| filepath | The path to the file to read. |
| open_flags | Additional flags to pass to open(2) (e.g. O_NONBLOCK). |
| result | Out parameter for the result of the initialization. |
- Returns
- A new source, or NULL on error (with result written to out param).
◆ pm_source_stream_new()
Create a new source by reading from a stream using the provided callbacks.
- Parameters
-
| stream | The stream to read from. |
| fgets | The function to use to read from the stream. |
| feof | The function to use to check if the stream is at EOF. |
- Returns
- A new source. Aborts on allocation failure.
◆ pm_source_free()
| PRISM_EXPORTED_FUNCTION void pm_source_free |
( |
pm_source_t * |
source | ) |
|
Free the given source and any memory it owns.
- Parameters
-
| source | The source to free. |
◆ pm_source_length()
| PRISM_EXPORTED_FUNCTION size_t pm_source_length |
( |
const pm_source_t * |
source | ) |
|
Returns the length of the source data in bytes.
- Parameters
-
| source | The source to get the length of. |
- Returns
- The length of the source data.
◆ pm_source_source()
| PRISM_EXPORTED_FUNCTION const uint8_t * pm_source_source |
( |
const pm_source_t * |
source | ) |
|
Returns a pointer to the source data.
- Parameters
-
| source | The source to get the data of. |
- Returns
- A pointer to the source data.