2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2015-10-07 12:34:21 -04:00
|
|
|
export class ParseLocation {
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
|
|
|
public file: ParseSourceFile, public offset: number, public line: number,
|
|
|
|
public col: number) {}
|
2015-10-07 12:34:21 -04:00
|
|
|
|
2015-11-10 18:56:25 -05:00
|
|
|
toString(): string { return `${this.file.url}@${this.line}:${this.col}`; }
|
2015-10-07 12:34:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ParseSourceFile {
|
|
|
|
constructor(public content: string, public url: string) {}
|
|
|
|
}
|
|
|
|
|
2016-02-16 19:46:51 -05:00
|
|
|
export class ParseSourceSpan {
|
|
|
|
constructor(public start: ParseLocation, public end: ParseLocation) {}
|
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return this.start.file.content.substring(this.start.offset, this.end.offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 22:52:24 -04:00
|
|
|
export enum ParseErrorLevel {
|
|
|
|
WARNING,
|
|
|
|
FATAL
|
|
|
|
}
|
|
|
|
|
2015-10-07 12:34:21 -04:00
|
|
|
export abstract class ParseError {
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(
|
|
|
|
public span: ParseSourceSpan, public msg: string,
|
|
|
|
public level: ParseErrorLevel = ParseErrorLevel.FATAL) {}
|
2015-10-07 12:34:21 -04:00
|
|
|
|
|
|
|
toString(): string {
|
2016-02-16 19:46:51 -05:00
|
|
|
var source = this.span.start.file.content;
|
|
|
|
var ctxStart = this.span.start.offset;
|
2015-11-10 18:56:25 -05:00
|
|
|
if (ctxStart > source.length - 1) {
|
|
|
|
ctxStart = source.length - 1;
|
|
|
|
}
|
|
|
|
var ctxEnd = ctxStart;
|
|
|
|
var ctxLen = 0;
|
|
|
|
var ctxLines = 0;
|
|
|
|
|
|
|
|
while (ctxLen < 100 && ctxStart > 0) {
|
|
|
|
ctxStart--;
|
|
|
|
ctxLen++;
|
2016-06-08 19:38:52 -04:00
|
|
|
if (source[ctxStart] == '\n') {
|
2015-11-10 18:56:25 -05:00
|
|
|
if (++ctxLines == 3) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxLen = 0;
|
|
|
|
ctxLines = 0;
|
|
|
|
while (ctxLen < 100 && ctxEnd < source.length - 1) {
|
|
|
|
ctxEnd++;
|
|
|
|
ctxLen++;
|
2016-06-08 19:38:52 -04:00
|
|
|
if (source[ctxEnd] == '\n') {
|
2015-11-10 18:56:25 -05:00
|
|
|
if (++ctxLines == 3) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-16 19:46:51 -05:00
|
|
|
let context = source.substring(ctxStart, this.span.start.offset) + '[ERROR ->]' +
|
2016-06-08 19:38:52 -04:00
|
|
|
source.substring(this.span.start.offset, ctxEnd + 1);
|
2015-11-10 18:56:25 -05:00
|
|
|
|
2016-02-16 19:46:51 -05:00
|
|
|
return `${this.msg} ("${context}"): ${this.span.start}`;
|
2015-10-07 12:34:21 -04:00
|
|
|
}
|
|
|
|
}
|