| 1 |
(* |
| 2 |
* Extended Backus-Naur Form of the CIF2 syntax and grammar |
| 3 |
* |
| 4 |
* The CIF2 syntax is closely related to the STAR2 syntax published |
| 5 |
* by Spadaccini and Hall (J. Chem. Inf. Model., 2012, 52 (8), |
| 6 |
* pp 1901-1906 DOI: 10.1021/ci300074v. The CIF1.1 syntax was derived from |
| 7 |
* the original STAR syntax. |
| 8 |
* |
| 9 |
* The allowed character set is those having Unicode |
| 10 |
* code points U+0009, U+000A, U+000D, U+0020 to U+D7FF, and |
| 11 |
* U+E000 to U+10FFFD, less code points of the form U+xFFFE and U+xFFFF, |
| 12 |
* where x is any hexadecimal digit (including 0). Note that the U+0007 |
| 13 |
* character used in STAR2 for escaping string terminators is not used |
| 14 |
* in CIF2. |
| 15 |
* |
| 16 |
* This document follows EBNF syntax as given in ISO/IEC 14977. In |
| 17 |
* particular, the common "+" notation is replaced by option or |
| 18 |
* repeat brackets, e.g. "(ab)+" is replaced by "(ab), {ab}". Moreover, |
| 19 |
* the provided EBNF applies to sequences of Unicode characters -- not |
| 20 |
* sequences of bytes or encoded characters -- independent of any |
| 21 |
* character encoding scheme (but see also below). |
| 22 |
* |
| 23 |
* This particular EBNF uses the "special sequence" mechanism to |
| 24 |
* represent Unicode code points and code point ranges. Individual |
| 25 |
* code points are represented in a special sequence by the form |
| 26 |
* U+[[h]h]hhhh -- that is, "U+" followed by the 4 to 6 hexadecimal |
| 27 |
* digits of the unsigned, 21-bit code point value. Ranges are |
| 28 |
* represented by two such code point values separated by a hyphen |
| 29 |
* character. Whitespace is permitted on either or both sides of |
| 30 |
* any code point value within this special sequence formulation. |
| 31 |
* |
| 32 |
* |
| 33 |
* CIF2 Grammar and Syntax |
| 34 |
* |
| 35 |
* CIF 2.0 is a binary format consisting of Unicode text encoded |
| 36 |
* in UTF-8. Notwithstanding its use of (nearly) the full Unicode |
| 37 |
* character repertoire, CIF applies only the semantics described below |
| 38 |
* to decoded character data, especially with respect to whitespace and |
| 39 |
* line termination. Software consuming CIF data and names generally |
| 40 |
* ascribes additional semantics to them, however, which may include |
| 41 |
* additional Unicode semantics. |
| 42 |
* |
| 43 |
* A CIF2 file consists of a sequence of data blocks separated by |
| 44 |
* whitespace, with a required magic code as the first characters. |
| 45 |
* Data may additionally be structured into save frames, which have |
| 46 |
* the same form as data blocks and nest within data blocks. The data |
| 47 |
* themselves may appear as individual (key, value) pairs, and / or they |
| 48 |
* may be organized into tabular structures called "loops". |
| 49 |
* |
| 50 |
* All data names appearing directly within a given data block or save |
| 51 |
* frame are required to be distinct with respect to a |
| 52 |
* normalization / case-folding procedure described elsewhere. |
| 53 |
* In the same way, data block names must be distinct within their CIF, |
| 54 |
* and save frame names must be distinct within their immediate |
| 55 |
* container (either a data block or a save frame). |
| 56 |
* |
| 57 |
* The main grammatic elements of CIFs are data block headers, |
| 58 |
* save frame headers, data names, data values, and a few keywords. |
| 59 |
* These all must always be separated from each other by whitespace. |
| 60 |
* However, text fields (one kind of data value) have a line terminator as |
| 61 |
* part of their delimiter, and that line terminator can do double duty |
| 62 |
* to satisfy the whitespace separation requirement. Additionally, |
| 63 |
* the requirement does not apply to separating the delimiters of |
| 64 |
* list and table values from member values. The productions below |
| 65 |
* explicitly include whitespace wherever it is allowed or required. |
| 66 |
*) |
| 67 |
|
| 68 |
(* |
| 69 |
* This formulation of the CIF2-file production accepts the end of the |
| 70 |
* input as whitespace for the purpose of satisfying the CIF 2.0 |
| 71 |
* requirement that the version comment be followed by whitespace. |
| 72 |
* It incorporates the CIF 2.0 limitation (also in CIF 1.1) that no |
| 73 |
* line of a CIF may exceed 2048 characters. |
| 74 |
*) |
| 75 |
CIF2-file = ( file-heading, [ line-term, [ |
| 76 |
wspace-any, data-block, |
| 77 |
{ wspace, data-block } |
| 78 |
], [ wspace ], [ comment ] ] ) |
| 79 |
- ( { allchars }, 2049 * char, { allchars } ); |
| 80 |
|
| 81 |
file-heading = [ ?U+FEFF? ], magic-code, { inline-wspace } ; |
| 82 |
|
| 83 |
(* |
| 84 |
* The "magic code" identifies the CIF version with which an instance |
| 85 |
* document claims to comply. |
| 86 |
*) |
| 87 |
magic-code = '#\#CIF_2.0' ; |
| 88 |
|
| 89 |
(* |
| 90 |
* A datablock consists of a data heading followed by zero or more |
| 91 |
* data items and save frames. |
| 92 |
*) |
| 93 |
data-block = data-heading, { block-content } ; |
| 94 |
|
| 95 |
data-heading = data-token, container-code ; |
| 96 |
|
| 97 |
(* |
| 98 |
* Each element inside a data block is either data or a save frame, separated |
| 99 |
* from the header or previous element by whitespace. |
| 100 |
*) |
| 101 |
block-content = wspace, ( data | save-frame ) ; |
| 102 |
|
| 103 |
(* |
| 104 |
* A save frame has content similar to a data block's, but it resides |
| 105 |
* inside a data block instead of at the top level. (Save frames do not nest.) |
| 106 |
*) |
| 107 |
save-frame = save-heading, { frame-content }, wspace, save-token ; |
| 108 |
|
| 109 |
save-heading = save-token, container-code; |
| 110 |
|
| 111 |
(* |
| 112 |
* A save frame contains a sequence of data elements, each separated from the |
| 113 |
* header or previous element by whitespace. |
| 114 |
*) |
| 115 |
frame-content = wspace, data ; |
| 116 |
|
| 117 |
(* A data block or save frame name is composed of one or more non-blank characters *) |
| 118 |
container-code = non-blank-char, { non-blank-char } ; |
| 119 |
|
| 120 |
(* Data appear either as key/value pairs, or within loops. *) |
| 121 |
data = ( data-name, wspace-data-value ) | data-loop ; |
| 122 |
|
| 123 |
(* |
| 124 |
* A data loop consists of a loop header (the case-insensitive word "loop_" |
| 125 |
* followed by a sequence of datanames) and then a sequence of one or more |
| 126 |
* whitespace-separated values. Though it cannot be expressed in EBNF, CIF |
| 127 |
* requires that a loop whose header contains N data names must contain an |
| 128 |
* integral multiple of N data values. |
| 129 |
*) |
| 130 |
data-loop = loop-token, wspace, data-name, { wspace, data-name }, |
| 131 |
wspace-data-value, { wspace-data-value } ; |
| 132 |
|
| 133 |
(* |
| 134 |
* A dataname begins with an underscore character, and contains one or more |
| 135 |
* additional, non-blank characters. |
| 136 |
*) |
| 137 |
data-name = '_' , non-blank-char, { non-blank-char } ; |
| 138 |
|
| 139 |
(* |
| 140 |
* A list contains zero or more whitespace-separated values. The delimiting |
| 141 |
* brackets may optionally be separated by whitespace from the values, |
| 142 |
* or from each other if there are no values. |
| 143 |
*) |
| 144 |
list = '[', [ list-values-start, { wspace-data-value } ], [ wspace ], ']' ; |
| 145 |
|
| 146 |
list-values-start = |
| 147 |
( wspace-any, nospace-value ) |
| 148 |
| ( wspace-any, [ comment ], text-field ) |
| 149 |
| ( [ { wspace-to-eol }, inline-wspace, { inline-wspace } ], wsdelim-string ) |
| 150 |
| ( wspace-to-eol, { wspace-to-eol }, wsdelim-string-sol ) ; |
| 151 |
|
| 152 |
(* |
| 153 |
* A table contains zero or more whitespace-separated key/value pairs. |
| 154 |
* The delimiting brackets may optionally be separated by whitespace from |
| 155 |
* the values, or from each other if there are no values. |
| 156 |
*) |
| 157 |
table = '{', [ wspace-any, table-entry, { wspace, table-entry } ], [ wspace ], '}' ; |
| 158 |
|
| 159 |
(* |
| 160 |
* Key-value pairs appearing in a table structure take the form 'key':value, |
| 161 |
* where key must be a delimited string (but not a text block) and the value |
| 162 |
* may be any data value. Whitespace is permitted between the colon and value. |
| 163 |
*) |
| 164 |
table-entry = ( quoted-string | triple-quoted-string ), |
| 165 |
':', ( nospace-value | wsdelim-string | wspace-data-value ) ; |
| 166 |
|
| 167 |
(* |
| 168 |
* In most contexts, data values must be preceded by explicit and/or |
| 169 |
* implicit whitespace. Only text fields have implicit leading whitespace. |
| 170 |
* Additionally, the whitespace preceding an whitespace-delimited string |
| 171 |
* affects the form that string may take. |
| 172 |
*) |
| 173 |
wspace-data-value = |
| 174 |
( wspace, nospace-value ) |
| 175 |
| ( [ wspace-lines ], inline-wspace, { inline-wspace }, wsdelim-string ) |
| 176 |
| ( wspace-lines, wsdelim-string-sol ) |
| 177 |
| ( [ wspace ], [ comment ], text-field ) ; |
| 178 |
|
| 179 |
(* |
| 180 |
* These data values have neither implicit leading whitespace nor any special |
| 181 |
* sensitivity to leading whitespace. |
| 182 |
*) |
| 183 |
nospace-value = |
| 184 |
quoted-string |
| 185 |
| triple-quoted-string |
| 186 |
| list |
| 187 |
| table ; |
| 188 |
|
| 189 |
(* |
| 190 |
* Whitespace-delimited strings draw from a subset of the CIF character set, |
| 191 |
* have an even more limited first character, and may not have the same form |
| 192 |
* as a data block header, save frame header, or any of several reserved |
| 193 |
* words. When they appear at the start of a line, whitespace-delimited |
| 194 |
* strings may not start with a semicolon. |
| 195 |
*) |
| 196 |
wsdelim-string-sol = wsdelim-string - ( ';', { non-blank-char } ) ; |
| 197 |
|
| 198 |
wsdelim-string = ( lead-char, {restrict-char} ) |
| 199 |
- ( ( ( data-token | save-token ), { non-blank-char } ) | loop-token | global-token | stop-token ) ; |
| 200 |
|
| 201 |
lead-char = restrict-char - ( '"' | '#' | '$' | "'" | '_' ) ; |
| 202 |
|
| 203 |
restrict-char = non-blank-char - ( '[' | ']' | '{' | '}' ) ; |
| 204 |
|
| 205 |
(* quote-delimited or apostrophe-delimited string *) |
| 206 |
quoted-string = ( quote-delim, quote-content, quote-delim ) |
| 207 |
| ( apostrophe-delim, apostrophe-content, apostrophe-delim ) ; |
| 208 |
|
| 209 |
quote-content = { char - quote-delim } ; |
| 210 |
|
| 211 |
quote-delim = '"' ; |
| 212 |
|
| 213 |
apostrophe-content = { char - apostrophe-delim } ; |
| 214 |
|
| 215 |
apostrophe-delim = "'" ; |
| 216 |
|
| 217 |
(* triple-quote-delimited and triple apostrophe-delimited strings *) |
| 218 |
triple-quoted-string = ( quote3-delim, quote3-content, quote3-delim ) |
| 219 |
| ( apostrophe3-delim, apostrophe3-content, apostrophe3-delim ) ; |
| 220 |
|
| 221 |
quote3-delim = '"""' ; |
| 222 |
|
| 223 |
quote3-content = { [ '"', [ '"' ] ], not-quote, { not-quote } } ; |
| 224 |
|
| 225 |
not-quote = allchars - '"' ; |
| 226 |
|
| 227 |
apostrophe3-delim = "'''" ; |
| 228 |
|
| 229 |
apostrophe3-content = { [ "'", [ "'" ] ], not-apostrophe, { not-apostrophe } } ; |
| 230 |
|
| 231 |
not-apostrophe = allchars - "'" ; |
| 232 |
|
| 233 |
(* text block *) |
| 234 |
text-field = text-delim, text-content, text-delim ; |
| 235 |
|
| 236 |
text-delim = line-term, ';' ; |
| 237 |
|
| 238 |
text-content = { allchars } - ( { allchars }, text-delim, { allchars } ) ; |
| 239 |
|
| 240 |
(* |
| 241 |
* CIF keywords are case-insensitive. |
| 242 |
* |
| 243 |
* The global and stop tokens are part of the original STAR specification; |
| 244 |
* they are reserved in case of future use in CIF. |
| 245 |
*) |
| 246 |
|
| 247 |
data-token = ( 'D' | 'd' ), ( 'A' | 'a' ), ( 'T' | 't' ), ( 'A' | 'a' ), '_'; |
| 248 |
|
| 249 |
save-token = ( 'S' | 's' ), ( 'A' | 'a' ), ( 'V' | 'v' ), ( 'E' | 'e' ), '_'; |
| 250 |
|
| 251 |
loop-token = ( 'L' | 'l' ), ( 'O' | 'o' ),( 'O' | 'o' ), ( 'P' | 'p' ) , '_' ; |
| 252 |
|
| 253 |
global-token = ( 'G' | 'g' ), ( 'L' | 'l' ), ( 'O' | 'o' ), ( 'B' | 'b' ), ( 'A' | 'a' ), ( 'L' | 'l' ), '_' ; |
| 254 |
|
| 255 |
stop-token = ( 'S' | 's' ), ( 'T' | 't' ), ( 'O' | 'o' ), ( 'P' | 'p' ), '_' ; |
| 256 |
|
| 257 |
(* A single non-whitespace character *) |
| 258 |
non-blank-char = char - inline-wspace ; |
| 259 |
|
| 260 |
(* |
| 261 |
* Runs of spaces, tabs, line terminators, and comments are |
| 262 |
* required to separate many higher-level elements of this grammar. In |
| 263 |
* most contexts such runs may not begin with comments. For the purposes |
| 264 |
* of these particular productions, such runs *never* end with comments. |
| 265 |
*) |
| 266 |
|
| 267 |
(* |
| 268 |
* a nonempty run of whitespace and possibly comments, beginning with inline |
| 269 |
* whitespace or a line terminator; may span multiple lines |
| 270 |
*) |
| 271 |
wspace = ( inline-wspace | line-term ), wspace-any; |
| 272 |
|
| 273 |
(* |
| 274 |
* a nonempty run of whitespace and possibly comments, beginning with inline |
| 275 |
* whitespace or a line terminator, and ending with a line terminator; may |
| 276 |
* span multiple lines |
| 277 |
*) |
| 278 |
wspace-lines = [ inline-wspace, { inline-wspace }, [ comment ] ], line-term, { wspace-to-eol } ; |
| 279 |
|
| 280 |
(* |
| 281 |
* a possibly-empty run of whitespace and comments; may begin with a comment, |
| 282 |
* and may span multiple lines |
| 283 |
*) |
| 284 |
wspace-any = { wspace-to-eol }, { inline-wspace } ; |
| 285 |
|
| 286 |
(* |
| 287 |
* a run of zero or more spaces and/or tabs, optionally followed by a comment, |
| 288 |
* always terminated by a line terminator. |
| 289 |
*) |
| 290 |
wspace-to-eol = { inline-wspace }, [ comment ], line-term ; |
| 291 |
|
| 292 |
(* |
| 293 |
* A comment is a hash symbol followed by every character up to, but not |
| 294 |
* including, the end of the line |
| 295 |
*) |
| 296 |
comment = '#', { char } ; |
| 297 |
|
| 298 |
(* 'char' represents any allowed character other than line-term *) |
| 299 |
char = allchars - line-term ; |
| 300 |
|
| 301 |
(* |
| 302 |
* Only ASCII space and tab characters are significant as inline |
| 303 |
* whitespace. Unicode's classification of certain other code points as |
| 304 |
* whitespace is not significant for the purposes of CIF. |
| 305 |
*) |
| 306 |
inline-wspace = ?U+0020? | ?U+0009? ; |
| 307 |
|
| 308 |
(* |
| 309 |
* The two-character sequence U+000D U+000A is recognized as a line |
| 310 |
* terminator, as are each of the characters U+000A and U+000D when they |
| 311 |
* appear outside such a sequence. Similarly to XML, CIF2 interprets |
| 312 |
* each of these line termination sequences as the single character U+000A, |
| 313 |
* wherever they appear in a CIF instance document, as if that |
| 314 |
* translation were performed prior to parsing. |
| 315 |
*) |
| 316 |
line-term = ( ?U+000D?, [ ?U+000A? ] ) | ?U+000A? ; |
| 317 |
|
| 318 |
(* For ease of specification we define a token for the full character set. *) |
| 319 |
allchars = ?U+0009? | ?U+000A? | ?U+000D? | ?U+0020 - U+007E? |
| 320 |
| ?U+00A0 - U+D7FF? | ?U+E000 - U+FDCF? | ?U+FDF0 - U+FFFD? |
| 321 |
| ?U+10000 - U+1FFFD? | ?U+20000 - U+2FFFD? | ?U+30000 - U+3FFFD? |
| 322 |
| ?U+40000 - U+4FFFD? | ?U+50000 - U+5FFFD? | ?U+60000 - U+6FFFD? |
| 323 |
| ?U+70000 - U+7FFFD? | ?U+80000 - U+8FFFD? | ?U+90000 - U+9FFFD? |
| 324 |
| ?U+A0000 - U+AFFFD? | ?U+B0000 - U+BFFFD? | ?U+C0000 - U+CFFFD? |
| 325 |
| ?U+D0000 - U+DFFFD? | ?U+E0000 - U+EFFFD? | ?U+F0000 - U+FFFFD? |
| 326 |
| ?U+100000 - U+10FFFD? ; |
| 327 |
|
| 328 |
|