Back to index

texmacs  1.0.7.15
from_scheme.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : to_scheme.cpp
00004 * DESCRIPTION: conversion of scheme expressions to TeXmacs trees
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 "convert.hpp"
00013 #include "analyze.hpp"
00014 #include "drd_std.hpp"
00015 #include "path.hpp"
00016 
00017 /******************************************************************************
00018 * Handling escape characters
00019 ******************************************************************************/
00020 
00021 string
00022 unslash (string s) {
00023   int i, n= N(s);
00024   string r;
00025   for (i=0; i<n; i++)
00026     if ((s[i]=='\\') && ((i+1)<n))
00027       switch (s[++i]) {
00028       case '0': r << ((char) 0); break;
00029       case 'n': r << '\n'; break;
00030       case 't': r << '\t'; break;
00031       default: r << s[i];
00032       }
00033     else r << s[i];
00034   return r;
00035 }
00036 
00037 /******************************************************************************
00038 * Converting strings to scheme trees
00039 ******************************************************************************/
00040 
00041 static bool
00042 is_spc (char c) {
00043   return (c==' ') || (c=='\t') || (c=='\n');
00044 }
00045 
00046 static scheme_tree
00047 string_to_scheme_tree (string s, int& i) {
00048   for (; i<N(s); i++)
00049     switch (s[i]) {
00050 
00051     case ' ':
00052     case '\t':
00053     case '\n':
00054       break;
00055       case '(':
00056       {
00057         scheme_tree p (TUPLE);
00058         i++;
00059         while (true) {
00060           while ((i<N(s)) && is_spc(s[i])) i++;
00061           if ((i==N(s)) || (s[i]==')')) break;
00062           p << string_to_scheme_tree (s, i);
00063         }
00064         if (i<N(s)) i++;
00065         return p;
00066       }
00067         
00068       case '\'':
00069         i++;
00070         return scheme_tree (TUPLE, "\'", string_to_scheme_tree (s, i));
00071         
00072       case '\"':
00073       { // "
00074         int start= i++;
00075         while ((i<N(s)) && (s[i]!='\"')) { // "
00076           if ((i<N(s)-1) && (s[i]=='\\')) i++;
00077           i++;
00078         }
00079         if (i<N(s)) i++;
00080         return scheme_tree (unslash (s (start, i)));
00081       }
00082         
00083       case ';':
00084         while ((i<N(s)) && (s[i]!='\n')) i++;
00085         break;
00086         
00087       default:
00088       {
00089         int start= i;
00090         while ((i<N(s)) && (!is_spc(s[i])) && (s[i]!='(') && (s[i]!=')')) {
00091           if ((i<N(s)-1) && (s[i]=='\\')) i++;
00092           i++;
00093         }
00094         return scheme_tree (unslash (s (start, i)));
00095       }
00096     }
00097   
00098   return "";
00099 }
00100 
00101 scheme_tree
00102 string_to_scheme_tree (string s) {
00103   s= replace (s, "\015", "");
00104   int i=0;
00105   return string_to_scheme_tree (s, i);
00106 }
00107 
00108 scheme_tree
00109 block_to_scheme_tree (string s) {
00110   scheme_tree p (TUPLE);
00111   int i=0;
00112   while ((i<N(s)) && (is_spc (s[i]) || s[i]==')')) i++;
00113   while (i<N(s)) {
00114     p << string_to_scheme_tree (s, i);
00115     while ((i<N(s)) && (is_spc (s[i]) || s[i]==')')) i++;
00116   }
00117   return p;
00118 }
00119 
00120 /******************************************************************************
00121 * Converting scheme trees to trees
00122 ******************************************************************************/
00123 
00124 tree
00125 scheme_tree_to_tree (scheme_tree t, hashmap<string,int> codes, bool flag) {
00126   if (is_atomic (t)) return scm_unquote (t->label);
00127   else if ((N(t) == 0) || is_compound (t[0])) {
00128     cerr << "\nTeXmacs] The tree was " << t << "\n";
00129     FAILED ("bad TeXmacs tree");
00130     return "";
00131   }
00132   else {
00133     int i, n= N(t);
00134     tree_label code= (tree_label) codes [t[0]->label];
00135     if (flag) code= make_tree_label (t[0]->label);
00136     if (code == UNKNOWN) {
00137       tree u (EXPAND, n);
00138       u[0]= copy (t[0]);
00139       for (i=1; i<n; i++)
00140        u[i]= scheme_tree_to_tree (t[i], codes, flag);
00141       return u;
00142     }
00143     else {
00144       tree u (code, n-1);
00145       for (i=1; i<n; i++)
00146        u[i-1]= scheme_tree_to_tree (t[i], codes, flag);
00147       return u;
00148     }
00149   }
00150 }
00151 
00152 tree
00153 scheme_tree_to_tree (scheme_tree t, string version) {
00154   version= scm_unquote (version);
00155   tree doc, error (ERROR, "bad format or data");
00156   if (version_inf (version, "1.0.2.4"))
00157     doc= scheme_tree_to_tree (t, get_codes (version), false);
00158   else doc= scheme_tree_to_tree (t);
00159   if (!is_document (doc)) return error;
00160   return upgrade (doc, version);
00161 }
00162 
00163 tree
00164 scheme_tree_to_tree (scheme_tree t) {
00165   return scheme_tree_to_tree (t, STD_CODE, true);
00166 }
00167 
00168 /******************************************************************************
00169 * Converting scheme strings to trees
00170 ******************************************************************************/
00171 
00172 tree
00173 scheme_to_tree (string s) {
00174   return scheme_tree_to_tree (string_to_scheme_tree (s));
00175 }
00176 
00177 tree
00178 scheme_document_to_tree (string s) {
00179   tree error (ERROR, "bad format or data");
00180   if (starts (s, "(document (apply \"TeXmacs\" ") ||
00181       starts (s, "(document (expand \"TeXmacs\" ") ||
00182       starts (s, "(document (TeXmacs "))
00183   {
00184     int i, begin=27;
00185     if (starts (s, "(document (expand \"TeXmacs\" ")) begin= 28;
00186     if (starts (s, "(document (TeXmacs ")) begin= 19;
00187     for (i=begin; i<N(s); i++)
00188       if (s[i] == ')') break;
00189     string version= s (begin, i);
00190     tree t  = string_to_scheme_tree (s);
00191     return scheme_tree_to_tree (t, version);
00192   }
00193   return error;
00194 }