[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 4/6] cex: factor the handling of graphical symbols
From: |
Akim Demaille |
Subject: |
[PATCH 4/6] cex: factor the handling of graphical symbols |
Date: |
Sat, 18 Jul 2020 07:59:04 +0200 |
* src/glyphs.h, src/glyphs.c: New.
---
src/glyphs.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++
src/glyphs.h | 34 +++++++++++++++++++++
src/gram.h | 2 --
src/local.mk | 2 ++
src/main.c | 2 ++
5 files changed, 124 insertions(+), 2 deletions(-)
create mode 100644 src/glyphs.c
create mode 100644 src/glyphs.h
diff --git a/src/glyphs.c b/src/glyphs.c
new file mode 100644
index 00000000..65f32536
--- /dev/null
+++ b/src/glyphs.c
@@ -0,0 +1,86 @@
+/* Graphical symbols.
+
+ Copyright (C) 2020 Free Software Foundation, Inc.
+
+ This file is part of Bison, the GNU Compiler Compiler.
+
+ 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 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include "glyphs.h"
+
+#include <assert.h>
+#include <attribute.h>
+#include <stdbool.h>
+#include <string.h>
+#include <mbswidth.h>
+#include <unicodeio.h>
+
+// In gnulib/lib/unicodeio.h unicode_to_mb uses a buffer of 25 bytes.
+typedef char glyph_buffer_t[26];
+
+
+static glyph_buffer_t arrow_buf;
+const char *arrow;
+int arrow_width;
+
+static glyph_buffer_t dot_buf;
+const char *dot;
+int dot_width;
+
+typedef struct
+{
+ const char **glyph;
+ char *buf;
+ const char *fallback;
+} callback_arg_t;
+
+
+static long
+on_success (const char *buf, size_t buflen, void *callback_arg)
+{
+ callback_arg_t *arg = (callback_arg_t *) callback_arg;
+ assert (buflen < sizeof arg->buf);
+ strncpy (arg->buf, buf, buflen);
+ *arg->glyph = arg->buf;
+ return 1;
+}
+
+static long
+on_failure (unsigned code MAYBE_UNUSED, const char *msg MAYBE_UNUSED,
+ void *callback_arg)
+{
+ callback_arg_t *arg = (callback_arg_t *) callback_arg;
+ *arg->glyph = arg->fallback;
+ return 0;
+}
+
+static bool
+glyph_set (const char **glyph,
+ char buf[26], int *width,
+ unsigned code, const char *fallback)
+{
+ callback_arg_t arg = { glyph, buf, fallback };
+ int res = unicode_to_mb (code, on_success, on_failure, &arg);
+ *width = mbswidth (*glyph, 0);
+ return res;
+}
+
+void
+glyphs_init (void)
+{
+ glyph_set (&arrow, arrow_buf, &arrow_width, 0x2192, "->");
+ glyph_set (&dot, dot_buf, &dot_width, 0x2022, ".");
+}
diff --git a/src/glyphs.h b/src/glyphs.h
new file mode 100644
index 00000000..1faeb5aa
--- /dev/null
+++ b/src/glyphs.h
@@ -0,0 +1,34 @@
+/* Graphical symbols.
+
+ Copyright (C) 2020 Free Software Foundation, Inc.
+
+ This file is part of Bison, the GNU Compiler Compiler.
+
+ 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 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef GLYPHS_H
+# define GLYPHS_H
+
+/* Initialize the following variables. */
+void glyphs_init (void);
+
+/* "→", separates the lhs of a rule from its rhs. */
+extern const char *arrow;
+extern int arrow_width;
+
+/* "•", a point in an item (aka, a dotted rule). */
+extern const char *dot;
+extern int dot_width;
+
+#endif /* GLYPHS_H */
diff --git a/src/gram.h b/src/gram.h
index 6e1da4e8..71e3db94 100644
--- a/src/gram.h
+++ b/src/gram.h
@@ -238,8 +238,6 @@ print_fallback (unsigned int code _GL_UNUSED,
return -1;
}
-/* Print "→", the symbol used to separate the lhs of a rule from its
- rhs. */
static inline void
print_arrow (FILE *out)
{
diff --git a/src/local.mk b/src/local.mk
index aa716188..76808906 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -53,6 +53,8 @@ src_bison_SOURCES = \
src/flex-scanner.h \
src/getargs.c \
src/getargs.h \
+ src/glyphs.c \
+ src/glyphs.h \
src/gram.c \
src/gram.h \
src/graphviz.c \
diff --git a/src/main.c b/src/main.c
index c0176c29..70954d5b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -38,6 +38,7 @@
#include "files.h"
#include "fixits.h"
#include "getargs.h"
+#include "glyphs.h"
#include "gram.h"
#include "ielr.h"
#include "lalr.h"
@@ -85,6 +86,7 @@ main (int argc, char *argv[])
atexit (close_stdout);
+ glyphs_init ();
uniqstrs_new ();
muscle_init ();
complain_init ();
--
2.27.0
- [PATCH 0/6] Print derivations as trees, Akim Demaille, 2020/07/18
- [PATCH 1/6] cex: more colors, Akim Demaille, 2020/07/18
- [PATCH 2/6] cex: simplify tests, Akim Demaille, 2020/07/18
- [PATCH 3/6] cex: style changes, Akim Demaille, 2020/07/18
- [PATCH 4/6] cex: factor the handling of graphical symbols,
Akim Demaille <=
- [PATCH 5/6] cex: use the glyphs, Akim Demaille, 2020/07/18
- [PATCH 6/6] cex: display derivations as trees, Akim Demaille, 2020/07/18