@@ -2,44 +2,125 @@ 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" ] )
8+
9+ const parseSheetInstancesChildren = (
10+ primitiveSexprs : PrimitiveSExpr [ ] ,
11+ ) : {
12+ projects : SheetInstancesProject [ ]
13+ paths : SheetInstancesRootPath [ ]
14+ } => {
15+ const { propertyMap, arrayPropertyMap } =
16+ SxClass . parsePrimitivesToClassProperties ( primitiveSexprs , "sheet_instances" )
17+
18+ const unsupportedSingularTokens = Object . keys ( propertyMap ) . filter (
19+ ( token ) => ! SUPPORTED_CHILD_TOKENS . has ( token ) ,
20+ )
21+ if ( unsupportedSingularTokens . length > 0 ) {
22+ throw new Error (
23+ `sheet_instances encountered unsupported child token${ unsupportedSingularTokens . length > 1 ? "s" : "" } ${ unsupportedSingularTokens
24+ . map ( ( token ) => `"${ token } "` )
25+ . join ( ", " ) } `,
26+ )
27+ }
28+
29+ const unsupportedArrayTokens = Object . keys ( arrayPropertyMap ) . filter (
30+ ( token ) => ! SUPPORTED_CHILD_TOKENS . has ( token ) ,
31+ )
32+ if ( unsupportedArrayTokens . length > 0 ) {
33+ throw new Error (
34+ `sheet_instances encountered unsupported repeated child token${ unsupportedArrayTokens . length > 1 ? "s" : "" } ${ unsupportedArrayTokens
35+ . map ( ( token ) => `"${ token } "` )
36+ . join ( ", " ) } `,
37+ )
38+ }
39+
40+ const projects = ( arrayPropertyMap . project as SheetInstancesProject [ ] ) ?? [ ]
41+ if ( ! projects . length && propertyMap . project ) {
42+ projects . push ( propertyMap . project as SheetInstancesProject )
43+ }
44+
45+ const paths = ( arrayPropertyMap . path as SheetInstancesRootPath [ ] ) ?? [ ]
46+ if ( ! paths . length && propertyMap . path ) {
47+ paths . push ( propertyMap . path as SheetInstancesRootPath )
48+ }
49+
50+ return { projects, paths }
51+ }
552
653export class SheetInstances extends SxClass {
754 static override token = "sheet_instances"
855 static override parentToken = "kicad_sch"
956 token = "sheet_instances"
1057
11- projects : SheetInstancesProject [ ] = [ ]
58+ private _projects : SheetInstancesProject [ ] = [ ]
59+ private _paths : SheetInstancesRootPath [ ] = [ ]
1260
1361 static override fromSexprPrimitives (
1462 primitiveSexprs : PrimitiveSExpr [ ] ,
1563 ) : SheetInstances {
1664 const instances = new SheetInstances ( )
17- const { arrayPropertyMap } = SxClass . parsePrimitivesToClassProperties (
18- primitiveSexprs ,
19- "sheet_instances" ,
20- )
65+ const { projects, paths } = parseSheetInstancesChildren ( primitiveSexprs )
66+ instances . projects = projects
67+ instances . paths = paths
68+ return instances
69+ }
2170
22- instances . projects =
23- ( arrayPropertyMap . project as SheetInstancesProject [ ] ) ?? [ ]
71+ get projects ( ) : SheetInstancesProject [ ] {
72+ return [ ...this . _projects ]
73+ }
2474
25- return instances
75+ set projects ( value : SheetInstancesProject [ ] ) {
76+ this . _projects = [ ...value ]
77+ }
78+
79+ get paths ( ) : SheetInstancesRootPath [ ] {
80+ return [ ...this . _paths ]
81+ }
82+
83+ set paths ( value : SheetInstancesRootPath [ ] ) {
84+ this . _paths = [ ...value ]
2685 }
2786
2887 override getChildren ( ) : SxClass [ ] {
29- return [ ...this . projects ]
88+ return [ ...this . _projects , ... this . _paths ]
3089 }
3190
3291 override getString ( ) : string {
33- const lines = [ "(sheet_instances" ]
34- for ( const project of this . projects ) {
35- lines . push ( project . getStringIndented ( ) )
92+ const children = this . getChildren ( )
93+ if ( children . length === 0 ) {
94+ return `(${ this . token } )`
95+ }
96+
97+ const lines = [ `(${ this . token } ` ]
98+ for ( const child of children ) {
99+ lines . push ( child . getStringIndented ( ) )
36100 }
37101 lines . push ( ")" )
38102 return lines . join ( "\n" )
39103 }
40104}
41105SxClass . register ( SheetInstances )
42106
107+ export class SheetInstancesForSheet extends SheetInstances {
108+ static override token = "instances"
109+ static override parentToken = "sheet"
110+ override token = "instances"
111+
112+ static override fromSexprPrimitives (
113+ primitiveSexprs : PrimitiveSExpr [ ] ,
114+ ) : SheetInstancesForSheet {
115+ const instances = new SheetInstancesForSheet ( )
116+ const { projects, paths } = parseSheetInstancesChildren ( primitiveSexprs )
117+ instances . projects = projects
118+ instances . paths = paths
119+ return instances
120+ }
121+ }
122+ SxClass . register ( SheetInstancesForSheet )
123+
43124export class SheetInstancesProject extends SxClass {
44125 static override token = "project"
45126 static override parentToken = "sheet_instances"
0 commit comments