schemas: improve "include" parsing

This commit is contained in:
Emery Hemingway 2022-11-20 14:12:23 -06:00
parent e1ab43578a
commit 65195a5eb7
3 changed files with 9 additions and 8 deletions

View File

@ -1,6 +1,6 @@
# Package # Package
version = "20221106" version = "20221120"
author = "Emery Hemingway" author = "Emery Hemingway"
description = "data model and serialization format" description = "data model and serialization format"
license = "Unlicense" license = "Unlicense"

View File

@ -23,7 +23,7 @@ proc joinWhitespace(s: string): string =
for token, isSep in tokenize(s, Whitespace + {','}): for token, isSep in tokenize(s, Whitespace + {','}):
if not isSep: add(result, token) if not isSep: add(result, token)
template unescape(buf: var string; capture: string) = template unescape*(buf: var string; capture: string) =
var i: int var i: int
while i < len(capture): while i < len(capture):
if capture[i] == '\\': if capture[i] == '\\':

View File

@ -75,14 +75,15 @@ const parser = peg("Schema", p: ParseState):
EmbeddedTypeName <- "embeddedType" * S * >("#f" | Ref) EmbeddedTypeName <- "embeddedType" * S * >("#f" | Ref)
Include <- "include" * +Space * (>(+Alnum) | >('"' * @'"')): Include <- "include" * S * '"' * >(+Preserves.char) * '"':
var var path: string
path = absolutePath(strip($1, true, true, {'"'}), p.directory) unescape(path, $1)
ip = ParseState( path = absolutePath(path, p.directory)
var state = ParseState(
schema: move p.schema, schema: move p.schema,
directory: parentDir path) directory: parentDir path)
match(readFile path, ip) match(readFile path, state)
p.schema = move ip.schema p.schema = move state.schema
Definition <- >id * S * '=' * S * (OrPattern | AndPattern | Pattern): Definition <- >id * S * '=' * S * (OrPattern | AndPattern | Pattern):
if p.schema.definitions.hasKey(Symbol $1): if p.schema.definitions.hasKey(Symbol $1):