JSON Grammar document in progress.
[Evergreen.git] / docs / Guides / JSONGrammar.xml
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <article version="5.0" xmlns="http://docbook.org/ns/docbook"
4         xmlns:xi="http://www.w3.org/2003/XInclude" xmlns:xlink="http://www.w3.org/1999/xlink">
5
6         <title>Grammar of JSON Queries</title>
7
8         <para>
9                 <author>
10                         <personname>
11                                 <firstname>Scott</firstname>
12                                 <surname>McKellar</surname>
13                         </personname>
14                         <affiliation>
15                                 <orgname>Equinox Software, Inc.</orgname>
16                         </affiliation>
17                 </author>
18         </para>
19
20         <sect1>
21                 <title>Introduction</title>
22                 <para> The format of this grammar approximates Extended Backus-Naur notation. However it is
23                         intended as input to human beings, not to parser generators such as Lex or Yacc. Do not
24                         expect formal rigor. Sometimes narrative text will explain things that are clumsy to
25                         express in formal notation. More often, the text will restate or summarize the formal
26                         productions. </para>
27                 <para> Conventions: </para>
28                 <orderedlist>
29                         <listitem>
30                                 <para>The grammar is a series of productions.</para>
31                         </listitem>
32                         <listitem>
33                                 <para>A production consists of a name, followed by "::=", followed by a definition
34                                         for the name. The name identifies a grammatical construct that can appear on the
35                                         right side of another production.</para>
36                         </listitem>
37                         <listitem>
38                                 <para>Literals (including punctuation) are enclosed in 'single quotes', or in
39                                         "double quotes" if case is not significant.</para>
40                         </listitem>
41                         <listitem>
42                                 <para>A single quotation mark within a literal is escaped with a preceding
43                                         backslash: 'dog\'s tail'.</para>
44                         </listitem>
45                         <listitem>
46                                 <para>If a construct can be defined more than one way, then the alternatives may
47                                         appear in separate productions; or, they may appear in the same production,
48                                         separated by pipe symbols. The choice between these representations is of only
49                                         cosmetic significance.</para>
50                         </listitem>
51                         <listitem>
52                                 <para>A construct enclosed within square brackets is optional.</para>
53                         </listitem>
54                         <listitem>
55                                 <para>A construct enclosed within curly braces may be repeated zero or more
56                                         times.</para>
57                         </listitem>
58                         <listitem>
59                                 <para>JSON allows arbitrary white space between tokens. To avoid ugly clutter, this
60                                         grammar ignores the optional white space. </para>
61                         </listitem>
62                         <listitem>
63                                 <para>In many cases a production defines a JSON object, i.e. a list of name-value
64                                         pairs, separated by commas. Since the order of these name/value pairs is not
65                                         significant, the grammar will not try to show all the possible sequences. In
66                                         general it will present the required pairs first, if any, followed by any
67                                         optional elements.</para>
68                         </listitem>
69                 </orderedlist>
70
71                 <para> Since both EBNF and JSON use curly braces and square brackets, pay close attention to
72                         whether these characters are in single quotes. If they're in single quotes, they are
73                         literal elements of the JSON notation. Otherwise they are elements of the EBNF notation.
74                 </para>
75         </sect1>
76
77         <sect1>
78                 <title>Primitives</title>
79                 <para> We'll start by defining some primitives, to get them out of the way. They're mostly
80                         just what you would expect. </para>
81
82                 <productionset>
83                         <production xml:id="ebnf.string">
84                                 <lhs> string </lhs>
85                                 <rhs> '"' chars '"' </rhs>
86                         </production>
87
88                         <production xml:id="ebnf.chars">
89                                 <lhs> chars </lhs>
90                                 <rhs> any valid sequence of UTF-8 characters, with certain special characters
91                                         escaped according to JSON rules </rhs>
92                         </production>
93
94                         <production xml:id="ebnf.int_literal">
95                                 <lhs> integer_literal </lhs>
96                                 <rhs> [ sign ] digit { digit } </rhs>
97                         </production>
98
99                         <production xml:id="ebnf.sign">
100                                 <lhs> sign </lhs>
101                                 <rhs> '+' | '-' </rhs>
102                         </production>
103
104                         <production xml:id="ebnf.digits">
105                                 <lhs> digit </lhs>
106                                 <rhs>digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'</rhs>
107                         </production>
108
109                         <production xml:id="ebnf.int_string">
110                                 <lhs> integer_string </lhs>
111                                 <rhs> '"' integer_literal '"' </rhs>
112                         </production>
113
114                         <production xml:id="ebnf.int">
115                                 <lhs> integer </lhs>
116                                 <rhs> integer_literal | integer_string </rhs>
117                         </production>
118
119                         <production xml:id="ebnf.num">
120                                 <lhs> number </lhs>
121                                 <rhs> any valid character sequence that is numeric according to JSON rules </rhs>
122                         </production>
123
124                 </productionset>
125
126                 <para> When json_query requires an integral value, it will usually accept a quoted string
127                         and convert it to an integer by brute force – to zero if necessary. Likewise it may
128                         truncate a floating point number to an integral value. Scientific notation will be
129                         accepted but may not give the intended results. </para>
130
131                 <productionset>
132
133                         <production xml:id="ebnf.bool">
134                                 <lhs> boolean </lhs>
135                                 <rhs> 'true' | 'false' | string | number </rhs>
136                         </production>
137
138                 </productionset>
139
140                 <para> The preferred way to encode a boolean is with the JSON reserved word true or false,
141                         in lower case without quotation marks. The string <literal>true</literal>, in upper,
142                         lower, or mixed case, is another way to encode true. Any other string evaluates to
143                         false. </para>
144                 <para> As an accommodation to perl, numbers may be used as booleans. A numeric value of 1
145                         means true, and any other numeric value means false. </para>
146                 <para> Any other valid JSON value, such as an array, will be accepted as a boolean but
147                         interpreted as false. </para>
148                 <para> The last couple of primitives aren't really very primitive, but we introduce them
149                         here for convenience: </para>
150
151                 <productionset>
152
153                         <production xml:id="ebnf.classname">
154                                 <lhs> class_name </lhs>
155                                 <rhs> string </rhs>
156                         </production>
157
158                 </productionset>
159
160                 <para> A class_name is a special case of a string: the name of a class as defined by the
161                         IDL. The class may refer either to a database table or to a source_definition, which is
162                         a subquery. </para>
163
164                 <productionset>
165
166                         <production xml:id="ebnf.field_name">
167                                 <lhs> field_name </lhs>
168                                 <rhs> string </rhs>
169                         </production>
170
171                 </productionset>
172
173                 <para> A field_name is another special case of a string: the name of a non-virtual field as
174                         defined by the IDL. A field_name is also a column name for the table corresponding to
175                         the relevant class. </para>
176
177         </sect1>
178
179         <sect1>
180                 <title>Query</title>
181
182                 <para> The following production applies not only to the main query but also to most
183                         subqueries. </para>
184
185                 <productionset>
186
187                         <production xml:id="ebnf.query">
188                                 <lhs> query </lhs>
189                                 <rhs> '{'<sbr/> '"from"' ':' from_list<sbr/> [ ',' '"select"' ':' select_list
190                                         ]<sbr/> [ ',' '"where"' ':' where_condition ]<sbr/> [ ',' '"having"' ':'
191                                         where_condition ]<sbr/> [ ',' '"order_by"' ':' order_by_list ]<sbr/> [ ','
192                                         '"limit"' ':' integer ]<sbr/> [ ',' '"offset"' ':' integer ]<sbr/> [ ','
193                                         '"distinct"' ':' boolean ]<sbr/> [ ',' '"no_i18n"' ':' boolean ]<sbr/> '}'
194                                 </rhs>
195                         </production>
196
197                 </productionset>
198
199                 <para> Except for the <literal>"distinct"</literal> and <literal>no_i18n</literal> entries,
200                         each name/value pair represents a major clause of the SELECT statement. The name/value
201                         pairs may appear in any order. </para>
202                 <para> There is no name/value pair for the GROUP BY clause, because json_query generates it
203                         automatically according to information encoded elsewhere. </para>
204                 <para> The <literal>"distinct"</literal> entry, if present and true, tells json_query that
205                         it may have to create a GROUP BY clause. If not present, it defaults to false. </para>
206                 <para> The <literal>"no_i18n"</literal> entry, if present and true, tells json_query to
207                         suppress internationalization. If not present, it defaults to false. (Note that
208                                 <literal>"no_i18n"</literal> contains the digit one, not the letter ell.) </para>
209                 <para> The values for <literal>limit</literal> and <literal>offset</literal> provide the
210                         arguments of the LIMIT and OFFSET clauses, respectively, of the SQL statement. Each
211                         value should be non-negative, if present, or else the SQL won't work. </para>
212
213         </sect1>
214
215
216 </article>