@@ -14,22 +14,42 @@ const LUErrorListener = require('./luErrorListener');
1414const SectionType = require ( './../utils/enums/lusectiontypes' ) ;
1515const DiagnosticSeverity = require ( './diagnostic' ) . DiagnosticSeverity ;
1616const BuildDiagnostic = require ( './diagnostic' ) . BuildDiagnostic ;
17+ const Range = require ( './diagnostic' ) . Range ;
18+ const Position = require ( './diagnostic' ) . Position ;
1719const NEWLINE = require ( 'os' ) . EOL ;
1820
1921class LUParser {
22+
2023 /**
21- * @param {string } text
24+ *
25+ * @param {string } text
26+ * @param {LUResource } luResource
2227 */
23- static parse ( text ) {
28+ static parseWithRef ( text , luResource ) {
2429 if ( text === undefined || text === '' ) {
2530 return new LUResource ( [ ] , '' , [ ] ) ;
2631 }
2732
28- let sections = [ ] ;
29- let content = text ;
33+ const sectionEnabled = luResource ? this . isSectionEnabled ( luResource . Sections ) : undefined ;
34+
35+ return this . parse ( text , sectionEnabled ) ;
36+ }
37+
38+ /**
39+ * @param {string } text
40+ */
41+ static parse ( text , sectionEnabled ) {
42+ if ( text === undefined || text === '' ) {
43+ return new LUResource ( [ ] , '' , [ ] ) ;
44+ }
3045
3146 let { fileContent, errors} = this . getFileContent ( text ) ;
3247
48+ return this . extractFileContent ( fileContent , text , errors , sectionEnabled ) ;
49+ }
50+
51+ static extractFileContent ( fileContent , content , errors , sectionEnabled ) {
52+ let sections = [ ] ;
3353 try {
3454 let modelInfoSections = this . extractModelInfoSections ( fileContent ) ;
3555 modelInfoSections . forEach ( section => errors = errors . concat ( section . Errors ) ) ;
@@ -41,7 +61,7 @@ class LUParser {
4161 }
4262
4363 try {
44- let isSectionEnabled = this . isSectionEnabled ( sections ) ;
64+ let isSectionEnabled = sectionEnabled === undefined ? this . isSectionEnabled ( sections ) : sectionEnabled ;
4565
4666 let nestedIntentSections = this . extractNestedIntentSections ( fileContent , content ) ;
4767 nestedIntentSections . forEach ( section => errors = errors . concat ( section . Errors ) ) ;
@@ -50,12 +70,21 @@ class LUParser {
5070 } else {
5171 nestedIntentSections . forEach ( section => {
5272 let emptyIntentSection = new SimpleIntentSection ( ) ;
53- emptyIntentSection . ParseTree = section . ParseTree . nestedIntentNameLine ( ) ;
5473 emptyIntentSection . Name = section . Name ;
74+ emptyIntentSection . Id = `${ emptyIntentSection . SectionType } _${ emptyIntentSection . Name } `
75+
76+ // get the end character index
77+ const firstLine = content . split ( / \r ? \n / ) [ 0 ] ;
78+ let endCharacter = section . Name . length + 2 ;
79+ if ( firstLine . includes ( section . Name ) ) {
80+ endCharacter = firstLine . length ;
81+ }
82+ const range = new Range ( section . Range . Start , new Position ( section . Range . Start . Line , endCharacter ) )
83+ emptyIntentSection . Range = range ;
5584 let errorMsg = `no utterances found for intent definition: "# ${ emptyIntentSection . Name } "`
5685 let error = BuildDiagnostic ( {
5786 message : errorMsg ,
58- context : emptyIntentSection . ParseTree ,
87+ range : emptyIntentSection . Range ,
5988 severity : DiagnosticSeverity . WARN
6089 } )
6190
@@ -301,23 +330,24 @@ class LUParser {
301330 * @param {string } content
302331 */
303332 static extractSectionBody ( sections , content ) {
304- sections . sort ( ( a , b ) => a . ParseTree . start . line - b . ParseTree . start . line )
333+ sections . sort ( ( a , b ) => a . Range . Start . Line - b . Range . Start . Line )
305334 const originList = content . split ( / \r ? \n / )
306335 let qnaSectionIndex = 0
307336 sections . forEach ( function ( section , index ) {
308337 if ( section . SectionType === SectionType . SIMPLEINTENTSECTION
309338 || section . SectionType === SectionType . NESTEDINTENTSECTION
310339 || section . SectionType === SectionType . QNASECTION ) {
311- const startLine = section . ParseTree . start . line - 1
340+ const startLine = section . Range . Start . Line - 1 ;
312341 let stopLine
313342 if ( index + 1 < sections . length ) {
314- stopLine = sections [ index + 1 ] . ParseTree . start . line - 1
315- if ( isNaN ( startLine ) || isNaN ( stopLine ) || startLine < 0 || startLine >= stopLine || originList . Length <= stopLine ) {
343+ stopLine = sections [ index + 1 ] . Range . Start . Line - 1
344+ if ( isNaN ( startLine ) || isNaN ( stopLine ) || startLine < 0 || startLine > stopLine ) {
316345 throw new Error ( "index out of range." )
317346 }
318347 } else {
319348 stopLine = originList . length
320349 }
350+ section . Range . End . Line = stopLine ;
321351
322352 let destList
323353 if ( section . SectionType === SectionType . QNASECTION ) {
@@ -329,15 +359,10 @@ class LUParser {
329359 }
330360
331361 section . Body = destList . join ( NEWLINE )
332- section . StartLine = startLine
333- section . StopLine = stopLine - 1
334362
335363 if ( section . SectionType === SectionType . NESTEDINTENTSECTION ) {
336364 LUParser . extractSectionBody ( section . SimpleIntentSections , originList . slice ( 0 , stopLine ) . join ( NEWLINE ) )
337365 }
338- } else {
339- section . StartLine = section . ParseTree . start . line
340- section . StopLine = section . ParseTree . stop . line - 1
341366 }
342367 } )
343368 }
0 commit comments