preserves-nim/src/preserves/pegs.nim

78 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":
ws <- *{ ' ', '\t', '\r', '\n' }
2023-12-20 10:20:18 +00:00
commas <- *(ws * ',') * ws
delimiter <- { ' ', '\t', '\r', '\n', '<', '>', '[', ']', '{', '}', '#', ':', '"', '|', '@', ';', ',' } | !1
2023-12-20 09:45:00 +00:00
2021-07-16 17:11:19 +00:00
Document <- Value * ws * !1
2024-02-06 14:24:55 +00:00
Atom <- Boolean | Double | DoubleRaw | SignedInteger | String | ByteString | Symbol
2023-12-25 08:54:20 +00:00
Collection <- Sequence | Dictionary | Set
2021-07-16 17:11:19 +00:00
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
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'} * &delimiter
2021-07-16 17:11:19 +00:00
nat <- +Digit
2023-12-22 18:57:53 +00:00
int <- ?('-'|'+') * nat
2021-07-16 17:11:19 +00:00
frac <- '.' * +Digit
exp <- 'e' * ?('-'|'+') * +Digit
flt <- int * ((frac * exp) | frac | exp)
2024-02-06 14:24:55 +00:00
Double <- >flt * &delimiter
2023-12-20 09:45:00 +00:00
SignedInteger <- int * &delimiter
2023-12-20 09:45:00 +00:00
2023-12-25 08:54:20 +00:00
unescaped <- utf8.any - { '\x00'..'\x19', '"', '\\', '|' }
unicodeEscaped <- 'u' * Xdigit[4]
escaped <- {'\\', '/', 'b', 'f', 'n', 'r', 't'}
escape <- '\\'
char <- unescaped | '|' | (escape * (escaped | '"' | unicodeEscaped))
2022-10-29 23:34:01 +00:00
String <- '"' * >(*char) * '"'
2021-07-16 17:11:19 +00:00
2023-12-25 08:54:20 +00:00
binunescaped <- {' '..'!', '#'..'[', ']'..'~'}
binchar <- binunescaped | (escape * (escaped | '"' | ('x' * Xdigit[2])))
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
2023-12-25 08:54:20 +00:00
symchar <- (utf8.any - {'\\', '|'}) | (escape * (escaped | unicodeEscaped)) | "\\|"
2023-12-20 10:20:18 +00:00
QuotedSymbol <- '|' * >(*symchar) * '|'
sympunct <- {'~', '!', '$', '%', '^', '&', '*', '?', '_', '=', '+', '-', '/', '.'}
symuchar <- utf8.any - { 0..127 }
SymbolOrNumber <- >(+(Alpha | Digit | sympunct | symuchar))
Symbol <- QuotedSymbol | (SymbolOrNumber * &delimiter)
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
2023-12-20 09:45:00 +00:00
DoubleRaw <- "#xd\"" * >((ws * Xdigit[2])[8]) * ws * '"'