summaryrefslogtreecommitdiffstats
path: root/scilab/modules/ast/src/cpp/parse/printerror.cpp
blob: 590d14d07a39e0b3feeaa6bca64e40954833119e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2007-2008 - INRIA - Bruno JOFRET
 *
 *  This file must be used under the terms of the CeCILL.
 *  This source file is licensed as described in the file COPYING, which
 *  you should have received as part of this distribution.  The terms
 *  are also available at
 *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 */

#include <fstream>
#include <string>
#include "parser_private.hxx"
#include "configvariable.hxx"

extern "C"
{
#include "charEncoding.h"
}

void ParserSingleInstance::PrintError(const std::string& msg)
{
    std::ostringstream ostr;
    char *codeLine = (char *) malloc(4096 * sizeof(char));

    //If the error is a the very beginning of a line
    if (yylloc.first_line == yylloc.last_line
            && yylloc.first_column == 1
            && yylloc.last_column == 1)
    {
        --yylloc.first_line;
    }

    // fill call stack
    ConfigVariable::fillWhereError(yylloc.first_line);

    /** Print where in the script the error is located */
    char* str = ParserSingleInstance::getCodeLine(yylloc.first_line, &codeLine);
    ostr << str;
    // add EOL only if the code line doesn't already contains it.
    if (strcmp(str + strlen(str) - 1, "\n") != 0)
    {
        ostr << std::endl;
    }
    free(codeLine);
    FREE(str);

    /** Underline what causes the trouble */
    int i = 0;
    for (i = 1 ; i < yylloc.first_column ; ++i)
    {
        ostr << " ";
    }
    ostr << "^";
    for (i = i + 1 ; i < yylloc.last_column ; ++i)
    {
        ostr << "~";
    }
    if ( yylloc.first_column != yylloc.last_column )
    {
        ostr << "^" ;
    }
    ostr << std::endl;

    /** Display Parser message  */
    ostr << _("Error: ") << msg << std::endl;

    ParserSingleInstance::appendErrorMessage(ostr.str());
}