aboutsummaryrefslogtreecommitdiff
path: root/src/lexer/tok_lst.c
blob: ac8ca4aff3c8c4efe5ca97d33beab090c03d3ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   tok_lst.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <me@cacharle.xyz>                  +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/08/27 09:32:58 by charles           #+#    #+#             */
/*   Updated: 2020/10/10 08:15:34 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "lexer.h"

t_tok_lst				*tok_lst_new(enum e_tok tag, char *content)
{
	return (tok_lst_new_until(
		tag, content, content == NULL ? 0 : ft_strlen(content)));
}

/*
** \brief          Create a new tok_lst
** \param tag      tok_lst tag
** \param content  tok_lst content
** \param n        The maximum number of character to take from content
** \return         An allocated tok_lst or NULL on error
*/

t_tok_lst				*tok_lst_new_until(
	enum e_tok tag, char *content, size_t n)
{
	t_tok_lst	*ret;

	if ((ret = malloc(sizeof(t_tok_lst))) == NULL)
		return (NULL);
	if (content == NULL)
		ret->content = NULL;
	else if ((ret->content = ft_strndup(content, n)) == NULL)
	{
		free(ret);
		return (NULL);
	}
	ret->tag = tag;
	ret->next = NULL;
	return (ret);
}

t_tok_lst				*tok_lst_push_front(
	t_tok_lst **tokens, t_tok_lst *pushed)
{
	if (pushed == NULL)
		return (NULL);
	ft_lstpush_front((t_ftlst**)tokens, (t_ftlst*)pushed);
	return (*tokens);
}

t_tok_lst				*tok_lst_uncons(t_tok_lst **tokens)
{
	t_tok_lst	*poped;

	if (tokens == NULL || *tokens == NULL)
		return (NULL);
	poped = *tokens;
	*tokens = poped->next;
	poped->next = NULL;
	return (poped);
}