libpulsar
A modular compiler for the pulsar programming language
Loading...
Searching...
No Matches
Macros | Functions
parser.c File Reference
#include <stddef.h>
#include <math.h>
#include "parser.h"
#include "tstream_short.h"
#include "error/error.h"
#include "util/arena.h"
#include "util/abort.h"
#include "frontend/analyzer/type.h"
#include "frontend/lexer/operator.h"

Go to the source code of this file.

Macros

#define try_get_float(from, bind_to, do_)
 
#define PS_GROUP_MISMATCH_REFER(tkn)
 
#define PS_END_STM(ctx)
 
#define _PS_IS_FN_QUAL()
 

Functions

struct ps_type_tuple * ps_parse_type_tuple (struct ps_tstream *ts)
 Parses a tuple type.
 
struct ps_typeps_parse_type_primary (struct ps_tstream *ts)
 Parses a primary type such as a tuple or unresolved (struct, enum).
 
struct ps_typeps_parse_type (struct ps_tstream *ts)
 Parses a type such as Rectangle or (Point, Point) from the token stream.
 
struct ps_name * ps_parse_name (struct ps_tstream *ts, struct ps_token *first)
 Parses a name such as foo::bar::baz.
 
struct ps_node_block * ps_parse_block (struct ps_tstream *ts)
 Parses a block like:
 
struct ps_nodeps_parse_let (struct ps_tstream *ts)
 
struct ps_nodeps_parse_fn (struct ps_tstream *ts)
 Parses a function definition.
 
struct ps_nodeps_parse_import (struct ps_tstream *ts)
 Parses an import statement.
 
struct ps_nodeps_parse_extern (struct ps_tstream *ts)
 Parses an eexternal declaration.
 
struct ps_nodeps_parse_struct (struct ps_tstream *ts)
 Parses a structure declaration.
 
struct ps_nodeps_parse_enum (struct ps_tstream *ts)
 Parses an enumeration declaration.
 
struct ps_nodeps_parse_for (struct ps_tstream *ts)
 Parses a for loop.
 
struct ps_nodeps_parse_while (struct ps_tstream *ts)
 Parses a while loop.
 
struct ps_nodeps_parse_if (struct ps_tstream *ts)
 Parses an if-else statement.
 
struct ps_nodeps_parse_return (struct ps_tstream *ts)
 Parses a return statement.
 
struct ps_nodeps_parse_expr_stm (struct ps_tstream *ts)
 An expression statement consists of an expression followed by a newline.
 
struct ps_exprps_parse_interp_str (struct ps_tstream *ts)
 
struct ps_exprps_parse_expr_lit (struct ps_tstream *ts)
 Parses an expression literal.
 
struct ps_exprps_parse_expr_call (struct ps_tstream *ts, struct ps_token *callee)
 Parses a call expression, which involves a token (TODO: an expression) followed by a set of parentheses enclosing arguments.
 
struct ps_exprps_parse_expr_primary (struct ps_tstream *ts)
 A primary expression is a literal or call expression.
 
struct ps_exprps_parse_expr_helper (struct ps_tstream *ts, struct ps_expr *lhs, ps_operator_precedence_t min_prec)
 Handles predence and associativity in parsing expressions.
 
struct ps_exprps_parse_expr (struct ps_tstream *ts)
 Parses a general expression.
 
struct ps_nodeps_parse_node (struct ps_tstream *ts)
 Parses and returns the next node in ts.
 
struct ps_node_arr * ps_parse (struct ps_token_arr *tokens, const struct ps_file_ctx *file_ctx)
 Constructs a series of nodes from the given tokens.
 

Macro Definition Documentation

◆ _PS_IS_FN_QUAL

#define _PS_IS_FN_QUAL ( )
Value:
(ps_tstream_peek(ts, 0, PS_TOKEN_FUNC_INLINE) \
|| ps_tstream_peek(ts, 0, PS_TOKEN_FUNC_PRIVATE) \
|| ps_tstream_peek(ts, 0, PS_TOKEN_FUNC_PUBLIC) \
|| ps_tstream_peek(ts, 0, PS_TOKEN_FUNC_STATIC))
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.
Definition tstream.c:27

Definition at line 223 of file parser.c.

◆ PS_END_STM

#define PS_END_STM (   ctx)
Value:
do { \
if (!ps_tstream_peek(ts, 0, PS_TOKEN_RBRACE)) { \
match(PS_TOKEN_NL, "at end of let statement") else { \
return NULL; \
} \
} \
} while (0)

Definition at line 25 of file parser.c.

◆ PS_GROUP_MISMATCH_REFER

#define PS_GROUP_MISMATCH_REFER (   tkn)
Value:
ps_error(PS_SCOPE_NOTE, PS_ECODE_MISMATCH, ts->file_ctx->buffer, \
(tkn)->loc, (tkn)->length, "to match opening parentheses", \
"Group opened here", NULL)
Represents an error or source-referencing display message.
Definition error.h:43

Definition at line 20 of file parser.c.

◆ try_get_float

#define try_get_float (   from,
  bind_to,
  do_ 
)
Value:
if (sscanf(from, "%lf", &bind_to) == 1) { \
do_ \
}

Definition at line 15 of file parser.c.

Function Documentation

◆ ps_parse()

struct ps_node_arr * ps_parse ( struct ps_token_arr *  tokens,
const struct ps_file_ctx file_ctx 
)

Constructs a series of nodes from the given tokens.

This is the only function in parser.h that you should call.

Parameters
tokensThe unmodified output of ps_lex().
file_ctxThe file information needed to produce helpful error messages.
Returns
The AST as an array of nodes.

Definition at line 761 of file parser.c.

◆ ps_parse_block()

struct ps_node_block * ps_parse_block ( struct ps_tstream ts)

Parses a block like:

{
<statements>...
}
Precondition
The current token in ts is '{'.

Definition at line 149 of file parser.c.

◆ ps_parse_enum()

struct ps_node * ps_parse_enum ( struct ps_tstream ts)

Parses an enumeration declaration.

Precondition
The current token in ts is 'enum'

Definition at line 391 of file parser.c.

◆ ps_parse_expr()

struct ps_expr * ps_parse_expr ( struct ps_tstream ts)

Parses a general expression.

Definition at line 717 of file parser.c.

◆ ps_parse_expr_call()

struct ps_expr * ps_parse_expr_call ( struct ps_tstream ts,
struct ps_token callee 
)

Parses a call expression, which involves a token (TODO: an expression) followed by a set of parentheses enclosing arguments.

Precondition
The current token in ts is '('.

Definition at line 578 of file parser.c.

◆ ps_parse_expr_helper()

struct ps_expr * ps_parse_expr_helper ( struct ps_tstream ts,
struct ps_expr lhs,
ps_operator_precedence_t  min_prec 
)

Handles predence and associativity in parsing expressions.

Precondition
The lhs expression has been entirely consumed in ts.
Parameters
lhsThe left hand side of the current expression.
min_precThe minimum precedence encountered so far.

Definition at line 674 of file parser.c.

◆ ps_parse_expr_lit()

struct ps_expr * ps_parse_expr_lit ( struct ps_tstream ts)

Parses an expression literal.

Definition at line 519 of file parser.c.

◆ ps_parse_expr_primary()

struct ps_expr * ps_parse_expr_primary ( struct ps_tstream ts)

A primary expression is a literal or call expression.

These are used as base cases invoked from ps_parse_expr() and ps_parse_expr_helper() to handle more complicated nesting.

Definition at line 609 of file parser.c.

◆ ps_parse_expr_stm()

struct ps_node * ps_parse_expr_stm ( struct ps_tstream ts)

An expression statement consists of an expression followed by a newline.

Definition at line 480 of file parser.c.

◆ ps_parse_extern()

struct ps_node * ps_parse_extern ( struct ps_tstream ts)

Parses an eexternal declaration.

Precondition
The current token in ts is 'extern'

Definition at line 362 of file parser.c.

◆ ps_parse_fn()

struct ps_node * ps_parse_fn ( struct ps_tstream ts)

Parses a function definition.

Precondition
The current token in ts is one of: 'fn', 'static', 'inline', 'public', 'private'.

Definition at line 229 of file parser.c.

◆ ps_parse_for()

struct ps_node * ps_parse_for ( struct ps_tstream ts)

Parses a for loop.

Precondition
The current token in ts is 'for'

Definition at line 399 of file parser.c.

◆ ps_parse_if()

struct ps_node * ps_parse_if ( struct ps_tstream ts)

Parses an if-else statement.

Precondition
The current token in ts is 'if'

TODO: handle the else branch and additional clauses.

Definition at line 427 of file parser.c.

◆ ps_parse_import()

struct ps_node * ps_parse_import ( struct ps_tstream ts)

Parses an import statement.

Precondition
The current token in ts is 'import'

Definition at line 344 of file parser.c.

◆ ps_parse_interp_str()

struct ps_expr * ps_parse_interp_str ( struct ps_tstream ts)
Precondition
The current token in ts is a string interpolation literal.

Definition at line 486 of file parser.c.

◆ ps_parse_let()

struct ps_node * ps_parse_let ( struct ps_tstream ts)
Precondition
The current token in ts is 'let'.

Definition at line 179 of file parser.c.

◆ ps_parse_name()

struct ps_name * ps_parse_name ( struct ps_tstream ts,
struct ps_token first 
)

Parses a name such as foo::bar::baz.

Parameters
firstThe first identifier in the name. In the above example, it would be foo.

Definition at line 133 of file parser.c.

◆ ps_parse_node()

struct ps_node * ps_parse_node ( struct ps_tstream ts)

Parses and returns the next node in ts.

Precondition
ts is not end-of-file.

Definition at line 721 of file parser.c.

◆ ps_parse_return()

struct ps_node * ps_parse_return ( struct ps_tstream ts)

Parses a return statement.

Precondition
The current token in ts is 'return'

Definition at line 457 of file parser.c.

◆ ps_parse_struct()

struct ps_node * ps_parse_struct ( struct ps_tstream ts)

Parses a structure declaration.

Precondition
The current token in ts is 'struct'

Definition at line 383 of file parser.c.

◆ ps_parse_type()

struct ps_type * ps_parse_type ( struct ps_tstream ts)

Parses a type such as Rectangle or (Point, Point) from the token stream.

Definition at line 122 of file parser.c.

◆ ps_parse_type_primary()

struct ps_type * ps_parse_type_primary ( struct ps_tstream ts)

Parses a primary type such as a tuple or unresolved (struct, enum).

Definition at line 98 of file parser.c.

◆ ps_parse_type_tuple()

struct ps_type_tuple * ps_parse_type_tuple ( struct ps_tstream ts)

Parses a tuple type.

Precondition
The current token in ts is '('.

Definition at line 65 of file parser.c.

◆ ps_parse_while()

struct ps_node * ps_parse_while ( struct ps_tstream ts)

Parses a while loop.

Definition at line 407 of file parser.c.