preserves-nim/src/preserves/schema.nim

216 lines
5.8 KiB
Nim
Raw Normal View History

2021-09-27 13:58:36 +00:00
2023-10-26 14:28:52 +00:00
import
preserves, std/tables
2021-09-27 13:58:36 +00:00
type
Ref* {.preservesRecord: "ref".} = object
`module`*: ModulePath
`name`*: Symbol
2021-09-27 13:58:36 +00:00
ModulePath* = seq[Symbol]
Bundle* {.preservesRecord: "bundle".} = object
`modules`*: Modules
2021-09-27 13:58:36 +00:00
CompoundPatternKind* {.pure.} = enum
`rec`, `tuple`, `tuplePrefix`, `dict`
CompoundPatternRec* {.preservesRecord: "rec".} = object
`label`*: NamedPattern
`fields`*: NamedPattern
2021-09-27 13:58:36 +00:00
CompoundPatternTuple* {.preservesRecord: "tuple".} = object
`patterns`*: seq[NamedPattern]
2021-09-27 13:58:36 +00:00
CompoundPatternTuplePrefix* {.preservesRecord: "tuplePrefix".} = object
`fixed`*: seq[NamedPattern]
`variable`*: NamedSimplePattern
2021-09-27 13:58:36 +00:00
CompoundPatternDict* {.preservesRecord: "dict".} = object
`entries`*: DictionaryEntries
2021-09-27 13:58:36 +00:00
2023-10-26 14:28:52 +00:00
`CompoundPattern`* {.acyclic, preservesOr.} = ref object
2021-09-27 13:58:36 +00:00
case orKind*: CompoundPatternKind
of CompoundPatternKind.`rec`:
`rec`*: CompoundPatternRec
2021-09-27 13:58:36 +00:00
of CompoundPatternKind.`tuple`:
`tuple`*: CompoundPatternTuple
2021-09-27 13:58:36 +00:00
of CompoundPatternKind.`tuplePrefix`:
`tupleprefix`*: CompoundPatternTuplePrefix
2021-09-27 13:58:36 +00:00
of CompoundPatternKind.`dict`:
`dict`*: CompoundPatternDict
2021-09-27 13:58:36 +00:00
Modules* = Table[ModulePath, Schema]
2021-09-27 13:58:36 +00:00
EmbeddedTypeNameKind* {.pure.} = enum
`false`, `Ref`
2021-10-17 12:50:27 +00:00
`EmbeddedTypeName`* {.preservesOr.} = object
2021-09-27 13:58:36 +00:00
case orKind*: EmbeddedTypeNameKind
2021-11-11 10:11:40 +00:00
of EmbeddedTypeNameKind.`false`:
`false`* {.preservesLiteral: "#f".}: bool
2021-09-27 13:58:36 +00:00
of EmbeddedTypeNameKind.`Ref`:
`ref`*: Ref
Definitions* = Table[Symbol, Definition]
2022-02-19 16:33:16 +00:00
`AtomKind`* {.preservesOr, pure.} = enum
2021-09-27 13:58:36 +00:00
`Boolean`, `Float`, `Double`, `SignedInteger`, `String`, `ByteString`,
`Symbol`
DictionaryEntries* = Table[Value, NamedSimplePattern]
2021-09-27 13:58:36 +00:00
NamedPatternKind* {.pure.} = enum
`named`, `anonymous`
2023-10-26 14:28:52 +00:00
`NamedPattern`* {.acyclic, preservesOr.} = ref object
2021-09-27 13:58:36 +00:00
case orKind*: NamedPatternKind
of NamedPatternKind.`named`:
`named`*: Binding
2021-09-27 13:58:36 +00:00
of NamedPatternKind.`anonymous`:
`anonymous`*: Pattern
2021-11-11 10:11:40 +00:00
2021-09-27 13:58:36 +00:00
SimplePatternKind* {.pure.} = enum
`any`, `atom`, `embedded`, `lit`, `seqof`, `setof`, `dictof`, `Ref`
SimplePatternAtom* {.preservesRecord: "atom".} = object
`atomKind`*: AtomKind
SimplePatternEmbedded* {.preservesRecord: "embedded".} = object
`interface`*: SimplePattern
2021-09-27 13:58:36 +00:00
SimplePatternLit* {.preservesRecord: "lit".} = object
`value`*: Value
2021-09-27 13:58:36 +00:00
SimplePatternSeqof* {.preservesRecord: "seqof".} = object
`pattern`*: SimplePattern
2021-09-27 13:58:36 +00:00
SimplePatternSetof* {.preservesRecord: "setof".} = object
`pattern`*: SimplePattern
2021-09-27 13:58:36 +00:00
SimplePatternDictof* {.preservesRecord: "dictof".} = object
`key`*: SimplePattern
`value`*: SimplePattern
2021-09-27 13:58:36 +00:00
2023-10-26 14:28:52 +00:00
`SimplePattern`* {.acyclic, preservesOr.} = ref object
2021-09-27 13:58:36 +00:00
case orKind*: SimplePatternKind
of SimplePatternKind.`any`:
`any`* {.preservesLiteral: "any".}: bool
of SimplePatternKind.`atom`:
`atom`*: SimplePatternAtom
of SimplePatternKind.`embedded`:
`embedded`*: SimplePatternEmbedded
2021-09-27 13:58:36 +00:00
of SimplePatternKind.`lit`:
`lit`*: SimplePatternLit
2021-09-27 13:58:36 +00:00
of SimplePatternKind.`seqof`:
`seqof`*: SimplePatternSeqof
2021-09-27 13:58:36 +00:00
of SimplePatternKind.`setof`:
`setof`*: SimplePatternSetof
2021-09-27 13:58:36 +00:00
of SimplePatternKind.`dictof`:
`dictof`*: SimplePatternDictof
2021-09-27 13:58:36 +00:00
of SimplePatternKind.`Ref`:
`ref`*: Ref
2021-09-27 13:58:36 +00:00
NamedSimplePatternKind* {.pure.} = enum
`named`, `anonymous`
`NamedSimplePattern`* {.preservesOr.} = object
2021-09-27 13:58:36 +00:00
case orKind*: NamedSimplePatternKind
of NamedSimplePatternKind.`named`:
`named`*: Binding
2021-09-27 13:58:36 +00:00
of NamedSimplePatternKind.`anonymous`:
`anonymous`*: SimplePattern
2021-11-11 10:11:40 +00:00
2021-09-27 13:58:36 +00:00
DefinitionKind* {.pure.} = enum
`or`, `and`, `Pattern`
DefinitionOrField0* {.preservesTuple.} = object
`pattern0`*: NamedAlternative
`pattern1`*: NamedAlternative
`patternN`* {.preservesTupleTail.}: seq[NamedAlternative]
2021-09-27 13:58:36 +00:00
DefinitionOr* {.preservesRecord: "or".} = object
2023-05-16 22:06:52 +00:00
`field0`*: DefinitionOrField0
2021-09-27 13:58:36 +00:00
DefinitionAndField0* {.preservesTuple.} = object
`pattern0`*: NamedPattern
`pattern1`*: NamedPattern
`patternN`* {.preservesTupleTail.}: seq[NamedPattern]
2021-09-27 13:58:36 +00:00
DefinitionAnd* {.preservesRecord: "and".} = object
2023-05-16 22:06:52 +00:00
`field0`*: DefinitionAndField0
2021-09-27 13:58:36 +00:00
`Definition`* {.preservesOr.} = object
2021-09-27 13:58:36 +00:00
case orKind*: DefinitionKind
of DefinitionKind.`or`:
`or`*: DefinitionOr
2021-09-27 13:58:36 +00:00
of DefinitionKind.`and`:
`and`*: DefinitionAnd
2021-09-27 13:58:36 +00:00
of DefinitionKind.`Pattern`:
`pattern`*: Pattern
2021-09-27 13:58:36 +00:00
NamedAlternative* {.preservesTuple.} = object
2021-09-27 13:58:36 +00:00
`variantLabel`*: string
`pattern`*: Pattern
2021-09-27 13:58:36 +00:00
SchemaField0* {.preservesDictionary.} = object
`definitions`*: Definitions
2023-10-26 14:28:52 +00:00
`embeddedType`*: EmbeddedTypeName
`version`* {.preservesLiteral: "1".}: tuple[]
2021-09-27 13:58:36 +00:00
Schema* {.preservesRecord: "schema".} = object
2023-05-16 22:06:52 +00:00
`field0`*: SchemaField0
2021-09-27 13:58:36 +00:00
PatternKind* {.pure.} = enum
`SimplePattern`, `CompoundPattern`
2023-10-26 14:28:52 +00:00
`Pattern`* {.acyclic, preservesOr.} = ref object
2021-09-27 13:58:36 +00:00
case orKind*: PatternKind
of PatternKind.`SimplePattern`:
`simplepattern`*: SimplePattern
2021-09-27 13:58:36 +00:00
of PatternKind.`CompoundPattern`:
`compoundpattern`*: CompoundPattern
2021-11-11 10:11:40 +00:00
Binding* {.preservesRecord: "named".} = object
`name`*: Symbol
`pattern`*: SimplePattern
proc `$`*(x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
EmbeddedTypeName |
Definitions |
DictionaryEntries |
NamedPattern |
SimplePattern |
NamedSimplePattern |
Definition |
NamedAlternative |
Schema |
Pattern |
Binding): string =
`$`(toPreserves(x))
2021-10-17 12:50:27 +00:00
proc encode*(x: Ref | ModulePath | Bundle | CompoundPattern | Modules |
EmbeddedTypeName |
Definitions |
DictionaryEntries |
NamedPattern |
SimplePattern |
NamedSimplePattern |
Definition |
NamedAlternative |
Schema |
Pattern |
Binding): seq[byte] =
encode(toPreserves(x))