preserves-nim/src/preserves/schema.nim

216 lines
5.8 KiB
Nim

import
std/typetraits, preserves, std/tables
type
Ref* {.preservesRecord: "ref".} = object
`module`*: ModulePath
`name`* {.preservesSymbol.}: string
ModulePath* = seq[string]
Bundle* {.preservesRecord: "bundle".} = object
`modules`*: Modules
CompoundPatternKind* {.pure.} = enum
`rec`, `tuple`, `tuplePrefix`, `dict`
CompoundPatternRec* {.preservesRecord: "rec".} = object
`label`*: NamedPattern
`fields`*: NamedPattern
CompoundPatternTuple* {.preservesRecord: "tuple".} = object
`patterns`*: seq[NamedPattern]
CompoundPatternTuplePrefix* {.preservesRecord: "tuplePrefix".} = object
`fixed`*: seq[NamedPattern]
`variable`*: NamedSimplePattern
CompoundPatternDict* {.preservesRecord: "dict".} = object
`entries`*: DictionaryEntries
`CompoundPattern`* {.preservesOr.} = ref object
case orKind*: CompoundPatternKind
of CompoundPatternKind.`rec`:
`rec`*: CompoundPatternRec
of CompoundPatternKind.`tuple`:
`tuple`*: CompoundPatternTuple
of CompoundPatternKind.`tuplePrefix`:
`tupleprefix`*: CompoundPatternTuplePrefix
of CompoundPatternKind.`dict`:
`dict`*: CompoundPatternDict
Modules* = Table[ModulePath, Schema]
EmbeddedTypeNameKind* {.pure.} = enum
`Ref`, `false`
`EmbeddedTypeName`* {.preservesOr.} = ref object
case orKind*: EmbeddedTypeNameKind
of EmbeddedTypeNameKind.`Ref`:
`ref`*: Ref
of EmbeddedTypeNameKind.`false`:
`false`* {.preservesLiteral: "#f".}: bool
`AtomKind`* {.preservesOr.} = enum
`Boolean`, `Float`, `Double`, `SignedInteger`, `String`, `ByteString`,
`Symbol`
Definitions* = Table[string, Definition]
DictionaryEntries* = Table[Preserve[void], NamedSimplePattern]
NamedPatternKind* {.pure.} = enum
`named`, `anonymous`
`NamedPattern`* {.preservesOr.} = ref object
case orKind*: NamedPatternKind
of NamedPatternKind.`named`:
`named`*: Binding
of NamedPatternKind.`anonymous`:
`anonymous`*: Pattern
SimplePatternKind* {.pure.} = enum
`any`, `atom`, `embedded`, `lit`, `seqof`, `setof`, `dictof`, `Ref`
SimplePatternAtom* {.preservesRecord: "atom".} = object
`atomKind`*: AtomKind
SimplePatternEmbedded* {.preservesRecord: "embedded".} = object
`interface`*: SimplePattern
SimplePatternLit* {.preservesRecord: "lit".} = object
`value`*: Preserve[void]
SimplePatternSeqof* {.preservesRecord: "seqof".} = object
`pattern`*: SimplePattern
SimplePatternSetof* {.preservesRecord: "setof".} = object
`pattern`*: SimplePattern
SimplePatternDictof* {.preservesRecord: "dictof".} = object
`key`*: SimplePattern
`value`*: SimplePattern
`SimplePattern`* {.preservesOr.} = ref object
case orKind*: SimplePatternKind
of SimplePatternKind.`any`:
`any`* {.preservesLiteral: "any".}: bool
of SimplePatternKind.`atom`:
`atom`*: SimplePatternAtom
of SimplePatternKind.`embedded`:
`embedded`*: SimplePatternEmbedded
of SimplePatternKind.`lit`:
`lit`*: SimplePatternLit
of SimplePatternKind.`seqof`:
`seqof`*: SimplePatternSeqof
of SimplePatternKind.`setof`:
`setof`*: SimplePatternSetof
of SimplePatternKind.`dictof`:
`dictof`*: SimplePatternDictof
of SimplePatternKind.`Ref`:
`ref`*: Ref
NamedSimplePatternKind* {.pure.} = enum
`named`, `anonymous`
`NamedSimplePattern`* {.preservesOr.} = ref object
case orKind*: NamedSimplePatternKind
of NamedSimplePatternKind.`named`:
`named`*: Binding
of NamedSimplePatternKind.`anonymous`:
`anonymous`*: SimplePattern
DefinitionKind* {.pure.} = enum
`or`, `and`, `Pattern`
DefinitionOrData* {.preservesTuple.} = object
`pattern0`*: NamedAlternative
`pattern1`*: NamedAlternative
`patternN`* {.preservesTupleTail.}: seq[NamedAlternative]
DefinitionOr* {.preservesRecord: "or".} = object
`data`*: DefinitionOrData
DefinitionAndData* {.preservesTuple.} = object
`pattern0`*: NamedPattern
`pattern1`*: NamedPattern
`patternN`* {.preservesTupleTail.}: seq[NamedPattern]
DefinitionAnd* {.preservesRecord: "and".} = object
`data`*: DefinitionAndData
`Definition`* {.preservesOr.} = ref object
case orKind*: DefinitionKind
of DefinitionKind.`or`:
`or`*: DefinitionOr
of DefinitionKind.`and`:
`and`*: DefinitionAnd
of DefinitionKind.`Pattern`:
`pattern`*: Pattern
NamedAlternative* {.preservesTuple.} = object
`variantLabel`*: string
`pattern`*: Pattern
SchemaData* {.preservesDictionary.} = object
`embeddedType`*: EmbeddedTypeName
`version`* {.preservesLiteral: "1".}: bool
`definitions`*: Definitions
Schema* {.preservesRecord: "schema".} = object
`data`*: SchemaData
PatternKind* {.pure.} = enum
`SimplePattern`, `CompoundPattern`
`Pattern`* {.preservesOr.} = ref object
case orKind*: PatternKind
of PatternKind.`SimplePattern`:
`simplepattern`*: SimplePattern
of PatternKind.`CompoundPattern`:
`compoundpattern`*: CompoundPattern
Binding* {.preservesRecord: "named".} = object
`name`* {.preservesSymbol.}: string
`pattern`*: SimplePattern
proc `$`*(x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
EmbeddedTypeName |
Definitions |
DictionaryEntries |
NamedPattern |
SimplePattern |
NamedSimplePattern |
Definition |
NamedAlternative |
Schema |
Pattern |
Binding): string =
`$`(toPreserve(x))
proc encode*[E](x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
EmbeddedTypeName |
Definitions |
DictionaryEntries |
NamedPattern |
SimplePattern |
NamedSimplePattern |
Definition |
NamedAlternative |
Schema |
Pattern |
Binding): seq[byte] =
encode(toPreserve(x, E))