Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
parser_utils.c File Reference

Low-level parsing helpers and include preprocessor for the Fun parser. More...

#include "parser.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for parser_utils.c:
This graph shows which files directly or indirectly include this file:

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).

Detailed Description

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.

Macro Definition Documentation

◆ DEFAULT_LIB_DIR

#define DEFAULT_LIB_DIR   "/usr/share/fun/lib/"

Function Documentation

◆ collect_exports_top_level()

void collect_exports_top_level ( const char * text,
NameList * out )
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.

◆ consume_char()

int consume_char ( const char * src,
size_t len,
size_t * pos,
char expected )
static

Consume expected character after skipping whitespace.

Returns
1 if consumed; 0 if not present.

Definition at line 144 of file parser_utils.c.

◆ map_expanded_line_to_include_path()

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.

Parameters
pathPath to the original top-level file that was expanded.
line1-based line number in the expanded text.
out_pathOutput buffer for the resolved file path.
out_path_capCapacity of out_path.
out_lineReceives 1-based line number within resolved file.
Returns
1 on success, 0 on failure.

Definition at line 996 of file parser_utils.c.

◆ nl_add()

void nl_add ( NameList * nl,
const char * name )
static

Add a copy of name to the list (ignores NULL/empty).

Definition at line 422 of file parser_utils.c.

◆ nl_free()

void nl_free ( NameList * nl)
static

Free all strings and internal storage in the list.

Definition at line 437 of file parser_utils.c.

◆ nl_init()

void nl_init ( NameList * nl)
static

Initialize an empty NameList.

Definition at line 413 of file parser_utils.c.

◆ parse_float_literal_value()

double parse_float_literal_value ( const char * src,
size_t len,
size_t * pos,
int * ok )
static

Parse a floating-point literal (supports . and scientific notation).

Parameters
okSet to 1 on success, 0 otherwise.
Returns
Parsed double value.

Definition at line 1111 of file parser_utils.c.

◆ parse_int_literal_value()

uint64_t parse_int_literal_value ( const char * src,
size_t len,
size_t * pos,
int * ok )
static

Parse an integer literal (decimal or 0x-hex) with optional sign.

Parameters
okSet to 1 on success, 0 on failure.
Returns
Parsed value (two's complement cast for negative inputs).

Definition at line 272 of file parser_utils.c.

◆ parse_string_literal_any_quote()

char * parse_string_literal_any_quote ( const char * src,
size_t len,
size_t * pos )
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.

◆ preprocess_includes()

char * preprocess_includes ( const char * src)

Public wrapper to preprocess includes without a current path.

Definition at line 965 of file parser_utils.c.

◆ preprocess_includes_internal()

char * preprocess_includes_internal ( const char * src,
const char * current_path,
int depth )
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.

Parameters
srcSource code to preprocess.
current_pathOptional path of the current file for initial marker.
depthRecursion depth guard.
Returns
Newly allocated expanded text or NULL on OOM.

Definition at line 618 of file parser_utils.c.

◆ preprocess_includes_with_path()

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.

◆ read_file_all()

char * read_file_all ( const char * path,
size_t * out_len )
static

Read entire file into a newly allocated buffer.

Parameters
pathPath to file.
out_lenOptional; receives number of bytes read (not including NUL).
Returns
Newly allocated NUL-terminated buffer on success, or NULL on error. Caller must free() the returned buffer.

Definition at line 33 of file parser_utils.c.

◆ read_identifier_into()

int read_identifier_into ( const char * src,
size_t len,
size_t * pos,
char ** out_name )
static

Read an identifier starting at pos and allocate its name.

Parameters
out_nameSet to malloc'd NUL-terminated name on success.
Returns
1 on success (pos advanced), 0 otherwise.

Definition at line 248 of file parser_utils.c.

◆ sb_append()

void sb_append ( StrBuf * sb,
const char * s )
static

Append a NUL-terminated string to the buffer.

Definition at line 386 of file parser_utils.c.

◆ sb_append_ch()

void sb_append_ch ( StrBuf * sb,
char c )
static

Append a single character to the buffer.

Definition at line 393 of file parser_utils.c.

◆ sb_append_n()

void sb_append_n ( StrBuf * sb,
const char * s,
size_t n )
static

Append n bytes from s to the buffer.

Definition at line 374 of file parser_utils.c.

◆ sb_init()

void sb_init ( StrBuf * sb)
static

Initialize a StrBuf with a small starting capacity.

Definition at line 350 of file parser_utils.c.

◆ sb_reserve()

void sb_reserve ( StrBuf * sb,
size_t need )
static

Ensure buffer capacity for at least need bytes (including terminator).

Definition at line 360 of file parser_utils.c.

◆ skip_comments()

void skip_comments ( const char * src,
size_t len,
size_t * pos )
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.

◆ skip_identifier()

void skip_identifier ( const char * src,
size_t len,
size_t * pos )
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.

◆ skip_line()

void skip_line ( const char * src,
size_t len,
size_t * pos )
static

Advance pos to the next line, consuming the trailing '
' if present.

Definition at line 78 of file parser_utils.c.

◆ skip_shebang_if_present()

void skip_shebang_if_present ( const char * src,
size_t len,
size_t * pos )
static

Skip a top-of-file shebang line that starts with "#!" if present.

Definition at line 120 of file parser_utils.c.

◆ skip_spaces()

void skip_spaces ( const char * src,
size_t len,
size_t * pos )
static

Skip only spaces, tabs and carriage returns (not newlines).

Definition at line 232 of file parser_utils.c.

◆ skip_ws()

void skip_ws ( const char * src,
size_t len,
size_t * pos )
static

Skip spaces, tabs, carriage returns and newlines.

Parameters
srcSource buffer.
lenBuffer length.
posIn/out byte offset; advanced past whitespace.

Definition at line 64 of file parser_utils.c.

◆ starts_with()

int starts_with ( const char * src,
size_t len,
size_t pos,
const char * kw )
static

Check if src starting at pos begins with kw and fits in len.

Returns
1 if matches, 0 otherwise.

Definition at line 111 of file parser_utils.c.

◆ xrealloc()

void * xrealloc ( void * ptr,
size_t newcap )
static

Thin wrapper over realloc used by local buffers.

Definition at line 333 of file parser_utils.c.