@@ -2,37 +2,92 @@ import { SxClass } from "../base-classes/SxClass"
22import type { PrimitiveSExpr } from "../parseToPrimitiveSExpr"
33import { quoteSExprString } from "../utils/quoteSExprString"
44import { toStringValue } from "../utils/toStringValue"
5+ import { SheetInstancesRootPath } from "./SheetInstancesRoot"
6+
7+ const SUPPORTED_CHILD_TOKENS = new Set ( [ "project" , "path" ] )
58
69export class SheetInstances extends SxClass {
710 static override token = "sheet_instances"
811 static override parentToken = "kicad_sch"
912 token = "sheet_instances"
1013
11- projects : SheetInstancesProject [ ] = [ ]
14+ private _projects : SheetInstancesProject [ ] = [ ]
15+ private _paths : SheetInstancesRootPath [ ] = [ ]
1216
1317 static override fromSexprPrimitives (
1418 primitiveSexprs : PrimitiveSExpr [ ] ,
1519 ) : SheetInstances {
1620 const instances = new SheetInstances ( )
17- const { arrayPropertyMap } = SxClass . parsePrimitivesToClassProperties (
18- primitiveSexprs ,
19- "sheet_instances" ,
21+ const { propertyMap, arrayPropertyMap } =
22+ SxClass . parsePrimitivesToClassProperties ( primitiveSexprs , this . token )
23+
24+ const unsupportedSingularTokens = Object . keys ( propertyMap ) . filter (
25+ ( token ) => ! SUPPORTED_CHILD_TOKENS . has ( token ) ,
26+ )
27+ if ( unsupportedSingularTokens . length > 0 ) {
28+ throw new Error (
29+ `sheet_instances encountered unsupported child token${ unsupportedSingularTokens . length > 1 ? "s" : "" } ${ unsupportedSingularTokens
30+ . map ( ( token ) => `"${ token } "` )
31+ . join ( ", " ) } `,
32+ )
33+ }
34+
35+ const unsupportedArrayTokens = Object . keys ( arrayPropertyMap ) . filter (
36+ ( token ) => ! SUPPORTED_CHILD_TOKENS . has ( token ) ,
2037 )
38+ if ( unsupportedArrayTokens . length > 0 ) {
39+ throw new Error (
40+ `sheet_instances encountered unsupported repeated child token${ unsupportedArrayTokens . length > 1 ? "s" : "" } ${ unsupportedArrayTokens
41+ . map ( ( token ) => `"${ token } "` )
42+ . join ( ", " ) } `,
43+ )
44+ }
45+
46+ const projects = ( arrayPropertyMap . project as SheetInstancesProject [ ] ) ?? [ ]
47+ if ( ! projects . length && propertyMap . project ) {
48+ projects . push ( propertyMap . project as SheetInstancesProject )
49+ }
50+
51+ const paths = ( arrayPropertyMap . path as SheetInstancesRootPath [ ] ) ?? [ ]
52+ if ( ! paths . length && propertyMap . path ) {
53+ paths . push ( propertyMap . path as SheetInstancesRootPath )
54+ }
2155
22- instances . projects =
23- ( arrayPropertyMap . project as SheetInstancesProject [ ] ) ?? [ ]
56+ instances . _projects = projects
57+ instances . _paths = paths
2458
2559 return instances
2660 }
2761
62+ get projects ( ) : SheetInstancesProject [ ] {
63+ return [ ...this . _projects ]
64+ }
65+
66+ set projects ( value : SheetInstancesProject [ ] ) {
67+ this . _projects = [ ...value ]
68+ }
69+
70+ get paths ( ) : SheetInstancesRootPath [ ] {
71+ return [ ...this . _paths ]
72+ }
73+
74+ set paths ( value : SheetInstancesRootPath [ ] ) {
75+ this . _paths = [ ...value ]
76+ }
77+
2878 override getChildren ( ) : SxClass [ ] {
29- return [ ...this . projects ]
79+ return [ ...this . _projects , ... this . _paths ]
3080 }
3181
3282 override getString ( ) : string {
83+ const children = this . getChildren ( )
84+ if ( children . length === 0 ) {
85+ return "(sheet_instances)"
86+ }
87+
3388 const lines = [ "(sheet_instances" ]
34- for ( const project of this . projects ) {
35- lines . push ( project . getStringIndented ( ) )
89+ for ( const child of children ) {
90+ lines . push ( child . getStringIndented ( ) )
3691 }
3792 lines . push ( ")" )
3893 return lines . join ( "\n" )
0 commit comments