Initial Import
[profile/ivi/json-glib.git] / json-glib / json-scanner.h
1 /* json-scanner.h: Tokenizer for JSON
2  *
3  * This file is part of JSON-GLib
4  * Copyright (C) 2008 OpenedHand
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /*
23  * JsonScanner is a specialized tokenizer for JSON adapted from
24  * the GScanner tokenizer in GLib; GScanner came with this notice:
25  * 
26  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
27  * file for a list of people on the GLib Team.  See the ChangeLog
28  * files for a list of changes.  These files are distributed with
29  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
30  *
31  * JsonScanner: modified by Emmanuele Bassi <ebassi@openedhand.com>
32  */
33
34 #ifndef __JSON_SCANNER_H__
35 #define __JSON_SCANNER_H__
36
37 #include <glib.h>
38
39 G_BEGIN_DECLS
40
41 typedef struct _JsonScanner       JsonScanner;
42 typedef struct _JsonScannerConfig JsonScannerConfig;
43
44 typedef void (* JsonScannerMsgFunc) (JsonScanner *scanner,
45                                      gchar       *message,
46                                      gboolean     is_error);
47
48 /**
49  * JsonTokenType:
50  * @JSON_TOKEN_INVALID: marker
51  * @JSON_TOKEN_TRUE: symbol for 'true' bareword
52  * @JSON_TOKEN_FALSE: symbol for 'false' bareword
53  * @JSON_TOKEN_NULL: symbol for 'null' bareword
54  * @JSON_TOKEN_VAR: symbol for 'var' bareword
55  * @JSON_TOKEN_LAST: marker
56  *
57  * Tokens for JsonScanner-based parser, extending #GTokenType.
58  */
59 typedef enum {
60   JSON_TOKEN_INVALID = G_TOKEN_LAST,
61
62   JSON_TOKEN_TRUE,
63   JSON_TOKEN_FALSE,
64   JSON_TOKEN_NULL,
65   JSON_TOKEN_VAR,
66
67   JSON_TOKEN_LAST
68 } JsonTokenType;
69
70 /**
71  * JsonScanner:
72  *
73  * Tokenizer scanner for JSON. See #GScanner
74  *
75  * Since: 0.6
76  */
77 struct _JsonScanner
78 {
79   /*< private >*/
80   /* unused fields */
81   gpointer user_data;
82   guint max_parse_errors;
83   
84   /* json_scanner_error() increments this field */
85   guint parse_errors;
86   
87   /* name of input stream, featured by the default message handler */
88   const gchar *input_name;
89   
90   /* quarked data */
91   GData *qdata;
92   
93   /* link into the scanner configuration */
94   JsonScannerConfig *config;
95   
96   /* fields filled in after json_scanner_get_next_token() */
97   GTokenType token;
98   GTokenValue value;
99   guint line;
100   guint position;
101   
102   /* fields filled in after json_scanner_peek_next_token() */
103   GTokenType next_token;
104   GTokenValue next_value;
105   guint next_line;
106   guint next_position;
107   
108   /* to be considered private */
109   GHashTable *symbol_table;
110   gint input_fd;
111   const gchar *text;
112   const gchar *text_end;
113   gchar *buffer;
114   guint scope_id;
115   
116   /* handler function for _warn and _error */
117   JsonScannerMsgFunc msg_handler;
118 };
119
120 JsonScanner *json_scanner_new                  (void);
121 void         json_scanner_destroy              (JsonScanner *scanner);
122 void         json_scanner_input_file           (JsonScanner *scanner,
123                                                 gint         input_fd);
124 void         json_scanner_sync_file_offset     (JsonScanner *scanner);
125 void         json_scanner_input_text           (JsonScanner *scanner,
126                                                 const gchar *text,
127                                                 guint        text_len);
128 GTokenType   json_scanner_get_next_token       (JsonScanner *scanner);
129 GTokenType   json_scanner_peek_next_token      (JsonScanner *scanner);
130 GTokenType   json_scanner_cur_token            (JsonScanner *scanner);
131 GTokenValue  json_scanner_cur_value            (JsonScanner *scanner);
132 guint        json_scanner_cur_line             (JsonScanner *scanner);
133 guint        json_scanner_cur_position         (JsonScanner *scanner);
134 gboolean     json_scanner_eof                  (JsonScanner *scanner);
135 guint        json_scanner_set_scope            (JsonScanner *scanner,
136                                                 guint        scope_id);
137 void         json_scanner_scope_add_symbol     (JsonScanner *scanner,
138                                                 guint        scope_id,
139                                                 const gchar *symbol,
140                                                 gpointer     value);
141 void         json_scanner_scope_remove_symbol  (JsonScanner *scanner,
142                                                 guint        scope_id,
143                                                 const gchar *symbol);
144 gpointer     json_scanner_scope_lookup_symbol  (JsonScanner *scanner,
145                                                 guint        scope_id,
146                                                 const gchar *symbol);
147 void         json_scanner_scope_foreach_symbol (JsonScanner *scanner,
148                                                 guint        scope_id,
149                                                 GHFunc       func,
150                                                 gpointer     user_data);
151 gpointer     json_scanner_lookup_symbol        (JsonScanner *scanner,
152                                                 const gchar *symbol);
153 void         json_scanner_unexp_token          (JsonScanner *scanner,
154                                                 GTokenType   expected_token,
155                                                 const gchar *identifier_spec,
156                                                 const gchar *symbol_spec,
157                                                 const gchar *symbol_name,
158                                                 const gchar *message,
159                                                 gint         is_error);
160 void         json_scanner_error                (JsonScanner *scanner,
161                                                 const gchar *format,
162                                                 ...) G_GNUC_PRINTF (2,3);
163 void         json_scanner_warn                 (JsonScanner *scanner,
164                                                 const gchar *format,
165                                                 ...) G_GNUC_PRINTF (2,3);
166
167 G_END_DECLS
168
169 #endif /* __JSON_SCANNER_H__ */