159 lines
3.2 KiB
C
159 lines
3.2 KiB
C
|
|
#include "../syntax.h"
|
||
|
|
|
||
|
|
#include <bshell/parse/lex.h>
|
||
|
|
#include <bshell/parse/parse.h>
|
||
|
|
#include <bshell/parse/token.h>
|
||
|
|
|
||
|
|
struct bshell_lex_token *claim_token(struct bshell_parse_ctx *ctx)
|
||
|
|
{
|
||
|
|
return bshell_lex_ctx_claim(ctx->p_src);
|
||
|
|
}
|
||
|
|
|
||
|
|
void discard_token(struct bshell_parse_ctx *ctx)
|
||
|
|
{
|
||
|
|
return bshell_lex_ctx_discard(ctx->p_src);
|
||
|
|
}
|
||
|
|
|
||
|
|
struct bshell_lex_token *peek_token(struct bshell_parse_ctx *ctx)
|
||
|
|
{
|
||
|
|
return bshell_lex_ctx_peek(ctx->p_src);
|
||
|
|
}
|
||
|
|
|
||
|
|
enum bshell_lex_token_type peek_token_type(struct bshell_parse_ctx *ctx)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
return tok ? tok->tok_type : BSHELL_TOK_NONE;
|
||
|
|
}
|
||
|
|
|
||
|
|
enum bshell_lex_symbol peek_unknown_symbol(struct bshell_parse_ctx *ctx)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
return (tok && tok->tok_type == BSHELL_TOK_SYMBOL) ? tok->tok_symbol
|
||
|
|
: BSHELL_SYM_NONE;
|
||
|
|
}
|
||
|
|
|
||
|
|
enum bshell_lex_keyword peek_unknown_keyword(struct bshell_parse_ctx *ctx)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
return (tok && tok->tok_type == BSHELL_TOK_KEYWORD) ? tok->tok_keyword
|
||
|
|
: BSHELL_KW_NONE;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool peek_word(struct bshell_parse_ctx *ctx, struct bshell_lex_token **out)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
if (tok && tok->tok_type == BSHELL_TOK_WORD) {
|
||
|
|
*out = tok;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool peek_linefeed(struct bshell_parse_ctx *ctx)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
if (tok && tok->tok_type == BSHELL_TOK_LINEFEED) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool peek_symbol(struct bshell_parse_ctx *ctx, enum bshell_lex_symbol sym)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
if (!tok) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (tok->tok_type != BSHELL_TOK_SYMBOL) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (tok->tok_symbol != sym) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool parse_linefeed(struct bshell_parse_ctx *ctx)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
if (tok && tok->tok_type == BSHELL_TOK_LINEFEED) {
|
||
|
|
discard_token(ctx);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool parse_symbol(struct bshell_parse_ctx *ctx, enum bshell_lex_symbol sym)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
if (!tok) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (tok->tok_type != BSHELL_TOK_SYMBOL) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (tok->tok_symbol != sym) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
discard_token(ctx);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool parse_keyword(struct bshell_parse_ctx *ctx, enum bshell_lex_keyword kw)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
if (!tok) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (tok->tok_type != BSHELL_TOK_KEYWORD) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (tok->tok_keyword != kw) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
discard_token(ctx);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool parse_word(struct bshell_parse_ctx *ctx, struct bshell_lex_token **out)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
if (!tok) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (tok->tok_type != BSHELL_TOK_WORD) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
*out = claim_token(ctx);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool parse_var(struct bshell_parse_ctx *ctx, struct bshell_lex_token **out)
|
||
|
|
{
|
||
|
|
struct bshell_lex_token *tok = peek_token(ctx);
|
||
|
|
if (!tok) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (tok->tok_type != BSHELL_TOK_VAR) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
*out = claim_token(ctx);
|
||
|
|
return true;
|
||
|
|
}
|