@@ -12,7 +12,7 @@ import { collectAst, collectTypeHierarchy, findReferenceTypes, isAstType, mergeT
1212import { generatedHeader } from './node-util.js' ;
1313import { collectKeywords , collectTerminalRegexps } from './langium-util.js' ;
1414
15- export function generateAst ( services : LangiumCoreServices , grammars : Grammar [ ] , config : LangiumConfig ) : string {
15+ export function generateAst ( services : LangiumCoreServices , grammars : Grammar [ ] , config : LangiumConfig , ) : string {
1616 const astTypes = collectAst ( grammars , services . shared . workspace . LangiumDocuments ) ;
1717 const importFrom = config . langiumInternal ? `../../syntax-tree${ config . importExtension } ` : 'langium' ;
1818
@@ -26,7 +26,7 @@ export function generateAst(services: LangiumCoreServices, grammars: Grammar[],
2626 ${ generateTerminalConstants ( grammars , config ) }
2727
2828 ${ joinToNode ( astTypes . unions , union => union . toAstTypesString ( isAstType ( union . type ) ) , { appendNewLineIfNotEmpty : true } ) }
29- ${ joinToNode ( astTypes . interfaces , iFace => iFace . toAstTypesString ( true , config . optionalProperties ) , { appendNewLineIfNotEmpty : true } ) }
29+ ${ joinToNode ( astTypes . interfaces , iFace => iFace . toAstTypesString ( true , false ) , { appendNewLineIfNotEmpty : true } ) }
3030 ${
3131 astTypes . unions = astTypes . unions . filter ( e => isAstType ( e . type ) ) ,
3232 generateAstReflection ( config , astTypes )
@@ -35,6 +35,30 @@ export function generateAst(services: LangiumCoreServices, grammars: Grammar[],
3535 return toString ( fileNode ) ;
3636 /* eslint-enable @typescript-eslint/indent */
3737}
38+ export function generateAstPartial ( services : LangiumCoreServices , grammars : Grammar [ ] , config : LangiumConfig , ) : string {
39+ const astTypes = collectAst ( grammars , services . shared . workspace . LangiumDocuments ) ;
40+ const importFrom = config . langiumInternal ? `../../syntax-tree${ config . importExtension } ` : 'langium' ;
41+
42+ /* eslint-disable @typescript-eslint/indent */
43+ const fileNode = expandToNode `
44+ ${ generatedHeader }
45+
46+ /* eslint-disable */
47+ import * as langium from '${ importFrom } ';
48+ import * as ast from './ast.js';
49+
50+ ${ generateTerminalConstantsPartial ( grammars , config ) }
51+
52+ ${ joinToNode ( astTypes . unions , union => union . toAstTypesString ( isAstType ( union . type ) , true ) , { appendNewLineIfNotEmpty : true } ) }
53+ ${ joinToNode ( astTypes . interfaces , iFace => iFace . toAstTypesString ( true , true ) , { appendNewLineIfNotEmpty : true } ) }
54+ ${
55+ astTypes . unions = astTypes . unions . filter ( e => isAstType ( e . type ) ) ,
56+ generateAstReflectionPartial ( config , astTypes )
57+ }
58+ ` ;
59+ return toString ( fileNode ) ;
60+ /* eslint-enable @typescript-eslint/indent */
61+ }
3862
3963function generateAstReflection ( config : LangiumConfig , astTypes : AstTypes ) : Generated {
4064 const typeNames : string [ ] = astTypes . interfaces . map ( t => t . name )
@@ -69,6 +93,14 @@ function generateAstReflection(config: LangiumConfig, astTypes: AstTypes): Gener
6993 ` . appendNewLine ( ) ;
7094}
7195
96+ function generateAstReflectionPartial ( config : LangiumConfig , _astTypes : AstTypes ) : Generated {
97+
98+ return expandToNode `
99+
100+ export type { ${ config . projectName } AstType, ${ config . projectName } AstReflection } from './ast.js';
101+ export const reflection = ast.reflection;
102+ ` . appendNewLine ( ) ;
103+ }
72104function buildTypeMetaDataMethod ( astTypes : AstTypes ) : Generated {
73105 /* eslint-disable @typescript-eslint/indent */
74106 return expandToNode `
@@ -253,3 +285,9 @@ function generateTerminalConstants(grammars: Grammar[], config: LangiumConfig):
253285 export type ${ config . projectName } TokenNames = ${ config . projectName } TerminalNames | ${ config . projectName } KeywordNames;
254286 ` . appendNewLine ( ) ;
255287}
288+
289+ function generateTerminalConstantsPartial ( grammars : Grammar [ ] , config : LangiumConfig ) : Generated {
290+ return expandToNode `
291+ export { ${ config . projectName } Terminals, type ${ config . projectName } TerminalNames, type ${ config . projectName } KeywordNames, type ${ config . projectName } TokenNames } from './ast.js';
292+ ` . appendNewLine ( ) ;
293+ }
0 commit comments