Updated parser/src/scanner.c to be splitted

This commit is contained in:
Maieul BOYER 2024-09-02 17:43:53 +02:00
parent 78a595ef60
commit b2440780fb
No known key found for this signature in database
11 changed files with 415 additions and 363 deletions

View file

@ -6,44 +6,42 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/01 19:33:04 by maiboyer #+# #+# */
/* Updated: 2024/09/01 20:02:49 by maiboyer ### ########.fr */
/* Updated: 2024/09/02 17:31:02 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser/inner/heredoc.h"
#include "me/char/char.h"
#include "me/str/str.h"
#include "me/types.h"
#include "me/vec/vec_heredoc.h"
#include "parser/inner/heredoc.h"
#include "parser/inner/scanner.h"
#include "parser/parser.h"
bool scan_heredoc_start(t_heredoc *heredoc, TSLexer *lexer)
bool scan_heredoc_start(t_heredoc *heredoc, TSLexer *lexer)
{
bool found_delimiter;
bool found_delimiter;
while (me_isspace(lexer->lookahead))
lexer->advance(lexer, true);
lexer->result_symbol = HEREDOC_START;
heredoc->is_raw = lexer->lookahead == '\'' || lexer->lookahead == '"'
|| lexer->lookahead == '\\';
heredoc->is_raw = lexer->lookahead == '\'' || lexer->lookahead == '"' || lexer->lookahead == '\\';
found_delimiter = advance_word(lexer, &heredoc->delimiter);
if (!found_delimiter)
return (string_clear(&heredoc->delimiter), false);
return (found_delimiter);
}
bool scan_heredoc_end_identifier(t_heredoc *heredoc, TSLexer *lexer)
bool scan_heredoc_end_identifier(t_heredoc *heredoc, TSLexer *lexer)
{
t_i32 size;
t_i32 size;
size = 0;
string_clear(&heredoc->current_leading_word);
if (heredoc->delimiter.len > 0)
{
while (lexer->lookahead != '\0' && lexer->lookahead != '\n'
&& (t_i32)heredoc->delimiter.buf[size] == lexer->lookahead
&& heredoc->current_leading_word.len < heredoc->delimiter.len)
while (lexer->lookahead != '\0' && lexer->lookahead != '\n' && (t_i32)heredoc->delimiter.buf[size] == lexer->lookahead &&
heredoc->current_leading_word.len < heredoc->delimiter.len)
{
string_push_char(&heredoc->current_leading_word, lexer->lookahead);
lexer->advance(lexer, false);
@ -52,26 +50,27 @@ bool scan_heredoc_end_identifier(t_heredoc *heredoc, TSLexer *lexer)
}
if (heredoc->delimiter.len == 0)
return (false);
return (str_compare(heredoc->current_leading_word.buf,
heredoc->delimiter.buf));
return (str_compare(heredoc->current_leading_word.buf, heredoc->delimiter.buf));
}
bool scan_heredoc_content_nullbyte(struct s_heredoc_scan_state *state);
bool scan_heredoc_content_backslash(struct s_heredoc_scan_state *state);
bool scan_heredoc_content_dollar(struct s_heredoc_scan_state *state);
bool scan_heredoc_content_newline(struct s_heredoc_scan_state *state);
bool scan_heredoc_content_other(struct s_heredoc_scan_state *state);
bool scan_heredoc_content_nullbyte(struct s_heredoc_scan_state *state);
bool scan_heredoc_content_backslash(struct s_heredoc_scan_state *state);
bool scan_heredoc_content_dollar(struct s_heredoc_scan_state *state);
bool scan_heredoc_content_newline(struct s_heredoc_scan_state *state);
bool scan_heredoc_content_other(struct s_heredoc_scan_state *state);
bool scan_heredoc_content(t_scanner *scanner, TSLexer *lexer,
enum e_token_type middle_type, enum e_token_type end_type)
bool scan_heredoc_content(t_scanner *scanner, TSLexer *lexer, enum e_token_type middle_type, enum e_token_type end_type)
{
struct s_heredoc_scan_state state;
struct s_heredoc_scan_state state;
t_heredoc_content_func func;
state = (struct s_heredoc_scan_state){.did_advance = false, \
.lexer = lexer, .heredoc = vec_heredoc_last(&scanner->heredocs), \
.scanner = scanner, .middle_type = middle_type, .end_type = end_type, \
.return_value = false};
state = (struct s_heredoc_scan_state){.did_advance = false,
.lexer = lexer,
.heredoc = vec_heredoc_last(&scanner->heredocs),
.scanner = scanner,
.middle_type = middle_type,
.end_type = end_type,
.return_value = false};
while (true)
{
if (lexer->lookahead == '\0')
@ -89,3 +88,15 @@ bool scan_heredoc_content(t_scanner *scanner, TSLexer *lexer,
}
return (false);
}
bool scan_heredoc_end(t_scanner *scanner, TSLexer *lexer)
{
t_heredoc *heredoc;
heredoc = vec_heredoc_last(&scanner->heredocs);
string_free(heredoc->current_leading_word);
string_free(heredoc->delimiter);
vec_heredoc_pop(&scanner->heredocs, NULL);
lexer->result_symbol = HEREDOC_END;
return (true);
}