<commit_before>﻿//﻿
// Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

///<reference path='typescript.ts' />

module TypeScript {
    export class ErrorReporter {
        public errorCallback: (minChar: number, charLen: number, message: string, fileName: string, lineMap: LineMap) => void = null;
        public locationInfo: LocationInfo = unknownLocationInfo;
        public lineCol = { line: 0, character: 0 };
        public hasErrors = false;
        public pushToErrorSink = false;
        public errorSink: string[] = [];

        constructor (public outfile: ITextWriter) { }

        public getCapturedErrors() { return this.errorSink; }
        public freeCapturedErrors() { this.errorSink = []; }
        public captureError(emsg: string) { this.errorSink[this.errorSink.length] = emsg; }

        public emitPrefix() {
            this.outfile.Write(this.locationInfo.fileName + "(" + this.lineCol.line + "," + this.lineCol.character + "): ");
        }

        public writePrefix(ast: AST): void {
            if (ast) {
                this.setError(ast);
            }
            else {
                this.lineCol.line = 0;
                this.lineCol.character = 0;
            }
            this.emitPrefix();
        }

        public writePrefixFromSym(symbol: Symbol): void {
            if (symbol && this.locationInfo.lineMap) {
                this.locationInfo.lineMap.fillLineAndCharacterFromPosition(symbol.location, this.lineCol);
                if (this.lineCol.line >= 0) {
                    this.lineCol.line++;
                }
            }
            else {
                this.lineCol.line = -1;
                this.lineCol.character = -1;
            }
            this.emitPrefix();
        }

        public setError(ast: AST) {
            if (ast) {
                // ast.flags |= ASTFlags.Error;
                if (this.locationInfo.lineMap) {
                    this.locationInfo.lineMap.fillLineAndCharacterFromPosition(ast.minChar, this.lineCol);
                    if (this.lineCol.line >= 0) {
                        this.lineCol.line++;
                    }
                }
            }
        }

        public reportError(ast: AST, message: string) {
            if (this.pushToErrorSink) {
                this.captureError(message);
                return;
            }

            this.hasErrors = true;
            if (ast && this.errorCallback) {
                var len = (ast.limChar - ast.minChar);
                this.errorCallback(ast.minChar, len, message, this.locationInfo.fileName, this.locationInfo.lineMap);
            }
            else {
                this.writePrefix(ast);
                this.outfile.WriteLine(message); // Right after the semi-colon
            }
        }

        public reportErrorFromSym(symbol: Symbol, message: string) {
            if (this.pushToErrorSink) {
                this.captureError(message);
                return;
            }

            this.hasErrors = true;
            if (this.errorCallback) {
                this.errorCallback(symbol.location, symbol.length, message, this.locationInfo.fileName, this.locationInfo.lineMap);
            }
            else {
                this.writePrefixFromSym(symbol);
                this.outfile.WriteLine(message);
            }
        }

        public duplicateIdentifier(ast: AST, name: string) {
            this.reportError(ast, "Duplicate identifier '" + name + "'");
        }

        public unresolvedSymbol(ast: AST, name: string) {
            this.reportError(ast, "The name '" + name + "' does not exist in the current scope");
        }

        public symbolDoesNotReferToAValue(ast: AST, name: string): void {
            this.reportError(ast, "The name '" + name + "' does not refer to a value");
        }

        public styleError(ast: AST, msg: string): void {
            var bkThrow = this.pushToErrorSink;
            this.pushToErrorSink = false;
            this.reportError(ast, "STYLE: " + msg);
            this.pushToErrorSink = bkThrow;
        }

        public simpleError(ast: AST, msg: string): void {<commit_msg>More decoupling of location info.<commit_after>﻿//﻿
// Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

///<reference path='typescript.ts' />

module TypeScript {
    export class ErrorReporter {
        public errorCallback: (minChar: number, charLen: number, message: string, fileName: string, lineMap: LineMap) => void = null;
        // public locationInfo: LocationInfo = unknownLocationInfo;
        public lineCol = { line: 0, character: 0 };
        public hasErrors = false;
        public pushToErrorSink = false;
        public errorSink: string[] = [];

        constructor (public outfile: ITextWriter) { }

        public getCapturedErrors() { return this.errorSink; }
        public freeCapturedErrors() { this.errorSink = []; }
        public captureError(emsg: string) { this.errorSink[this.errorSink.length] = emsg; }

        public emitPrefix() {
            this.outfile.Write(/*this.locationInfo.fileName + */"(" + this.lineCol.line + "," + this.lineCol.character + "): ");
        }

        public writePrefix(ast: AST): void {
            if (ast) {
                this.setError(ast);
            }
            else {
                this.lineCol.line = 0;
                this.lineCol.character = 0;
            }
            this.emitPrefix();
        }

        public writePrefixFromSym(symbol: Symbol): void {
            if (symbol /*&& this.locationInfo.lineMap*/) {
                // this.locationInfo.lineMap.fillLineAndCharacterFromPosition(symbol.location, this.lineCol);
                if (this.lineCol.line >= 0) {
                    this.lineCol.line++;
                }
            }
            else {
                this.lineCol.line = -1;
                this.lineCol.character = -1;
            }
            this.emitPrefix();
        }

        public setError(ast: AST) {
            if (ast) {
                // ast.flags |= ASTFlags.Error;
                //if (this.locationInfo.lineMap) {
                //    this.locationInfo.lineMap.fillLineAndCharacterFromPosition(ast.minChar, this.lineCol);
                //    if (this.lineCol.line >= 0) {
                //        this.lineCol.line++;
                //    }
                //}
            }
        }

        public reportError(ast: AST, message: string) {
            if (this.pushToErrorSink) {
                this.captureError(message);
                return;
            }

            this.hasErrors = true;
            if (ast && this.errorCallback) {
                var len = (ast.limChar - ast.minChar);
                this.errorCallback(ast.minChar, len, message, null, null /*this.locationInfo.fileName, this.locationInfo.lineMap*/);
            }
            else {
                this.writePrefix(ast);
                this.outfile.WriteLine(message); // Right after the semi-colon
            }
        }

        public reportErrorFromSym(symbol: Symbol, message: string) {
            if (this.pushToErrorSink) {
                this.captureError(message);
                return;
            }

            this.hasErrors = true;
            if (this.errorCallback) {
                this.errorCallback(symbol.location, symbol.length, message, null, null /*this.locationInfo.fileName, this.locationInfo.lineMap*/);
            }
            else {
                this.writePrefixFromSym(symbol);
                this.outfile.WriteLine(message);
            }
        }

        public duplicateIdentifier(ast: AST, name: string) {
            this.reportError(ast, "Duplicate identifier '" + name + "'");
        }

        public unresolvedSymbol(ast: AST, name: string) {
            this.reportError(ast, "The name '" + name + "' does not exist in the current scope");
        }

        public symbolDoesNotReferToAValue(ast: AST, name: string): void {
            this.reportError(ast, "The name '" + name + "' does not refer to a value");
        }

        public styleError(ast: AST, msg: string): void {
            var bkThrow = this.pushToErrorSink;
            this.pushToErrorSink = false;
            this.reportError(ast, "STYLE: " + msg);
            this.pushToErrorSink = bkThrow;
        }

        public simpleError(ast: AST, msg: string): void {
{'fraction_non_alphanumeric': 0.10934204976803037, 'fraction_numerical': 0.0025305778152678194, 'mean_word_length': 1.7445023148148149, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5190992', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2461, 'n_words': 624}