|
| 1 | +import type { AbapNode, Identifier, Visibility } from './base'; |
| 2 | +import { AbapAstError } from './errors'; |
| 3 | +import type { TypeDef } from './types'; |
| 4 | +import type { ConstantDecl } from './data'; |
| 5 | +import type { AttributeDef, EventDef, MethodDef, MethodImpl } from './members'; |
| 6 | + |
| 7 | +/** A member that may appear inside a class section. */ |
| 8 | +export type SectionMember = |
| 9 | + | TypeDef |
| 10 | + | AttributeDef |
| 11 | + | MethodDef |
| 12 | + | ConstantDecl |
| 13 | + | EventDef; |
| 14 | + |
| 15 | +/** A visibility section of a class definition. */ |
| 16 | +export interface Section extends AbapNode { |
| 17 | + readonly kind: 'Section'; |
| 18 | + readonly visibility: Visibility; |
| 19 | + readonly members: readonly SectionMember[]; |
| 20 | +} |
| 21 | + |
| 22 | +export function section(input: { |
| 23 | + visibility: Visibility; |
| 24 | + members?: readonly SectionMember[]; |
| 25 | +}): Section { |
| 26 | + if (!input.visibility) { |
| 27 | + throw new AbapAstError('Section: required field "visibility" is missing'); |
| 28 | + } |
| 29 | + const members = input.members ?? []; |
| 30 | + for (const m of members) { |
| 31 | + if ('visibility' in m && m.visibility !== input.visibility) { |
| 32 | + throw new AbapAstError( |
| 33 | + `Section: member "${m.name}" has visibility "${m.visibility}" but section is "${input.visibility}"`, |
| 34 | + ); |
| 35 | + } |
| 36 | + } |
| 37 | + return Object.freeze({ |
| 38 | + kind: 'Section' as const, |
| 39 | + visibility: input.visibility, |
| 40 | + members: Object.freeze([...members]), |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +/** Top-level global class (`CLASS ... DEFINITION` + `IMPLEMENTATION`). */ |
| 45 | +export interface ClassDef extends AbapNode { |
| 46 | + readonly kind: 'ClassDef'; |
| 47 | + readonly name: Identifier; |
| 48 | + readonly superclass?: Identifier; |
| 49 | + readonly interfaces: readonly Identifier[]; |
| 50 | + readonly isFinal?: boolean; |
| 51 | + readonly isAbstract?: boolean; |
| 52 | + readonly isForTesting?: boolean; |
| 53 | + readonly isCreatePrivate?: boolean; |
| 54 | + readonly sections: readonly Section[]; |
| 55 | + readonly implementations: readonly MethodImpl[]; |
| 56 | +} |
| 57 | + |
| 58 | +export function classDef(input: { |
| 59 | + name: Identifier; |
| 60 | + superclass?: Identifier; |
| 61 | + interfaces?: readonly Identifier[]; |
| 62 | + isFinal?: boolean; |
| 63 | + isAbstract?: boolean; |
| 64 | + isForTesting?: boolean; |
| 65 | + isCreatePrivate?: boolean; |
| 66 | + sections?: readonly Section[]; |
| 67 | + implementations?: readonly MethodImpl[]; |
| 68 | +}): ClassDef { |
| 69 | + if (!input.name) { |
| 70 | + throw new AbapAstError('ClassDef: required field "name" is missing'); |
| 71 | + } |
| 72 | + if (input.isFinal && input.isAbstract) { |
| 73 | + throw new AbapAstError('ClassDef: class cannot be both FINAL and ABSTRACT'); |
| 74 | + } |
| 75 | + return Object.freeze({ |
| 76 | + kind: 'ClassDef' as const, |
| 77 | + name: input.name, |
| 78 | + superclass: input.superclass, |
| 79 | + interfaces: Object.freeze([...(input.interfaces ?? [])]), |
| 80 | + isFinal: input.isFinal, |
| 81 | + isAbstract: input.isAbstract, |
| 82 | + isForTesting: input.isForTesting, |
| 83 | + isCreatePrivate: input.isCreatePrivate, |
| 84 | + sections: Object.freeze([...(input.sections ?? [])]), |
| 85 | + implementations: Object.freeze([...(input.implementations ?? [])]), |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +/** A local class inside a CLAS-POOL (same shape as ClassDef, flagged local). */ |
| 90 | +export interface LocalClassDef extends AbapNode { |
| 91 | + readonly kind: 'LocalClassDef'; |
| 92 | + readonly name: Identifier; |
| 93 | + readonly superclass?: Identifier; |
| 94 | + readonly interfaces: readonly Identifier[]; |
| 95 | + readonly isFinal?: boolean; |
| 96 | + readonly isAbstract?: boolean; |
| 97 | + readonly isForTesting?: boolean; |
| 98 | + readonly sections: readonly Section[]; |
| 99 | + readonly implementations: readonly MethodImpl[]; |
| 100 | + readonly local: true; |
| 101 | +} |
| 102 | + |
| 103 | +export function localClassDef(input: { |
| 104 | + name: Identifier; |
| 105 | + superclass?: Identifier; |
| 106 | + interfaces?: readonly Identifier[]; |
| 107 | + isFinal?: boolean; |
| 108 | + isAbstract?: boolean; |
| 109 | + isForTesting?: boolean; |
| 110 | + sections?: readonly Section[]; |
| 111 | + implementations?: readonly MethodImpl[]; |
| 112 | +}): LocalClassDef { |
| 113 | + if (!input.name) { |
| 114 | + throw new AbapAstError('LocalClassDef: required field "name" is missing'); |
| 115 | + } |
| 116 | + if (input.isFinal && input.isAbstract) { |
| 117 | + throw new AbapAstError( |
| 118 | + 'LocalClassDef: class cannot be both FINAL and ABSTRACT', |
| 119 | + ); |
| 120 | + } |
| 121 | + return Object.freeze({ |
| 122 | + kind: 'LocalClassDef' as const, |
| 123 | + name: input.name, |
| 124 | + superclass: input.superclass, |
| 125 | + interfaces: Object.freeze([...(input.interfaces ?? [])]), |
| 126 | + isFinal: input.isFinal, |
| 127 | + isAbstract: input.isAbstract, |
| 128 | + isForTesting: input.isForTesting, |
| 129 | + sections: Object.freeze([...(input.sections ?? [])]), |
| 130 | + implementations: Object.freeze([...(input.implementations ?? [])]), |
| 131 | + local: true as const, |
| 132 | + }); |
| 133 | +} |
0 commit comments