]> git.lyx.org Git - lyx.git/blob - src/mathed/math_hash.C
Check for duplicate symbols.
[lyx.git] / src / mathed / math_hash.C
1 #include <config.h>
2
3 #include "math_parser.h"
4 #include "lyxlex.h"
5 #include "debug.h"
6 #include "support/filetools.h" // LibFileSearch
7
8 #include <map>
9
10
11 namespace {
12
13 struct latexkeys_a {
14         ///
15         char const * name;
16         ///
17         short token;
18         ///
19         unsigned int id;
20 };
21
22 latexkeys_a wordlist_array[] = 
23 {
24         {"!",  LM_TK_SPACE, 0},
25         {"#",  LM_TK_SPECIAL, '#'},
26         {"$",  LM_TK_SPECIAL, '$'},
27         {"%",  LM_TK_SPECIAL, '%'},
28         {"&",  LM_TK_SPECIAL, '&'},
29         {"(",  LM_TK_BEGIN, LM_OT_SIMPLE},
30         {")",  LM_TK_END, LM_OT_SIMPLE},
31         {",",  LM_TK_SPACE, 1},
32         {".",  LM_TK_SPECIAL, '.'},
33         {":",  LM_TK_SPACE, 2},
34         {";",  LM_TK_SPACE, 3},
35         {"Pr",  LM_TK_FUNCLIM, 0},
36         {"[",  LM_TK_BEGIN, LM_OT_EQUATION},
37         {"\\",  LM_TK_NEWLINE, static_cast<unsigned>(-1)}, // -1 needed in mathed_parse_lines!
38         {"]",  LM_TK_END, LM_OT_EQUATION},
39         {"_",  LM_TK_SPECIAL, '_'},
40         {"acute",  LM_TK_DECORATION, 0},
41         {"arccos",  LM_TK_FUNC, 0},
42         {"arcsin",  LM_TK_FUNC, 0},
43         {"arctan",  LM_TK_FUNC, 0},
44         {"arg",  LM_TK_FUNC, 0},
45         {"atop",  LM_TK_ATOP, 0},
46         {"backslash",  LM_TK_SPECIAL, '\\'},
47         {"bar",  LM_TK_DECORATION, 0},
48         {"begin",  LM_TK_BEGIN, 0},
49         {"binom",  LM_TK_BINOM, 0},
50         {"bmod",  LM_TK_FUNC, 0},
51         {"breve",  LM_TK_DECORATION, 0},
52         {"cal",  LM_TK_OLDFONT, LM_TC_CAL},
53         {"cdots",  LM_TK_DOTS, 0},
54         {"check",  LM_TK_DECORATION, 0},
55         {"choose",  LM_TK_CHOOSE, 0},
56         {"cos",  LM_TK_FUNC, 0},
57         {"cosh",  LM_TK_FUNC, 0},
58         {"cot",  LM_TK_FUNC, 0},
59         {"coth",  LM_TK_FUNC, 0},
60         {"csc",  LM_TK_FUNC, 0},
61         {"ddot",  LM_TK_DECORATION, 0},
62         {"ddots",  LM_TK_DOTS, 0},
63         {"deg",  LM_TK_FUNC, 0},
64         {"det",  LM_TK_FUNCLIM, 0},
65         {"dim",  LM_TK_FUNC, 0},
66         //{"displaystyle",  LM_TK_STY, LM_ST_DISPLAY},
67         {"dot",  LM_TK_DECORATION, 0},
68         {"end",  LM_TK_END, 0},
69         {"exp",  LM_TK_FUNC, 0},
70         {"frac",  LM_TK_FRAC, 0},
71         {"gcd",  LM_TK_FUNCLIM, 0},
72         {"grave",  LM_TK_DECORATION, 0},
73         {"hat",  LM_TK_DECORATION, 0},
74         {"hom",  LM_TK_FUNC, 0},
75         {"inf",  LM_TK_FUNCLIM, 0},
76         {"ker",  LM_TK_FUNC, 0},
77         {"kern",  LM_TK_KERN, 0},
78         {"label",  LM_TK_LABEL, 0},
79         {"ldots",  LM_TK_DOTS, 0},
80         {"left",  LM_TK_LEFT, 0},
81         {"lg",  LM_TK_FUNC, 0},
82         {"lim",  LM_TK_FUNCLIM, 0},
83         {"liminf",  LM_TK_FUNCLIM, 0},
84         {"limits",  LM_TK_LIMIT, 1 },
85         {"limsup",  LM_TK_FUNCLIM, 0},
86         {"ln",  LM_TK_FUNC, 0},
87         {"log",  LM_TK_FUNC, 0},
88         {"mathbb",  LM_TK_FONT, LM_TC_BB},
89         {"mathbf",  LM_TK_FONT, LM_TC_BF},
90         {"mathcal",  LM_TK_FONT, LM_TC_CAL},
91         {"mathit",  LM_TK_FONT, LM_TC_IT},
92         {"mathnormal",  LM_TK_FONT, LM_TC_VAR},
93         {"mathrm",  LM_TK_FONT, LM_TC_RM},
94         {"mathsf",  LM_TK_FONT, LM_TC_SF},
95         {"mathtt",  LM_TK_FONT, LM_TC_TT},
96         {"max",  LM_TK_FUNCLIM, 0},
97         {"min",  LM_TK_FUNCLIM, 0},
98         {"newcommand",  LM_TK_NEWCOMMAND, 0 },
99         {"nolimits",  LM_TK_LIMIT, static_cast<unsigned>(-1)},
100         {"nonumber",  LM_TK_NONUM, 0},
101         {"not",  LM_TK_NOT, 0},
102         {"over",  LM_TK_OVER, 0},
103         {"overbrace",  LM_TK_DECORATION, 0},
104         {"overleftarrow",  LM_TK_DECORATION, 0},
105         {"overline",  LM_TK_DECORATION, 0},
106         {"overrightarrow",  LM_TK_DECORATION, 0},
107         {"protect",  LM_TK_PROTECT, 0},
108         {"qquad",  LM_TK_SPACE, 5},
109         {"quad",  LM_TK_SPACE, 4},
110         {"right",  LM_TK_RIGHT, 0},
111         {"root",  LM_TK_ROOT, 0},
112         //{"scriptscriptstyle",  LM_TK_STY, LM_ST_SCRIPTSCRIPT},
113         //{"scriptstyle",  LM_TK_STY, LM_ST_SCRIPT},
114         {"sec",  LM_TK_FUNC, 0},
115         {"sin",  LM_TK_FUNC, 0},
116         {"sinh",  LM_TK_FUNC, 0},
117         {"sqrt",  LM_TK_SQRT, 0},
118         {"stackrel",  LM_TK_STACK, 0},
119         {"sup",  LM_TK_FUNCLIM, 0},
120         {"tan",  LM_TK_FUNC, 0},
121         {"tanh",  LM_TK_FUNC, 0},
122         {"textrm",  LM_TK_FONT, LM_TC_TEXTRM},
123         //{"textstyle",  LM_TK_STY, LM_ST_TEXT},
124         {"tilde",  LM_TK_DECORATION, 0},
125         {"underbrace",  LM_TK_DECORATION, 0},
126         {"underline",  LM_TK_DECORATION, 0},
127         {"vdots",  LM_TK_DOTS, 0},
128         {"vec",  LM_TK_DECORATION, 0},
129         {"widehat",  LM_TK_DECORATION, 0},
130         {"widetilde",  LM_TK_DECORATION, 0},
131         {"{",  LM_TK_SPECIAL, '{'},
132         {"|",  LM_TK_UNDEF, '|'},
133         {"}",  LM_TK_SPECIAL, '}'}
134 };
135
136
137 std::map<string, latexkeys> wordlist;
138
139
140
141 MathTokenEnum tokenEnum(const string & font)
142 {
143         if (font == "cmr")
144                 return LM_TK_CMR;
145         if (font == "cmsy")
146                 return LM_TK_CMSY;
147         if (font == "cmm")
148                 return LM_TK_CMM;
149         if (font == "cmex")
150                 return LM_TK_CMEX;
151         if (font == "msa")
152                 return LM_TK_MSA;
153         if (font == "msb")
154                 return LM_TK_MSB;
155         return LM_TK_SYM;
156 }
157
158 MathSymbolTypes symbolType(const string & type)
159 {
160         if (type == "mathrel")
161                 return LMB_RELATION;
162         if (type == "mathbin")
163                 return LMB_OPERATOR;    
164         return LMB_NONE;
165 }
166
167
168 } // namespace anon
169
170
171 void ReadSymbols(string const & filename)
172 {
173         LyXLex lex(0, 0);
174         lex.setFile(filename);
175         while (lex.isOK() && lex.next()) {
176                 latexkeys tmp;
177                 tmp.name = lex.getString();
178                 if (lex.next())
179                         tmp.token = tokenEnum(lex.getString());
180                 if (lex.next())
181                         tmp.latex_font_id = lex.getInteger();
182                 if (lex.next())
183                         tmp.id = lex.getInteger();
184                 if (lex.next())
185                         tmp.type = symbolType(lex.getString());
186                 if (wordlist.find(tmp.name) != wordlist.end())
187                         lyxerr << "ReadSymbols: token " << tmp.name
188                                << " already exists.\n";
189                 else
190                         wordlist[tmp.name] = tmp;
191         }
192 }
193
194
195 void initSymbols()
196 {
197         unsigned const n = sizeof(wordlist_array) / sizeof(wordlist_array[0]);
198         for (latexkeys_a * p = wordlist_array; p != wordlist_array + n; ++p) {
199                 latexkeys tmp;
200                 tmp.name          = p->name;
201                 tmp.token         = p->token;
202                 tmp.id            = p->id;
203                 tmp.type          = LMB_NONE;
204                 tmp.latex_font_id = 0;
205                 wordlist[p->name] = tmp;
206         }
207
208         lyxerr[Debug::MATHED] << "Reading symbols file\n";
209         string const file = LibFileSearch(string(), "symbols");
210         if (file.empty())
211                 lyxerr << "Could not find symbols file\n";
212         else
213                 ReadSymbols(file);
214 }
215
216
217 latexkeys const * in_word_set(string const & str)
218 {
219         static bool initialized = false;
220
221         if (!initialized) {
222                 initSymbols();
223                 initialized = true;
224         }
225
226         std::map<string, latexkeys>::iterator it = wordlist.find(str);
227         return (it != wordlist.end()) ? &(it->second) : 0;
228 }