MiniDevil As beautiful as a shell
parser_grammar_error.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* parser_grammar_error.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/11 18:18:08 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 04:47:13 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "parser.h"
14 #include "libft.h"
15 
22 static const char *get_error_msg(t_syntax_error err)
23 {
24  if (err == ERR_EMPTY_INPUT)
25  return ("minishell: syntax error: empty input\n");
26  if (err == ERR_PIPE_START || err == ERR_PIPE_END || err == ERR_PIPE_DOUBLE)
27  return ("minishell: syntax error near unexpected token `|'\n");
28  if (err == ERR_PIPE_NO_CMD)
29  return ("minishell: syntax error: missing command after pipe\n");
30  if (err == ERR_REDIR_NO_FILE)
31  return ("minishell: syntax error: missing filename "
32  "after redirection\n");
33  if (err == ERR_REDIR_AFTER_PIPE)
34  return ("minishell: syntax error: redirection after pipe\n");
35  return (NULL);
36 }
37 
45 {
46  const char *msg;
47 
48  if (err == ERR_NONE)
49  return (-1);
50  msg = get_error_msg(err);
51  if (msg)
52  ft_putstr_fd((char *)msg, 2);
53  return (-1);
54 }
55 
63 {
64  return (type == TOKEN_REDIR_IN || type == TOKEN_REDIR_OUT
65  || type == TOKEN_APPEND || type == TOKEN_HEREDOC);
66 }
Parser entry point & sub parser prototypes.
int print_syntax_error(t_syntax_error err)
Print an error message to STDERR.
static const char * get_error_msg(t_syntax_error err)
Convert an error code to the corresponding message.
int is_redirection(t_token_type type)
Check if a token type is a redirection operator.
t_syntax_error
Syntax error codes (for parser)
Definition: structs.h:184
@ ERR_REDIR_NO_FILE
Definition: structs.h:191
@ ERR_PIPE_DOUBLE
Definition: structs.h:189
@ ERR_EMPTY_INPUT
Definition: structs.h:186
@ ERR_NONE
Definition: structs.h:185
@ ERR_PIPE_START
Definition: structs.h:187
@ ERR_PIPE_NO_CMD
Definition: structs.h:190
@ ERR_REDIR_AFTER_PIPE
Definition: structs.h:192
@ ERR_PIPE_END
Definition: structs.h:188
t_token_type
Token types produced by Lexer.
Definition: structs.h:24
@ TOKEN_REDIR_OUT
Definition: structs.h:28
@ TOKEN_HEREDOC
Definition: structs.h:30
@ TOKEN_REDIR_IN
Definition: structs.h:27
@ TOKEN_APPEND
Definition: structs.h:29