Skip to content

Commit

Permalink
Add lex_is lexer method
Browse files Browse the repository at this point in the history
For testing the current token without advancing the lexer.
  • Loading branch information
gw committed Aug 28, 2017
1 parent d4bc228 commit 5e8f131
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
18 changes: 12 additions & 6 deletions src/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@ lex_scan(struct lexer *l) {
}
}

// Return 1 if current token is type `t`,
// 0 otherwise.
int
lex_is(struct lexer *l, enum tok_type t) {
return l->tok.cls == t;
}

// Assert current token type.
// If assertion passes, returns 1 and advances lexer,
// otherwise returns 0 and does not advance lexer.
int
lex_consume(struct lexer *l, enum tok_type want) {
enum tok_type curr = l->tok.cls;
if (curr == want) {
lex_consume(struct lexer *l, enum tok_type t) {
if (lex_is(l, t)) {
lex_scan(l);
return 1;
} else {
Expand All @@ -106,12 +112,12 @@ lex_consume(struct lexer *l, enum tok_type want) {
// If assertion passes, returns 1 and advances lexer,
// otherwise prints an error message and dies.
int
lex_consumef(struct lexer *l, enum tok_type want) {
if (!lex_consume(l, want)) {
lex_consumef(struct lexer *l, enum tok_type t) {
if (!lex_consume(l, t)) {
fprintf(
stderr,
"Expected token '%s', got '%s'\n",
lilc_token_str[want], lilc_token_str[l->tok.cls]
lilc_token_str[t], lilc_token_str[l->tok.cls]
);
exit(1);
}
Expand Down
5 changes: 3 additions & 2 deletions src/lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ struct lexer {
};

void lex_init(struct lexer *l, char *source);
int lex_consume(struct lexer *l, enum tok_type want);
int lex_consumef(struct lexer *l, enum tok_type want);
int lex_is(struct lexer *l, enum tok_type t);
int lex_consume(struct lexer *l, enum tok_type t);
int lex_consumef(struct lexer *l, enum tok_type t);
enum tok_type lex_scan(struct lexer *l);
int tok_strm_readf(char *buf, struct lexer *l);

Expand Down
6 changes: 3 additions & 3 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ funcdef_prefix(struct parser *p, struct token t) {

char *params[MAX_FUNC_PARAMS];
unsigned int param_count = 0;
while (p->lex->tok.cls != LILC_TOK_RPAREN && param_count <= MAX_FUNC_PARAMS) {
if (p->lex->tok.cls != LILC_TOK_ID) {
while (!lex_is(p->lex, LILC_TOK_RPAREN) && param_count <= MAX_FUNC_PARAMS) {
if (!lex_is(p->lex, LILC_TOK_ID)) {
fprintf(stderr, "Expected id, got %s", lilc_token_str[p->lex->tok.cls]);
exit(1);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ program(struct parser *p) {
struct lilc_block_node_t *block = lilc_block_node_new();

lex_scan(p->lex); // Load first token
while (p->lex->tok.cls != LILC_TOK_EOS) {
while (!lex_is(p->lex, LILC_TOK_EOS)) {
node = stmt(p);
kv_push(struct lilc_node_t *, *block->stmts, node);
lex_consumef(p->lex, LILC_TOK_SEMI);
Expand Down

0 comments on commit 5e8f131

Please sign in to comment.