home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Tex / Tex29 / StTeXsrc.zoo / src / tokenlists.c < prev    next >
C/C++ Source or Header  |  1988-03-13  |  12KB  |  526 lines

  1.  
  2. /*
  3.  * @(#)tokenlists.c 2.5 EPA
  4.  *
  5.  * Copyright 1987,1988 Pat J Monardo
  6.  *
  7.  * Redistribution of this file is permitted through
  8.  * the specifications in the file COPYING.
  9.  *
  10.  * 
  11.  */
  12.  
  13. #include "tex.h"
  14. #include "box.h"
  15. #include "token.h"
  16. #include "tokenstack.h"
  17. #include "scan.h"
  18. #include "expand.h"
  19. #include "def.h"
  20. #include "file.h"
  21. #include "tfm.h"
  22. #include "tokenlists.h"
  23.  
  24. ptr
  25. str_toks ()
  26. {
  27.     int     k;
  28.     ptr     p;
  29.     ptr     q;
  30.     tok     t;
  31.  
  32.     str_room(1);
  33.     p = temp_toks;
  34.     token_link(p) = NULL;
  35.     k = str_start[str_ptr];
  36.     while (k < pool_ptr) {
  37.         t = str_pool[k];
  38.         if (t == ' ')
  39.             t = SPACE_TOKEN;
  40.         else t += OTHER_TOKEN;
  41.         fast_store_new_token(t);
  42.         incr(k);
  43.     }
  44.     pool_ptr = str_start[str_ptr];
  45.     return p;
  46. }
  47.         
  48. ptr
  49. the_toks ()
  50. {
  51.     ptr     p;
  52.     ptr     q;
  53.     ptr     r;
  54.     int     old_setting;
  55.  
  56.     get_x_token();
  57.     scan_something_internal(TOK_VAL, FALSE);
  58.     if (cur_val_level >= IDENT_VAL) {
  59.         p = temp_toks;
  60.         token_link(p) = NULL;
  61.         if (cur_val_level == IDENT_VAL) {
  62.             store_new_token(CS_TOKEN_FLAG + cur_val);
  63.         } else if (cur_val != NULL) {
  64.             r = token_link(cur_val);
  65.             while (r != NULL) {
  66.                 fast_store_new_token(token(r));
  67.                 r = token_link(r);
  68.             }
  69.         }
  70.         return p;
  71.     } else {
  72.         old_setting = selector;
  73.         selector = NEW_STRING;
  74.         switch (cur_val_level)
  75.         {
  76.         case INT_VAL:
  77.             print_val(cur_val);
  78.             break;
  79.  
  80.         case DIMEN_VAL:
  81.             print_scaled(cur_val);
  82.             print("pt");
  83.             break;
  84.  
  85.         case GLUE_VAL:
  86.             print_spec((ptr) cur_val, "pt");
  87.             delete_glue_ref((ptr) cur_val);
  88.             break;
  89.  
  90.         case MU_VAL:
  91.             print_spec((ptr) cur_val,"mu");
  92.             delete_glue_ref((ptr) cur_val);
  93.             break;
  94.         }
  95.         selector = old_setting;
  96.         return (str_toks());
  97.     }
  98. }
  99.  
  100. ins_the_toks ()
  101. {
  102.     the_toks();
  103.     ins_list(token_link(temp_toks));
  104. }
  105.  
  106. conv_toks ()
  107. {
  108.     int     c;
  109.     int     old_setting;
  110.     int     save_scanner_status;
  111.  
  112.     c = cur_chr;
  113.     switch (c)
  114.     {
  115.     case NUMBER_CODE: 
  116.     case ROMAN_NUMERAL_CODE:
  117.         scan_int();
  118.         break;
  119.  
  120.     case STRING_CODE:
  121.     case MEANING_CODE:
  122.         save_scanner_status = scanner_status;
  123.         scanner_status = NORMAL;
  124.         get_token();
  125.         scanner_status = save_scanner_status;
  126.         break;
  127.     
  128.     case FONT_NAME_CODE:
  129.         scan_font_ident();
  130.         break;
  131.  
  132.     case JOB_NAME_CODE:
  133.         if (job_name == 0)
  134.             open_log_file();
  135.         break;
  136.     }
  137.     old_setting = selector;
  138.     selector = NEW_STRING;
  139.     switch (c)
  140.     {
  141.     case NUMBER_CODE:
  142.         print_val(cur_val);
  143.         break;
  144.     
  145.     case ROMAN_NUMERAL_CODE:
  146.         print_roman_int(cur_val);
  147.         break;
  148.  
  149.     case STRING_CODE:
  150.         if (cur_cs != 0)
  151.             sprint_cs(cur_cs);
  152.         else print_char((ascii) cur_chr);
  153.         break;
  154.     
  155.     case MEANING_CODE:
  156.         print_meaning();
  157.         break;
  158.  
  159.     case FONT_NAME_CODE:
  160.         print_str(font_name[cur_val]);
  161.         if (font_size[cur_val] != font_dsize[cur_val]) {
  162.             print(" at ");
  163.             print_scaled(font_size[cur_val]);
  164.             print("pt");
  165.         }
  166.         break;
  167.  
  168.     case JOB_NAME_CODE:
  169.         print_str(job_name);
  170.         break;
  171.     }
  172.     selector = old_setting; 
  173.     str_toks();
  174.     ins_list(token_link(temp_toks));
  175. }
  176.  
  177. ptr
  178. scan_toks (macro_def, xpand)
  179.     bool    macro_def;
  180.     bool    xpand;
  181. {
  182.     ptr     p;
  183.     ptr     q;
  184.     tok     s;
  185.     tok     t;
  186.     int     unbalance;
  187.     tok     hash_brace;
  188.  
  189.     if (macro_def)
  190.         scanner_status = DEFINING;
  191.     else scanner_status = ABSORBING;
  192.     warning_index = cur_cs;
  193.     def_ref = new_token();
  194.     token_ref_count(def_ref) = NULL;
  195.     p = def_ref;
  196.     hash_brace = 0;
  197.     t = ZERO_TOKEN;
  198.     if (macro_def) {
  199.         loop {
  200.             get_token();
  201.             if (cur_tok < RIGHT_BRACE_LIMIT)
  202.                 break;
  203.             if (cur_cmd == MAC_PARAM) {
  204.                 s = MATCH_TOKEN + cur_chr;
  205.                 get_token();
  206.                 if (cur_cmd == LEFT_BRACE) {
  207.                     hash_brace = cur_tok; 
  208.                     store_new_token(cur_tok);
  209.                     store_new_token(END_MATCH_TOKEN);
  210.                     goto done;
  211.                 }
  212.                 if (t == ZERO_TOKEN + 9) {
  213.                     print_err("You already have nine parameters");
  214.                     help_param_count();
  215.                     error();
  216.                 } else {
  217.                     incr(t);
  218.                     if (cur_tok != t) {
  219.                         print_err("Parameters must be numbered consecutively");
  220.                         help_param_num();
  221.                         back_error();
  222.                     }
  223.                     cur_tok = s;
  224.                 }
  225.             }
  226.             store_new_token(cur_tok);
  227.         }
  228.         store_new_token(END_MATCH_TOKEN);
  229.         if (cur_cmd == RIGHT_BRACE) {
  230.             print_err("Missing { inserted");
  231.             incr(align_state); 
  232.             help_left_brace();
  233.             error();
  234.             goto found;
  235.         }
  236.     } else 
  237.         scan_left_brace();
  238.  
  239. done:
  240.     unbalance = 1;
  241.     loop {
  242.         if (xpand) {
  243.             loop {
  244.                 get_next();
  245.                 if (cur_cmd <= MAX_COMMAND)
  246.                     break;
  247.                 if (cur_cmd != THE)
  248.                     expand();
  249.                 else {
  250.                     q = the_toks(); 
  251.                     if (token_link(temp_toks) != NULL) {
  252.                         token_link(p) = token_link(temp_toks);
  253.                         p = q;
  254.                     }
  255.                 }
  256.             }
  257.             x_token();
  258.         } else get_token();
  259.         if (cur_tok < RIGHT_BRACE_LIMIT) {
  260.             if (cur_cmd < RIGHT_BRACE) {
  261.                 incr(unbalance);
  262.             } else {
  263.                 decr(unbalance);
  264.                 if (unbalance == 0)
  265.                     break;
  266.             }
  267.         } else if (cur_cmd == MAC_PARAM && macro_def) {
  268.             s = cur_tok;
  269.             if (xpand)
  270.                 get_x_token();
  271.             else get_token();
  272.             if (cur_cmd != MAC_PARAM) {
  273.                 if (cur_tok <= ZERO_TOKEN || cur_tok > t) {
  274.                     print_err("Illegal parameter number in definition of ");
  275.                     sprint_cs(warning_index);
  276.                     help_param_use();
  277.                     back_error(); 
  278.                     cur_tok = s;
  279.                 } else {
  280.                     cur_tok = OUT_PARAM_TOKEN + cur_chr - '0';
  281.                 }
  282.             }
  283.         }
  284.         store_new_token(cur_tok);
  285.     }
  286.  
  287. found:
  288.     scanner_status = NORMAL;
  289.     if (hash_brace != 0)
  290.         store_new_token(hash_brace);
  291.     return p;
  292. }
  293.  
  294. read_toks (n, r)
  295.     int     n;
  296.     ptr     r;
  297. {
  298.     int     m;
  299.     ptr     p;
  300.     ptr     q;
  301.     val     s;
  302.  
  303.     scanner_status  = DEFINING;
  304.     warning_index = r;
  305.     def_ref = new_token();
  306.     token_ref_count(def_ref) = NULL;
  307.     p = def_ref;
  308.     store_new_token(END_MATCH_TOKEN);
  309.     if (n < 0 || n > 15)
  310.         m = 16;
  311.     else m = n;
  312.     s = align_state;
  313.     align_state = 1000000;
  314.     do {
  315.         begin_file_reading();
  316.         name = m + 1;
  317.         if (read_open[m] == CLOSED) {
  318.             if (interaction > NONSTOP_MODE) {
  319.                 if (n < 0) {
  320.                     prompt_input("");
  321.                 } else {
  322.                     wake_up_terminal();
  323.                     print_ln();
  324.                     sprint_cs(r);
  325.                     prompt_input("=");
  326.                     n = -1;
  327.                 }
  328.             } else 
  329.                 fatal_error
  330.                     ("*** (cannot \\read from terminal in nonstop modes)");
  331.         } else if (read_open[m] == JUST_OPENED) {
  332.             if (input_ln(read_file[m], FALSE)) {
  333.                 read_open[m] = NORMAL;
  334.             } else {    
  335.                 a_close(read_file[m]);
  336.                 read_open[m] = CLOSED;
  337.             }
  338.         } else {
  339.             if (!input_ln(read_file[m], TRUE)) {
  340.                 a_close(read_file[m]);
  341.                 read_open[m] = CLOSED;
  342.                 if (align_state != 1000000) {
  343.                     runaway();
  344.                     print_err("File ended within ");
  345.                     print_esc("read");
  346.                     help_read();
  347.                     align_state = 1000000;
  348.                     error();
  349.                 }
  350.             }
  351.         }
  352.         limit = last;
  353.         if (end_line_char < 0 || end_line_char > 255)
  354.             decr(limit);
  355.         else buffer[limit] = end_line_char;
  356.         first = limit + 1;
  357.         loc = start;
  358.         state = NEW_LINE;
  359.         loop {
  360.             get_token();
  361.             if (cur_tok == 0)
  362.                 break; 
  363.             store_new_token(cur_tok);
  364.         }
  365.         end_file_reading();
  366.     } while (align_state != 1000000);
  367.     cur_val = def_ref;
  368.     scanner_status = NORMAL; 
  369.     align_state = s;
  370. }
  371.  
  372. show_token_list (p, q, l)
  373.     ptr     p;
  374.     ptr     q;
  375.     val     l;
  376. {
  377.     hword   c;
  378.     hword   m;
  379.     ascii   n;
  380.     ascii   match_chr;
  381.  
  382.     match_chr = '#';
  383.     n = '0';
  384.     for (tally = 0; p != NULL && tally < l; p = token_link(p)) {
  385.         if (p == q)
  386.             magic_c();
  387.         if (p > tok_end) {
  388.             print_esc("CLOBBERED.");
  389.             return;
  390.         }
  391.         if (token(p) >= CS_TOKEN_FLAG) {
  392.             print_cs(token(p) - CS_TOKEN_FLAG);
  393.         } else {
  394.             m = token(p) / 0400;
  395.             c = token(p) % 0400;
  396.             if (token(p) < 0 || c > 255) {
  397.                 print_esc("BAD.");
  398.             } else {
  399.                 switch (m)
  400.                 {
  401.                 case LEFT_BRACE:
  402.                 case RIGHT_BRACE:
  403.                 case MATH_SHIFT:
  404.                 case TAB_MARK:
  405.                 case SUP_MARK:
  406.                 case SUB_MARK:
  407.                 case SPACER:
  408.                 case LETTER:
  409.                 case OTHER_CHAR:
  410.                     print_str(c); 
  411.                     break;
  412.                 
  413.                 case MAC_PARAM:
  414.                     print_str(c);
  415.                     print_str(c);
  416.                     break;
  417.                 
  418.                 case OUT_PARAM:
  419.                     print_str(match_chr);
  420.                     if (c <= 9) {
  421.                         print_char(c + '0');
  422.                     } else {
  423.                         print_char('!');
  424.                         return;
  425.                     }
  426.                     break;
  427.                 
  428.                 case MATCH:
  429.                     match_chr = c;
  430.                     print_str(c);
  431.                     incr(n);
  432.                     print_char(n);
  433.                     if (n > '9')
  434.                         return;
  435.                     break;
  436.                 
  437.                 case END_MATCH:
  438.                     print("->");
  439.                     break;
  440.                 
  441.                 default:
  442.                     print_esc("BAD.");
  443.                     break;
  444.                 }
  445.             }
  446.         }
  447.     }
  448.     if (p != NULL)
  449.         print_esc("ETC.");
  450. }
  451.  
  452. token_show (p)
  453.     ptr     p;
  454.     if (p == NULL)  
  455.         print("(null)");
  456.     else show_token_list(token_link(p), NULL, 1000L);
  457. }
  458.  
  459. print_meaning ()
  460. {
  461.     print_cmd_chr(cur_cmd, cur_chr);
  462.     if (cur_cmd >= CALL) {
  463.         print_char(':');
  464.         print_ln();
  465.         token_show(cur_chr);
  466.     } else if (cur_cmd == TOP_BOT_MARK) {
  467.         print_char(':');
  468.         print_ln();
  469.         token_show(cur_mark[cur_chr]);
  470.     }
  471. }
  472.  
  473. flush_list (p)
  474.     ptr     p;
  475. {
  476.     ptr     q;
  477.     ptr     r;
  478.  
  479.     if (p != NULL) {
  480.         r = p;
  481.         do {
  482.             q = r;
  483.             r = token_link(r);
  484. #ifdef  STAT
  485.             decr(tok_used);
  486. #endif
  487.         } while (r != NULL);
  488.         token_link(q) = tok_head;
  489.         tok_head = p;
  490.     }
  491. }
  492.  
  493. /*
  494.  *  Help text
  495.  */
  496.  
  497. help_param_num ()
  498. {
  499.     help2("I've inserted the digit you should have used after the #.",
  500.     "Type `1' to delete what you did use.");
  501. }
  502.  
  503. help_param_count ()
  504. {
  505.     help1("I'm going to ignore the # sign you just used.");
  506. }
  507.  
  508. help_left_brace ()
  509. {
  510.     help2("Where was the left brace? You said something like `\\def\\a}',",
  511.     "which I'm going to interpret as `\\def\\a{}'.");
  512. }
  513.  
  514. help_param_use ()
  515. {
  516.     help3("You meant to type ## instead of #, right?",
  517.     "Or maybe a } was forgotten somewhere earlier, and things",
  518.     "are all screwed up? I'm going to assume that you meant ##.");
  519. }
  520.  
  521. help_read ()
  522. {
  523.     help1("This \\read has unbalanced braces.");
  524. }
  525.