summaryrefslogtreecommitdiff
path: root/json-glib/json-scanner.h
blob: 49d7b1ef09e82a89ffc3f141646d6350db86d825 (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
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
/* json-scanner.h: Tokenizer for JSON
 *
 * This file is part of JSON-GLib
 * Copyright (C) 2008 OpenedHand
 *
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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 Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * JsonScanner is a specialized tokenizer for JSON adapted from
 * the GScanner tokenizer in GLib; GScanner came with this notice:
 * 
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 *
 * JsonScanner: modified by Emmanuele Bassi <ebassi@openedhand.com>
 */

#ifndef __JSON_SCANNER_H__
#define __JSON_SCANNER_H__

#include <glib/gdataset.h>
#include <glib/ghash.h>
#include <glib/gscanner.h>

G_BEGIN_DECLS

typedef struct _JsonScanner       JsonScanner;
typedef struct _JsonScannerConfig JsonScannerConfig;

typedef void (* JsonScannerMsgFunc) (JsonScanner *scanner,
                                     gchar       *message,
                                     gboolean     is_error);

/**
 * JsonTokenType:
 * @JSON_TOKEN_INVALID: marker
 * @JSON_TOKEN_TRUE: symbol for 'true' bareword
 * @JSON_TOKEN_FALSE: symbol for 'false' bareword
 * @JSON_TOKEN_NULL: symbol for 'null' bareword
 * @JSON_TOKEN_VAR: symbol for 'var' bareword
 * @JSON_TOKEN_LAST: marker
 *
 * Tokens for JsonScanner-based parser, extending #GTokenType.
 */
typedef enum {
  JSON_TOKEN_INVALID = G_TOKEN_LAST,

  JSON_TOKEN_TRUE,
  JSON_TOKEN_FALSE,
  JSON_TOKEN_NULL,
  JSON_TOKEN_VAR,

  JSON_TOKEN_LAST
} JsonTokenType;

/**
 * JsonScanner:
 *
 * Tokenizer scanner for JSON. See #GScanner
 *
 * Since: 0.6
 */
struct _JsonScanner
{
  /*< private >*/
  /* unused fields */
  gpointer user_data;
  guint max_parse_errors;
  
  /* json_scanner_error() increments this field */
  guint parse_errors;
  
  /* name of input stream, featured by the default message handler */
  const gchar *input_name;
  
  /* quarked data */
  GData *qdata;
  
  /* link into the scanner configuration */
  JsonScannerConfig *config;
  
  /* fields filled in after json_scanner_get_next_token() */
  GTokenType token;
  GTokenValue value;
  guint line;
  guint position;
  
  /* fields filled in after json_scanner_peek_next_token() */
  GTokenType next_token;
  GTokenValue next_value;
  guint next_line;
  guint next_position;
  
  /* to be considered private */
  GHashTable *symbol_table;
  gint input_fd;
  const gchar *text;
  const gchar *text_end;
  gchar *buffer;
  guint scope_id;
  
  /* handler function for _warn and _error */
  JsonScannerMsgFunc msg_handler;
};

JsonScanner *json_scanner_new                  (void);
void         json_scanner_destroy              (JsonScanner *scanner);
void         json_scanner_input_file           (JsonScanner *scanner,
                                                gint         input_fd);
void         json_scanner_sync_file_offset     (JsonScanner *scanner);
void         json_scanner_input_text           (JsonScanner *scanner,
                                                const gchar *text,
                                                guint        text_len);
GTokenType   json_scanner_get_next_token       (JsonScanner *scanner);
GTokenType   json_scanner_peek_next_token      (JsonScanner *scanner);
GTokenType   json_scanner_cur_token            (JsonScanner *scanner);
GTokenValue  json_scanner_cur_value            (JsonScanner *scanner);
guint        json_scanner_cur_line             (JsonScanner *scanner);
guint        json_scanner_cur_position         (JsonScanner *scanner);
gboolean     json_scanner_eof                  (JsonScanner *scanner);
guint        json_scanner_set_scope            (JsonScanner *scanner,
                                                guint        scope_id);
void         json_scanner_scope_add_symbol     (JsonScanner *scanner,
                                                guint        scope_id,
                                                const gchar *symbol,
                                                gpointer     value);
void         json_scanner_scope_remove_symbol  (JsonScanner *scanner,
                                                guint        scope_id,
                                                const gchar *symbol);
gpointer     json_scanner_scope_lookup_symbol  (JsonScanner *scanner,
                                                guint        scope_id,
                                                const gchar *symbol);
void         json_scanner_scope_foreach_symbol (JsonScanner *scanner,
                                                guint        scope_id,
                                                GHFunc       func,
                                                gpointer     user_data);
gpointer     json_scanner_lookup_symbol        (JsonScanner *scanner,
                                                const gchar *symbol);
void         json_scanner_unexp_token          (JsonScanner *scanner,
                                                GTokenType   expected_token,
                                                const gchar *identifier_spec,
                                                const gchar *symbol_spec,
                                                const gchar *symbol_name,
                                                const gchar *message,
                                                gint         is_error);
void         json_scanner_error                (JsonScanner *scanner,
                                                const gchar *format,
                                                ...) G_GNUC_PRINTF (2,3);
void         json_scanner_warn                 (JsonScanner *scanner,
                                                const gchar *format,
                                                ...) G_GNUC_PRINTF (2,3);

G_END_DECLS

#endif /* __JSON_SCANNER_H__ */