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

Simple syntax error reporting. #1955

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/GDLLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,33 @@

#include <cstdlib>

#include <fstream>
#include <iostream>
#include <string>

static void printLineErrorHelper(std::string filename, int line, int col) {
if (filename.size() > 0) {
std::ifstream ifs;
ifs.open(filename, std::ifstream::in);
int linenum = 0;
std::string str;
while (std::getline(ifs, str)) {
linenum++;
if (linenum == line) {
std::cerr << std::endl << str << std::endl; //skip one line, print line
break;
}
}
ifs.close();
}
for (auto i = 0; i < col; ++i) std::cerr << ' ';
std::cerr << '^';
std::cerr << '\n';
std::cerr << "% Syntax error.\n";
if ( filename.size() > 0) std::cerr <<" At: "<<filename<<", Line "<<line<<std::endl;
return;
}

GDLLexer::GDLLexer(std::istream& in)
: antlr::CharScanner(new antlr::CharBuffer(in),false)
{
Expand Down
50 changes: 34 additions & 16 deletions src/GDLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@

#include <cstdlib>

#include <fstream>
#include <iostream>
#include <string>

static void printLineErrorHelper(std::string filename, int line, int col) {
if (filename.size() > 0) {
std::ifstream ifs;
ifs.open(filename, std::ifstream::in);
int linenum = 0;
std::string str;
while (std::getline(ifs, str)) {
linenum++;
if (linenum == line) {
std::cerr << std::endl << str << std::endl; //skip one line, print line
break;
}
}
ifs.close();
}
for (auto i = 0; i < col; ++i) std::cerr << ' ';
std::cerr << '^';
std::cerr << '\n';
std::cerr << "% Syntax error.\n";
if ( filename.size() > 0) std::cerr <<" At: "<<filename<<", Line "<<line<<std::endl;
return;
}

GDLParser::GDLParser(antlr::TokenBuffer& tokenBuf, int k)
: antlr::LLkParser(tokenBuf,k)
{
Expand Down Expand Up @@ -869,9 +896,8 @@ void GDLParser::translation_unit() {
}
catch ( antlr::NoViableAltException& e) {
if (inputState->guessing==0) {

// PARSER SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Parser syntax error: "+e.getMessage());

printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());

} else {
throw;
Expand All @@ -880,8 +906,7 @@ void GDLParser::translation_unit() {
catch ( antlr::NoViableAltForCharException& e) {
if (inputState->guessing==0) {

// LEXER SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Lexer syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());

} else {
throw;
Expand All @@ -890,8 +915,7 @@ void GDLParser::translation_unit() {
catch ( antlr::RecognitionException& e) {
if (inputState->guessing==0) {

// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Lexer/Parser syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());

} else {
throw;
Expand Down Expand Up @@ -1633,9 +1657,7 @@ void GDLParser::interactive() {
catch ( antlr::NoViableAltException& e) {
if (inputState->guessing==0) {

// PARSER SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Parser syntax error: "+
e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());

} else {
throw;
Expand All @@ -1644,9 +1666,7 @@ void GDLParser::interactive() {
catch ( antlr::NoViableAltForCharException& e) {
if (inputState->guessing==0) {

// LEXER SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Lexer syntax error: "+
e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());

} else {
throw;
Expand All @@ -1655,9 +1675,7 @@ void GDLParser::interactive() {
catch ( antlr::RecognitionException& e) {
if (inputState->guessing==0) {

// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(),
"Lexer/Parser syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());

} else {
throw;
Expand Down
38 changes: 30 additions & 8 deletions src/GDLTreeParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@
#include <memory>

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

static void printLineErrorHelper(std::string filename, int line, int col) {
if (filename.size() > 0) {
std::ifstream ifs;
ifs.open(filename, std::ifstream::in);
int linenum = 0;
std::string str;
while (std::getline(ifs, str)) {
linenum++;
if (linenum == line) {
std::cerr << std::endl << str << std::endl; //skip one line, print line
break;
}
}
ifs.close();
}
for (auto i = 0; i < col; ++i) std::cerr << ' ';
std::cerr << '^';
std::cerr << '\n';
std::cerr << "% Syntax error.\n";
if ( filename.size() > 0) std::cerr <<" At: "<<filename<<", Line "<<line<<std::endl;
return;
}

// ****
#include "print_tree.hpp"
Expand Down Expand Up @@ -186,14 +212,12 @@ void GDLTreeParser::translation_unit(RefDNode _t) {
}
catch ( antlr::NoViableAltException& e) {

// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Compiler syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());

}
catch ( antlr::RecognitionException& e) {

// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "General syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());

}
returnAST = translation_unit_AST;
Expand Down Expand Up @@ -946,14 +970,12 @@ void GDLTreeParser::interactive(RefDNode _t) {
}
catch ( antlr::NoViableAltException& e) {

// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Compiler syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());

}
catch ( antlr::RecognitionException& e) {

// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "General syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());

}
returnAST = interactive_AST;
Expand Down
56 changes: 37 additions & 19 deletions src/gdlc.g
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ header "post_include_cpp" {
#include <errno.h>

#include <cstdlib>

#include <fstream>
#include <iostream>
#include <string>

static void printLineErrorHelper(std::string filename, int line, int col) {
if (filename.size() > 0) {
std::ifstream ifs;
ifs.open(filename, std::ifstream::in);
int linenum = 0;
std::string str;
while (std::getline(ifs, str)) {
linenum++;
if (linenum == line) {
std::cerr << std::endl << str << std::endl; //skip one line, print line
break;
}
}
ifs.close();
}
for (auto i = 0; i < col; ++i) std::cerr << ' ';
std::cerr << '^';
std::cerr << '\n';
std::cerr << "% Syntax error.\n";
if ( filename.size() > 0) std::cerr <<" At: "<<filename<<", Line "<<line<<std::endl;
return;
}
}

header {
Expand Down Expand Up @@ -308,20 +335,17 @@ translation_unit
throw;
}
catch [ antlr::NoViableAltException& e]
{
// PARSER SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Parser syntax error: "+e.getMessage());
{
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());
}
catch [ antlr::NoViableAltForCharException& e]
{
// LEXER SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Lexer syntax error: "+e.getMessage());
}
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());
}
catch [ antlr::RecognitionException& e]
{
// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Lexer/Parser syntax error: "+e.getMessage());
}
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());
}
catch [ antlr::TokenStreamIOException& e]
{
// IO ERROR
Expand Down Expand Up @@ -364,21 +388,15 @@ interactive
}
catch [ antlr::NoViableAltException& e]
{
// PARSER SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Parser syntax error: "+
e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());
}
catch [ antlr::NoViableAltForCharException& e]
{
// LEXER SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Lexer syntax error: "+
e.getMessage());
}
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());
}
catch [ antlr::RecognitionException& e]
{
// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(),
"Lexer/Parser syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());
}
catch [ antlr::TokenStreamIOException& e]
{
Expand Down
38 changes: 30 additions & 8 deletions src/gdlc.tree.g
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ header "post_include_cpp" {
#include <memory>

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

static void printLineErrorHelper(std::string filename, int line, int col) {
if (filename.size() > 0) {
std::ifstream ifs;
ifs.open(filename, std::ifstream::in);
int linenum = 0;
std::string str;
while (std::getline(ifs, str)) {
linenum++;
if (linenum == line) {
std::cerr << std::endl << str << std::endl; //skip one line, print line
break;
}
}
ifs.close();
}
for (auto i = 0; i < col; ++i) std::cerr << ' ';
std::cerr << '^';
std::cerr << '\n';
std::cerr << "% Syntax error.\n";
if ( filename.size() > 0) std::cerr <<" At: "<<filename<<", Line "<<line<<std::endl;
return;
}

// ****
#include "print_tree.hpp"
Expand Down Expand Up @@ -236,13 +262,11 @@ translation_unit
}
catch [ antlr::NoViableAltException& e]
{
// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Compiler syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());
}
catch [ antlr::RecognitionException& e]
{
// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "General syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());
}
;

Expand All @@ -256,13 +280,11 @@ interactive
}
catch [ antlr::NoViableAltException& e]
{
// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "Compiler syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());
}
catch [ antlr::RecognitionException& e]
{
// SYNTAX ERROR
throw GDLException( e.getLine(), e.getColumn(), "General syntax error: "+e.getMessage());
printLineErrorHelper(e.getFilename(), e.getLine(), e.getColumn());
}
;

Expand Down
Loading