Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port: Refactor LG Api #1740

Merged
merged 13 commits into from
Feb 20, 2020
2 changes: 1 addition & 1 deletion libraries/botbuilder-lg/src/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class Analyzer extends AbstractParseTreeVisitor<AnalyzerResult> implement
}

if (this.evalutationTargetStack.find((u: EvaluationTarget): boolean => u.templateName === templateName) !== undefined) {
throw new Error(`Loop deteced: ${ this.evalutationTargetStack.reverse()
throw new Error(`Loop detected: ${ this.evalutationTargetStack.reverse()
.map((u: EvaluationTarget): string => u.templateName)
.join(' => ') }`);
}
Expand Down
6 changes: 5 additions & 1 deletion libraries/botbuilder-lg/src/diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ export class Diagnostic {
public constructor(
range: Range,
message: string,
severity: DiagnosticSeverity = DiagnosticSeverity.Error) {
severity: DiagnosticSeverity = DiagnosticSeverity.Error,
source: string = undefined,
code: string = undefined) {
this.message = message;
this.range = range;
this.severity = severity;
this.source = source;
this.code = code;
}

public toString(): string {
Expand Down
4 changes: 2 additions & 2 deletions libraries/botbuilder-lg/src/errorListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Licensed under the MIT License.
*/
import { ANTLRErrorListener, RecognitionException, Recognizer } from 'antlr4ts';
import { Diagnostic } from './diagnostic';
import { Diagnostic, DiagnosticSeverity } from './diagnostic';
import { LGException } from './lgException';
import { Position } from './position';
import { Range } from './range';
Expand Down Expand Up @@ -35,7 +35,7 @@ export class ErrorListener implements ANTLRErrorListener<any> {
if (this.source !== undefined && this.source !== '') {
msg = `source: ${ this.source }, ${ msg }`;
}
const diagnostic: Diagnostic = new Diagnostic(range, msg);
const diagnostic: Diagnostic = new Diagnostic(range, msg, DiagnosticSeverity.Error, this.source);

throw new LGException(diagnostic.toString(), [diagnostic]);
}
Expand Down
9 changes: 4 additions & 5 deletions libraries/botbuilder-lg/src/evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* @module botbuilder-lg
*/
Expand All @@ -15,7 +14,7 @@ import { EvaluationTarget } from './evaluationTarget';
import * as lp from './generated/LGFileParser';
import { LGFileParserVisitor } from './generated/LGFileParserVisitor';
import { LGTemplate } from './lgTemplate';
import { ImportResolver } from './importResolver';
import { normalizePath } from './extensions';
import * as path from 'path';
import * as fs from 'fs';
import { LGExtensions } from './lgExtensions';
Expand Down Expand Up @@ -59,7 +58,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
}

if (this.evalutationTargetStack.find((u: EvaluationTarget): boolean => u.templateName === templateName) !== undefined) {
throw new Error(`Loop deteced: ${ this.evalutationTargetStack.reverse()
throw new Error(`Loop detected: ${ this.evalutationTargetStack.reverse()
.map((u: EvaluationTarget): string => u.templateName)
.join(' => ') }`);
}
Expand Down Expand Up @@ -441,7 +440,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
}

private readonly fromFile = (): any => (args: readonly any[]): any => {
const filePath: string = path.normalize(ImportResolver.normalizePath(args[0].toString()));
const filePath: string = normalizePath(args[0].toString());
const resourcePath: string = this.getResourcePath(filePath);
const stringContent = fs.readFileSync(resourcePath, 'utf-8');

Expand All @@ -460,7 +459,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
throw new Error('relative path is not support in browser.');
}
const template: LGTemplate = this.templateMap[this.currentTarget().templateName];
const sourcePath: string = path.normalize(ImportResolver.normalizePath(template.source));
const sourcePath: string = normalizePath(template.source);
let baseFolder: string = __dirname;
if (path.isAbsolute(sourcePath)){
baseFolder = path.dirname(sourcePath);
Expand Down
2 changes: 1 addition & 1 deletion libraries/botbuilder-lg/src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Expander extends AbstractParseTreeVisitor<string[]> implements LGFi
}

if (this.evalutationTargetStack.find((u: EvaluationTarget): boolean => u.templateName === templateName)) {
throw new Error(`Loop deteced: ${ this.evalutationTargetStack.reverse()
throw new Error(`Loop detected: ${ this.evalutationTargetStack.reverse()
.map((u: EvaluationTarget): string => u.templateName)
.join(' => ') }`);
}
Expand Down
28 changes: 28 additions & 0 deletions libraries/botbuilder-lg/src/extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

/**
* @module botbuilder-lg
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

/// <summary>
/// Normalize authored path to os path.
/// </summary>
/// <remarks>
/// path is from authored content which doesn't know what OS it is running on.
/// This method treats / and \ both as seperators regardless of OS, for windows that means / -> \ and for linux/mac \ -> /.
/// This allows author to use ../foo.lg or ..\foo.lg as equivelents for importing.
/// </remarks>
/// <param name="ambiguousPath">authoredPath.</param>
/// <returns>path expressed as OS path.</returns>
export function normalizePath(ambiguousPath: string): string {
if (process.platform === 'win32') {
// map linux/mac sep -> windows
return ambiguousPath.replace(/\//g, '\\');
} else {
// map windows sep -> linux/mac
return ambiguousPath.replace(/\\/g, '/');
}
}
54 changes: 0 additions & 54 deletions libraries/botbuilder-lg/src/importResolver.ts

This file was deleted.

4 changes: 1 addition & 3 deletions libraries/botbuilder-lg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
export * from './templateEngine';
export * from './lgFile';
export * from './evaluator';
export * from './lgParser';
export * from './generated';
Expand All @@ -15,8 +15,6 @@ export * from './mslgTool';
export * from './lgTemplate';
export * from './diagnostic';
export * from './lgException';
export * from './lgResource';
export * from './importResolver';
export * from './extractor';
export * from './lgImport';
export * from './range';
Expand Down
72 changes: 72 additions & 0 deletions libraries/botbuilder-lg/src/lgErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export class LGErrors
{
public static noTemplate = 'File must have at least one template definition';

public static invalidTemplateName = 'Not a valid template name line';

public static invalidTemplateBody = 'Invalid template body line, did you miss "-" at line begin';

public static invalidStrucName = 'structured name format error.';

public static missingStrucEnd = 'structured LG missing ending "]"';

public static emptyStrucContent = 'Structured content is empty';

public static invalidStrucBody = 'structured body format error.';

public static invalidWhitespaceInCondition = 'At most 1 whitespace is allowed between IF/ELSEIF/ELSE and :';

public static notStartWithIfInCondition = 'condition is not start with if';

public static multipleIfInCondition = 'condition can not have more than one if';

public static notEndWithElseInCondition = 'condition is not end with else';

public static invalidMiddleInCondition = 'only elseif is allowed in middle of condition';

public static invalidExpressionInCondition = 'if and elseif should followed by one valid expression';

public static extraExpressionInCondition = 'else should not followed by any expression';

public static missingTemplateBodyInCondition = 'no normal template body in condition block';

public static invalidWhitespaceInSwitchCase = 'At most 1 whitespace is allowed between SWITCH/CASE/DEFAULT and :.';

public static notStartWithSwitchInSwitchCase = 'control flow is not starting with switch';

public static multipleSwithStatementInSwitchCase = 'control flow can not have more than one switch statement';

public invalidStatementInMiddlerOfSwitchCase = 'only case statement is allowed in the middle of control flow';

public static notEndWithDefaultInSwitchCase = 'control flow is not ending with default statement';

public static missingCaseInSwitchCase = 'control flow should have at least one case statement';

public static invalidExpressionInSwiathCase = 'switch and case should followed by one valid expression';

public static extraExpressionInSwitchCase = 'default should not followed by any expression or any text';

public static missingTemplateBodyInSwitchCase = 'no normal template body in case or default block';

public static noEndingInMultiline = 'Close ``` is missing';

public static loopDetected = 'Loop detected:';

public static duplicatedTemplateInSameTemplate = (templateName: string): string => `Duplicated definitions found for template: ${ templateName }`;

public static duplicatedTemplateInDiffTemplate = (templateName: string, source: string): string => `Duplicated definitions found for template: ${ templateName } in ${ source }`;

public static noTemplateBody = (templateName: string): string => `There is no template body in template ${ templateName }`;

public static templateNotExist = (templateName: string): string => `No such template ${ templateName }`;

public static errorExpression = (expression: string, error: string): string => `Error occurs when evaluating expression ${ expression }: ${ error }`;

public static nullExpression = (expression: string): string => `Error occurs when evaluating expression ${ expression }: ${ expression } is evaluated to null`;

public static argumentMismatch = (templateName: string,expectedCount: number, actualCount: number): string => `arguments mismatch for template ${ templateName }, expect ${ expectedCount } actual ${ actualCount }`;

public static errorTemplateNameformat = (templateName: string): string => `${ templateName } can't be used as a template name, must be a string value`;

public static templateExist = (templateName: string): string => `template ${ templateName } already exists.`;
}
Loading