scan: New library for high-level PSPP syntax lexical analysis.
[pspp-builds.git] / src / ui / gui / syntax-editor-source.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2006, 2009  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 #include <config.h>
19
20 #include <libpspp/getl.h>
21 #include <libpspp/compiler.h>
22 #include <libpspp/cast.h>
23 #include <libpspp/str.h>
24
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28
29 #include "syntax-editor-source.h"
30 #include "psppire-syntax-window.h"
31
32 #include "xalloc.h"
33
34 struct syntax_editor_source
35   {
36     struct getl_interface parent;
37     GtkTextBuffer *buffer;
38     GtkTextIter i;
39     GtkTextIter end;
40     const gchar *name;
41   };
42
43
44 static bool
45 always_false (const struct getl_interface *i UNUSED)
46 {
47   return false;
48 }
49
50 /* Returns the name of the source */
51 static const char *
52 name (const struct getl_interface *i)
53 {
54   const struct syntax_editor_source *ses = (const struct syntax_editor_source *) i;
55   return ses->name;
56 }
57
58
59 /* Returns the location within the source */
60 static int
61 location (const struct getl_interface *i)
62 {
63   const struct syntax_editor_source *ses = (const struct syntax_editor_source *) i;
64
65   return gtk_text_iter_get_line (&ses->i);
66 }
67
68
69 static bool
70 read_line_from_buffer (struct getl_interface *i,
71                        struct string *line)
72 {
73   gchar *text;
74   GtkTextIter next_line;
75
76   struct syntax_editor_source *ses
77     = UP_CAST (i, struct syntax_editor_source, parent);
78
79   if ( gtk_text_iter_compare (&ses->i, &ses->end) >= 0)
80     return false;
81
82   next_line = ses->i;
83   gtk_text_iter_forward_line (&next_line);
84
85   text = gtk_text_buffer_get_text (ses->buffer,
86                                    &ses->i, &next_line,
87                                    FALSE);
88   g_strchomp (text);
89
90   ds_assign_cstr (line, text);
91
92   g_free (text);
93
94   gtk_text_iter_forward_line (&ses->i);
95
96   return true;
97 }
98
99
100 static void
101 do_close (struct getl_interface *i )
102 {
103   free (i);
104 }
105
106 struct getl_interface *
107 create_syntax_editor_source (GtkTextBuffer *buffer,
108                              GtkTextIter start,
109                              GtkTextIter stop,
110                              const gchar *nm
111                              )
112 {
113   struct syntax_editor_source *ses = xzalloc (sizeof *ses);
114
115   ses->buffer = buffer;
116   ses->i = start;
117   ses->end = stop;
118   ses->name = nm;
119
120
121   ses->parent.interactive = always_false;
122   ses->parent.read = read_line_from_buffer;
123   ses->parent.close = do_close;
124
125   ses->parent.name = name;
126   ses->parent.location = location;
127
128
129   return &ses->parent;
130 }