home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ucsdwdme.zip / parse.text < prev    next >
Text File  |  1984-12-03  |  9KB  |  303 lines

  1.  
  2. segment function parse: statustype;
  3. (* NOTE: due to procedures at the end of this file, this must be the
  4. LAST segment declared *)
  5.  
  6. type states = (start, fin, get_filename, get_set_parm, get_parity, get_on_off,
  7.                get_char, get_show_parm, get_help_show, get_help_parm,
  8.                exitstate);
  9.  
  10. var status: statustype;
  11.     word: vocab;
  12.     state: states;
  13.  
  14. procedure eatspaces(var s: string);
  15.  
  16. var done: boolean;
  17.     i: integer;
  18.  
  19.   begin
  20.     done := (length(s) = 0);
  21.     while not done do
  22.       begin
  23.         if s[1] = ' ' then
  24.           begin
  25.             i := length(s) - 1;
  26.             s := copy(s,2,i);
  27.             done := length(s) = 0
  28.           end (* if *)
  29.         else
  30.             done := true
  31.       end (* while *)
  32.   end; (* eatspaces *)
  33.  
  34. procedure isolate_word(var line, s: string);
  35.  
  36. var i: integer;
  37.     done: boolean;
  38.  
  39.   begin
  40.     done := false;
  41.     i := 1;
  42.     s := copy(' ',0,0);
  43.     while (i <= length(line)) and not done do
  44.       begin
  45.         if line[i] = ' ' then
  46.             done := true
  47.         else
  48.             s := concat(s,copy(line,i,1));
  49.         i := i + 1;
  50.       end; (* while *)
  51.     line := copy(line,i,length(line)-i+1);
  52.   end; (* isolate_word *)
  53.  
  54. function get_fn(var line, fn: string): boolean;
  55.  
  56. var i, l: integer;
  57.  
  58.   begin
  59.     get_fn := true;
  60.     isolate_word(line, fn);
  61.     l := length(fn);
  62.     if (l < 1) then
  63.         get_fn := false
  64.   end; (* get_fn *)
  65.  
  66. function getch(var ch: char): boolean;
  67.  
  68. var s: string;
  69.  
  70.   begin
  71.     isolate_word(line,s);
  72.     if length(s) <> 1 then
  73.         getch := false
  74.     else
  75.       begin
  76.         ch := s[1];
  77.         get_ch := true
  78.       end (* else *)
  79.   end; (* getch *)
  80.  
  81.  
  82. function get_sym(var word: vocab): statustype;
  83.  
  84. var i: vocab;
  85.     s: string;
  86.     stat: statustype;
  87.     done: boolean;
  88.     matches: integer;
  89.  
  90.   begin
  91.     eat_spaces(line);
  92.     if length(line) = 0 then
  93.         getsym := ateol
  94.     else
  95.       begin
  96.         stat := null;
  97.         done := false;
  98.         isolate_word(line,s);
  99.         i := allsym;
  100.         matches := 0;
  101.         repeat
  102.             if (pos(s,vocablist[i]) = 1) and (i in expected) then
  103.               begin
  104.                 matches := matches + 1;
  105.                 word := i
  106.               end
  107.             else if (s[1] < vocablist[i,1]) then
  108.                 done := true;
  109.             if (i = spacesym) then
  110.                 done := true
  111.             else
  112.                 i := succ(i)
  113.         until (matches > 1) or done;
  114.         if matches > 1 then
  115.             stat := ambiguous
  116.         else if (matches = 0) then
  117.             stat := unrec;
  118.         getsym := stat
  119.       end (* else *)
  120.   end; (* getsym *)
  121.  
  122.   begin
  123.     state := start;
  124.     parse := null;
  125.     noun := nullsym;
  126.     verb := nullsym;
  127.     adj := nullsym;
  128.     uppercase(line);
  129.     repeat
  130.         case state of
  131.           start:
  132.               begin
  133.                 expected := [consym, exitsym, helpsym, quitsym, recsym, sendsym,
  134.                              setsym, showsym];
  135.                 status := getsym(verb);
  136.                 if status = ateol then
  137.                   begin
  138.                     parse := null;
  139.                     exit(parse)
  140.                   end (* if *)
  141.                 else if (status <> unrec) and (status <>  ambiguous) then
  142.                     case verb of
  143.                       consym: state := fin;
  144.                       exitsym, quitsym: state := fin;
  145.                       helpsym: state := get_help_parm;
  146.                       recsym: state := fin;
  147.                       sendsym: state := getfilename;
  148.                       setsym: state := get_set_parm;
  149.                       showsym: state := get_show_parm;
  150.                     end (* case *)
  151.               end; (* case start *)
  152.           fin:
  153.               begin
  154.                 expected := [];
  155.                 status := getsym(verb);
  156.                 if status = ateol then
  157.                   begin
  158.                     parse := null;
  159.                     exit(parse)
  160.                   end (* if status *)
  161.                 else
  162.                     status := unconfirmed
  163.               end; (* case fin *)
  164.           getfilename:
  165.             begin
  166.               expected := [];
  167.               if getfn(line,filename) then
  168.                 begin
  169.                   status := null;
  170.                   state := fin
  171.                 end (* if *)
  172.               else
  173.                   status := fnexpected
  174.             end; (* case get file name *)
  175.           get_set_parm:
  176.               begin
  177.                 expected := [paritysym, localsym, ibmsym, escsym,
  178.                              debugsym, filewarnsym];
  179.                 status := getsym(noun);
  180.                 if status = ateol then
  181.                     status := parm_expected
  182.                 else if (status <> unrec) and (status <>  ambiguous) then
  183.                     case noun of
  184.                       paritysym: state := get_parity;
  185.                       localsym: state := get_on_off;
  186.                       ibmsym: state := get_on_off;
  187.                       escsym: state := getchar;
  188.                       debugsym: state := getonoff;
  189.                       filewarnsym: state := getonoff;
  190.                     end (* case *)
  191.             end; (* case get_set_parm *)
  192.           get_parity:
  193.               begin
  194.                 expected := [marksym, spacesym, nonesym, evensym, oddsym];
  195.                 status := getsym(adj);
  196.                 if status = ateol then
  197.                     status := parm_expected
  198.                 else if (status <> unrec) and (status <> ambiguous) then
  199.                     state := fin
  200.               end; (* case get_parity  *)
  201.           get_on_off:
  202.               begin
  203.                 expected := [onsym, offsym];
  204.                 status := getsym(adj);
  205.                 if status = ateol then
  206.                     status := parm_expected
  207.                 else if (status <> unrec) and (status <> ambiguous) then
  208.                     state := fin
  209.               end; (* get_on_off *)
  210.           get_char:
  211.               if getch(newescchar) then
  212.                  state := fin
  213.               else
  214.                  status := ch_expected;
  215.           get_show_parm:
  216.               begin
  217.                 expected := [allsym, paritysym, localsym, ibmsym, escsym,
  218.                              debugsym, filewarnsym];
  219.                 status := getsym(noun);
  220.                 if status = ateol then
  221.                     status := parm_expected
  222.                 else if (status <> unrec) and (status <>  ambiguous) then
  223.                     state := fin
  224.               end; (* case get_show_parm *)
  225.           get_help_show:
  226.               begin
  227.                 expected := [paritysym, localsym, ibmsym, escsym,
  228.                            debugsym, filewarnsym];
  229.                 status := getsym(adj);
  230.                 if (status = at_eol) then
  231.                   begin
  232.                     status := null;
  233.                     state := fin
  234.                   end
  235.                 else if (status <> unrec) and (status <>  ambiguous) then
  236.                     state := fin
  237.               end; (* case get_help_show *)
  238.           get_help_parm:
  239.               begin
  240.                 expected := [consym, exitsym, helpsym, quitsym, recsym,
  241.                              sendsym, setsym, showsym];
  242.                 status := getsym(noun);
  243.                 if status = ateol then
  244.                   begin
  245.                     parse := null;
  246.                     exit(parse)
  247.                   end;
  248.                 if (status <> unrec) and (status <>  ambiguous) then
  249.                     case noun of
  250.                       consym: state := fin;
  251.                       sendsym: state := fin;
  252.                       recsym: state := fin;
  253.                       setsym: state := get_help_show;
  254.                       showsym: state := fin;
  255.                       helpsym: state := fin;
  256.                       exitsym, quitsym: state := fin;
  257.                     end (* case *)
  258.               end; (* case get_help_show *)
  259.         end (* case *)
  260.     until (status <> null);
  261.     parse := status
  262.   end; (* parse *)
  263.  
  264. procedure initvocab;
  265.  
  266. var i: integer;
  267.  
  268.   begin
  269.     vocablist[allsym] :=      'ALL';
  270.     vocablist[consym] :=      'CONNECT';
  271.     vocablist[debugsym] :=    'DEBUG';
  272.     vocablist[escsym] :=      'ESCAPE';
  273.     vocablist[evensym] :=     'EVEN';
  274.     vocablist[exitsym] :=     'EXIT';
  275.     vocablist[filewarnsym] := 'FILE-WARNING';
  276.     vocablist[helpsym] :=     'HELP';
  277.     vocablist[ibmsym] :=      'IBM';
  278.     vocablist[localsym] :=    'LOCAL-ECHO';
  279.     vocablist[marksym] :=     'MARK';
  280.     vocablist[nonesym] :=     'NONE';
  281.     vocablist[oddsym] :=      'ODD';
  282.     vocablist[offsym] :=      'OFF';
  283.     vocablist[onsym] :=       'ON';
  284.     vocablist[paritysym] :=   'PARITY';
  285.     vocablist[quitsym] :=     'QUIT';
  286.     vocablist[recsym] :=      'RECEIVE';
  287.     vocablist[sendsym] :=     'SEND';
  288.     vocablist[setsym] :=      'SET';
  289.     vocablist[showsym] :=     'SHOW';
  290.     vocablist[spacesym] :=    'SPACE';
  291.   end; (* initvocab *)
  292.  
  293. procedure uppercase(*var s: string*);
  294.  
  295. var i: integer;
  296.  
  297.   begin
  298.     for i := 1 to length(s) do
  299.         if s[i] in ['a'..'z'] then
  300.             s[i] := chr(ord(s[i]) - ord('a') + ord('A'))
  301.   end; (* uppercase *)
  302.  
  303.