heredoc changes to lexer done

This commit is contained in:
Jack Halford 2017-03-08 15:53:10 +01:00
parent 95be4e209c
commit 794a3f9936
10 changed files with 75 additions and 70 deletions

View file

@ -206,11 +206,11 @@ lexer/lexer_bquote.c\
lexer/lexer_curly_braces.c\
lexer/lexer_default.c\
lexer/lexer_delim.c\
lexer/lexer_dless.c\
lexer/lexer_dquote.c\
lexer/lexer_end.c\
lexer/lexer_great.c\
lexer/lexer_greatand.c\
lexer/lexer_heredoc.c\
lexer/lexer_init.c\
lexer/lexer_less.c\
lexer/lexer_lessand.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/03/06 18:28:10 by ariard ### ########.fr */
/* Updated: 2017/03/08 15:52:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,6 +25,7 @@ enum e_lexstate
{
DEFAULT,
PAREN,
HEREDOC,
NEWLINE,
DELIM,
SEP,
@ -34,7 +35,6 @@ enum e_lexstate
GREAT,
LESSAND,
GREATAND,
DLESS,
QUOTE,
DQUOTE,
BQUOTE,
@ -99,10 +99,13 @@ t_lexstate get_state_redir(t_lexer *lexer);
int get_lexer_stack(t_lexer lexer);
int get_reserved_words(t_list **alst);
int insert_newline(t_list **alst);
void lexer_init(t_lexer *lexer);
int lexer_lex(t_list **alst, t_lexer *lexer);
int lexer_default(t_list **alst, t_lexer *lexer);
int lexer_newline(t_list **alst, t_lexer *lexer);
int lexer_heredoc(t_list **alst, t_lexer *lexer);
int lexer_delim(t_list **alst, t_lexer *lexer);
int lexer_sep(t_list **alst, t_lexer *lexer);
int lexer_word(t_list **alst, t_lexer *lexer);
@ -111,7 +114,6 @@ int lexer_less(t_list **alst, t_lexer *lexer);
int lexer_great(t_list **alst, t_lexer *lexer);
int lexer_greatand(t_list **alst, t_lexer *lexer);
int lexer_lessand(t_list **alst, t_lexer *lexer);
int lexer_dless(t_list **alst, t_lexer *lexer);
int lexer_quote(t_list **alst, t_lexer *lexer);
int lexer_dquote(t_list **alst, t_lexer *lexer);
int lexer_bquote(t_list **alst, t_lexer *lexer);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */
/* Updated: 2017/03/08 00:22:56 by wescande ### ########.fr */
/* Updated: 2017/03/08 15:50:49 by jhalford ### ########.fr */
/* Updated: 2017/03/07 18:35:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -192,6 +192,7 @@ enum e_sym
MATH_SUP,
REDIR,
CMD,
HEREDOCDATA,
ALL = 200,
TERMINUS = 300,
};

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */
/* Updated: 2017/03/08 14:51:22 by jhalford ### ########.fr */
/* Updated: 2017/03/08 14:55:35 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -1,41 +0,0 @@
#include "minishell.h"
int lexer_dless(t_list **alst, t_lexer *lexer)
{
t_list *heredoc_lst;
t_token *eof_tok;
t_token *heredoc_tok;
(void)alst;
(void)lexer;
heredoc_lst = *(t_list**)lexer->heredoc_stack->content;
heredoc_tok = heredoc_lst->content;
if (!(heredoc_lst->next))
{
ft_dprintf(2, "{red}%s: parse error near `\\n'{eoc}\n", SHELL_NAME);
return (1);
}
eof_tok = heredoc_lst->next->content;
if (!(eof_tok->type == TK_WORD))
{
ft_dprintf(2, "{red}%s: expected word token after <<{eoc}\n", SHELL_NAME);
return (1);
}
/* DG("heredoc contains [%s]", heredoc_tok->data); */
/* DG("heredoc ends at [%s]", eof_tok->data); */
/* DG("input is [%s]", lexer->str + lexer->pos); */
if (ft_strcmp(eof_tok->data, lexer->str + lexer->pos) == 0)
{
pop(&lexer->stack);
pop(&lexer->heredoc_stack);
while (lexer->str[++lexer->pos])
;
ft_strappend(&lexer->str, (char[]){'\n', 0});
lexer->pos++;
return (0);
}
else
while (lexer->str[lexer->pos])
token_append_char(heredoc_tok, lexer->str[lexer->pos++], 0, 0);
return (lexer_end(alst, lexer));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/05 16:58:24 by jhalford #+# #+# */
/* Updated: 2017/03/08 12:12:40 by jhalford ### ########.fr */
/* Updated: 2017/03/08 15:52:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,21 +16,14 @@ int lexer_end(t_list **alst, t_lexer *lexer)
{
t_token *token;
if ((*alst && (lexer->state == QUOTE || lexer->state == DQUOTE
|| lexer->state == BQUOTE)) || get_lexer_stack(*lexer) == DLESS)
if (*alst && (lexer->state == QUOTE
|| lexer->state == DQUOTE
|| lexer->state == BQUOTE))
{
token = (*alst)->content;
ft_strappend(&lexer->str, (char[]){'\n', 0});
token_append_char(token, '\n', 1, 0);
lexer->pos++;
if (get_lexer_stack(*lexer) == DLESS)
{
token = (*(t_list**)lexer->heredoc_stack->content)->content;
token_append_char(token, '\n', 0, 0);
}
else
{
token = (*alst)->content;
token_append_char(token, '\n', 1, 0);
}
return (0);
}
return (0);

View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_heredoc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 15:51:17 by jhalford #+# #+# */
/* Updated: 2017/03/08 15:52:48 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int lexer_heredoc(t_list **alst, t_lexer *lexer)
{
t_token *token;
token = (*alst)->content;
token->type = HEREDOCDATA;
token_append_str(token, lexer->str, 0, 0);
return (0);
/* heredoc_lst = *(t_list**)lexer->heredoc_stack->content; */
/* heredoc_tok = heredoc_lst->content; */
/* if (!(heredoc_lst->next)) */
/* { */
/* ft_dprintf(2, "{red}%s: parse error near `\\n'{eoc}\n", SHELL_NAME); */
/* return (1); */
/* } */
/* eof_tok = heredoc_lst->next->content; */
/* if (!(eof_tok->type == TK_WORD)) */
/* { */
/* ft_dprintf(2, "{red}%s: expected word token after <<{eoc}\n", SHELL_NAME); */
/* return (1); */
/* } */
/* DG("heredoc contains [%s]", heredoc_tok->data); */
/* DG("heredoc ends at [%s]", eof_tok->data); */
/* DG("input is [%s]", lexer->str + lexer->pos); */
/* if (ft_strcmp(eof_tok->data, lexer->str + lexer->pos) == 0) */
/* { */
/* /1* pop(&lexer->stack); *1/ */
/* pop(&lexer->heredoc_stack); */
/* while (lexer->str[++lexer->pos]) */
/* ; */
/* ft_strappend(&lexer->str, (char[]){'\n', 0}); */
/* lexer->pos++; */
/* return (0); */
/* } */
/* else */
/* while (lexer->str[lexer->pos]) */
/* token_append_char(heredoc_tok, lexer->str[lexer->pos++], 0, 0); */
/* return (lexer_end(alst, lexer)); */
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 12:06:53 by jhalford #+# #+# */
/* Updated: 2017/03/08 13:27:33 by jhalford ### ########.fr */
/* Updated: 2017/03/08 15:35:20 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,17 +21,14 @@ int lexer_less(t_list **alst, t_lexer *lexer)
if (lexer->str[lexer->pos] == '&')
{
token->type = TK_LESSAND;
token_append(token, lexer, 0, 0);
lexer->pos++;
return (lexer_lessand(alst, lexer));
}
if (lexer->str[lexer->pos] == '<')
{
token->type = TK_DLESS;
push(&lexer->stack, DLESS);
lexer->pos++;
lexer->state = DEFAULT;
ft_lsteadd(&lexer->heredoc_stack, ft_lstnew(alst, sizeof(alst)));
return (lexer_lex(&(*alst)->next, lexer));
}
token->type = TK_LESS;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:08:51 by jhalford #+# #+# */
/* Updated: 2017/03/06 14:55:25 by ariard ### ########.fr */
/* Updated: 2017/03/08 15:51:39 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@ int (*g_lexer[])(t_list **alst, t_lexer *lexer) =
{
&lexer_default,
&lexer_paren,
&lexer_heredoc,
&lexer_newline,
&lexer_delim,
&lexer_sep,
@ -25,7 +26,6 @@ int (*g_lexer[])(t_list **alst, t_lexer *lexer) =
&lexer_great,
&lexer_lessand,
&lexer_greatand,
&lexer_dless,
&lexer_quote,
&lexer_dquote,
&lexer_bquote,

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/03/08 14:39:33 by jhalford ### ########.fr */
/* Updated: 2017/03/08 15:36:38 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,12 +39,12 @@ int handle_instruction(int fd)
ft_strappend(&lexer.str, str);
if (get_lexer_stack(lexer) == BACKSLASH)
pop(&lexer.stack);
else if (get_lexer_stack(lexer) == DLESS)
lexer.state = DLESS;
/* else if (get_lexer_stack(lexer) == DLESS) */
/* lexer.state = DLESS; */
ltoken = ft_lstlast(token);
if (lexer_lex(token ? &ltoken : &token, &lexer))
return (1);
if (get_lexer_stack(lexer) > 1)
if (get_lexer_stack(lexer) > 2)
continue ;
lexer.state = DEFAULT;
if (get_reserved_words(&token))