preserves-nim/src/preserves/pegs.nim

79 lines
2.3 KiB
Nim
Raw Normal View History

2023-12-15 19:45:27 +00:00
# SPDX-FileCopyrightText: ☭ Emery Hemingway
2021-08-31 15:00:00 +00:00
# SPDX-License-Identifier: Unlicense
2021-07-16 17:11:19 +00:00
## NPEG rules for Preserves.
2021-07-16 17:11:19 +00:00
import npeg, npeg/lib/utf8
when defined(nimHasUsed): {.used.}
grammar "Preserves":
2023-12-20 10:20:18 +00:00
ws <- *(' ' | '\t' | '\r' | '\n' )
commas <- *(ws * ',') * ws
2023-12-20 09:45:00 +00:00
2021-07-16 17:11:19 +00:00
Document <- Value * ws * !1
Value <-
(ws * (Record | Collection | Atom | Embedded | Compact)) |
2022-11-02 18:58:37 +00:00
(ws * Annotation) |
2023-12-15 19:45:27 +00:00
(ws * '#' * @'\n' * Value)
2021-07-16 17:11:19 +00:00
Collection <- Sequence | Dictionary | Set
2023-12-20 09:45:00 +00:00
Atom <- Boolean | Float | Double | FloatRaw | DoubleRaw | SignedInteger | String | ByteString | Symbol
2021-07-16 17:11:19 +00:00
2023-12-20 10:20:18 +00:00
Record <- '<' * +Value * ws * '>'
2021-07-16 17:11:19 +00:00
2023-12-20 10:20:18 +00:00
Sequence <- '[' * *(commas * Value) * commas * ']'
2021-07-16 17:11:19 +00:00
2023-12-20 10:20:18 +00:00
Dictionary <- '{' * *(commas * Value * ws * ':' * Value) * commas * '}'
2021-07-16 17:11:19 +00:00
2023-12-20 10:20:18 +00:00
Set <- "#{" * *(commas * Value) * commas * '}'
2021-07-16 17:11:19 +00:00
Boolean <- "#f" | "#t"
nat <- '0' | (Digit-'0') * *Digit
int <- ?'-' * nat
frac <- '.' * +Digit
exp <- 'e' * ?('-'|'+') * +Digit
flt <- int * ((frac * exp) | frac | exp)
2023-12-20 09:45:00 +00:00
Float <- >flt * 'f'
Double <- flt
SignedInteger <- int
2022-10-29 23:34:01 +00:00
char <- unescaped | '|' | (escape * (escaped | '"' | ('u' * Xdigit[4])))
String <- '"' * >(*char) * '"'
2021-07-16 17:11:19 +00:00
ByteString <- charByteString | hexByteString | b64ByteString
2022-10-29 06:05:47 +00:00
charByteString <- "#\"" * >(*binchar) * '"'
2023-12-20 09:45:00 +00:00
hexByteString <- "#x\"" * >(*(ws * Xdigit[2])) * ws * '"'
base64char <- {'A'..'Z', 'a'..'z', '0'..'9', '+', '/', '-', '_', '='}
b64ByteString <- "#[" * >(*(ws * base64char)) * ws * ']'
2021-07-16 17:11:19 +00:00
binchar <- binunescaped | (escape * (escaped | '"' | ('x' * Xdigit[2])))
2022-10-29 23:34:01 +00:00
binunescaped <- {' '..'!', '#'..'[', ']'..'~'}
2021-07-16 17:11:19 +00:00
2023-12-20 10:20:18 +00:00
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
2021-07-16 17:11:19 +00:00
Embedded <- "#!" * Value
2022-11-02 18:58:37 +00:00
Annotation <- '@' * Value * Value
2021-07-16 17:11:19 +00:00
Compact <- "#=" * ws * ByteString
2022-10-29 23:34:01 +00:00
unescaped <- utf8.any - { '\x00'..'\x19', '"', '\\', '|' }
2021-07-16 17:11:19 +00:00
unicodeEscaped <- 'u' * Xdigit[4]
2022-10-29 23:34:01 +00:00
escaped <- {'\\', '/', 'b', 'f', 'n', 'r', 't'}
2021-07-16 17:11:19 +00:00
escape <- '\\'
2023-12-20 09:45:00 +00:00
FloatRaw <- "#xf\"" * >((ws * Xdigit[2])[4]) * ws * '"'
DoubleRaw <- "#xd\"" * >((ws * Xdigit[2])[8]) * ws * '"'