home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / LIBRARY / CHAR.SA < prev    next >
Text File  |  1995-02-05  |  20KB  |  419 lines

  1. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  2. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  3. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  4. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  5. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  6. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  7.  
  8. -- char.sa: Characters.
  9. -------------------------------------------------------------------
  10. value class CHAR < $IS_EQ{CHAR}, $IS_LT{CHAR}, $HASH is
  11.    -- Objects which represent characters.
  12.    -- This version is for ASCII. Other implementations might use
  13.    -- UNICODE, etc.
  14.    
  15.    -- AVAL isn't handled right yet, leave out for the moment.
  16.    include AVAL{BOOL} asize->;
  17.    
  18.    const asize:=SYS::char_size;
  19.  
  20.    is_alpha:BOOL is
  21.       -- True if self is an alphabetic character.
  22.       case self
  23.       when 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  24.      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  25.      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
  26.      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
  27.       then return true 
  28. --    else return false end end;                                                -- NLP
  29.       else; end; return false; end;                                             -- NLP
  30.  
  31.    is_upper:BOOL is
  32.       -- True if self is uppercase.
  33.       case self
  34.       when 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  35.      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' 
  36.       then return true
  37. --    else return false end end;                                                -- NLP
  38.       else; end; return false; end;                                             -- NLP
  39.    
  40.    is_lower:BOOL is
  41.       -- True if self is lowercase.
  42.       case self
  43.       when 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  44.      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' 
  45.       then return true
  46. --    else return false end end;                                                -- NLP
  47.       else; end; return false; end;                                             -- NLP
  48.  
  49.    is_octal_digit:BOOL is
  50.       -- True if self is an octal digit.
  51.       case self
  52.       when '0', '1', '2', '3', '4', '5', '6', '7'
  53.       then return true
  54. --    else return false end end;                                                -- NLP
  55.       else; end; return false; end;                                             -- NLP
  56.       
  57.    octal_digit_value:INT is
  58.       -- The numerical value of self as an octal digit. -1 if not.
  59.       case self 
  60.       when '0' then return 0   when '1' then return 1
  61.       when '2' then return 2   when '3' then return 3
  62.       when '4' then return 4   when '5' then return 5
  63.       when '6' then return 6   when '7' then return 7 
  64. --    else return -1 end end;                                                   -- NLP
  65.       else; end; return -1; end;                                                -- NLP
  66.       
  67.    is_digit:BOOL is 
  68.       -- True if self is a digit.
  69.       case self
  70.       when '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 
  71.       then return true
  72. --    else return false end end;                                                -- NLP
  73.       else; end; return false; end;                                             -- NLP
  74.  
  75.    digit_value:INT is
  76.       -- The numerical value of self as a decimal digit.
  77.       -- -1 if it isn't.
  78.       case self 
  79.       when '0' then return 0   when '1' then return 1
  80.       when '2' then return 2   when '3' then return 3
  81.       when '4' then return 4   when '5' then return 5
  82.       when '6' then return 6   when '7' then return 7
  83.       when '8' then return 8   when '9' then return 9 
  84. --    else return -1 end end;                                                   -- NLP
  85.       else; end; return -1; end;                                                -- NLP
  86.    
  87.    is_hex_digit:BOOL is 
  88.       -- True if self is a hexadecimal digit.
  89.       case self
  90.       when '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  91.      'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'
  92.       then return true
  93. --    else return false end end;                                                -- NLP
  94.       else; end; return false; end;                                             -- NLP
  95.  
  96.    hex_digit_value:INT is
  97.       -- The numerical value of self as a hexadecimal digit.
  98.       -- -1 if it isn't.
  99.       case self 
  100.       when '0' then return 0   when '1' then return 1
  101.       when '2' then return 2   when '3' then return 3
  102.       when '4' then return 4   when '5' then return 5
  103.       when '6' then return 6   when '7' then return 7
  104.       when '8' then return 8   when '9' then return 9 
  105.       when 'a','A' then return 10  when 'b','B' then return 11 
  106.       when 'c','C' then return 12  when 'd','D' then return 13 
  107.       when 'e','E' then return 14  when 'f','F' then return 15 
  108. --    else return -1 end end;                                                   -- NLP
  109.       else; end; return -1; end;                                                -- NLP
  110.    
  111.    is_alphanum:BOOL is
  112.       -- True if self is alphanumeric.
  113.       case self
  114.       when 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
  115.      'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 
  116.      'z',
  117.      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
  118.      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  119.      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 
  120.       then return true
  121. --    else return false end end;                                                -- NLP
  122.       else; end; return false; end;                                             -- NLP
  123.    
  124.    is_space:BOOL is
  125.       -- True if self is one of the whitespace characters:
  126.       -- space, form feed, newline, carriage return, tab, vertical tab.
  127.       -- NOTE: temporarily Ctrl-Z will be treated as whitespace. This
  128.       --       ensures OS/2 compatibility until FILE classes are fixed.
  129.       case self
  130.       when ' ', '\f', '\n', '\r', '\t', '\v', '\032'
  131.       then return true
  132. --    else return false end end;                                                -- NLP
  133.       else; end; return false; end;                                             -- NLP
  134.  
  135.    is_print:BOOL is
  136.       -- True if self is a printing character.
  137.       case self
  138.       when ' ', 
  139.      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  140.      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  141.      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 
  142.      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  143.      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  144.      '!', '\"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', 
  145.      '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', 
  146.      ']', '^', '_',    '`', '{', '|', '}', '~',','
  147.       then return true
  148. --    else return false end end;                                                -- NLP
  149.       else; end; return false; end;                                             -- NLP
  150.    
  151.    is_punct:BOOL is 
  152.       -- True if self is punctuation.
  153.       case self
  154.       when '!', '\"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', 
  155.      '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', 
  156.      ']', '^', '_',    '`', '{', '|', '}','~',','
  157.       then return true
  158. --    else return false end end;                                                -- NLP
  159.       else; end; return false; end;                                             -- NLP
  160.  
  161.    is_control:BOOL is
  162.       -- True if self is a control character.
  163.       return self.int<32;
  164.    end;
  165.  
  166.    int:INT is
  167.       -- The integer version of self. Built-in.
  168.       raise "CHAR::int:INT undefined";
  169.       end;
  170.  
  171.    hash:INT is
  172.       -- A very cheap, but not very good hash value. It just returns 
  173.       -- the integer representation of the char.
  174.       return int; end;
  175.       
  176.    ascii_int:INT is
  177.       -- The ASCII integer associated with each character in the
  178.       -- minimal Sather character set. Returns -1 for other characters.
  179.       case self
  180.       -- This case commented out due to a bug in gdb not recognizing \a
  181.       -- when '\a' then return 7
  182.       when '\b' then return 8      
  183.       when '\t' then return 9       when '\n' then return 10      
  184.       when '\v' then return 11      when '\r' then return 13
  185.       when ' ' then return 32       when '!' then return 33 
  186.       when '\"' then return 34      when '#' then return 35 
  187.       when '$' then return 36       when '%' then return 37 
  188.       when '&' then return 38       when '\'' then return 39
  189.       when '(' then return 40       when ')' then return 41
  190.       when '*' then return 42       when '+' then return 43
  191.       when ',' then return 44       when '-' then return 45
  192.       when '.' then return 46       when '/' then return 47
  193.       when '0' then return 48       when '1' then return 49
  194.       when '2' then return 50       when '3' then return 51
  195.       when '4' then return 52       when '5' then return 53
  196.       when '6' then return 54       when '7' then return 55
  197.       when '8' then return 56       when '9' then return 57
  198.       when ':' then return 58       when ';' then return 59
  199.       when '<' then return 60       when '=' then return 61
  200.       when '>' then return 62       when '?' then return 63
  201.       when '@' then return 64       when 'A' then return 65
  202.       when 'B' then return 66       when 'C' then return 67
  203.       when 'D' then return 68       when 'E' then return 69
  204.       when 'F' then return 70       when 'G' then return 71
  205.       when 'H' then return 72       when 'I' then return 73
  206.       when 'J' then return 74       when 'K' then return 75
  207.       when 'L' then return 76       when 'M' then return 77
  208.       when 'N' then return 78       when 'O' then return 79
  209.       when 'P' then return 80       when 'Q' then return 81
  210.       when 'R' then return 82       when 'S' then return 83
  211.       when 'T' then return 84       when 'U' then return 85
  212.       when 'V' then return 86       when 'W' then return 87
  213.       when 'X' then return 88       when 'Y' then return 89
  214.       when 'Z' then return 90       when '[' then return 91
  215.       when '\\' then return 92      when ']' then return 93
  216.       when '^' then return 94       when '_' then return 95
  217.       when '`' then return 96       when 'a' then return 97
  218.       when 'b' then return 98       when 'c' then return 99
  219.       when 'd' then return 100      when 'e' then return 101
  220.       when 'f' then return 102      when 'g' then return 103
  221.       when 'h' then return 104      when 'i' then return 105
  222.       when 'j' then return 106      when 'k' then return 107
  223.       when 'l' then return 108      when 'm' then return 109
  224.       when 'n' then return 110      when 'o' then return 111
  225.       when 'p' then return 112      when 'q' then return 113
  226.       when 'r' then return 114      when 's' then return 115
  227.       when 't' then return 116      when 'u' then return 117
  228.       when 'v' then return 118      when 'w' then return 119
  229.       when 'x' then return 120      when 'y' then return 121
  230.       when 'z' then return 122      when '{' then return 123
  231.       when '|' then return 124      when '}' then return 125
  232.       when '~' then return 126      
  233. --    else return -1 end end;                                                   -- NLP
  234.       else; end; return -1; end;                                                -- NLP
  235.  
  236.    from_ascii_int(i:INT):SAME 
  237.       -- The ASCII character corresponding to `i' for characters
  238.       -- in the Sather set, and the character with code `i' for
  239.       -- the remainder.
  240.       pre i.is_bet(0,127) is
  241.       return ("\0\1\2\3\4\5\6\a"  "\b\t\n\v\14\r\16\17"
  242.       "\20\21\22\23\24\25\26\27" "\30\31\32\33\34\35\36\37"
  243.       " !\"#$%&'"  "()*+,-./"  "01234567"  "89:;<=>?"  "@ABCDEFG"
  244.       "HIJKLMNO"  "PQRSTUVW"  "XYZ[\\]^_"  "`abcdefg"  "hijklmno"
  245.       "pqrstuvw"  "xyz{|}~\177")[i] end;      
  246.  
  247.    str:STR is
  248.       return ""+self; end;
  249.       
  250.    pretty:STR is
  251.       -- A pretty version of self. It includes single quotes
  252.       -- around self and uses special codes or the octal representation
  253.       -- for non printing characters.
  254.       if is_print and self/='\'' and self/='\\' then 
  255.      return "'" + self + "'" end; 
  256.       case self 
  257.       when '\a' then return "'\\a'"  when '\b' then return "'\\b'"     
  258.       when '\f' then return "'\\f'"  when '\n' then return "'\\n'"
  259.       when '\r' then return "'\\r'"  when '\t' then return "'\\t'"     
  260.       when '\v' then return "'\\v'"  when '\\' then return "'\\\\'"
  261.       when '\'' then return "'\\''"  
  262. --    else s::=int.octal_str; return "'\\".append(s.tail(s.size-2),"'")         -- NLP
  263.       else; end; s::=int.octal_str; return "'\\".append(s.tail(s.size-2),"'");  -- NLP
  264. --    end end;                                                                  -- NLP
  265.       end;                                                                      -- NLP
  266.  
  267.    upper:CHAR is 
  268.       -- An upper case version of self. 
  269.       -- Leaves non-alphabetic chars unchanged.
  270.       case self
  271.       when 'a' then return 'A'      when 'b' then return 'B'  
  272.       when 'c' then return 'C'      when 'd' then return 'D'  
  273.       when 'e' then return 'E'      when 'f' then return 'F'
  274.       when 'g' then return 'G'      when 'h' then return 'H'  
  275.       when 'i' then return 'I'      when 'j' then return 'J'  
  276.       when 'k' then return 'K'      when 'l' then return 'L'
  277.       when 'm' then return 'M'      when 'n' then return 'N'  
  278.       when 'o' then return 'O'      when 'p' then return 'P'  
  279.       when 'q' then return 'Q'      when 'r' then return 'R'
  280.       when 's' then return 'S'      when 't' then return 'T'  
  281.       when 'u' then return 'U'      when 'v' then return 'V'  
  282.       when 'w' then return 'W'      when 'x' then return 'X'
  283.       when 'y' then return 'Y'      when 'z' then return 'Z'
  284. --    else return self end end;                                                 -- NLP
  285.       else; end; return self; end;                                              -- NLP
  286.      
  287.    lower:CHAR is 
  288.       -- A lower case version of self.
  289.       -- Leaves non-alphabetic chars unchanged.
  290.       case self
  291.       when 'A' then return 'a'      when 'B' then return 'b'  
  292.       when 'C' then return 'c'      when 'D' then return 'd'  
  293.       when 'E' then return 'e'      when 'F' then return 'f'
  294.       when 'G' then return 'g'      when 'H' then return 'h'  
  295.       when 'I' then return 'i'      when 'J' then return 'j'  
  296.       when 'K' then return 'k'      when 'L' then return 'l'
  297.       when 'M' then return 'm'      when 'N' then return 'n'  
  298.       when 'O' then return 'o'      when 'P' then return 'p'  
  299.       when 'Q' then return 'q'      when 'R' then return 'r'
  300.       when 'S' then return 's'      when 'T' then return 't'  
  301.       when 'U' then return 'u'      when 'V' then return 'v'  
  302.       when 'W' then return 'w'      when 'X' then return 'x'
  303.       when 'Y' then return 'y'      when 'Z' then return 'z'
  304. --    else return self end end;                                                 -- NLP
  305.       else; end; return self; end;                                              -- NLP
  306.  
  307.    is_eq(c:SAME):BOOL is
  308.       -- True if self and `c' are equal.
  309.       raise "CHAR::is_eq(CHAR):BOOL undefined" end;
  310.    
  311.    is_neq(c:SAME):BOOL is
  312.       -- True if self and `c' are equal.
  313.       raise "CHAR::is_neq(CHAR):BOOL undefined" end;
  314.    
  315.    is_lt(c:SAME):BOOL is 
  316.       -- True if self is earlier than `c' in the ordering of
  317.       -- characters.
  318.       return int < c.int end;
  319.  
  320.    is_leq(c:SAME):BOOL is
  321.       -- True if self is earlier than `c' in the ordering of
  322.       -- characters or is equal to it.
  323.       return int <= c.int end;
  324.    
  325.    is_gt(c:SAME):BOOL is 
  326.       -- True if self is after `c' in the ordering of characters.
  327.       return int > c.int end;
  328.    
  329.    is_geq(c:SAME):BOOL is
  330.       -- True if self is later than `c' in the ordering of
  331.       -- characters or is equal to it.
  332.       return int >= c.int end;      
  333.  
  334. end; -- value class CHAR
  335.  
  336. -------------------------------------------------------------------   
  337. class TEST_CHAR is
  338.    -- Test of `CHAR'.
  339.    
  340.    include TEST;
  341.    
  342.    main is
  343.       -- Test `CHAR'.
  344.       class_name("CHAR");
  345.      -- is_alpha
  346.       test("' '.is_alpha", ' '.is_alpha.str, "false");
  347.       test("'.'.is_alpha", '.'.is_alpha.str, "false");
  348.       test("'1'.is_alpha", '1'.is_alpha.str, "false");
  349.       test("'a'.is_alpha", 'a'.is_alpha.str, "true");
  350.       test("'A'.is_alpha", 'A'.is_alpha.str, "true");
  351.       test("3.char.is_alpha", 3.char.is_alpha.str, "false");
  352.      -- is_upper
  353.       test("' '.is_upper", ' '.is_upper.str, "false");
  354.       test("'.'.is_upper", '.'.is_upper.str, "false");
  355.       test("'1'.is_upper", '1'.is_upper.str, "false");
  356.       test("'a'.is_upper", 'a'.is_upper.str, "false");
  357.       test("'A'.is_upper", 'A'.is_upper.str, "true");
  358.       test("3.char.is_upper", 3.char.is_upper.str, "false");
  359.      -- is_lower
  360.       test("' '.is_lower", ' '.is_lower.str, "false");
  361.       test("'.'.is_lower", '.'.is_lower.str, "false");
  362.       test("'1'.is_lower", '1'.is_lower.str, "false");
  363.       test("'a'.is_lower", 'a'.is_lower.str, "true");
  364.       test("'A'.is_lower", 'A'.is_lower.str, "false");
  365.       test("3.char.is_lower", 3.char.is_lower.str, "false");
  366.      -- is_digit
  367.       test("' '.is_digit", ' '.is_digit.str, "false");
  368.       test("'.'.is_digit", '.'.is_digit.str, "false");
  369.       test("'1'.is_digit", '1'.is_digit.str, "true");
  370.       test("'a'.is_digit", 'a'.is_digit.str, "false");
  371.       test("'A'.is_digit", 'A'.is_digit.str, "false");
  372.       test("3.char.is_digit", 3.char.is_digit.str, "false");
  373.      -- is_alphanum
  374.       test("' '.is_alphanum", ' '.is_alphanum.str, "false");
  375.       test("'.'.is_alphanum", '.'.is_alphanum.str, "false");
  376.       test("'1'.is_alphanum", '1'.is_alphanum.str, "true");
  377.       test("'a'.is_alphanum", 'a'.is_alphanum.str, "true");
  378.       test("'A'.is_alphanum", 'A'.is_alphanum.str, "true");
  379.       test("3.char.is_alphanum", 3.char.is_alphanum.str, "false");      
  380.      -- is_space
  381.       test("' '.is_space", ' '.is_space.str, "true");
  382.       test("'.'.is_space", '.'.is_space.str, "false");
  383.       test("'1'.is_space", '1'.is_space.str, "false");
  384.       test("'a'.is_space", 'a'.is_space.str, "false");
  385.       test("'A'.is_space", 'A'.is_space.str, "false");
  386.       test("3.char.is_space", 3.char.is_space.str, "false");
  387.      -- is_print
  388.       test("' '.is_print", ' '.is_print.str, "true");
  389.       test("'.'.is_print", '.'.is_print.str, "true");
  390.       test("'1'.is_print", '1'.is_print.str, "true");
  391.       test("'a'.is_print", 'a'.is_print.str, "true");
  392.       test("'A'.is_print", 'A'.is_print.str, "true");
  393.       test("3.char.is_print", 3.char.is_print.str, "false");
  394.      -- is_punct
  395.       test("' '.is_punct", ' '.is_punct.str, "false");
  396.       test("'.'.is_punct", '.'.is_punct.str, "true");
  397.       test("'1'.is_punct", '1'.is_punct.str, "false");
  398.       test("'a'.is_punct", 'a'.is_punct.str, "false");
  399.       test("'A'.is_punct", 'A'.is_punct.str, "false");
  400.       test("3.char.is_punct", 3.char.is_punct.str, "false");
  401.      -- is_control
  402.       test("' '.is_control", ' '.is_control.str, "false");
  403.       test("'.'.is_control", '.'.is_control.str, "false");
  404.       test("'1'.is_control", '1'.is_control.str, "false");
  405.       test("'a'.is_control", 'a'.is_control.str, "false");
  406.       test("'A'.is_control", 'A'.is_control.str, "false");
  407.       test("3.char.is_control", 3.char.is_control.str, "true");
  408.      -- other
  409.       test("'a'.int", 'a'.int.str, "97");
  410.       test("'a'.str", 'a'.str, "a");
  411.       test("'a'.upper", 'a'.upper.str, "A");
  412.       test("'A'.lower", 'A'.lower.str, "a");
  413.       finish;
  414.    end; -- char_test
  415.    
  416. end; 
  417.  
  418. -------------------------------------------------------------------
  419.