aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2013-03-28 18:33:13 +0100
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2014-02-15 15:21:52 +0100
commitc6b6aefd36d49aaf873bca20175b929f828589df (patch)
tree47e46a9e6c869d9d50c27e0d436911cb8bd63ab9
parentd374448af8ab690c810757f73ba44f208db96f30 (diff)
downloadsciteco-c6b6aefd36d49aaf873bca20175b929f828589df.tar.gz
String::get_coord() calculates line and column of a string position
* use to get line and column into a stack frame
-rw-r--r--src/main.cpp23
-rw-r--r--src/parser.cpp17
-rw-r--r--src/parser.h21
-rw-r--r--src/sciteco.h4
4 files changed, 53 insertions, 12 deletions
diff --git a/src/main.cpp b/src/main.cpp
index d0dfd84..d6b8ab3 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -111,6 +111,29 @@ Interface::process_notify(SCNotification *notify)
#endif
}
+void
+String::get_coord(const gchar *str, gint pos,
+ gint &line, gint &column)
+{
+ line = column = 1;
+
+ for (gint i = 0; i < pos; i++) {
+ switch (str[i]) {
+ case '\r':
+ if (str[i+1] == '\n')
+ i++;
+ /* fall through */
+ case '\n':
+ line++;
+ column = 1;
+ break;
+ default:
+ column++;
+ break;
+ }
+ }
+}
+
#ifdef G_OS_WIN32
/*
diff --git a/src/parser.cpp b/src/parser.cpp
index 016b8bd..8921942 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -89,6 +89,8 @@ Execute::step(const gchar *macro, gint stop_pos)
State::input(macro[macro_pc]);
} catch (State::Error &error) {
error.pos = macro_pc;
+ String::get_coord(macro, error.pos,
+ error.line, error.column);
throw; /* forward */
}
macro_pc++;
@@ -170,6 +172,8 @@ Execute::file(const gchar *filename, bool locals)
macro(p, locals);
} catch (State::Error &error) {
error.pos += p - macro_str;
+ if (*macro_str == '#')
+ error.line++;
error.add_frame(new State::Error::FileFrame(filename));
g_free(macro_str);
@@ -191,7 +195,8 @@ ReplaceCmdline::ReplaceCmdline()
pos++;
}
-State::Error::Error(const gchar *fmt, ...) : frames(NULL), pos(0)
+State::Error::Error(const gchar *fmt, ...)
+ : frames(NULL), pos(0), line(0), column(0)
{
va_list ap;
@@ -201,6 +206,16 @@ State::Error::Error(const gchar *fmt, ...) : frames(NULL), pos(0)
}
void
+State::Error::add_frame(Frame *frame)
+{
+ frame->pos = pos;
+ frame->line = line;
+ frame->column = column;
+
+ frames = g_slist_prepend(frames, frame);
+}
+
+void
State::Error::display_short(void)
{
interface.msg(Interface::MSG_ERROR,
diff --git a/src/parser.h b/src/parser.h
index 8aa7ef5..a829b6d 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -44,10 +44,13 @@ public:
public:
gint pos;
+ gint line, column;
class Frame {
public:
gint pos;
+ gint line, column;
+
virtual ~Frame() {}
virtual void display(gint nr) = 0;
@@ -69,8 +72,8 @@ public:
display(gint nr)
{
interface.msg(Interface::MSG_INFO,
- "#%d in Q-Register \"%s\" at %d",
- nr, name, pos);
+ "#%d in Q-Register \"%s\" at %d (%d:%d)",
+ nr, name, pos, line, column);
}
};
@@ -90,8 +93,8 @@ public:
display(gint nr)
{
interface.msg(Interface::MSG_INFO,
- "#%d in file \"%s\" at %d",
- nr, name, pos);
+ "#%d in file \"%s\" at %d (%d:%d)",
+ nr, name, pos, line, column);
}
};
@@ -101,19 +104,15 @@ public:
display(gint nr)
{
interface.msg(Interface::MSG_INFO,
- "#%d in toplevel macro at %d", nr, pos);
+ "#%d in toplevel macro at %d (%d:%d)",
+ nr, pos, line, column);
}
};
Error(const gchar *fmt, ...) G_GNUC_PRINTF(2, 3);
~Error();
- inline void
- add_frame(Frame *frame)
- {
- frame->pos = pos;
- frames = g_slist_prepend(frames, frame);
- }
+ void add_frame(Frame *frame);
void display_short(void);
void display_full(void);
diff --git a/src/sciteco.h b/src/sciteco.h
index a1e2d4b..80f0c5e 100644
--- a/src/sciteco.h
+++ b/src/sciteco.h
@@ -87,6 +87,10 @@ append(gchar *&str, gchar chr)
append(str, CHR2STR(chr));
}
+/* in main.cpp */
+void get_coord(const gchar *str, gint pos,
+ gint &line, gint &column);
+
} /* namespace String */
namespace Validate {