🚀 add colorful error info print.
This commit is contained in:
parent
27ceeb517d
commit
b022b25cea
4
main.cpp
4
main.cpp
|
@ -61,7 +61,7 @@ void logo()
|
||||||
<<" / \\/ / _` / __|/ _` | |\n"
|
<<" / \\/ / _` / __|/ _` | |\n"
|
||||||
<<" / /\\ / (_| \\__ \\ (_| | |\n"
|
<<" / /\\ / (_| \\__ \\ (_| | |\n"
|
||||||
<<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n"
|
<<" \\_\\ \\/ \\__,_|___/\\__,_|_|\n"
|
||||||
<<"nasal ver : "<<__nasver<<" ("<<__DATE__<<" "<<__TIME__<<")\n"
|
<<"version : "<<__nasver<<" ("<<__DATE__<<" "<<__TIME__<<")\n"
|
||||||
<<"c++ std : "<<__cplusplus<<"\n"
|
<<"c++ std : "<<__cplusplus<<"\n"
|
||||||
<<"thanks to : https://github.com/andyross/nasal\n"
|
<<"thanks to : https://github.com/andyross/nasal\n"
|
||||||
<<"code repo : https://github.com/ValKmjolnir/Nasal-Interpreter\n"
|
<<"code repo : https://github.com/ValKmjolnir/Nasal-Interpreter\n"
|
||||||
|
@ -135,7 +135,7 @@ i32 main(i32 argc,const char* argv[])
|
||||||
{
|
{
|
||||||
string s(argv[1]);
|
string s(argv[1]);
|
||||||
if(s=="-v" || s=="--version")
|
if(s=="-v" || s=="--version")
|
||||||
logo();
|
std::clog<<"nasal "<<__nasver<<" ("<<__DATE__<<" "<<__TIME__<<")\n";
|
||||||
else if(s=="-h" || s=="--help")
|
else if(s=="-h" || s=="--help")
|
||||||
help();
|
help();
|
||||||
else if(s[0]!='-')
|
else if(s[0]!='-')
|
||||||
|
|
4
nasal.h
4
nasal.h
|
@ -138,11 +138,7 @@ string rawstr(const string& str,const usize maxlen=0)
|
||||||
case '\v': ret+="\\v"; break;
|
case '\v': ret+="\\v"; break;
|
||||||
case '\f': ret+="\\f"; break;
|
case '\f': ret+="\\f"; break;
|
||||||
case '\r': ret+="\\r"; break;
|
case '\r': ret+="\\r"; break;
|
||||||
#ifdef _MSC_VER
|
|
||||||
case '\033':ret+="\\e";break;
|
case '\033':ret+="\\e";break;
|
||||||
#else
|
|
||||||
case '\e': ret+="\\e"; break;
|
|
||||||
#endif
|
|
||||||
case '\"': ret+="\\\"";break;
|
case '\"': ret+="\\\"";break;
|
||||||
case '\'': ret+="\\\'";break;
|
case '\'': ret+="\\\'";break;
|
||||||
case '\\': ret+="\\\\";break;
|
case '\\': ret+="\\\\";break;
|
||||||
|
|
29
nasal_err.h
29
nasal_err.h
|
@ -6,6 +6,10 @@
|
||||||
#include <sstream> // MSVC need this to use std::getline
|
#include <sstream> // MSVC need this to use std::getline
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h> // use SetConsoleTextAttribute
|
||||||
|
#endif
|
||||||
|
|
||||||
class fstreamline
|
class fstreamline
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
|
@ -45,18 +49,36 @@ class nasal_err:public fstreamline
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
u32 error;
|
u32 error;
|
||||||
|
void printstg(const char* stage)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
CONSOLE_SCREEN_BUFFER_INFO scrinfo;
|
||||||
|
GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE),&scrinfo);
|
||||||
|
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),0x03);
|
||||||
|
std::cerr<<"[";
|
||||||
|
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),0x0c);
|
||||||
|
std::cerr<<stage;
|
||||||
|
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),0x03);
|
||||||
|
std::cerr<<"] ";
|
||||||
|
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE),scrinfo.wAttributes);
|
||||||
|
#else
|
||||||
|
std::cerr<<"\033[36m[\033[91m"<<stage<<"\033[36m]\033[0m ";
|
||||||
|
#endif
|
||||||
|
}
|
||||||
public:
|
public:
|
||||||
nasal_err():error(0){}
|
nasal_err():error(0){}
|
||||||
void err(const char* stage,const string& info)
|
void err(const char* stage,const string& info)
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
std::cerr<<"["<<stage<<"] "<<info<<"\n";
|
printstg(stage);
|
||||||
|
std::cerr<<info<<"\n";
|
||||||
}
|
}
|
||||||
void err(const char* stage,u32 line,u32 column,const string& info)
|
void err(const char* stage,u32 line,u32 column,const string& info)
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
const string& code=res[line-1];
|
const string& code=res[line-1];
|
||||||
std::cerr<<"["<<stage<<"] "<<file<<":"<<line<<":"<<column<<" "<<info<<"\n"<<code<<"\n";
|
printstg(stage);
|
||||||
|
std::cerr<<file<<":"<<line<<":"<<column<<" "<<info<<"\n"<<code<<"\n";
|
||||||
for(i32 i=0;i<(i32)column-1;++i)
|
for(i32 i=0;i<(i32)column-1;++i)
|
||||||
std::cerr<<char(" \t"[code[i]=='\t']);
|
std::cerr<<char(" \t"[code[i]=='\t']);
|
||||||
std::cerr<<"^\n";
|
std::cerr<<"^\n";
|
||||||
|
@ -64,7 +86,8 @@ public:
|
||||||
void err(const char* stage,u32 line,const string& info)
|
void err(const char* stage,u32 line,const string& info)
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
std::cerr<<"["<<stage<<"] "<<file<<":"<<line<<" "<<info<<"\n"<<res[line-1]<<'\n';
|
printstg(stage);
|
||||||
|
std::cerr<<file<<":"<<line<<" "<<info<<"\n"<<res[line-1]<<'\n';
|
||||||
}
|
}
|
||||||
void chkerr(){if(error)std::exit(1);}
|
void chkerr(){if(error)std::exit(1);}
|
||||||
};
|
};
|
||||||
|
|
|
@ -298,11 +298,7 @@ string nasal_lexer::str_gen()
|
||||||
case '0': str+='\0'; break;
|
case '0': str+='\0'; break;
|
||||||
case 'a': str+='\a'; break;
|
case 'a': str+='\a'; break;
|
||||||
case 'b': str+='\b'; break;
|
case 'b': str+='\b'; break;
|
||||||
#ifdef _MSC_VER
|
|
||||||
case 'e': str+='\033'; break;
|
case 'e': str+='\033'; break;
|
||||||
#else
|
|
||||||
case 'e': str+='\e'; break;
|
|
||||||
#endif
|
|
||||||
case 't': str+='\t'; break;
|
case 't': str+='\t'; break;
|
||||||
case 'n': str+='\n'; break;
|
case 'n': str+='\n'; break;
|
||||||
case 'v': str+='\v'; break;
|
case 'v': str+='\v'; break;
|
||||||
|
|
|
@ -18,7 +18,7 @@ var prt=func(s,path){
|
||||||
foreach(var j;s)
|
foreach(var j;s)
|
||||||
print("\e[34m",j,"\e[0m");
|
print("\e[34m",j,"\e[0m");
|
||||||
if(unix.isdir(path~"/"~f)){
|
if(unix.isdir(path~"/"~f)){
|
||||||
println("\e[34m",i==last?" └─":" ├─","\e[0m\e[36m[",f,"]>\e[0m");
|
println("\e[34m",i==last?" └─":" ├─","\e[0m\e[33m[",f,"]\e[36m>\e[0m");
|
||||||
append(s,i==last?" ":" │ ");
|
append(s,i==last?" ":" │ ");
|
||||||
prt(s,path~"/"~f);
|
prt(s,path~"/"~f);
|
||||||
pop(s);
|
pop(s);
|
||||||
|
@ -32,5 +32,5 @@ var prt=func(s,path){
|
||||||
|
|
||||||
if(os.platform()=="windows")
|
if(os.platform()=="windows")
|
||||||
system("chcp 65001");
|
system("chcp 65001");
|
||||||
println("\e[36m[",unix.getcwd(),"]>\e[0m");
|
println("\e[33m[",unix.getcwd(),"]\e[36m>\e[0m");
|
||||||
prt([""],".");
|
prt([""],".");
|
Loading…
Reference in New Issue