[PATCH setup 2/7] Pull yyerror() handling up into IniParseFeedback
Jon Turney
jon.turney@dronecode.org.uk
Sat Feb 15 17:04:00 GMT 2020
Pull yyerror() handling up into IniParseFeedback
Collect the parser errors in IniParseFeedback
Make IniParseFeedback an abstract base class
---
IniParseFeedback.cc | 24 ------------------------
IniParseFeedback.h | 13 +++++++------
Makefile.am | 2 --
ini.cc | 44 +++++++++++++++++++++++++++++++++-----------
ini.h | 9 ---------
inilex.ll | 20 ++------------------
6 files changed, 42 insertions(+), 70 deletions(-)
delete mode 100644 IniParseFeedback.cc
diff --git a/IniParseFeedback.cc b/IniParseFeedback.cc
deleted file mode 100644
index 8b7ebd1..0000000
--- a/IniParseFeedback.cc
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (c) 2002 Robert Collins.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * A copy of the GNU General Public License can be found at
- * http://www.gnu.org/
- *
- * Written by Robert Collins <robertc@hotmail.com>
- *
- */
-
-#include "IniParseFeedback.h"
-
-IniParseFeedback::~IniParseFeedback(){}
-
-void IniParseFeedback::progress(unsigned long const, unsigned long const) {}
-void IniParseFeedback::iniName (const std::string& ) {}
-void IniParseFeedback::babble(const std::string& ) const {}
-void IniParseFeedback::warning (const std::string& ) const {}
-void IniParseFeedback::error(const std::string& ) const {}
diff --git a/IniParseFeedback.h b/IniParseFeedback.h
index 23f9d31..c3c7803 100644
--- a/IniParseFeedback.h
+++ b/IniParseFeedback.h
@@ -26,12 +26,13 @@
class IniParseFeedback
{
public:
- virtual void progress (unsigned long const, unsigned long const);
- virtual void iniName (const std::string& );
- virtual void babble (const std::string& ) const;
- virtual void warning (const std::string& ) const;
- virtual void error (const std::string& ) const;
- virtual ~ IniParseFeedback ();
+ virtual void progress (unsigned long const, unsigned long const) = 0;
+ virtual void iniName (const std::string& ) = 0;
+ virtual void babble (const std::string& ) const = 0;
+ virtual void warning (const std::string& ) const = 0;
+ virtual void show_errors () const = 0;
+ virtual void note_error(int lineno, const std::string &error) = 0;
+ virtual bool has_errors () const = 0;
};
#endif /* SETUP_INIPARSEFEEDBACK_H */
diff --git a/Makefile.am b/Makefile.am
index 3c41389..cc869e0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -75,7 +75,6 @@ inilint_SOURCES = \
inilintmain.cc \
inilex.ll \
iniparse.yy \
- IniParseFeedback.cc \
IniParseFeedback.h \
io_stream.h \
io_stream.cc \
@@ -162,7 +161,6 @@ inilint_SOURCES = \
IniDBBuilderPackage.h \
inilex.ll \
iniparse.yy \
- IniParseFeedback.cc \
IniParseFeedback.h \
install.cc \
io_stream.cc \
diff --git a/ini.cc b/ini.cc
index 78684a7..2c568e1 100644
--- a/ini.cc
+++ b/ini.cc
@@ -66,8 +66,6 @@ static BoolOption NoVersionCheckOption (false, '\0', "no-version-check", "Suppre
extern int yyparse ();
-/*extern int yydebug;*/
-
class GuiParseFeedback : public IniParseFeedback
{
public:
@@ -76,6 +74,9 @@ public:
Progress.SetText2 ("");
Progress.SetText3 ("");
Progress.SetText4 ("Progress:");
+
+ yyerror_count = 0;
+ yyerror_messages.clear ();
}
virtual void progress (unsigned long const pos, unsigned long const max)
{
@@ -102,6 +103,7 @@ public:
Progress.SetText1 ("Parsing...");
Progress.SetText2 (name.c_str ());
Progress.SetText3 ("");
+ filename = name;
}
virtual void babble (const std::string& message)const
{
@@ -111,9 +113,26 @@ public:
{
mbox (Progress.GetHWND(), message.c_str (), "Warning", 0);
}
- virtual void error (const std::string& message)const
+ virtual void note_error(int lineno, const std::string &error)
+ {
+ char tmp[16];
+ sprintf (tmp, "%d", lineno);
+
+ std::string e = filename + " line " + tmp + ": " + error;
+
+ if (!yyerror_messages.empty ())
+ yyerror_messages += "\n";
+
+ yyerror_messages += e;
+ yyerror_count++;
+ }
+ virtual bool has_errors () const
+ {
+ return (yyerror_count > 0);
+ }
+ virtual void show_errors () const
{
- mbox (Progress.GetHWND(), message.c_str (), "Parse Errors", 0);
+ mbox (Progress.GetHWND(), yyerror_messages.c_str (), "Parse Errors", 0);
}
virtual ~ GuiParseFeedback ()
{
@@ -121,10 +140,13 @@ public:
}
private:
unsigned int lastpct;
+ std::string filename;
+ std::string yyerror_messages;
+ int yyerror_count;
};
static io_stream*
-decompress_ini (io_stream *ini_file)
+decompress_ini (io_stream *ini_file, std::string ¤t_ini_name)
{
// Replace the current compressed setup stream with its decompressed
// version. Which decompressor to use is determined by file magic.
@@ -228,7 +250,7 @@ do_local_ini (HWND owner)
ini_file = check_ini_sig (ini_file, ini_sig_file, sig_fail,
"localdir", current_ini_sig_name.c_str (), owner);
if (ini_file)
- ini_file = decompress_ini (ini_file);
+ ini_file = decompress_ini (ini_file, current_ini_name);
if (!ini_file || sig_fail)
{
// no setup found or signature invalid
@@ -247,9 +269,9 @@ do_local_ini (HWND owner)
rfc1738_unescape (current_ini_name.substr (ldl, cap - ldl));
ini_init (ini_file, &aBuilder, myFeedback);
- if (yyparse () || yyerror_count > 0)
+ if (yyparse () || myFeedback.has_errors())
{
- myFeedback.error (yyerror_messages);
+ myFeedback.show_errors ();
ini_error = true;
}
@@ -299,7 +321,7 @@ do_remote_ini (HWND owner)
break;
}
if (ini_file)
- ini_file = decompress_ini (ini_file);
+ ini_file = decompress_ini (ini_file, current_ini_name);
if (!ini_file || sig_fail)
{
// no setup found or signature invalid
@@ -313,9 +335,9 @@ do_remote_ini (HWND owner)
aBuilder.parse_mirror = n->url;
ini_init (ini_file, &aBuilder, myFeedback);
- if (yyparse () || yyerror_count > 0)
+ if (yyparse () || myFeedback.has_errors())
{
- myFeedback.error (yyerror_messages);
+ myFeedback.show_errors ();
ini_error = true;
}
else
diff --git a/ini.h b/ini.h
index 07e1a46..3ff9617 100644
--- a/ini.h
+++ b/ini.h
@@ -42,15 +42,6 @@ void ini_init (io_stream *, IniDBBuilderPackage *, IniParseFeedback &);
packages (the chosen "install" field). install.cc installs
selected packages. */
-/* The following three vars are used to facilitate error handling between the
- parser/lexer and its callers, namely ini.cc:do_remote_ini() and
- IniParseFindVisitor::visitFile(). */
-
-extern std::string current_ini_name; /* current filename/URL being parsed */
-extern std::string current_ini_sig_name; /* current filename/URL for sig file */
-extern std::string yyerror_messages; /* textual parse error messages */
-extern int yyerror_count; /* number of parse errors */
-
/* The following definitions are used in the parser implementation */
#define hexnibble(val) ('\xff' & (val > '9') ? val - 'a' + 10 : val - '0')
diff --git a/inilex.ll b/inilex.ll
index 209152f..0147b4d 100644
--- a/inilex.ll
+++ b/inilex.ll
@@ -168,8 +168,6 @@ B64 [a-zA-Z0-9_-]
static io_stream *input_stream = 0;
extern IniDBBuilderPackage *iniBuilder;
static IniParseFeedback *iniFeedback;
-std::string current_ini_name, yyerror_messages;
-int yyerror_count;
void
ini_init(io_stream *stream, IniDBBuilderPackage *aBuilder, IniParseFeedback &aFeedback)
@@ -179,8 +177,6 @@ ini_init(io_stream *stream, IniDBBuilderPackage *aBuilder, IniParseFeedback &aFe
iniFeedback = &aFeedback;
YY_FLUSH_BUFFER;
yylineno = 1;
- yyerror_count = 0;
- yyerror_messages.clear ();
}
static int
@@ -214,20 +210,8 @@ ignore_line ()
}
}
-int
+void
yyerror (const std::string& s)
{
- char tmp[16];
- sprintf (tmp, "%d", yylineno - (!!YY_AT_BOL ()));
-
- std::string e = current_ini_name + " line " + tmp + ": " + s;
-
- if (!yyerror_messages.empty ())
- yyerror_messages += "\n";
-
- yyerror_messages += e;
- // OutputDebugString (e.c_str ());
- yyerror_count++;
- /* TODO: is return 0 correct? */
- return 0;
+ iniFeedback->note_error(yylineno - (!!YY_AT_BOL ()), s);
}
--
2.21.0
More information about the Cygwin-apps
mailing list