Skip to content

Commit

Permalink
parser: start making use of match
Browse files Browse the repository at this point in the history
  • Loading branch information
mptre committed Aug 14, 2024
1 parent b3fcb1f commit de25920
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 74 deletions.
13 changes: 4 additions & 9 deletions parser-cpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "doc.h"
#include "expr.h"
#include "lexer.h"
#include "match.h"
#include "parser-expr.h"
#include "parser-priv.h"
#include "parser-type.h"
Expand Down Expand Up @@ -54,16 +55,10 @@ parser_cpp_peek_type(struct parser *pr, struct token **rparen)
return 1;

/* Detect LIST_ENTRY(list, struct s) from libks:list(3). */
lexer_peek_enter(lx, &s);
if (lexer_if(lx, TOKEN_IDENT, &ident) && is_list_entry(ident) &&
lexer_if(lx, TOKEN_LPAREN, NULL) &&
lexer_if(lx, TOKEN_IDENT, NULL) &&
lexer_if(lx, TOKEN_COMMA, NULL) &&
lexer_if(lx, TOKEN_IDENT, NULL) &&
lexer_if(lx, TOKEN_RPAREN, rparen) &&
lexer_if(lx, TOKEN_SEMI, NULL))
if (match_peek(lx,
"IDENT= LPAREN IDENT COMMA IDENT RPAREN= SEMI", &ident, rparen) &&
is_list_entry(ident))
peek = 1;
lexer_peek_leave(lx, &s);

return peek;
}
Expand Down
75 changes: 10 additions & 65 deletions parser-type.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "doc.h"
#include "lexer.h"
#include "match.h"
#include "parser-cpp.h"
#include "parser-expr.h"
#include "parser-func.h"
Expand Down Expand Up @@ -348,89 +349,33 @@ peek_type_func_ptr(struct lexer *lx, struct token **lhs, struct token **rhs)
static int
peek_type_ptr_array(struct lexer *lx, struct token **rsquare)
{
struct lexer_state s;
int peek = 0;

lexer_peek_enter(lx, &s);
if (lexer_if(lx, TOKEN_LPAREN, NULL) &&
lexer_if(lx, TOKEN_STAR, NULL)) {
(void)lexer_if(lx, TOKEN_IDENT, NULL);
if (lexer_if(lx, TOKEN_RPAREN, NULL) &&
lexer_if(lx, TOKEN_LSQUARE, NULL) &&
(lexer_if(lx, TOKEN_LITERAL, NULL) ||
lexer_if(lx, TOKEN_IDENT, NULL)) &&
lexer_if(lx, TOKEN_RSQUARE, rsquare))
peek = 1;
}
lexer_peek_leave(lx, &s);
return peek;
return match_peek(lx,
"LPAREN STAR IDENT? RPAREN LSQUARE (LITERAL | IDENT) RSQUARE=",
rsquare);
}

static int
peek_type_ident_after_type(struct lexer *lx)
{
struct lexer_state s;
int peek = 0;

lexer_peek_enter(lx, &s);
if (lexer_if(lx, TOKEN_IDENT, NULL) &&
(lexer_if_flags(lx, TOKEN_FLAG_ASSIGN, NULL) ||
lexer_if(lx, TOKEN_LSQUARE, NULL) ||
(lexer_if(lx, TOKEN_LPAREN, NULL) &&
!lexer_peek_if(lx, TOKEN_STAR, NULL)) ||
lexer_if(lx, TOKEN_RPAREN, NULL) ||
lexer_if(lx, TOKEN_SEMI, NULL) ||
lexer_if(lx, TOKEN_COMMA, NULL) ||
lexer_if(lx, TOKEN_COLON, NULL) ||
lexer_if(lx, TOKEN_ATTRIBUTE, NULL)))
peek = 1;
lexer_peek_leave(lx, &s);

return peek;
return match_peek(lx,
"IDENT (FLAGS(ASSIGN) | LSQUARE | (LPAREN !STAR) | RPAREN | SEMI"
" | COMMA | COLON | ATTRIBUTE)", NULL);
}

static int
peek_type_noident(struct lexer *lx, struct token **tk)
{
struct lexer_state s;
int peek = 0;

lexer_peek_enter(lx, &s);
if (lexer_if(lx, TOKEN_IDENT, tk) &&
(lexer_if(lx, TOKEN_RPAREN, NULL) ||
lexer_if(lx, TOKEN_COMMA, NULL)))
peek = 1;
lexer_peek_leave(lx, &s);
return peek;
return match_peek(lx, "IDENT= (RPAREN | COMMA)", tk);
}

static int
peek_type_unknown_array(struct lexer *lx, struct token **tk)
{
struct lexer_state s;
int peek = 0;

lexer_peek_enter(lx, &s);
if (lexer_if(lx, TOKEN_IDENT, NULL) &&
lexer_if(lx, TOKEN_LSQUARE, NULL)) {
(void)lexer_if(lx, TOKEN_LITERAL, NULL);
if (lexer_if(lx, TOKEN_RSQUARE, tk))
peek = 1;
}
lexer_peek_leave(lx, &s);
return peek;
return match_peek(lx, "IDENT LSQUARE LITERAL? RSQUARE=", tk);
}

static int
peek_type_unknown_bitfield(struct lexer *lx, struct token **tk)
{
struct lexer_state s;
int peek;

lexer_peek_enter(lx, &s);
peek = lexer_if(lx, TOKEN_IDENT, tk) &&
lexer_if(lx, TOKEN_COLON, NULL) &&
lexer_if(lx, TOKEN_LITERAL, NULL);
lexer_peek_leave(lx, &s);
return peek;
return match_peek(lx, "IDENT= COLON LITERAL", tk);
}

0 comments on commit de25920

Please sign in to comment.