9
9
Node ,
10
10
NODE_TYPES ,
11
11
PackageDescription ,
12
+ PropertyDefinition ,
12
13
registerNodeDefinition ,
13
14
registerNodeProperty
14
15
} from "../model/model" ;
@@ -18,19 +19,29 @@ import {Issue, IssueSeverity, IssueType} from "../validation";
18
19
import { addLiteral , getEPackage } from "./ecore-basic" ;
19
20
import {
20
21
STARLASU_URI_V2 ,
22
+ THE_ENTITY_DECLARATION_INTERFACE ,
23
+ THE_EXPRESSION_INTERFACE ,
21
24
THE_ISSUE_ECLASS ,
22
25
THE_ISSUE_SEVERITY_EENUM ,
23
26
THE_ISSUE_TYPE_EENUM ,
24
27
THE_LOCAL_DATE_ECLASS ,
25
28
THE_LOCAL_DATE_TIME_ECLASS ,
26
29
THE_LOCAL_TIME_ECLASS ,
27
- THE_NODE_ECLASS , THE_NODE_ORIGIN_ECLASS ,
30
+ THE_NODE_ECLASS as THE_NODE_ECLASS_V2 ,
31
+ THE_NODE_ECLASS ,
32
+ THE_NODE_ORIGIN_ECLASS ,
28
33
THE_POINT_ECLASS ,
29
- THE_POSITION_ECLASS , THE_REFERENCE_BY_NAME_ECLASS ,
30
- THE_RESULT_ECLASS , THE_SIMPLE_ORIGIN_ECLASS , THE_TEXT_FILE_DESTINATION_ECLASS
34
+ THE_POSITION_ECLASS ,
35
+ THE_REFERENCE_BY_NAME_ECLASS ,
36
+ THE_RESULT_ECLASS ,
37
+ THE_SIMPLE_ORIGIN_ECLASS ,
38
+ THE_STATEMENT_INTERFACE ,
39
+ THE_TEXT_FILE_DESTINATION_ECLASS
31
40
} from "./starlasu-v2-metamodel" ;
32
- import { KOLASU_URI_V1 } from "./kolasu-v1-metamodel" ;
41
+ import { KOLASU_URI_V1 , THE_NODE_ECLASS as THE_NODE_ECLASS_V1 } from "./kolasu-v1-metamodel" ;
33
42
import { EBigDecimal , EBigInteger } from "./ecore-patching" ;
43
+ import { ExternalNode } from "../trace/trace-node" ;
44
+
34
45
export * as starlasu_v2 from "./starlasu-v2-metamodel" ;
35
46
export * as kolasu_v1 from "./kolasu-v1-metamodel" ;
36
47
@@ -316,7 +327,7 @@ export function fromEObject(obj: ECore.EObject | any, parent?: Node): ASTElement
316
327
const ePackage = eClass . eContainer as ECore . EPackage ;
317
328
const constructor = NODE_TYPES [ ePackage . get ( "name" ) ] ?. nodes [ eClass . get ( "name" ) ] ;
318
329
if ( constructor ) {
319
- const node = new constructor ( ) ;
330
+ const node = new constructor ( ) . withParent ( parent ) ;
320
331
node . parent = parent ;
321
332
eClass . get ( "eAllStructuralFeatures" ) . forEach ( ft => {
322
333
const name = ft . get ( "name" ) ;
@@ -804,3 +815,117 @@ export interface EcoreMetamodelSupport {
804
815
generateMetamodel ( resource : ECore . Resource , includingKolasuMetamodel : boolean ) : void ;
805
816
}
806
817
818
+ export class ECoreNode extends ExternalNode {
819
+
820
+ parent ?: ECoreNode ;
821
+
822
+ constructor ( public eo : ECore . EObject ) {
823
+ super ( ) ;
824
+ const container = this . eo . eContainer ;
825
+ if ( container ?. isKindOf ( THE_NODE_ECLASS_V2 ) || container ?. isKindOf ( THE_NODE_ECLASS_V1 ) ) {
826
+ this . parent = new ECoreNode ( container ) ;
827
+ }
828
+ }
829
+
830
+ get nodeDefinition ( ) {
831
+ return {
832
+ package : this . eo . eClass . eContainer . get ( "name" ) as string ,
833
+ name : this . eo . eClass . get ( "name" ) as string ,
834
+ properties : this . getProperties ( )
835
+ } ;
836
+ }
837
+
838
+ get ( ...path : string [ ] ) : ExternalNode | undefined {
839
+ let eo : ECore . EObject = this . eo ;
840
+ for ( const component of path ) {
841
+ eo = eo ?. get ( component ) ;
842
+ }
843
+ if ( eo ) {
844
+ return new ECoreNode ( eo ) ;
845
+ } else {
846
+ return undefined ;
847
+ }
848
+ }
849
+
850
+ getAttribute ( name : string ) : any {
851
+ return this . eo . get ( name ) ;
852
+ }
853
+
854
+ getAttributes ( ) : { [ p : string ] : any } {
855
+ const result : any = { } ;
856
+ for ( const attr of this . eo . eClass . get ( "eAllAttributes" ) ) {
857
+ const name = attr . get ( "name" ) ;
858
+ result [ name ] = this . eo . get ( name ) ;
859
+ }
860
+ return result ;
861
+ }
862
+
863
+ getChildren ( role ?: string ) : ExternalNode [ ] {
864
+ return this . getChildrenEObjects ( role ) . map ( c => new ECoreNode ( c ) ) ;
865
+ }
866
+
867
+ getId ( ) : string {
868
+ return this . eo . fragment ( ) ;
869
+ }
870
+
871
+ getIssues ( property = "issues" ) : Issue [ ] | undefined {
872
+ const raw = this . eo . get ( property ) ;
873
+ if ( raw ) {
874
+ return fromEObject ( raw ) as Issue [ ] ;
875
+ } else {
876
+ return undefined ;
877
+ }
878
+ }
879
+
880
+ getPosition ( property = "position" ) : Position | undefined {
881
+ const raw = this . eo . get ( property ) ;
882
+ if ( raw ) {
883
+ return fromEObject ( raw ) as Position ;
884
+ } else {
885
+ return undefined ;
886
+ }
887
+ }
888
+
889
+ getRole ( ) : string | undefined {
890
+ return this . eo . eContainingFeature ?. get ( "name" ) ;
891
+ }
892
+
893
+ getProperties ( ) : { [ name : string | symbol ] : PropertyDefinition } {
894
+ const result : { [ name : string | symbol ] : PropertyDefinition } = { } ;
895
+ for ( const attr of this . eo . eClass . get ( "eAllAttributes" ) ) {
896
+ const name = attr . get ( "name" ) ;
897
+ result [ name ] = { name : name , child : false } ;
898
+ }
899
+ this . eo . eContents ( )
900
+ . filter ( ( c ) => c . eContainingFeature . get ( "name" ) != "position" )
901
+ . forEach ( ( c ) => {
902
+ const name = c . eContainingFeature . get ( "name" ) ;
903
+ result [ name ] = { name, child : true , multiple : c . eContainingFeature . get ( "many" ) } ;
904
+ } ) ;
905
+ return result ;
906
+ }
907
+
908
+ protected getChildrenEObjects ( role : string | undefined ) {
909
+ return this . eo . eContents ( )
910
+ . filter ( ( c ) => c . isKindOf ( THE_NODE_ECLASS_V2 ) || c . isKindOf ( THE_NODE_ECLASS_V1 ) )
911
+ . filter ( ( c ) => c . eContainingFeature . get ( "name" ) != "origin" )
912
+ . filter ( ( c ) => c . eContainingFeature . get ( "name" ) != "destination" )
913
+ . filter ( ( c ) => role == null || role == c . eContainingFeature . get ( "name" ) ) ;
914
+ }
915
+
916
+ isDeclaration ( ) : boolean {
917
+ return this . eo . isKindOf ( THE_ENTITY_DECLARATION_INTERFACE ) ;
918
+ }
919
+
920
+ isExpression ( ) : boolean {
921
+ return this . eo . isKindOf ( THE_EXPRESSION_INTERFACE ) ;
922
+ }
923
+
924
+ isStatement ( ) : boolean {
925
+ return this . eo . isKindOf ( THE_STATEMENT_INTERFACE ) ;
926
+ }
927
+
928
+ equals ( other : ExternalNode ) : boolean {
929
+ return super . equals ( other ) || ( other instanceof ECoreNode && other . eo == this . eo ) ;
930
+ }
931
+ }
0 commit comments