libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
Macros | Functions
tstream.c File Reference
#include "tstream_short.h"
#include "error/error.h"
#include "util/arena.h"
#include "util/io.h"

Go to the source code of this file.

Macros

#define _PS_MATCH_N_ERROR_FIX_MSG
 

Functions

void ps_tstream_init (struct ps_tstream *ts, const struct ps_token_arr *tokens, const struct ps_file_ctx *file_ctx)
 Points tstream to the first token in tokens and reads from it there on.
 
bool ps_tstream_is_eof (struct ps_tstream *ts)
 Whether tstream has no more tokens.
 
struct ps_tokenps_tstream_current (struct ps_tstream *ts)
 The current token in tstream.
 
void ps_tstream_advance_n (struct ps_tstream *ts, isize n)
 Advances tstream to its nth next token.
 
struct ps_tokenps_tstream_peek (struct ps_tstream *ts, usize n, enum ps_token_type type)
 If the nth next token in ts is of the given type, returns it.
 
struct ps_tokenps_tstream_match (struct ps_tstream *ts, enum ps_token_type type, STR ctx)
 Attempts to match the next token with the given token type.
 
struct ps_tokenps_tstream_match_any (struct ps_tstream *ts, enum ps_token_type types[], usize count, STR ctx)
 Attempts to match the next token with one of the given token types.
 
void ps_tstream_eof_error (struct ps_tstream *ts, int code, STR ctx, STR fix)
 Reports an end-of-file error with the given context ctx, using the last token in ts for location.
 

Macro Definition Documentation

◆ _PS_MATCH_N_ERROR_FIX_MSG

#define _PS_MATCH_N_ERROR_FIX_MSG
Value:
ast_sprintf("Expected one of %zu options here, received '%s' instead", \
count, ps_token_type_to_string(current->type))
#define ast_sprintf(fmt,...)
Definition arena.h:77
STR ps_token_type_to_string(enum ps_token_type type)
Returns: the string representation of the given token type type.
Definition token.c:13

Definition at line 65 of file tstream.c.

Function Documentation

◆ ps_tstream_advance_n()

void ps_tstream_advance_n ( struct ps_tstream ts,
isize  n 
)
inline

Advances tstream to its nth next token.

Definition at line 23 of file tstream.c.

◆ ps_tstream_current()

struct ps_token * ps_tstream_current ( struct ps_tstream ts)
inline

The current token in tstream.

Definition at line 19 of file tstream.c.

◆ ps_tstream_eof_error()

void ps_tstream_eof_error ( struct ps_tstream ts,
int  code,
STR  ctx,
STR  fix 
)

Reports an end-of-file error with the given context ctx, using the last token in ts for location.

Precondition
The token stream ts must be non-empty.
Parameters
codeThe error code for the error. Supply -1 for a default error code.

Definition at line 99 of file tstream.c.

◆ ps_tstream_init()

void ps_tstream_init ( struct ps_tstream ts,
const struct ps_token_arr *  tokens,
const struct ps_file_ctx file_ctx 
)
inline

Points tstream to the first token in tokens and reads from it there on.

Definition at line 8 of file tstream.c.

◆ ps_tstream_is_eof()

bool ps_tstream_is_eof ( struct ps_tstream ts)
inline

Whether tstream has no more tokens.

Definition at line 15 of file tstream.c.

◆ ps_tstream_match()

struct ps_token * ps_tstream_match ( struct ps_tstream ts,
enum ps_token_type  type,
STR  ctx 
)

Attempts to match the next token with the given token type.

Returns NULL and generates an error on failure.

Parameters
ctxThe context to use for error messages.

Definition at line 36 of file tstream.c.

◆ ps_tstream_match_any()

struct ps_token * ps_tstream_match_any ( struct ps_tstream ts,
enum ps_token_type  types[],
usize  count,
STR  ctx 
)

Attempts to match the next token with one of the given token types.

Returns NULL and generates an error on failure.

Parameters
ctxThe context to use for error messages.

Definition at line 69 of file tstream.c.

◆ ps_tstream_peek()

struct ps_token * ps_tstream_peek ( struct ps_tstream ts,
usize  n,
enum ps_token_type  type 
)

If the nth next token in ts is of the given type, returns it.

Otherwise, returns NULL.

Definition at line 27 of file tstream.c.