Back to index

texmacs  1.0.7.15
translator.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : translator.cpp
00004 * DESCRIPTION: used for the translation of tokens, mainly to name symbols
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "translator.hpp"
00013 #include "file.hpp"
00014 #include "convert.hpp"
00015 #include "iterator.hpp"
00016 #include "analyze.hpp"
00017 
00018 RESOURCE_CODE(translator);
00019 
00020 /******************************************************************************
00021 * Routines for translators
00022 ******************************************************************************/
00023 
00024 translator&
00025 operator << (translator& trl, int i) {
00026   trl->cur_c= i;
00027   return trl;
00028 }
00029 
00030 translator&
00031 operator << (translator& trl, string s) {
00032   if (N(s)>0) {
00033     if (N(s)>1) s= "<" * s * ">";
00034     trl->dict(s) = trl->cur_c;
00035     if (starts (s, "<large-")) {
00036       string sub= s (7, N(s)-1);
00037       trl->dict ("<left-"  * sub * ">") = trl->cur_c;
00038       trl->dict ("<mid-"   * sub * ">") = trl->cur_c;
00039       trl->dict ("<right-" * sub * ">") = trl->cur_c;
00040       if (ends (s, "-0>")) return trl << s (1, N(s)-3);
00041     }
00042   }
00043   trl->cur_c++;
00044   return trl;
00045 }
00046 
00047 translator&
00048 operator << (translator& trl, translator trm) {
00049   if ((trl->cur_c & 255) != 0) return trl;
00050   iterator<string> it= iterate (trm->dict);
00051   while (it->busy()) {
00052     string key= it->next();
00053     trl->dict (key)= trl->cur_c+ trm->dict [key];
00054   }
00055   trl->cur_c += 256;
00056   return trl;
00057 }
00058 
00059 /******************************************************************************
00060 * Loading virtual fonts as translators
00061 ******************************************************************************/
00062 
00063 translator
00064 load_virtual (string name) {
00065   if (translator::instances -> contains (name))
00066     return translator (name);
00067   translator trl= tm_new<translator_rep> (name);
00068 
00069   string s, r;
00070   name= name * ".vfn";
00071   if (DEBUG_STD) cout << "TeXmacs] Loading " << name << "\n";
00072   url u ("$TEXMACS_HOME_PATH/fonts/virtual:$TEXMACS_PATH/fonts/virtual", name);
00073   load_string (u, s, true);
00074   tree t= string_to_scheme_tree (s);
00075   ASSERT (is_tuple (t, "virtual-font"), "bad virtual font format");
00076 
00077   int i, n= N(t);
00078   trl->virt_def= array<tree> (n);
00079   for (i=1; i<n; i++)
00080     if (is_func (t[i], TUPLE, 2) && is_atomic (t[i][0])) {
00081       string s= as_string (t[i][0]);
00082       if (N(s)>1) s= "<" * s * ">";
00083       trl->dict (s)= i;
00084       trl->virt_def[i]= t[i][1];
00085       // cout << s << "\t" << i << "\t" << t[i][1] << "\n";
00086     }
00087   return trl;
00088 }
00089 
00090 /******************************************************************************
00091 * Loading translators
00092 ******************************************************************************/
00093 
00094 translator
00095 load_translator (string name) {
00096   if (translator::instances -> contains (name))
00097     return translator (name);
00098 
00099   string s, r;
00100   string file_name= name * ".enc";
00101   if (DEBUG_STD) cout << "TeXmacs] Loading " << file_name << "\n";
00102   url u ("$TEXMACS_HOME_PATH/fonts/enc:$TEXMACS_PATH/fonts/enc", file_name);
00103   if (load_string (u, s, false)) return load_virtual (name);
00104 
00105   translator trl= tm_new<translator_rep> (name);
00106   int i, j, num=0;
00107   for (i=0; i<N(s); i++)
00108     switch (s[i]) {
00109     case '\"': // "
00110       r= "";
00111       for (i++; i<N(s); i++) {
00112        if ((s[i]=='\\') && (i<N(s)-1)) i++;
00113        else if (s[i]=='\"') break; // "
00114        r << s[i];
00115       }
00116       trl << r;
00117       num= 0;
00118       break;
00119     case '0': case '1': case '2': case '3': case '4':
00120     case '5': case '6': case '7': case '8': case '9':
00121       if (i==N(s)-1) break;
00122       num= 10*num+ ((int) s[i])- ((int) '0');
00123       trl << num;
00124       break;
00125     case '*':
00126       if (i==N(s)-1) break;
00127       num= 256*num;
00128       trl << num;
00129       break;
00130     case '[':
00131       i++; j=i;
00132       while ((i<N(s)) && (s[i]!=']')) i++;
00133       trl << load_translator (s (j, i));
00134       break;
00135     default:
00136       num= 0;
00137     }
00138   return trl;
00139 }