refactoring lexing

This commit is contained in:
LuckyLaszlo
2021-12-01 16:00:36 +01:00
parent ffd06c4b95
commit 025ef76c01
5 changed files with 47 additions and 63 deletions

View File

@@ -6,45 +6,34 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 08:38:55 by lperrey #+# #+# */
/* Updated: 2021/11/26 21:02:10 by lperrey ### ########.fr */
/* Updated: 2021/12/01 15:48:05 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int check_operators(t_token *t, char *input, int *i, int *t_i);
int check_operators(t_token *t, char *input, int *i, int *t_i);
static int quoting(int *quotes_state, char *input, int *i);
int fill_token(t_token *t, char *input, int *i, int *t_i)
{
static int in_quotes = 0;
static int quotes_state = 0;
// operators
if (!in_quotes)
if (!quotes_state)
{
if (check_operators(t, input, i, t_i) == DELIMITE_TOKEN)
return (DELIMITE_TOKEN);
}
// quoting
if (input[*i] == '\'' && in_quotes != IN_DQUOTES)
if (quoting(&quotes_state, input, i))
{
t->content[(*t_i)++] = input[(*i)++];
if (in_quotes == IN_QUOTES)
in_quotes = 0;
else if (ft_strchr(&input[*i], '\'')) // if closed quotes
in_quotes = IN_QUOTES;
return (CONTINUE_TOKEN);
}
else if (input[*i] == '\"' && in_quotes != IN_QUOTES)
{
t->content[(*t_i)++] = input[(*i)++];
if (in_quotes == IN_DQUOTES)
in_quotes = 0;
else if (ft_strchr(&input[*i], '\"')) // if closed dquotes
in_quotes = IN_DQUOTES;
return (CONTINUE_TOKEN);
}
// blanks
if (!in_quotes && (input[*i] == ' ' || input[*i] == '\t'))
if (!quotes_state && (input[*i] == ' ' || input[*i] == '\t'))
{
while (input[*i] == ' ' || input[*i] == '\t')
(*i)++;
@@ -55,17 +44,23 @@ int fill_token(t_token *t, char *input, int *i, int *t_i)
return (CONTINUE_TOKEN);
}
/*
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03
1 - OK
2 - OK
3 - OK
4 - OK
5 - OK / SEMI-OSEF
6 - OK
7 - OK
8 - OK
9 - OSEF
10 - OK
*/
static int quoting(int *quotes_state, char *input, int *i)
{
if (input[*i] == '\'' && *quotes_state != IN_DQUOTES)
{
if (*quotes_state == IN_QUOTES)
*quotes_state = 0;
else if (ft_strchr(&input[*i + 1], '\'')) // if closed quotes
*quotes_state = IN_QUOTES;
return (CONTINUE_TOKEN);
}
else if (input[*i] == '\"' && *quotes_state != IN_QUOTES)
{
if (*quotes_state == IN_DQUOTES)
*quotes_state = 0;
else if (ft_strchr(&input[*i + 1], '\"')) // if closed dquotes
*quotes_state = IN_DQUOTES;
return (CONTINUE_TOKEN);
}
return (0);
}