From 7a8bd55597f4f796f8f55723dd21104ce9d1a579 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 30 Oct 2014 15:58:51 -0400 Subject: [PATCH] Added location information to error messages --- source/sclpl/grammar.c | 4 ++-- source/sclpl/main.c | 3 ++- source/sclpl/parser.c | 2 ++ spec/parser_spec.rb | 12 ++++++------ 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/source/sclpl/grammar.c b/source/sclpl/grammar.c index 4beecf7..7b63a5c 100644 --- a/source/sclpl/grammar.c +++ b/source/sclpl/grammar.c @@ -24,7 +24,7 @@ tree_t* grammar_toplevel(parser_t* p) grammar_expression(p); p_tree = parser_get_tree(p); } catch(ParseException) { - fprintf(stderr, "Invalid Syntax\n"); + /* Do nothing, the tree is bad */ } return p_tree; } @@ -136,7 +136,7 @@ void grammar_literal(parser_t* p) break; default: - parser_error(p, "Not a valid expression"); + parser_error(p, "Expected a literal"); } } diff --git a/source/sclpl/main.c b/source/sclpl/main.c index 9b77876..91aef76 100644 --- a/source/sclpl/main.c +++ b/source/sclpl/main.c @@ -266,7 +266,8 @@ str_t* syntax_file(str_t* in) { fclose(output); } else { fclose(output); - remove(str_cstr(ofname)); + if (NULL != ofname) + remove(str_cstr(ofname)); mem_release(ofname); ofname = NULL; } diff --git a/source/sclpl/parser.c b/source/sclpl/parser.c index 3d08830..5930d18 100644 --- a/source/sclpl/parser.c +++ b/source/sclpl/parser.c @@ -59,6 +59,8 @@ void parser_resume(parser_t* p_parser) { void parser_error(parser_t* p_parser, const char* p_text) { (void)p_parser; + lex_tok_t* tok = parser_peek(p_parser); + fprintf(stderr, ":%zu:%zu:Error: %s\n", tok->line, tok->col, p_text); throw_msg(ParseException, p_text); } diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index 89b2542..a7262cd 100644 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -11,19 +11,19 @@ describe "sclpl grammar" do end it "should error on missing semicolon" do - expect{ast('require "foo"')}.to raise_error /Invalid Syntax/ + expect{ast('require "foo"')}.to raise_error /Error/ end it "should error on missing filename" do - expect{ast('require ;')}.to raise_error /Invalid Syntax/ + expect{ast('require ;')}.to raise_error /Error/ end it "should error on invalid filename type" do - expect{ast('require 123;')}.to raise_error /Invalid Syntax/ + expect{ast('require 123;')}.to raise_error /Error/ end it "should error on too many parameters" do - expect{ast('require foo bar;')}.to raise_error /Invalid Syntax/ + expect{ast('require foo bar;')}.to raise_error /Error/ end end @@ -216,11 +216,11 @@ describe "sclpl grammar" do context "corner cases" do it "an unexpected terminator should error" do - expect{ast(';')}.to raise_error /Invalid Syntax/ + expect{ast(';')}.to raise_error /Error/ end it "an invalid literal should error" do - expect{ast('\'')}.to raise_error /Invalid Syntax/ + expect{ast('\'')}.to raise_error /Error/ end end end -- 2.52.0