/******************************************************************************
 * Copyright 2021 TypeFox GmbH
 * This program and the accompanying materials are made available under the
 * terms of the MIT License, which is available in the project root.
 ******************************************************************************/ import { CompositeGeneratorNode , Grammar , LangiumServices , NL , processGeneratorNode } from 'langium' ; import { EOL } from 'os' ; import { LangiumConfig } from '../package' ; import { generatedHeader } from './util' ; export function serializeGrammar ( services , grammars , config ) { const node = new CompositeGeneratorNode ( ) ; node . append ( generatedHeader ) ; if ( config . langiumInternal ) { node . append ( "import { loadGrammar } from '../grammar-util';" , NL , "import { Grammar } from './ast';" ) ; } else { node . append ( "import { loadGrammar, Grammar } from 'langium';" ) ; } node . append ( NL , NL ) ; for ( let i = 0 ; i < grammars . length ; i ++ ) { const grammar = grammars [ i ] ; const json = services . serializer . JsonSerializer . serialize ( grammar , 2 ) . replace ( /\\/g , '\\\\' ) . split ( '\n' ) . join ( EOL ) ; node . append ( 'let loaded' , grammar . name , 'Grammar: Grammar | undefined;' , NL , 'export const ' , grammar . name , 'Grammar = (): Grammar => loaded' , grammar . name , 'Grammar ||' , '(loaded' , grammar . name , 'Grammar = loadGrammar(`' , json , '`));' , NL ) ; if ( i < grammars . length - 1 ) { node . append ( NL ) ; } } return processGeneratorNode ( node ) ; }