Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit 7ded18e

Browse files
authored
fix lu parser error when invalid square brackets occur in utterances (#825)
* fix lu parser error when [] in utterances * add more corner cases
1 parent bac9de7 commit 7ded18e

File tree

11 files changed

+1120
-1068
lines changed

11 files changed

+1120
-1068
lines changed

packages/lu/src/parser/lufile/LUFileLexer.g4

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,8 @@ AT
7777
: '@' -> pushMode(NEW_ENTITY_MODE)
7878
;
7979

80-
IMPORT_DESC
81-
: '[' .*? ']'
82-
;
83-
84-
IMPORT_PATH
85-
: '(' .*? ')'
80+
IMPORT
81+
: '[' ~[\r\n[\]]*? ']' '(' ~[\r\n()]*? ')'
8682
;
8783

8884
FILTER_MARK

packages/lu/src/parser/lufile/LUFileParser.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ importSection
167167
;
168168

169169
importDefinition
170-
: IMPORT_DESC IMPORT_PATH
170+
: WS* IMPORT WS*
171171
;
172172

173173
qnaSection

packages/lu/src/parser/lufile/generated/LUFileLexer.interp

Lines changed: 3 additions & 6 deletions
Large diffs are not rendered by default.

packages/lu/src/parser/lufile/generated/LUFileLexer.js

Lines changed: 467 additions & 469 deletions
Large diffs are not rendered by default.

packages/lu/src/parser/lufile/generated/LUFileLexer.tokens

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,37 @@ HASH=7
88
DASH=8
99
DOLLAR=9
1010
AT=10
11-
IMPORT_DESC=11
12-
IMPORT_PATH=12
13-
FILTER_MARK=13
14-
QNA_ID_MARK=14
15-
MULTI_LINE_TEXT=15
16-
PROMPT_MARK=16
17-
INVALID_TOKEN_DEFAULT_MODE=17
18-
EQUAL=18
19-
COMMA=19
20-
HAS_ROLES_LABEL=20
21-
HAS_FEATURES_LABEL=21
22-
NEW_ENTITY_TYPE_IDENTIFIER=22
23-
NEW_COMPOSITE_ENTITY=23
24-
NEW_REGEX_ENTITY=24
25-
NEW_ENTITY_IDENTIFIER=25
26-
NEW_ENTITY_IDENTIFIER_WITH_WS=26
27-
NEWLINE_IN_NAME=27
28-
IDENTIFIER=28
29-
DOT=29
30-
ESCAPE_CHARACTER=30
31-
EXPRESSION=31
32-
TEXT=32
33-
NEWLINE_IN_ENTITY=33
34-
COMPOSITE_ENTITY=34
35-
REGEX_ENTITY=35
36-
ENTITY_TEXT=36
37-
COLON_MARK=37
38-
NEWLINE_IN_QNA=38
39-
QNA_TEXT=39
11+
IMPORT=11
12+
FILTER_MARK=12
13+
QNA_ID_MARK=13
14+
MULTI_LINE_TEXT=14
15+
PROMPT_MARK=15
16+
INVALID_TOKEN_DEFAULT_MODE=16
17+
EQUAL=17
18+
COMMA=18
19+
HAS_ROLES_LABEL=19
20+
HAS_FEATURES_LABEL=20
21+
NEW_ENTITY_TYPE_IDENTIFIER=21
22+
NEW_COMPOSITE_ENTITY=22
23+
NEW_REGEX_ENTITY=23
24+
NEW_ENTITY_IDENTIFIER=24
25+
NEW_ENTITY_IDENTIFIER_WITH_WS=25
26+
NEWLINE_IN_NAME=26
27+
IDENTIFIER=27
28+
DOT=28
29+
ESCAPE_CHARACTER=29
30+
EXPRESSION=30
31+
TEXT=31
32+
NEWLINE_IN_ENTITY=32
33+
COMPOSITE_ENTITY=33
34+
REGEX_ENTITY=34
35+
ENTITY_TEXT=35
36+
COLON_MARK=36
37+
NEWLINE_IN_QNA=37
38+
QNA_TEXT=38
4039
'$'=9
4140
'@'=10
42-
'='=18
43-
','=19
44-
'.'=29
45-
':'=37
41+
'='=17
42+
','=18
43+
'.'=28
44+
':'=36

packages/lu/src/parser/lufile/generated/LUFileParser.interp

Lines changed: 2 additions & 4 deletions
Large diffs are not rendered by default.

packages/lu/src/parser/lufile/generated/LUFileParser.js

Lines changed: 533 additions & 497 deletions
Large diffs are not rendered by default.

packages/lu/src/parser/lufile/generated/LUFileParser.tokens

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,37 @@ HASH=7
88
DASH=8
99
DOLLAR=9
1010
AT=10
11-
IMPORT_DESC=11
12-
IMPORT_PATH=12
13-
FILTER_MARK=13
14-
QNA_ID_MARK=14
15-
MULTI_LINE_TEXT=15
16-
PROMPT_MARK=16
17-
INVALID_TOKEN_DEFAULT_MODE=17
18-
EQUAL=18
19-
COMMA=19
20-
HAS_ROLES_LABEL=20
21-
HAS_FEATURES_LABEL=21
22-
NEW_ENTITY_TYPE_IDENTIFIER=22
23-
NEW_COMPOSITE_ENTITY=23
24-
NEW_REGEX_ENTITY=24
25-
NEW_ENTITY_IDENTIFIER=25
26-
NEW_ENTITY_IDENTIFIER_WITH_WS=26
27-
NEWLINE_IN_NAME=27
28-
IDENTIFIER=28
29-
DOT=29
30-
ESCAPE_CHARACTER=30
31-
EXPRESSION=31
32-
TEXT=32
33-
NEWLINE_IN_ENTITY=33
34-
COMPOSITE_ENTITY=34
35-
REGEX_ENTITY=35
36-
ENTITY_TEXT=36
37-
COLON_MARK=37
38-
NEWLINE_IN_QNA=38
39-
QNA_TEXT=39
11+
IMPORT=11
12+
FILTER_MARK=12
13+
QNA_ID_MARK=13
14+
MULTI_LINE_TEXT=14
15+
PROMPT_MARK=15
16+
INVALID_TOKEN_DEFAULT_MODE=16
17+
EQUAL=17
18+
COMMA=18
19+
HAS_ROLES_LABEL=19
20+
HAS_FEATURES_LABEL=20
21+
NEW_ENTITY_TYPE_IDENTIFIER=21
22+
NEW_COMPOSITE_ENTITY=22
23+
NEW_REGEX_ENTITY=23
24+
NEW_ENTITY_IDENTIFIER=24
25+
NEW_ENTITY_IDENTIFIER_WITH_WS=25
26+
NEWLINE_IN_NAME=26
27+
IDENTIFIER=27
28+
DOT=28
29+
ESCAPE_CHARACTER=29
30+
EXPRESSION=30
31+
TEXT=31
32+
NEWLINE_IN_ENTITY=32
33+
COMPOSITE_ENTITY=33
34+
REGEX_ENTITY=34
35+
ENTITY_TEXT=35
36+
COLON_MARK=36
37+
NEWLINE_IN_QNA=37
38+
QNA_TEXT=38
4039
'$'=9
4140
'@'=10
42-
'='=18
43-
','=19
44-
'.'=29
45-
':'=37
41+
'='=17
42+
','=18
43+
'.'=28
44+
':'=36

packages/lu/src/parser/lufile/importSection.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,37 @@ class ImportSection {
99
*/
1010
constructor(parseTree) {
1111
this.ParseTree = parseTree;
12+
this.Errors = []
1213
this.SectionType = LUSectionTypes.IMPORTSECTION;
13-
this.Description = this.ExtractDescription(parseTree);
14-
let result = this.ExtractPath(parseTree);
15-
this.Path = result.importPath;
16-
this.Errors = result.errors;
14+
let result = this.ExtractDescriptionAndPath(parseTree);
15+
this.Description = result.description;
16+
this.Path = result.path;
1717
this.Id = `${this.SectionType}_${this.Path}`;;
1818
}
1919

20-
ExtractDescription(parseTree) {
21-
return parseTree.importDefinition().IMPORT_DESC().getText();
22-
}
20+
ExtractDescriptionAndPath(parseTree) {
21+
let importRegex = new RegExp(/\[([^\]]*)\]\(([^\)]*)\)/);
22+
let importStr = parseTree.importDefinition().IMPORT().getText();
23+
24+
let description
25+
let path
2326

24-
ExtractPath(parseTree) {
25-
let errors = [];
26-
let importPath = parseTree.importDefinition().IMPORT_PATH().getText().replace('(', '').replace(')', '');
27-
if (importPath === undefined || importPath === '') {
28-
let errorMsg = `LU file reference path is empty: "${parseTree.getText()}"`;
29-
let error = BuildDiagnostic({
30-
message: errorMsg,
31-
context: parseTree
32-
})
27+
let groups = importStr.match(importRegex);
28+
if (groups && groups.length === 3) {
29+
description = groups[1].trim();
30+
path = groups[2].trim();
31+
if (path === undefined || path === '') {
32+
let errorMsg = `LU file reference path is empty: "${parseTree.getText()}"`;
33+
let error = BuildDiagnostic({
34+
message: errorMsg,
35+
context: parseTree
36+
})
3337

34-
errors.push(error);
38+
this.Errors.push(error);
39+
}
3540
}
3641

37-
return { importPath, errors };
42+
return { description, path }
3843
}
3944
}
4045

packages/lu/test/parser/lufile/qnaMultiTurnAndReferences.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('QnA document', function() {
7171
luMerger.Build([new luObj(qnaContent, new luOptions('stdin'))], false, undefined, findLuFiles)
7272
.then(res => done(res))
7373
.catch(err => {
74-
assert(err.text.includes("line 7:18 - line 7:19"))
74+
assert(err.text.includes("line 9:12 - line 9:15"))
7575
done()
7676
})
7777
})

0 commit comments

Comments
 (0)