1 /* This file is part of QJSon
3 * Copyright (C) 2008 Flavio Castelli <flavio.castelli@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
23 %define "parser_class_name" "json_parser"
27 #include "json_scanner.h"
28 #include "qjson_debug.h"
30 #include <QtCore/QByteArray>
31 #include <QtCore/QMap>
32 #include <QtCore/QString>
33 #include <QtCore/QVariant>
41 #define YYERROR_VERBOSE 1
44 %parse-param { QJson::ParserPrivate* driver }
45 %lex-param { QJson::ParserPrivate* driver }
52 %token END 0 "end of file"
54 %token CURLY_BRACKET_OPEN 1 "{"
55 %token CURLY_BRACKET_CLOSE 2 "}"
56 %token SQUARE_BRACKET_OPEN 3 "["
57 %token SQUARE_BRACKET_CLOSE 4 "]"
63 %token DIGIT 9 "digit"
64 %token E 10 "exponential"
65 %token TRUE_VAL 11 "true"
66 %token FALSE_VAL 12 "false"
67 %token NULL_VAL 13 "null"
68 %token QUOTMARKOPEN 14 "open quotation mark"
69 %token QUOTMARKCLOSE 15 "close quotation mark"
71 %token STRING 16 "string"
73 // define the initial token
81 driver->m_result = $1;
82 qjsonDebug() << "json_parser - parsing finished";
85 data: object {$$ = $1; }
89 qCritical()<< "json_parser - syntax error found, "
95 object: CURLY_BRACKET_OPEN members CURLY_BRACKET_CLOSE { $$ = $2; };
97 members: /* empty */ { $$ = QVariant (QVariantMap()); }
99 QVariantMap members = $2.toMap();
100 $2 = QVariant(); // Allow reuse of map
101 $$ = QVariant(members.unite ($1.toMap()));
104 r_members: /* empty */ { $$ = QVariant (QVariantMap()); }
105 | COMMA pair r_members {
106 QVariantMap members = $3.toMap();
107 $3 = QVariant(); // Allow reuse of map
108 $$ = QVariant(members.unite ($2.toMap()));
111 pair: string COLON value {
113 pair.insert ($1.toString(), QVariant($3));
114 $$ = QVariant (pair);
117 array: SQUARE_BRACKET_OPEN values SQUARE_BRACKET_CLOSE { $$ = $2; };
119 values: /* empty */ { $$ = QVariant (QVariantList()); }
121 QVariantList members = $2.toList();
122 $2 = QVariant(); // Allow reuse of list
123 members.prepend ($1);
124 $$ = QVariant(members);
127 r_values: /* empty */ { $$ = QVariant (QVariantList()); }
128 | COMMA value r_values {
129 QVariantList members = $3.toList();
130 $3 = QVariant(); // Allow reuse of list
131 members.prepend ($2);
132 $$ = QVariant(members);
135 value: string { $$ = $1; }
136 | number { $$ = $1; }
137 | object { $$ = $1; }
139 | TRUE_VAL { $$ = QVariant (true); }
140 | FALSE_VAL { $$ = QVariant (false); }
142 QVariant null_variant;
147 if ($1.toByteArray().startsWith('-')) {
148 $$ = QVariant (QVariant::LongLong);
149 $$.setValue($1.toLongLong());
152 $$ = QVariant (QVariant::ULongLong);
153 $$.setValue($1.toULongLong());
157 const QByteArray value = $1.toByteArray() + $2.toByteArray();
158 $$ = QVariant(QVariant::Double);
159 $$.setValue(value.toDouble());
161 | int exp { $$ = QVariant ($1.toByteArray() + $2.toByteArray()); }
163 const QByteArray value = $1.toByteArray() + $2.toByteArray() + $3.toByteArray();
164 $$ = QVariant (value);
167 int: DIGIT digits { $$ = QVariant ($1.toByteArray() + $2.toByteArray()); }
168 | MINUS DIGIT digits { $$ = QVariant (QByteArray("-") + $2.toByteArray() + $3.toByteArray()); };
170 digits: /* empty */ { $$ = QVariant (QByteArray("")); }
172 $$ = QVariant($1.toByteArray() + $2.toByteArray());
176 $$ = QVariant(QByteArray(".") + $2.toByteArray());
179 exp: E digits { $$ = QVariant($1.toByteArray() + $2.toByteArray()); };
181 string: QUOTMARKOPEN string_arg QUOTMARKCLOSE { $$ = $2; };
183 string_arg: /*empty */ { $$ = QVariant (QString(QLatin1String(""))); }
190 int yy::yylex(YYSTYPE *yylval, yy::location *yylloc, QJson::ParserPrivate* driver)
192 JSonScanner* scanner = driver->m_scanner;
194 int ret = scanner->yylex(yylval, yylloc);
196 qjsonDebug() << "json_parser::yylex - calling scanner yylval==|"
197 << yylval->toByteArray() << "|, ret==|" << QString::number(ret) << "|";
202 void yy::json_parser::error (const yy::location& yyloc,
203 const std::string& error)
205 /*qjsonDebug() << yyloc.begin.line;
206 qjsonDebug() << yyloc.begin.column;
207 qjsonDebug() << yyloc.end.line;
208 qjsonDebug() << yyloc.end.column;*/
209 qjsonDebug() << "json_parser::error [line" << yyloc.end.line << "] -" << error.c_str() ;
210 driver->setError(QString::fromLatin1(error.c_str()), yyloc.end.line);