summaryrefslogtreecommitdiff
path: root/json-glib/json-scanner.h
blob: 4d1f9820b138f42a4626725b54ea2afb500f6ae9 (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
/* 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, see <http://www.gnu.org/licenses/>.
 */

/*
 * 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.h>

G_BEGIN_DECLS

typedef struct _JsonScanner       JsonScanner;
typedef struct _JsonScannerConfig JsonScannerConfig;

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

/**
 * 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;
  const gchar *text;
  const gchar *text_end;
  gchar *buffer;
  guint scope_id;
  
  /* handler function for _warn and _error */
  JsonScannerMsgFunc msg_handler;
};

G_GNUC_INTERNAL
JsonScanner *json_scanner_new                  (void);
G_GNUC_INTERNAL
void         json_scanner_destroy              (JsonScanner *scanner);
G_GNUC_INTERNAL
void         json_scanner_input_text           (JsonScanner *scanner,
                                                const gchar *text,
                                                guint        text_len);
G_GNUC_INTERNAL
GTokenType   json_scanner_get_next_token       (JsonScanner *scanner);
G_GNUC_INTERNAL
GTokenType   json_scanner_peek_next_token      (JsonScanner *scanner);
G_GNUC_INTERNAL
void         json_scanner_scope_add_symbol     (JsonScanner *scanner,
                                                guint        scope_id,
                                                const gchar *symbol,
                                                gpointer     value);
G_GNUC_INTERNAL
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);
G_GNUC_INTERNAL
void         json_scanner_error                (JsonScanner *scanner,
                                                const gchar *format,
                                                ...) G_GNUC_PRINTF (2,3);

G_END_DECLS

#endif /* __JSON_SCANNER_H__ */