Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/lu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"get-stdin": "^6.0.0",
"globby": "^10.0.1",
"intercept-stdout": "^0.1.2",
"lodash": "^4.17.15",
"lodash": "^4.17.19",
"node-fetch": "~2.6.0",
"semver": "^5.5.1",
"tslib": "^1.10.0"
Expand Down
19 changes: 19 additions & 0 deletions packages/lu/src/parser/lufile/baseSection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class BaseSection {
constructor(parameters) {
this.Errors = [];
this.SectionType = '';
this.Id = '';
this.Body = '';
this.Range;

if (parameters) {
this.Errors = parameters.Errors || [];
this.SectionType = parameters.SectionType || '';
this.Id = parameters.Id || '';
this.Body = parameters.Body || '';
this.Range = parameters.Range;
}
}
}

module.exports = BaseSection;
8 changes: 6 additions & 2 deletions packages/lu/src/parser/lufile/diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Diagnostic {
}

toString() {
// ignore error range if source is "inline"
if (this.Range === undefined) {
return `[${DiagnosticSeverity[this.Severity]}] ${this.Message.toString()}`;
}
Expand Down Expand Up @@ -78,8 +77,13 @@ const BuildDiagnostic =function(parameter) {
const severity = parameter.severity === undefined ? DiagnosticSeverity.ERROR : parameter.severity;

let range;
const rangeInput = parameter.range;
const context = parameter.context;
if (context !== undefined) {
if (rangeInput !== undefined) {
const startPosition = new Position(rangeInput.Start.Line, rangeInput.Start.Character);
const stopPosition = new Position(rangeInput.End.Line, rangeInput.End.Character);
range = new Range(startPosition, stopPosition);
} else if (context !== undefined) {
const startPosition = new Position(context.start.line, context.start.column);
const stopPosition = new Position(context.stop.line, context.stop.column + context.stop.text.length);
range = new Range(startPosition, stopPosition);
Expand Down
11 changes: 8 additions & 3 deletions packages/lu/src/parser/lufile/entitySection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@ const DiagnosticSeverity = require('./diagnostic').DiagnosticSeverity;
const BuildDiagnostic = require('./diagnostic').BuildDiagnostic;
const LUSectionTypes = require('./../utils/enums/lusectiontypes');
const InvalidCharsInIntentOrEntityName = require('./../utils/enums/invalidchars').InvalidCharsInIntentOrEntityName;
const BaseSection = require('./baseSection');
const Range = require('./diagnostic').Range;
const Position = require('./diagnostic').Position;

class EntitySection {
class EntitySection extends BaseSection {
/**
*
* @param {EntitySectionContext} parseTree
*/
constructor(parseTree) {
this.ParseTree = parseTree;
super();
this.SectionType = LUSectionTypes.ENTITYSECTION;
this.Errors = []
this.Name = this.ExtractName(parseTree);
this.Type = this.ExtractType(parseTree);
this.SynonymsOrPhraseList = this.ExtractSynonymsOrPhraseList(parseTree);
this.Id = `${this.SectionType}_${this.Name}`;
const startPosition = new Position(parseTree.start.line, parseTree.start.column);
const stopPosition = new Position(parseTree.stop.line, parseTree.stop.column + parseTree.stop.text.length);
this.Range = new Range(startPosition, stopPosition);
}

ExtractName(parseTree) {
Expand Down
12 changes: 9 additions & 3 deletions packages/lu/src/parser/lufile/importSection.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
const ImportSectionContext = require('./generated/LUFileParser').LUFileParser.ImportSectionContext;
const BuildDiagnostic = require('./diagnostic').BuildDiagnostic;
const LUSectionTypes = require('./../utils/enums/lusectiontypes');
const BaseSection = require('./baseSection');
const Range = require('./diagnostic').Range;
const Position = require('./diagnostic').Position;

class ImportSection {
class ImportSection extends BaseSection {
/**
*
* @param {ImportSectionContext} parseTree
*/
constructor(parseTree) {
this.ParseTree = parseTree;
super();
this.Errors = []
this.SectionType = LUSectionTypes.IMPORTSECTION;
let result = this.ExtractDescriptionAndPath(parseTree);
this.Description = result.description;
this.Path = result.path;
this.Id = `${this.SectionType}_${this.Path}`;;
this.Id = `${this.SectionType}_${this.Path}`;
const startPosition = new Position(parseTree.start.line, parseTree.start.column);
const stopPosition = new Position(parseTree.stop.line, parseTree.stop.column + parseTree.stop.text.length);
this.Range = new Range(startPosition, stopPosition);
}

ExtractDescriptionAndPath(parseTree) {
Expand Down
57 changes: 41 additions & 16 deletions packages/lu/src/parser/lufile/luParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,42 @@ const LUErrorListener = require('./luErrorListener');
const SectionType = require('./../utils/enums/lusectiontypes');
const DiagnosticSeverity = require('./diagnostic').DiagnosticSeverity;
const BuildDiagnostic = require('./diagnostic').BuildDiagnostic;
const Range = require('./diagnostic').Range;
const Position = require('./diagnostic').Position;
const NEWLINE = require('os').EOL;

class LUParser {

/**
* @param {string} text
*
* @param {string} text
* @param {LUResource} luResource
*/
static parse(text) {
static parseWithRef(text, luResource) {
Comment thread
feich-ms marked this conversation as resolved.
if (text === undefined || text === '') {
return new LUResource([], '', []);
}

let sections = [];
let content = text;
const sectionEnabled = luResource ? this.isSectionEnabled(luResource.Sections) : undefined;

return this.parse(text, sectionEnabled);
}

/**
* @param {string} text
*/
static parse(text, sectionEnabled) {
if (text === undefined || text === '') {
return new LUResource([], '', []);
}

let {fileContent, errors} = this.getFileContent(text);

return this.extractFileContent(fileContent, text, errors, sectionEnabled);
}

static extractFileContent(fileContent, content, errors, sectionEnabled) {
let sections = [];
try {
let modelInfoSections = this.extractModelInfoSections(fileContent);
modelInfoSections.forEach(section => errors = errors.concat(section.Errors));
Expand All @@ -41,7 +61,7 @@ class LUParser {
}

try {
let isSectionEnabled = this.isSectionEnabled(sections);
let isSectionEnabled = sectionEnabled === undefined ? this.isSectionEnabled(sections) : sectionEnabled;

let nestedIntentSections = this.extractNestedIntentSections(fileContent, content);
nestedIntentSections.forEach(section => errors = errors.concat(section.Errors));
Expand All @@ -50,12 +70,21 @@ class LUParser {
} else {
nestedIntentSections.forEach(section => {
let emptyIntentSection = new SimpleIntentSection();
emptyIntentSection.ParseTree = section.ParseTree.nestedIntentNameLine();
emptyIntentSection.Name = section.Name;
emptyIntentSection.Id = `${emptyIntentSection.SectionType}_${emptyIntentSection.Name}`

// get the end character index
const firstLine = content.split(/\r?\n/)[0];
let endCharacter = section.Name.length + 2;
Comment thread
feich-ms marked this conversation as resolved.
if (firstLine.includes(section.Name)) {
endCharacter = firstLine.length;
}
const range = new Range(section.Range.Start, new Position(section.Range.Start.Line, endCharacter))
emptyIntentSection.Range = range;
let errorMsg = `no utterances found for intent definition: "# ${emptyIntentSection.Name}"`
let error = BuildDiagnostic({
message: errorMsg,
context: emptyIntentSection.ParseTree,
range: emptyIntentSection.Range,
severity: DiagnosticSeverity.WARN
})

Expand Down Expand Up @@ -301,23 +330,24 @@ class LUParser {
* @param {string} content
*/
static extractSectionBody(sections, content) {
sections.sort((a, b) => a.ParseTree.start.line - b.ParseTree.start.line)
sections.sort((a, b) => a.Range.Start.Line - b.Range.Start.Line)
const originList = content.split(/\r?\n/)
let qnaSectionIndex = 0
sections.forEach(function (section, index) {
if (section.SectionType === SectionType.SIMPLEINTENTSECTION
|| section.SectionType === SectionType.NESTEDINTENTSECTION
|| section.SectionType === SectionType.QNASECTION) {
const startLine = section.ParseTree.start.line - 1
const startLine = section.Range.Start.Line - 1;
let stopLine
if (index + 1 < sections.length) {
stopLine = sections[index + 1].ParseTree.start.line - 1
if (isNaN(startLine) || isNaN(stopLine) || startLine < 0 || startLine >= stopLine || originList.Length <= stopLine) {
stopLine = sections[index + 1].Range.Start.Line - 1
if (isNaN(startLine) || isNaN(stopLine) || startLine < 0 || startLine > stopLine) {
throw new Error("index out of range.")
}
} else {
stopLine = originList.length
}
section.Range.End.Line = stopLine;

let destList
if (section.SectionType === SectionType.QNASECTION) {
Expand All @@ -329,15 +359,10 @@ class LUParser {
}

section.Body = destList.join(NEWLINE)
section.StartLine = startLine
section.StopLine = stopLine - 1

if (section.SectionType === SectionType.NESTEDINTENTSECTION) {
LUParser.extractSectionBody(section.SimpleIntentSections, originList.slice(0, stopLine).join(NEWLINE))
}
} else {
section.StartLine = section.ParseTree.start.line
section.StopLine = section.ParseTree.stop.line - 1
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions packages/lu/src/parser/lufile/luResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class LUResource {
* @param {any[]} errors
*/
constructor(sections, content, errors) {
this.Sections = sections;
this.Sections = sections || [];
this.Content = content;
this.Errors = errors;
this.Errors = errors || [];
}
}

Expand Down
10 changes: 8 additions & 2 deletions packages/lu/src/parser/lufile/modelInfoSection.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
const ModelInfoSectionContext = require('./generated/LUFileParser').LUFileParser.ModelInfoSectionContext;
const LUSectionTypes = require('./../utils/enums/lusectiontypes');
const BaseSection = require('./baseSection');
const Range = require('./diagnostic').Range;
const Position = require('./diagnostic').Position;

class LUModelInfo {
class LUModelInfo extends BaseSection {
/**
*
* @param {ModelInfoSectionContext} parseTree
*/
constructor(parseTree) {
this.ParseTree = parseTree;
super();
this.SectionType = LUSectionTypes.MODELINFOSECTION;
this.ModelInfo = parseTree.modelInfoDefinition().getText();
this.Errors = [];
this.Id = `${this.SectionType}_${this.ModelInfo}`;
const startPosition = new Position(parseTree.start.line, parseTree.start.column);
const stopPosition = new Position(parseTree.stop.line, parseTree.stop.column + parseTree.stop.text.length);
this.Range = new Range(startPosition, stopPosition);
}
}

Expand Down
14 changes: 10 additions & 4 deletions packages/lu/src/parser/lufile/nestedIntentSection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ const NestedIntentSectionContext = require('./generated/LUFileParser').LUFilePar
const SimpleIntentSection = require('./simpleIntentSection');
const LUSectionTypes = require('./../utils/enums/lusectiontypes');
const NEWLINE = require('os').EOL;
const BaseSection = require('./baseSection');
const Range = require('./diagnostic').Range;
const Position = require('./diagnostic').Position;

class NestedIntentSection {
class NestedIntentSection extends BaseSection {
/**
*
* @param {NestedIntentSectionContext} parseTree
*/
constructor(parseTree, content) {
this.ParseTree = parseTree;
super();
this.SectionType = LUSectionTypes.NESTEDINTENTSECTION;
this.Name = this.ExtractName(parseTree);
this.Body = this.ExtractBody(parseTree, content);
Expand All @@ -22,15 +25,18 @@ class NestedIntentSection {
}

this.Id = `${this.SectionType}_${this.Name}`;
const startPosition = new Position(parseTree.start.line, parseTree.start.column);
const stopPosition = new Position(parseTree.stop.line, parseTree.stop.column + parseTree.stop.text.length);
this.Range = new Range(startPosition, stopPosition);
}

ExtractName(parseTree) {
return parseTree.nestedIntentNameLine().nestedIntentName().getText().trim();
}

ExtractBody(parseTree, content) {
const startLine = parseTree.start.line - 1
const stopLine = parseTree.stop.line - 1
const startLine = parseTree.start.line - 1;
const stopLine = parseTree.stop.line - 1;
const originList = content.split(/\r?\n/)
if (isNaN(startLine) || isNaN(stopLine) || startLine < 0 || startLine > stopLine || originList.Length <= stopLine) {
throw new Error("index out of range.")
Expand Down
10 changes: 8 additions & 2 deletions packages/lu/src/parser/lufile/newEntitySection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ const DiagnosticSeverity = require('./diagnostic').DiagnosticSeverity;
const BuildDiagnostic = require('./diagnostic').BuildDiagnostic;
const LUSectionTypes = require('./../utils/enums/lusectiontypes');
const InvalidCharsInIntentOrEntityName = require('./../utils/enums/invalidchars').InvalidCharsInIntentOrEntityName;
const BaseSection = require('./baseSection');
const Range = require('./diagnostic').Range;
const Position = require('./diagnostic').Position;

class NewEntitySection {
class NewEntitySection extends BaseSection {
/**
*
* @param {NewEntitySectionContext} parseTree
*/
constructor(parseTree) {
this.ParseTree = parseTree;
super();
this.SectionType = LUSectionTypes.NEWENTITYSECTION;
this.Errors = []
this.Name = this.ExtractName(parseTree);
Expand All @@ -21,6 +24,9 @@ class NewEntitySection {
this.RegexDefinition = this.ExtractRegexDefinition(parseTree);
this.ListBody = this.ExtractSynonymsOrPhraseList(parseTree);
this.Id = `${this.SectionType}_${this.Name}`;
const startPosition = new Position(parseTree.start.line, parseTree.start.column);
const stopPosition = new Position(parseTree.stop.line, parseTree.stop.column + parseTree.stop.text.length);
this.Range = new Range(startPosition, stopPosition);
}

ExtractName(parseTree) {
Expand Down
Loading