MiniDevil As beautiful as a shell
parser_grammar.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* parser_grammar.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/10 18:11:17 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 07:14:20 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "parser.h"
14 
21 static int validate_empty(t_token *tokens)
22 {
23  if (!tokens)
25  return (0);
26 }
27 
34 static int validate_pipe_position(t_token *tokens)
35 {
36  t_token *current;
37 
38  if (!tokens)
39  return (0);
40  if (tokens->type == TOKEN_PIPE)
42  current = tokens;
43  while (current)
44  {
45  if (current->type == TOKEN_PIPE)
46  {
47  if (!current->next)
49  if (current->next->type == TOKEN_PIPE)
51  }
52  current = current->next;
53  }
54  return (0);
55 }
56 
63 static int validate_redir_syntax(t_token *tokens)
64 {
65  t_token *current;
66 
67  current = tokens;
68  while (current)
69  {
70  if (is_redirection(current->type))
71  {
72  if (!current->next)
74  if (current->next->type != TOKEN_WORD)
76  }
77  current = current->next;
78  }
79  return (0);
80 }
81 
89 {
90  if (validate_empty(tokens) < 0)
91  return (-1);
92  if (validate_pipe_position(tokens) < 0)
93  return (-1);
94  if (validate_redir_syntax(tokens) < 0)
95  return (-1);
96  return (0);
97 }
Parser entry point & sub parser prototypes.
static int validate_empty(t_token *tokens)
Validate that the token list isn't empty.
static int validate_pipe_position(t_token *tokens)
Validate pipe positions (not at start nor end and no consecutive pipe)
static int validate_redir_syntax(t_token *tokens)
Validate that every redirection is followed by a word token.
int validate_syntax(t_token *tokens)
Run all validation checks on a token list.
int print_syntax_error(t_syntax_error err)
Print an error message to STDERR.
int is_redirection(t_token_type type)
Check if a token type is a redirection operator.
@ ERR_REDIR_NO_FILE
Definition: structs.h:191
@ ERR_PIPE_DOUBLE
Definition: structs.h:189
@ ERR_EMPTY_INPUT
Definition: structs.h:186
@ ERR_PIPE_START
Definition: structs.h:187
@ ERR_PIPE_END
Definition: structs.h:188
@ TOKEN_WORD
Definition: structs.h:25
@ TOKEN_PIPE
Definition: structs.h:26
Lexer token (singly linked list)
Definition: structs.h:71
t_token_type type
Token type (word, pipe, redir)
Definition: structs.h:72
struct s_token * next
Next token.
Definition: structs.h:76