]> git.lyx.org Git - features.git/blob - src/CmdDef.cpp
simplify Lexer use a bit
[features.git] / src / CmdDef.cpp
1 /**
2  * \file CmdDef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bernhard Roider
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "CmdDef.h"
14
15 #include "LyXAction.h"
16 #include "Lexer.h"
17
18 #include "support/debug.h"
19 #include "support/FileName.h"
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
22
23 #include <ostream>
24 #include <string>
25
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31 namespace {
32
33 enum CmdDefTags {
34         BN_DEFFILE,
35         BN_DEFINE
36 };
37
38 LexerKeyword cmdDefTags[] = {
39         { "\\def_file", BN_DEFFILE },
40         { "\\define", BN_DEFINE }
41 };
42
43 }
44
45
46 bool CmdDef::read(string const & def_file)
47 {
48         Lexer lexrc(cmdDefTags);
49         if (lyxerr.debugging(Debug::PARSER))
50                 lexrc.printTable(lyxerr);
51
52         FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
53         lexrc.setFile(tmp);
54         if (!lexrc.isOK()) {
55                 lyxerr << "CmdDef::read: cannot open def file:"
56                        << tmp << endl;
57                 return false;
58         }
59
60         bool error = false;
61         while (lexrc.isOK()) {
62                 switch (lexrc.lex()) {
63                 case Lexer::LEX_UNDEF:
64                         lexrc.printError("Unknown tag `$$Token'");
65                         error = true;
66                         continue;
67                 case Lexer::LEX_FEOF:
68                         continue;
69                 case BN_DEFINE:
70                 {
71                         string name, def;
72
73                         if (lexrc.next()) {
74                                 name = lexrc.getString();
75                         } else {
76                                 lexrc.printError("BN_DEFINE: Missing command name");
77                                 error = true;
78                                 break;
79                         }
80
81                         if (lexrc.next(true)) {
82                                 def = lexrc.getString();
83                         } else {
84                                 lexrc.printError("BN_DEFINE: missing command definition");
85                                 error = true;
86                                 break;
87                         }
88
89                         newCmdDefResult e = newCmdDef(name, def);
90                         switch (e) {
91                         case CmdDefNameEmpty:
92                                 lexrc.printError("BN_DEFINE: Command name is empty");
93                                 error = true;
94                                 break;
95                         case CmdDefExists:
96                                 lexrc.printError("BN_DEFINE: Command `" + name + "' already defined");
97                                 error = true;
98                                 break;
99                         case CmdDefInvalid:
100                                 lexrc.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
101                                 error = true;
102                                 break;
103                         case CmdDefOk:
104                                 break;
105                         }
106
107                         break;
108                 }
109                 case BN_DEFFILE:
110                         if (lexrc.next()) {
111                                 string const tmp(lexrc.getString());
112                                 error |= !read(tmp);
113                         } else {
114                                 lexrc.printError("BN_DEFFILE: Missing file name");
115                                 error = true;
116                                 break;
117
118                         }
119                         break;
120                 }
121         }
122
123         if (error)
124                 lyxerr << "CmdDef::read: error while reading def file:"
125                        << tmp << endl;
126         return !error;
127 }
128
129
130 bool CmdDef::lock(string const & name, FuncRequest & func)
131 {
132         if (cmdDefMap.empty()) {
133                 func = FuncRequest::unknown;
134                 return false;
135         }
136
137         string const name2 = trim(name);
138
139         if (lockSet.find(name2) != lockSet.end()) {
140                 func = FuncRequest::noaction;
141                 return false;
142         }
143
144         CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
145
146         if (pos == cmdDefMap.end()) {
147                 func = FuncRequest::unknown;
148                 return false;
149         }
150
151         lockSet.insert(name2);
152         func = pos->second;
153         return true;
154 }
155
156
157 void CmdDef::release(string const & name)
158 {
159         string const name2 = trim(name);
160
161         lockSet.erase(name2);
162 }
163
164 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name, 
165                                                                                   string const & def)
166 {
167         string const name2 = trim(name);
168
169         if (name2.empty()) 
170                 return CmdDefNameEmpty;
171
172         if (cmdDefMap.find(name) != cmdDefMap.end())
173                 return CmdDefExists;
174
175         FuncRequest     func = lyxaction.lookupFunc(def);
176         if (func.action == LFUN_NOACTION ||
177                 func.action == LFUN_UNKNOWN_ACTION) {
178                         return CmdDefInvalid;
179         }
180
181         cmdDefMap[name2] = func;
182
183         return CmdDefOk;
184 }
185
186
187 } // namespace lyx