![]() |
Fun 0.41.5
The programming language that makes you have fun!
|
Low-level parsing helpers and include preprocessor for the Fun parser. More...
Go to the source code of this file.
Data Structures | |
| struct | StrBuf |
| Simple growable string buffer. More... | |
| struct | NameList |
| List of exported symbol names discovered at top level. More... | |
Macros | |
| #define | DEFAULT_LIB_DIR "/usr/share/fun/lib/" |
Functions | |
| static char * | read_file_all (const char *path, size_t *out_len) |
| Read entire file into a newly allocated buffer. | |
| static void | skip_ws (const char *src, size_t len, size_t *pos) |
| Skip spaces, tabs, carriage returns and newlines. | |
| static void | skip_line (const char *src, size_t len, size_t *pos) |
Advance pos to the next line, consuming the trailing '' if present. | |
| static void | skip_comments (const char *src, size_t len, size_t *pos) |
| Skip whitespace, then line and block comments. Continues until the next non-comment, non-whitespace character. | |
| static int | starts_with (const char *src, size_t len, size_t pos, const char *kw) |
| Check if src starting at pos begins with kw and fits in len. | |
| static void | skip_shebang_if_present (const char *src, size_t len, size_t *pos) |
| Skip a top-of-file shebang line that starts with "#!" if present. | |
| static void | skip_identifier (const char *src, size_t len, size_t *pos) |
| If an identifier starts at pos, advance pos to its end. Recognizes [A-Za-z_][A-Za-z0-9_]*. | |
| static int | consume_char (const char *src, size_t len, size_t *pos, char expected) |
| Consume expected character after skipping whitespace. | |
| static char * | parse_string_literal_any_quote (const char *src, size_t len, size_t *pos) |
| Parse a single-quoted or double-quoted string literal. | |
| static void | skip_spaces (const char *src, size_t len, size_t *pos) |
| Skip only spaces, tabs and carriage returns (not newlines). | |
| static int | read_identifier_into (const char *src, size_t len, size_t *pos, char **out_name) |
| Read an identifier starting at pos and allocate its name. | |
| static uint64_t | parse_int_literal_value (const char *src, size_t len, size_t *pos, int *ok) |
| Parse an integer literal (decimal or 0x-hex) with optional sign. | |
| static void * | xrealloc (void *ptr, size_t newcap) |
| Thin wrapper over realloc used by local buffers. | |
| static void | sb_init (StrBuf *sb) |
| Initialize a StrBuf with a small starting capacity. | |
| static void | sb_reserve (StrBuf *sb, size_t need) |
| Ensure buffer capacity for at least need bytes (including terminator). | |
| static void | sb_append_n (StrBuf *sb, const char *s, size_t n) |
| Append n bytes from s to the buffer. | |
| static void | sb_append (StrBuf *sb, const char *s) |
| Append a NUL-terminated string to the buffer. | |
| static void | sb_append_ch (StrBuf *sb, char c) |
| Append a single character to the buffer. | |
| static void | nl_init (NameList *nl) |
| Initialize an empty NameList. | |
| static void | nl_add (NameList *nl, const char *name) |
| Add a copy of name to the list (ignores NULL/empty). | |
| static void | nl_free (NameList *nl) |
| Free all strings and internal storage in the list. | |
| static void | collect_exports_top_level (const char *text, NameList *out) |
| Collect top-level exported symbols (fun/class) from source text. Ignores strings and comments. Only lines with zero indentation count. | |
| static char * | preprocess_includes_internal (const char *src, const char *current_path, int depth) |
| Expand include directives in Fun source. | |
| char * | preprocess_includes (const char *src) |
| Public wrapper to preprocess includes without a current path. | |
| char * | preprocess_includes_with_path (const char *src, const char *current_path) |
| Preprocess includes with a known file path to improve span markers. | |
| int | map_expanded_line_to_include_path (const char *path, int line, char *out_path, size_t out_path_cap, int *out_line) |
| Map a line number in expanded source back to original include path/line. | |
| static double | parse_float_literal_value (const char *src, size_t len, size_t *pos, int *ok) |
| Parse a floating-point literal (supports . and scientific notation). | |
Low-level parsing helpers and include preprocessor for the Fun parser.
This module provides character scanners, token helpers, simple string/number literal readers, a lightweight include preprocessor that can expand include directives, and mapping utilities to translate expanded line numbers back to original files for diagnostics.
Definition in file parser_utils.c.
| #define DEFAULT_LIB_DIR "/usr/share/fun/lib/" |
|
static |
Collect top-level exported symbols (fun/class) from source text. Ignores strings and comments. Only lines with zero indentation count.
Definition at line 452 of file parser_utils.c.
|
static |
Consume expected character after skipping whitespace.
Definition at line 144 of file parser_utils.c.
| int map_expanded_line_to_include_path | ( | const char * | path, |
| int | line, | ||
| char * | out_path, | ||
| size_t | out_path_cap, | ||
| int * | out_line ) |
Map a line number in expanded source back to original include path/line.
Scans the expanded text for the nearest preceding __include_begin__ marker and computes the corresponding inner line number.
| path | Path to the original top-level file that was expanded. |
| line | 1-based line number in the expanded text. |
| out_path | Output buffer for the resolved file path. |
| out_path_cap | Capacity of out_path. |
| out_line | Receives 1-based line number within resolved file. |
Definition at line 996 of file parser_utils.c.
|
static |
Add a copy of name to the list (ignores NULL/empty).
Definition at line 422 of file parser_utils.c.
|
static |
Free all strings and internal storage in the list.
Definition at line 437 of file parser_utils.c.
|
static |
Initialize an empty NameList.
Definition at line 413 of file parser_utils.c.
|
static |
Parse a floating-point literal (supports . and scientific notation).
| ok | Set to 1 on success, 0 otherwise. |
Definition at line 1111 of file parser_utils.c.
|
static |
Parse an integer literal (decimal or 0x-hex) with optional sign.
| ok | Set to 1 on success, 0 on failure. |
Definition at line 272 of file parser_utils.c.
|
static |
Parse a single-quoted or double-quoted string literal.
Supports common C-style escapes:
, \r, \t, \, ", \' . Returns a newly allocated buffer on success and advances pos. On failure returns NULL and does not modify the source. Caller must free() returned buffer.
Definition at line 160 of file parser_utils.c.
| char * preprocess_includes | ( | const char * | src | ) |
Public wrapper to preprocess includes without a current path.
Definition at line 965 of file parser_utils.c.
|
static |
Expand include directives in Fun source.
Recognizes both #include "..." and include "..."/include <...> at the beginning of a line (after spaces/tabs). Angle-bracket includes search in FUN_LIB_DIR, then DEFAULT_LIB_DIR, then local lib/. Emits span markers of the form // __include_begin__: <path> [as alias] @line N to enable later mapping back to original files.
| src | Source code to preprocess. |
| current_path | Optional path of the current file for initial marker. |
| depth | Recursion depth guard. |
Definition at line 618 of file parser_utils.c.
| char * preprocess_includes_with_path | ( | const char * | src, |
| const char * | current_path ) |
Preprocess includes with a known file path to improve span markers.
Definition at line 973 of file parser_utils.c.
|
static |
Read entire file into a newly allocated buffer.
| path | Path to file. |
| out_len | Optional; receives number of bytes read (not including NUL). |
Definition at line 33 of file parser_utils.c.
|
static |
Read an identifier starting at pos and allocate its name.
| out_name | Set to malloc'd NUL-terminated name on success. |
Definition at line 248 of file parser_utils.c.
|
static |
Append a NUL-terminated string to the buffer.
Definition at line 386 of file parser_utils.c.
|
static |
Append a single character to the buffer.
Definition at line 393 of file parser_utils.c.
|
static |
Append n bytes from s to the buffer.
Definition at line 374 of file parser_utils.c.
|
static |
Initialize a StrBuf with a small starting capacity.
Definition at line 350 of file parser_utils.c.
|
static |
Ensure buffer capacity for at least need bytes (including terminator).
Definition at line 360 of file parser_utils.c.
|
static |
Skip whitespace, then line and block comments. Continues until the next non-comment, non-whitespace character.
Definition at line 88 of file parser_utils.c.
|
static |
If an identifier starts at pos, advance pos to its end. Recognizes [A-Za-z_][A-Za-z0-9_]*.
Definition at line 130 of file parser_utils.c.
|
static |
Advance pos to the next line, consuming the trailing '
' if present.
Definition at line 78 of file parser_utils.c.
|
static |
Skip a top-of-file shebang line that starts with "#!" if present.
Definition at line 120 of file parser_utils.c.
|
static |
Skip only spaces, tabs and carriage returns (not newlines).
Definition at line 232 of file parser_utils.c.
|
static |
Skip spaces, tabs, carriage returns and newlines.
| src | Source buffer. |
| len | Buffer length. |
| pos | In/out byte offset; advanced past whitespace. |
Definition at line 64 of file parser_utils.c.
|
static |
Check if src starting at pos begins with kw and fits in len.
Definition at line 111 of file parser_utils.c.
|
static |
Thin wrapper over realloc used by local buffers.
Definition at line 333 of file parser_utils.c.