[go: up one dir, main page]

File: tmpl-token.c

package info (click to toggle)
template-glib 3.38.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 788 kB
  • sloc: ansic: 6,774; yacc: 288; lex: 237; javascript: 16; makefile: 14; python: 12
file content (190 lines) | stat: -rw-r--r-- 3,793 bytes parent folder | download | duplicates (3)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* tmpl-token.c
 *
 * Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
 *
 * This file is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#include "tmpl-token-private.h"

static TmplToken *
tmpl_token_new (void)
{
  TmplToken *self;

  self = g_slice_new0 (TmplToken);

  return self;
}

void
tmpl_token_free (TmplToken *self)
{
  if (self != NULL)
    {
      g_free (self->text);
      g_slice_free (TmplToken, self);
    }
}

TmplToken *
tmpl_token_new_eof (void)
{
  TmplToken *ret;

  ret = tmpl_token_new ();
  ret->type = TMPL_TOKEN_EOF;

  return ret;
}

/**
 * tmpl_token_new_text:
 * @text: (transfer full): The text for the token.
 *
 * Creates a new #TmplToken containing @text.
 * This is a text literal type.
 *
 * Returns: (transfer full): A newly allocated #TmplToken.
 */
TmplToken *
tmpl_token_new_text (gchar *text)
{
  TmplToken *self;

  self = tmpl_token_new ();
  self->type = TMPL_TOKEN_TEXT;
  self->text = text;

  return self;
}

TmplToken *
tmpl_token_new_unichar (gunichar ch)
{
  gchar utf8[8];
  gint len;

  len = g_unichar_to_utf8 (ch, utf8);
  utf8 [len] = '\0';

  return tmpl_token_new_text (g_strdup (utf8));
}

TmplTokenType
tmpl_token_type (TmplToken *self)
{
  g_return_val_if_fail (self != NULL, 0);

  return self->type;
}

gchar *
tmpl_token_include_get_path (TmplToken *self)
{
  const char *str;
  char *path = NULL;

  g_return_val_if_fail (self != NULL, NULL);
  g_return_val_if_fail (self->type == TMPL_TOKEN_INCLUDE, NULL);

  str = self->text;

  if (!g_str_has_prefix (str, "include "))
    return NULL;

  str += strlen ("include ");
  while (*str == ' ') str++;
  if (str[0] != '"')
    return NULL;
  path = g_strdup (str+1);

  if (path[0] == 0 || path[strlen(path)-1] != '"')
    {
      g_free (path);
      return NULL;
    }

  path[strlen(path)-1] = 0;

  return path;
}

TmplToken *
tmpl_token_new_generic (gchar *text)
{
  TmplToken *self;

  g_return_val_if_fail (text != NULL, NULL);

  self = g_slice_new0 (TmplToken);

  if (g_str_has_prefix (text, "if "))
    {
      self->type = TMPL_TOKEN_IF;
      self->text = g_strstrip (g_strdup (text + 3));
    }
  else if (g_str_has_prefix (text, "else if "))
    {
      self->type = TMPL_TOKEN_ELSE_IF;
      self->text = g_strstrip (g_strdup (text + 8));
    }
  else if (g_str_has_prefix (text, "else"))
    {
      self->type = TMPL_TOKEN_ELSE;
      self->text = NULL;
    }
  else if (g_str_has_prefix (text, "end"))
    {
      self->type = TMPL_TOKEN_END;
      self->text = NULL;
    }
  else if (g_str_has_prefix (text, "for "))
    {
      self->type = TMPL_TOKEN_FOR;
      self->text = g_strstrip (g_strdup (text + 4));
    }
  else if (g_str_has_prefix (text, "include "))
    {
      self->type = TMPL_TOKEN_INCLUDE;
      self->text = g_strstrip (g_strdup (text));
    }
  else
    {
      self->type = TMPL_TOKEN_EXPRESSION;
      self->text = g_strstrip (text);
      text = NULL;
    }

  g_free (text);

  return self;
}

const gchar *
tmpl_token_get_text (TmplToken *self)
{
  g_return_val_if_fail (self != NULL, NULL);

  return self->text;
}