PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/source/src/Data/Convert/Scheme/from_scheme.cpp

http://itexmacs.googlecode.com/
C++ | 194 lines | 150 code | 23 blank | 21 comment | 56 complexity | ccf1c65c0fc6bf0d65b67be7d5db82de MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.0
  1. /******************************************************************************
  2. * MODULE : to_scheme.cpp
  3. * DESCRIPTION: conversion of scheme expressions to TeXmacs trees
  4. * COPYRIGHT : (C) 1999 Joris van der Hoeven
  5. *******************************************************************************
  6. * This software falls under the GNU general public license version 3 or later.
  7. * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
  8. * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
  9. ******************************************************************************/
  10. #include "convert.hpp"
  11. #include "analyze.hpp"
  12. #include "drd_std.hpp"
  13. #include "path.hpp"
  14. /******************************************************************************
  15. * Handling escape characters
  16. ******************************************************************************/
  17. string
  18. unslash (string s) {
  19. int i, n= N(s);
  20. string r;
  21. for (i=0; i<n; i++)
  22. if ((s[i]=='\\') && ((i+1)<n))
  23. switch (s[++i]) {
  24. case '0': r << ((char) 0); break;
  25. case 'n': r << '\n'; break;
  26. case 't': r << '\t'; break;
  27. default: r << s[i];
  28. }
  29. else r << s[i];
  30. return r;
  31. }
  32. /******************************************************************************
  33. * Converting strings to scheme trees
  34. ******************************************************************************/
  35. static bool
  36. is_spc (char c) {
  37. return (c==' ') || (c=='\t') || (c=='\n');
  38. }
  39. static scheme_tree
  40. string_to_scheme_tree (string s, int& i) {
  41. for (; i<N(s); i++)
  42. switch (s[i]) {
  43. case ' ':
  44. case '\t':
  45. case '\n':
  46. break;
  47. case '(':
  48. {
  49. scheme_tree p (TUPLE);
  50. i++;
  51. while (true) {
  52. while ((i<N(s)) && is_spc(s[i])) i++;
  53. if ((i==N(s)) || (s[i]==')')) break;
  54. p << string_to_scheme_tree (s, i);
  55. }
  56. if (i<N(s)) i++;
  57. return p;
  58. }
  59. case '\'':
  60. i++;
  61. return scheme_tree (TUPLE, "\'", string_to_scheme_tree (s, i));
  62. case '\"':
  63. { // "
  64. int start= i++;
  65. while ((i<N(s)) && (s[i]!='\"')) { // "
  66. if ((i<N(s)-1) && (s[i]=='\\')) i++;
  67. i++;
  68. }
  69. if (i<N(s)) i++;
  70. return scheme_tree (unslash (s (start, i)));
  71. }
  72. case ';':
  73. while ((i<N(s)) && (s[i]!='\n')) i++;
  74. break;
  75. default:
  76. {
  77. int start= i;
  78. while ((i<N(s)) && (!is_spc(s[i])) && (s[i]!='(') && (s[i]!=')')) {
  79. if ((i<N(s)-1) && (s[i]=='\\')) i++;
  80. i++;
  81. }
  82. return scheme_tree (unslash (s (start, i)));
  83. }
  84. }
  85. return "";
  86. }
  87. scheme_tree
  88. string_to_scheme_tree (string s) {
  89. int i=0;
  90. return string_to_scheme_tree (s, i);
  91. }
  92. scheme_tree
  93. block_to_scheme_tree (string s) {
  94. scheme_tree p (TUPLE);
  95. int i=0;
  96. while ((i<N(s)) && (is_spc (s[i]) || s[i]==')')) i++;
  97. while (i<N(s)) {
  98. p << string_to_scheme_tree (s, i);
  99. while ((i<N(s)) && (is_spc (s[i]) || s[i]==')')) i++;
  100. }
  101. return p;
  102. }
  103. /******************************************************************************
  104. * Converting scheme trees to trees
  105. ******************************************************************************/
  106. tree
  107. scheme_tree_to_tree (scheme_tree t, hashmap<string,int> codes, bool flag) {
  108. if (is_atomic (t)) return scm_unquote (t->label);
  109. else if ((N(t) == 0) || is_compound (t[0])) {
  110. cerr << "\nTeXmacs] The tree was " << t << "\n";
  111. FAILED ("bad TeXmacs tree");
  112. return "";
  113. }
  114. else {
  115. int i, n= N(t);
  116. tree_label code= (tree_label) codes [t[0]->label];
  117. if (flag) code= make_tree_label (t[0]->label);
  118. if (code == UNKNOWN) {
  119. tree u (EXPAND, n);
  120. u[0]= copy (t[0]);
  121. for (i=1; i<n; i++)
  122. u[i]= scheme_tree_to_tree (t[i], codes, flag);
  123. return u;
  124. }
  125. else {
  126. tree u (code, n-1);
  127. for (i=1; i<n; i++)
  128. u[i-1]= scheme_tree_to_tree (t[i], codes, flag);
  129. return u;
  130. }
  131. }
  132. }
  133. tree
  134. scheme_tree_to_tree (scheme_tree t, string version) {
  135. version= scm_unquote (version);
  136. tree doc, error (ERROR, "bad format or data");
  137. if (version_inf (version, "1.0.2.4"))
  138. doc= scheme_tree_to_tree (t, get_codes (version), false);
  139. else doc= scheme_tree_to_tree (t);
  140. if (!is_document (doc)) return error;
  141. return upgrade (doc, version);
  142. }
  143. tree
  144. scheme_tree_to_tree (scheme_tree t) {
  145. return scheme_tree_to_tree (t, STD_CODE, true);
  146. }
  147. /******************************************************************************
  148. * Converting scheme strings to trees
  149. ******************************************************************************/
  150. tree
  151. scheme_to_tree (string s) {
  152. return scheme_tree_to_tree (string_to_scheme_tree (s));
  153. }
  154. tree
  155. scheme_document_to_tree (string s) {
  156. tree error (ERROR, "bad format or data");
  157. if (starts (s, "(document (apply \"TeXmacs\" ") ||
  158. starts (s, "(document (expand \"TeXmacs\" ") ||
  159. starts (s, "(document (TeXmacs "))
  160. {
  161. int i, begin=27;
  162. if (starts (s, "(document (expand \"TeXmacs\" ")) begin= 28;
  163. if (starts (s, "(document (TeXmacs ")) begin= 19;
  164. for (i=begin; i<N(s); i++)
  165. if (s[i] == ')') break;
  166. string version= s (begin, i);
  167. tree t = string_to_scheme_tree (s);
  168. return scheme_tree_to_tree (t, version);
  169. }
  170. return error;
  171. }