lexing up and running

This commit is contained in:
Ellen Arvidsson 2025-04-15 20:02:25 +02:00
parent 10e16147ba
commit b9266cdf96
6 changed files with 434 additions and 102 deletions

193
include/msph/token.h Normal file
View file

@ -0,0 +1,193 @@
#ifndef _MSPH_EXPR_H
#define _MSPH_EXPR_H
/*
* TYPES:
* Conj
* Disj
* Impl
* Arrow
* Box
* (Sub)
* Forall
*
* True
* False
* Var
* Nominal
*
* Record
*
* DEFINITIONS/DIRECTIVES:
* Type definition (type A[X..] = T)
* Nominal definition (nominal N)
* Member definition (member mname : T)
*
* {
* member a: A
* member b: B
* }
*
* Subtyping check (check A <: B, checks if A <: B)
*
* EXTRA DEFINITIONS
* Class definition (class C[...] { ... }, shorthand)
*
* EXPRESSIONS
* Conj
* Disj
* Impl
* Arrow
* Box
* (Sub)
* Forall
*
* True
* False
* Var
* Nominal
*
* Trait ({ ... }, creates scoping)
*
*
* TOKENS
* lbrace {
* rbrace }
* lbrak [
* rbrak ]
* lparen (
* rparen )
* colon :
* equals =
*
* op_and &
* op_or |
* op_impl =>
* op_sub <:
*
* kw_type type
* kw_nominal nominal
* kw_member member
* kw_check check
* kw_box box
* kw_forall forall
*
* k_true True
* k_false False
*
* ident C, anid, ...
*
*/
struct msph_ctx {
int err;
int err_info;
char errbuf[SPHO_ERR_BUF_LEN];
#ifdef SPHO_DEBUG
char filebuf[SPHO_ERR_FILEBUF_LEN];
int errline;
#endif
};
enum msph_tok_type {
TOK_LBRACE, // {
TOK_RBRACE, // }
TOK_LBRAK, // [
TOK_RBRAK, // ]
TOK_LPAREN, // (
TOK_RPAREN, // )
TOK_COLON, // :
TOK_EQUALS, // =
TOK_AMP, // &
TOK_PIPE, // |
TOK_RARROW, // =>
TOK_SUB, // <:
TOK_KW_TYPE, // type
TOK_KW_NOMINAL, // nominal
TOK_KW_MEMBER, // member
TOK_KW_CHECK, // check
TOK_KW_BOX, // box
TOK_KW_FORALL, // forall
TOK_CONST_TRUE, // True
TOK_CONST_FALSE, // False
TOK_IDENT, // identifiers
TOK_WSPACE, // special: whitespace
TOK_END // special: denote end of array
};
#define MSPH_TOKEN_BUF_LEN 128
struct token_s {
char buf[MSPH_TOKEN_BUF_LEN];
};
union token_data {
struct token_s s;
};
struct msph_token {
int type;
union token_data d;
};
#define MSPH_FILE_NAME_LEN 1024
#define MSPH_FILE_BUF_LEN 4096
#define MSPH_TOKEN_SRC_FILE 1
struct msph_token_src_file {
FILE *f;
/* circular buffer for reading */
size_t pos;
size_t end;
char buf[MSPH_FILE_BUF_LEN];
/* file path */
char name[MSPH_FILE_NAME_LEN];
};
#define MSPH_TOKEN_SRC_STR 2
struct msph_token_src_str {
const char *s;
size_t len;
size_t pos;
};
union msph_token_src_data {
struct msph_token_src_file file;
struct msph_token_src_str str;
};
struct msph_token_src {
int type;
union msph_token_src_data inner;
};
struct msph_token_stream {
struct msph_ctx *ctx;
struct msph_token_src src;
};
void msph_ctx_init(struct msph_ctx *);
struct msph_token_stream *msph_token_stream_file(struct msph_ctx *,
FILE *, const char *);
struct msph_token_stream *msph_token_stream_frombuf(struct msph_ctx *,
const char *, size_t);
int msph_token_stream_close(struct msph_token_stream*);
ssize_t msph_token_stream_read_tok(struct msph_token *, size_t,
struct msph_token_stream *);
int msph_token_stream_print(struct msph_token_stream *, FILE *);
size_t msph_token_str(char *buf, size_t len, struct msph_token *tok);
#endif /* _MSPH_EXPR_H */