78 lines
2.3 KiB
Nim
78 lines
2.3 KiB
Nim
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
## NPEG rules for Preserves.
|
|
|
|
import npeg, npeg/lib/utf8
|
|
|
|
when defined(nimHasUsed): {.used.}
|
|
|
|
grammar "Preserves":
|
|
|
|
ws <- *{ ' ', '\t', '\r', '\n' }
|
|
commas <- *(ws * ',') * ws
|
|
delimiter <- { ' ', '\t', '\r', '\n', '<', '>', '[', ']', '{', '}', '#', ':', '"', '|', '@', ';', ',' } | !1
|
|
|
|
Document <- Value * ws * !1
|
|
|
|
Atom <- Boolean | Double | DoubleRaw | SignedInteger | String | ByteString | Symbol
|
|
|
|
Collection <- Sequence | Dictionary | Set
|
|
|
|
Value <-
|
|
(ws * (Record | Collection | Atom | Embedded | Compact)) |
|
|
(ws * Annotation) |
|
|
(ws * '#' * @'\n' * Value)
|
|
|
|
Record <- '<' * +Value * ws * '>'
|
|
|
|
Sequence <- '[' * *(commas * Value) * commas * ']'
|
|
|
|
Dictionary <- '{' * *(commas * Value * ws * ':' * Value) * commas * '}'
|
|
|
|
Set <- "#{" * *(commas * Value) * commas * '}'
|
|
|
|
Boolean <- '#' * {'f', 't'} * &delimiter
|
|
|
|
nat <- +Digit
|
|
int <- ?('-'|'+') * nat
|
|
frac <- '.' * +Digit
|
|
exp <- 'e' * ?('-'|'+') * +Digit
|
|
flt <- int * ((frac * exp) | frac | exp)
|
|
|
|
Double <- >flt * &delimiter
|
|
|
|
SignedInteger <- int * &delimiter
|
|
|
|
unescaped <- utf8.any - { '\x00'..'\x19', '"', '\\', '|' }
|
|
unicodeEscaped <- 'u' * Xdigit[4]
|
|
escaped <- {'\\', '/', 'b', 'f', 'n', 'r', 't'}
|
|
escape <- '\\'
|
|
|
|
char <- unescaped | '|' | (escape * (escaped | '"' | unicodeEscaped))
|
|
String <- '"' * >(*char) * '"'
|
|
|
|
binunescaped <- {' '..'!', '#'..'[', ']'..'~'}
|
|
binchar <- binunescaped | (escape * (escaped | '"' | ('x' * Xdigit[2])))
|
|
|
|
ByteString <- charByteString | hexByteString | b64ByteString
|
|
charByteString <- "#\"" * >(*binchar) * '"'
|
|
hexByteString <- "#x\"" * >(*(ws * Xdigit[2])) * ws * '"'
|
|
base64char <- {'A'..'Z', 'a'..'z', '0'..'9', '+', '/', '-', '_', '='}
|
|
b64ByteString <- "#[" * >(*(ws * base64char)) * ws * ']'
|
|
|
|
symchar <- (utf8.any - {'\\', '|'}) | (escape * (escaped | unicodeEscaped)) | "\\|"
|
|
QuotedSymbol <- '|' * >(*symchar) * '|'
|
|
sympunct <- {'~', '!', '$', '%', '^', '&', '*', '?', '_', '=', '+', '-', '/', '.'}
|
|
symuchar <- utf8.any - { 0..127 }
|
|
SymbolOrNumber <- >(+(Alpha | Digit | sympunct | symuchar))
|
|
Symbol <- QuotedSymbol | (SymbolOrNumber * &delimiter)
|
|
|
|
Embedded <- "#:" * Value
|
|
|
|
Annotation <- '@' * Value * Value
|
|
|
|
Compact <- "#=" * ws * ByteString
|
|
|
|
DoubleRaw <- "#xd\"" * >((ws * Xdigit[2])[8]) * ws * '"'
|