@@ -19,48 +19,65 @@ const Range = require('./diagnostic').Range;
1919const Position = require ( './diagnostic' ) . Position ;
2020const NEWLINE = require ( 'os' ) . EOL ;
2121
22+ const defaultConfig = {
23+ enableModelDescription : true ,
24+ enableComments : true // Temporarily enabled by default, cannot be configured
25+ }
26+
2227class LUParser {
2328
2429 /**
25- *
26- * @param {string } text
27- * @param {LUResource } luResource
30+ *
31+ * @param {string } text
32+ * @param {LUResource } luResource
2833 */
29- static parseWithRef ( text , luResource ) {
34+ static parseWithRef ( text , luResource , config ) {
35+ config = config || { } ;
36+ config = { ...defaultConfig , ...config } ;
3037 if ( text === undefined || text === '' ) {
3138 return new LUResource ( [ ] , '' , [ ] ) ;
3239 }
3340
3441 const sectionEnabled = luResource ? this . isSectionEnabled ( luResource . Sections ) : undefined ;
3542
36- return this . parse ( text , sectionEnabled ) ;
43+ return this . parse ( text , sectionEnabled , config ) ;
3744 }
3845
3946 /**
4047 * @param {string } text
4148 */
42- static parse ( text , sectionEnabled ) {
49+ static parse ( text , sectionEnabled , config ) {
50+ config = config || { } ;
51+ config = { ...defaultConfig , ...config } ;
4352 if ( text === undefined || text === '' ) {
4453 return new LUResource ( [ ] , '' , [ ] ) ;
4554 }
4655
4756 let { fileContent, errors} = this . getFileContent ( text ) ;
4857
49- return this . extractFileContent ( fileContent , text , errors , sectionEnabled ) ;
58+ return this . extractFileContent ( fileContent , text , errors , sectionEnabled , config ) ;
5059 }
5160
52- static extractFileContent ( fileContent , content , errors , sectionEnabled ) {
61+ static extractFileContent ( fileContent , content , errors , sectionEnabled , config ) {
5362 let sections = [ ] ;
63+ let modelInfoSections = [ ] ;
64+
5465 try {
55- let modelInfoSections = this . extractModelInfoSections ( fileContent ) ;
56- modelInfoSections . forEach ( section => errors = errors . concat ( section . Errors ) ) ;
57- sections = sections . concat ( modelInfoSections ) ;
66+ modelInfoSections = this . extractModelInfoSections ( fileContent ) ;
5867 } catch ( err ) {
5968 errors . push ( BuildDiagnostic ( {
6069 message : `Error happened when parsing model information: ${ err . message } `
6170 } ) )
6271 }
6372
73+ if ( modelInfoSections && modelInfoSections . length > 0 && ! config . enableModelDescription ) {
74+ errors . push ( BuildDiagnostic ( {
75+ message : `Do not support Model Description. Please make sure enableModelDescription is set to true.`
76+ } ) )
77+ }
78+ modelInfoSections . forEach ( section => errors = errors . concat ( section . Errors ) ) ;
79+ sections = sections . concat ( modelInfoSections ) ;
80+
6481 try {
6582 let isSectionEnabled = sectionEnabled === undefined ? this . isSectionEnabled ( sections ) : sectionEnabled ;
6683
@@ -73,12 +90,12 @@ class LUParser {
7390 let emptyIntentSection = new SimpleIntentSection ( ) ;
7491 emptyIntentSection . Name = section . Name ;
7592 emptyIntentSection . Id = `${ emptyIntentSection . SectionType } _${ emptyIntentSection . Name } `
76-
93+
7794 // get the end character index
7895 // this is default value
7996 // it will be reset in function extractSectionBody()
8097 let endCharacter = section . Name . length + 2 ;
81-
98+
8299 const range = new Range ( section . Range . Start , new Position ( section . Range . Start . Line , endCharacter ) )
83100 emptyIntentSection . Range = range ;
84101 let errorMsg = `no utterances found for intent definition: "# ${ emptyIntentSection . Name } "`
@@ -177,7 +194,7 @@ class LUParser {
177194 if ( text === undefined
178195 || text === ''
179196 || text === null ) {
180-
197+
181198 return undefined ;
182199 }
183200
@@ -191,13 +208,13 @@ class LUParser {
191208 parser . addErrorListener ( listener ) ;
192209 parser . buildParseTrees = true ;
193210 const fileContent = parser . file ( ) ;
194-
211+
195212 return { fileContent, errors } ;
196213 }
197214
198215 /**
199216 * @param {FileContext } fileContext
200- * @param {string } content
217+ * @param {string } content
201218 */
202219 static extractNestedIntentSections ( fileContext , content ) {
203220 if ( fileContext === undefined
@@ -215,8 +232,8 @@ class LUParser {
215232 }
216233
217234 /**
218- * @param {FileContext } fileContext
219- * @param {string } content
235+ * @param {FileContext } fileContext
236+ * @param {string } content
220237 */
221238 static extractSimpleIntentSections ( fileContext , content ) {
222239 if ( fileContext === undefined
@@ -234,7 +251,7 @@ class LUParser {
234251 }
235252
236253 /**
237- * @param {FileContext } fileContext
254+ * @param {FileContext } fileContext
238255 */
239256 static extractEntitiesSections ( fileContext ) {
240257 if ( fileContext === undefined
@@ -252,7 +269,7 @@ class LUParser {
252269 }
253270
254271 /**
255- * @param {FileContext } fileContext
272+ * @param {FileContext } fileContext
256273 */
257274 static extractNewEntitiesSections ( fileContext ) {
258275 if ( fileContext === undefined
@@ -263,14 +280,14 @@ class LUParser {
263280 let newEntitySections = fileContext . paragraph ( )
264281 . map ( x => x . newEntitySection ( ) )
265282 . filter ( x => x && x . newEntityDefinition ( ) ) ;
266-
283+
267284 let newEntitySectionList = newEntitySections . map ( x => new NewEntitySection ( x ) ) ;
268285
269286 return newEntitySectionList ;
270287 }
271288
272289 /**
273- * @param {FileContext } fileContext
290+ * @param {FileContext } fileContext
274291 */
275292 static extractImportSections ( fileContext ) {
276293 if ( fileContext === undefined
@@ -288,7 +305,7 @@ class LUParser {
288305 }
289306
290307 /**
291- * @param {FileContext } fileContext
308+ * @param {FileContext } fileContext
292309 */
293310 static extractReferenceSections ( fileContext ) {
294311 if ( fileContext === undefined
@@ -306,7 +323,7 @@ class LUParser {
306323 }
307324
308325 /**
309- * @param {FileContext } fileContext
326+ * @param {FileContext } fileContext
310327 */
311328 static extractQnaSections ( fileContext ) {
312329 if ( fileContext === undefined
@@ -324,7 +341,7 @@ class LUParser {
324341 }
325342
326343 /**
327- * @param {FileContext } fileContext
344+ * @param {FileContext } fileContext
328345 */
329346 static extractModelInfoSections ( fileContext ) {
330347 if ( fileContext === undefined
@@ -342,7 +359,7 @@ class LUParser {
342359 }
343360
344361 /**
345- * @param {any[] } sections
362+ * @param {any[] } sections
346363 */
347364 static reconstractIntentSections ( sections ) {
348365 let newSections = [ ]
@@ -363,7 +380,7 @@ class LUParser {
363380 simpleIntentSections [ simpleIntentSections . length - 1 ] . Errors . push ( ...sections [ index + 1 ] . Errors )
364381 index ++
365382
366- while ( index + 1 < sections . length
383+ while ( index + 1 < sections . length
367384 && ( sections [ index + 1 ] . SectionType === SectionType . ENTITYSECTION
368385 || sections [ index + 1 ] . SectionType === SectionType . NEWENTITYSECTION
369386 || ( sections [ index + 1 ] . SectionType === SectionType . SIMPLEINTENTSECTION && sections [ index + 1 ] . IntentNameLine . includes ( '##' ) ) ) ) {
@@ -459,4 +476,4 @@ class LUParser {
459476 }
460477}
461478
462- module . exports = LUParser ;
479+ module . exports = LUParser ;
0 commit comments