home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / euphor10.zip / GET.E < prev    next >
Text File  |  1993-04-19  |  5KB  |  268 lines

  1. ---------------------------------------------------------
  2. -- read a Euphoria object from an input stream         --
  3. -- get(filenumber) returns {error_status, input_value} --
  4. ---------------------------------------------------------
  5.  
  6. -- error status values returned:
  7. global constant GET_SUCCESS = 0,
  8.         GET_EOF = -1,
  9.         GET_FAIL = 1
  10.  
  11. constant UNDEFINED_CHAR = -2
  12.  
  13. type positive_int(integer x)
  14.     return x >= 0
  15. end type
  16.  
  17. type char(integer x)
  18.     return x >= UNDEFINED_CHAR and x <= 255
  19. end type
  20.  
  21. positive_int input_file
  22.  
  23. char ungot_char
  24. ungot_char = UNDEFINED_CHAR
  25.  
  26.  
  27. function get_char()
  28. -- read next logical char in input stream
  29.     char temp
  30.  
  31.     if ungot_char = UNDEFINED_CHAR then
  32.     return getc(input_file)
  33.     else
  34.     temp = ungot_char
  35.     ungot_char = UNDEFINED_CHAR
  36.     return temp
  37.     end if
  38. end function
  39.  
  40.  
  41. procedure unget_char(char c)
  42. -- "unget" a character - push it back on the input stream
  43.     ungot_char = c
  44. end procedure
  45.  
  46.  
  47. procedure skip_blanks()
  48. -- skip white space
  49.     char c
  50.  
  51.     while 1 do     
  52.         c = get_char()
  53.     if not find(c, " \t\n") then
  54.         exit
  55.     end if
  56.     end while
  57.     unget_char(c)
  58. end procedure
  59.  
  60. constant ESCAPE_CHARS = "nt'\"\\r",
  61.      ESCAPED_CHARS = "\n\t'\"\\\r"
  62.  
  63. function escape_char(char c)
  64. -- return escape character
  65.     positive_int i
  66.  
  67.     i = find(c, ESCAPE_CHARS)
  68.     if i = 0 then
  69.     return GET_FAIL
  70.     else
  71.     return ESCAPED_CHARS[i]
  72.     end if
  73. end function
  74.  
  75.  
  76. function get_qchar()
  77. -- get a single quoted character
  78.  
  79.     char c
  80.  
  81.     c = get_char()
  82.     if c = '\\' then
  83.     c = escape_char(get_char())
  84.     if c = GET_FAIL then
  85.         return {GET_FAIL, 0}
  86.     end if
  87.     end if
  88.     if get_char() != '\'' then
  89.     return {GET_FAIL, 0}
  90.     else
  91.     return {GET_SUCCESS, c} 
  92.     end if
  93. end function
  94.  
  95.  
  96. function get_string()
  97.     sequence text
  98.     char c
  99.  
  100.     text = ""
  101.     while 1 do
  102.     c = get_char()
  103.     if c = GET_EOF or c = '\n' then
  104.         return {GET_FAIL, 0}
  105.     end if
  106.     if c = '"' then
  107.         c = get_char()
  108.         if c != '"' then
  109.         exit
  110.         end if
  111.     elsif c = '\\' then
  112.         c = escape_char(get_char())
  113.         if c = GET_FAIL then
  114.         return {GET_FAIL, 0}
  115.         end if
  116.     end if
  117.     text = text & c
  118.     end while
  119.     unget_char(c)
  120.     return {GET_SUCCESS, text}
  121. end function
  122.  
  123. type plus_or_minus(integer x)
  124.     return x = -1 or x = +1
  125. end type
  126.  
  127. function get_number()
  128. -- read a number
  129.     char c
  130.     plus_or_minus sign, e_sign
  131.     positive_int ndigits
  132.     atom mantissa, dec, e_mag, exponent
  133.  
  134.     sign = +1
  135.     mantissa = 0
  136.     e_sign = +1
  137.     e_mag = 0    
  138.     ndigits = 0
  139.  
  140.     c = get_char()
  141.  
  142.     -- process sign
  143.     if c = '-' then
  144.     sign = -1
  145.     elsif c != '+' then
  146.     unget_char(c)
  147.     end if
  148.  
  149.     -- get mantissa
  150.     c = get_char()
  151.     while find(c, "0123456789") do
  152.     ndigits = ndigits + 1
  153.     mantissa = mantissa * 10 + (c - '0')    
  154.     c = get_char()
  155.     end while
  156.     if c = '.' then
  157.     -- get fraction
  158.     c = get_char()
  159.     dec = 10
  160.     while find(c, "0123456789") do
  161.         ndigits = ndigits + 1
  162.         mantissa = mantissa + (c - '0') / dec             
  163.         dec = dec * 10
  164.         c = get_char()
  165.     end while
  166.     end if
  167.     if ndigits = 0 then
  168.     return {GET_FAIL, 0}
  169.     end if
  170.     if c = 'e' or c = 'E' then
  171.     -- get exponent sign
  172.     c = get_char()
  173.     if c = '-' then
  174.         e_sign = -1
  175.     elsif c != '+' then
  176.         unget_char(c)
  177.     end if
  178.     -- get exponent magnitude
  179.     c = get_char()
  180.     if find(c, "0123456789") then
  181.         e_mag = c - '0'
  182.         c = get_char()
  183.         if find(c, "0123456789") then
  184.         e_mag = e_mag * 10 + c - '0'
  185.         else
  186.         unget_char(c)
  187.         end if
  188.     else
  189.         return {GET_FAIL, 0} -- no exponent
  190.     end if
  191.     else
  192.     unget_char(c)
  193.     end if
  194.     exponent = 1
  195.     if e_sign >= 0 then
  196.         for i = 1 to e_mag do
  197.         exponent = exponent * 10
  198.         end for
  199.     else
  200.     for i = 1 to e_mag do
  201.         exponent = exponent * 0.1
  202.     end for
  203.     end if
  204.     return {GET_SUCCESS, sign * mantissa * exponent}    
  205. end function
  206.  
  207.  
  208. function Get()
  209. -- read a Euphoria data object as a string of characters 
  210. -- and return {error_flag, value}
  211.     char c
  212.     sequence s, e
  213.  
  214.     skip_blanks()
  215.     c = get_char()
  216.  
  217.     if find(c, "-+.0123456789") then
  218.     unget_char(c)
  219.     return get_number()
  220.  
  221.     elsif c = '{' then
  222.     -- process a sequence
  223.     s = {}
  224.     while 1 do
  225.         skip_blanks()
  226.         c = get_char()
  227.         if c = '}' then
  228.         return {GET_SUCCESS, s}
  229.         else
  230.         unget_char(c)
  231.         end if
  232.         e = Get()
  233.         if e[1] != GET_SUCCESS then
  234.         return e
  235.         end if
  236.         s = append(s, e[2])
  237.         skip_blanks()
  238.         c = get_char()
  239.         if c = '}' then
  240.         return {GET_SUCCESS, s}
  241.         elsif c != ',' then
  242.         return {GET_FAIL, 0} 
  243.         end if
  244.     end while
  245.  
  246.     elsif c = '\"' then
  247.     return get_string()
  248.  
  249.     elsif c = '\'' then
  250.     return get_qchar()
  251.  
  252.     elsif c = -1 then
  253.     return {GET_EOF, 0}
  254.  
  255.     else
  256.     return {GET_FAIL, 0}
  257.  
  258.     end if  
  259. end function
  260.  
  261.  
  262. global function get(positive_int file_no)
  263. -- main routine, sets input_file 
  264.     input_file = file_no
  265.     return Get()
  266. end function
  267.  
  268.