preserves-nim/src/preserves/pegs.nim

79 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
Document <- Value * ws * !1
Value <-
(ws * (Record | Collection | Atom | Embedded | Compact)) |
(ws * Annotation) |
(ws * '#' * @'\n' * Value)
Collection <- Sequence | Dictionary | Set
Atom <- Boolean | Float | Double | FloatRaw | DoubleRaw | SignedInteger | String | ByteString | Symbol
Record <- '<' * +Value * ws * '>'
Sequence <- '[' * *(commas * Value) * commas * ']'
Dictionary <- '{' * *(commas * Value * ws * ':' * Value) * commas * '}'
Set <- "#{" * *(commas * Value) * commas * '}'
Boolean <- "#f" | "#t"
nat <- '0' | (Digit-'0') * *Digit
int <- ?'-' * nat
frac <- '.' * +Digit
exp <- 'e' * ?('-'|'+') * +Digit
flt <- int * ((frac * exp) | frac | exp)
Float <- >flt * 'f'
Double <- flt
SignedInteger <- int
char <- unescaped | '|' | (escape * (escaped | '"' | ('u' * Xdigit[4])))
String <- '"' * >(*char) * '"'
ByteString <- charByteString | hexByteString | b64ByteString
charByteString <- "#\"" * >(*binchar) * '"'
hexByteString <- "#x\"" * >(*(ws * Xdigit[2])) * ws * '"'
base64char <- {'A'..'Z', 'a'..'z', '0'..'9', '+', '/', '-', '_', '='}
b64ByteString <- "#[" * >(*(ws * base64char)) * ws * ']'
binchar <- binunescaped | (escape * (escaped | '"' | ('x' * Xdigit[2])))
binunescaped <- {' '..'!', '#'..'[', ']'..'~'}
symchar <- (utf8.any - { 0..127, '\\', '|' }) | (escape * (escaped | ('u' * Xdigit[4]))) | "\\|"
QuotedSymbol <- '|' * >(*symchar) * '|'
sympunct <- {'~', '!', '$', '%', '^', '&', '*', '?', '_', '=', '+', '-', '/', '.'}
symuchar <- utf8.any - { 0..127 }
SymbolOrNumber <- >(+(Alpha | Digit | sympunct | symuchar))
Symbol <- QuotedSymbol | SymbolOrNumber
Embedded <- "#!" * Value
Annotation <- '@' * Value * Value
Compact <- "#=" * ws * ByteString
unescaped <- utf8.any - { '\x00'..'\x19', '"', '\\', '|' }
unicodeEscaped <- 'u' * Xdigit[4]
escaped <- {'\\', '/', 'b', 'f', 'n', 'r', 't'}
escape <- '\\'
FloatRaw <- "#xf\"" * >((ws * Xdigit[2])[4]) * ws * '"'
DoubleRaw <- "#xd\"" * >((ws * Xdigit[2])[8]) * ws * '"'