[go: up one dir, main page]

File: tmpl-lexer.c

package info (click to toggle)
template-glib 3.30.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 652 kB
  • sloc: ansic: 5,637; lex: 186; yacc: 176; makefile: 13; python: 12
file content (211 lines) | stat: -rw-r--r-- 6,194 bytes parent folder | download | duplicates (4)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* tmpl-lexer.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/>.
 */

#define G_LOG_DOMAIN "tmpl-lexer"

#include "tmpl-error.h"
#include "tmpl-debug.h"
#include "tmpl-lexer.h"
#include "tmpl-template-locator.h"
#include "tmpl-token-input-stream.h"

struct _TmplLexer
{
  GQueue               *stream_stack;
  TmplTemplateLocator  *locator;
  GHashTable           *circular;
  GSList               *unget;
};

G_DEFINE_POINTER_TYPE (TmplLexer, tmpl_lexer)

TmplLexer *
tmpl_lexer_new (GInputStream        *stream,
                TmplTemplateLocator *locator)
{
  TmplLexer *self;

  g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
  g_return_val_if_fail (!locator || TMPL_IS_TEMPLATE_LOCATOR (locator), NULL);

  self = g_slice_new0 (TmplLexer);
  self->stream_stack = g_queue_new ();
  self->locator = locator ? g_object_ref (locator) : tmpl_template_locator_new ();
  self->circular = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

  g_queue_push_head (self->stream_stack, tmpl_token_input_stream_new (stream));

  return self;
}

void
tmpl_lexer_free (TmplLexer *self)
{
  if (self != NULL)
    {
      const GList *iter;

      for (iter = self->stream_stack->head; iter != NULL; iter = iter->next)
        {
          TmplTokenInputStream *stream = iter->data;

          g_object_unref (stream);
        }

      g_clear_pointer (&self->circular, g_hash_table_unref);
      g_clear_pointer (&self->stream_stack, g_queue_free);
      g_clear_object (&self->locator);
      g_slice_free (TmplLexer, self);
    }
}

/**
 * tmpl_lexer_next:
 * @self: A #TmplLexer.
 * @token: (out) (transfer full): A location for a #TmplToken.
 * @cancellable: (nullable): A #GCancellable or %NULL.
 * @error: A location for a #GError or %NULL.
 *
 * Reads the next token.
 *
 * It is possible for %FALSE to be returned and @error left unset.
 * This happens at the end of the stream.
 *
 * Returns: %TRUE if @token was set, otherwise %FALSE.
 */
gboolean
tmpl_lexer_next (TmplLexer     *self,
                 TmplToken    **token,
                 GCancellable  *cancellable,
                 GError       **error)
{
  TmplTokenInputStream *stream;
  GError *local_error = NULL;
  gboolean ret = FALSE;

  TMPL_ENTRY;

  g_return_val_if_fail (self != NULL, FALSE);
  g_return_val_if_fail (token != NULL, FALSE);
  g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);

  *token = NULL;

  if (self->unget != NULL)
    {
      *token = self->unget->data;
      self->unget = g_slist_remove_link (self->unget, self->unget);
      TMPL_RETURN (TRUE);
    }

  while ((stream = g_queue_peek_head (self->stream_stack)))
    {
      if (!(*token = tmpl_token_input_stream_read_token (stream, cancellable, &local_error)))
        {
          /*
           * If we finished this stream, try to move to the next one.
           */
          if (local_error == NULL)
            {
              const gchar *path = g_object_get_data (G_OBJECT (stream), "PATH");
              if (path != NULL && self->circular != NULL)
                g_hash_table_remove (self->circular, path);
              g_queue_pop_head (self->stream_stack);
              g_object_unref (stream);
              continue;
            }

          TMPL_GOTO (finish);
        }

      /*
       * If the current token is an include token, we need to resolve the
       * include path and read tokens from it.
       */
      if (tmpl_token_type (*token) == TMPL_TOKEN_INCLUDE)
        {
          const gchar *path = tmpl_token_include_get_path (*token);
          GInputStream *input;

          g_assert (self->circular != NULL);

          if (path == NULL)
            {
              local_error = g_error_new (TMPL_ERROR,
                                         TMPL_ERROR_NOT_A_VALUE,
                                         "Expected template path, got null");
              g_clear_pointer (token, tmpl_token_free);
              TMPL_GOTO (finish);
            }

          if (g_hash_table_contains (self->circular, path))
            {
              local_error = g_error_new (TMPL_ERROR,
                                         TMPL_ERROR_CIRCULAR_INCLUDE,
                                         "A circular include was detected: \"%s\"",
                                         path);
              g_clear_pointer (token, tmpl_token_free);
              TMPL_GOTO (finish);
            }

          if (!(input = tmpl_template_locator_locate (self->locator, path, &local_error)))
            {
              g_clear_pointer (token, tmpl_token_free);
              TMPL_GOTO (finish);
            }

          g_hash_table_insert (self->circular, g_strdup (path), NULL);

          stream = tmpl_token_input_stream_new (input);
          g_object_set_data_full (G_OBJECT (stream), "PATH", g_strdup (path), g_free);
          g_queue_push_head (self->stream_stack, stream);

          g_clear_pointer (token, tmpl_token_free);
          g_object_unref (input);

          continue;
        }

      ret = TRUE;
      break;
    }

  if (*token == NULL)
    {
      *token = tmpl_token_new_eof ();
      ret = TRUE;
    }

finish:
  if ((ret == FALSE) && (local_error != NULL))
    g_propagate_error (error, local_error);

  g_assert (ret == FALSE || *token != NULL);

  TMPL_RETURN (ret);
}

void
tmpl_lexer_unget (TmplLexer *self,
                  TmplToken *token)
{
  g_return_if_fail (self != NULL);
  g_return_if_fail (token != NULL);

  self->unget = g_slist_prepend (self->unget, token);
}