streams.anubis 13.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562

 *Project*                                  Anubis
   
 *Title*                           A simple notion of stream.
   
 *Copyright*                     Copyright (c) Alain Prouté 2005. 


 *Author*       Alain Prouté

   
 *Overview*  
   
   A 'stream'  is an object  (in the sens of  OOP) from which  bytes may be read,  or into
   which bytes may be written.  We have defined the opaque type:

public type Stream:...

   in a  'implementation independant' way.   This means that  the actual source  or target
   behind  a   stream  may  be  a   file,  a  network  connection,   a  character  string,
   etc... Whatever it is, the stream is used the same way by the tools provided here.
   
   Different streams  may have different  capabilities. For example,  a stream may  not be
   writable. A  tool returns the capabilities  of the stream. If  you use a  function on a
   stream not having the corresponding capability, the function has just no effect. 
   
   
   *** Creating a stream. 
   
   As said above, a stream may be  created from various sources.  We provide below several
   functions for creating streams:
   
public define Stream      make_stream(RAddr(Int8)     file). 
public define Stream      make_stream(WAddr(Int8)     file). 
public define Stream      make_stream(RWAddr(Int8)    file).
public define Stream      make_stream(String          s).    
   
   
   
   
   *** Getting the capabilities. 
   
public type StreamCapability:
   readable,
   writable,
   seekable.
   
public define List(StreamCapability)   get_capabilities(Stream s). 
   
   
   
   
   *** Reading from a stream. 
   
public define Maybe(Int8)     read_byte        (Stream s). 
public define Maybe(String)   read_line        (Stream s).
public define One             skip_blanks      (Stream s).
public define Maybe(String)   read_symbol      (Stream s).   
public define Maybe(String)   read_integer     (Stream s). 
public define List($T)        read_several     (Stream s, Stream -> Maybe($T) read_one).  
public define Maybe(List($T)) read_list        (Stream s, Stream -> Maybe($T) read_one). 
   
   
   'read_byte' reads the next byte
   
   'read_line' reads from the current position to  end of current line. The end of line is
      any of:     LF
                  CR LF
                  CR
   
   'skip_blanks' skips blank  characters until the next non blank  character (which is not
      read)
   
   'read_symbol' reads  a symbol, skiping leading blanks  if necessary. A symbol  is a non
      empty concatenation of letters from 'a' to 'z', 'A' to 'Z', '0' to '9' and '_'. 
   
   'read_integer' is like 'read_symbol' but accepts only the characters '0' to '9'. 
   
   'read_several' applies 'read_one' repeatedly until it returns 'failure'. 'read_several'
      returns the list of all the entities read by 'read_one' (in their natural order). 
   
   'read_list'  expects an  opening parenthese,  then applies  'read_one'  repeatedly, and
      expects a closing parenthese. It returns the list of entities read by 'read_one'. 

   
   You may also unput an arbitrary number of bytes in the stream:
   
public define One        unput_byte      (Int8 c, Stream s).   
   
   If   the  bytes   unput  are   not  those   read  from   the  stream   the   result  is
   unpredictable. Unputing a '\n' may induce a wrong column count. 

   
   
   *** Informations about the current state of the stream. 
   
public define Int32           current_offset   (Stream s).
public define Int32           current_line     (Stream s).
public define Int32           current_column   (Stream s). 
          
   'current_offset' returns the number of bytes read so far.
   
   'current_line'   returns the number of '\n' read so_far.
   
   'current_column' returns the number of bytes read since the last '\n' (or the beginning
       of the input).
   

   
   *** Writting into a stream. 
   
public define Maybe(One)    write_byte       (Stream  s, Int8  byte). 
public define Maybe(One)    write_string     (Stream  s, String t).    
   
   
   
   
   --- That's all for the public part ! --------------------------------------------------

   
   
   
   
public type Stream:
   stream(One -> Maybe(Int8)       rb,       // read a byte
          Int8 -> Maybe(One)       wb,       // write a byte
          Int8 -> One              ub,       // unput a byte
          One -> Int32             of,       // offset
          One -> Int32             ln,       // line
          One -> Int32             cl).      // column
   
         

define One -> Maybe(Int8)
   make_rb   // making the 'rb' function 
     (
       RAddr(Int8)       file,
       Var(List(Int8))   v, 
       Var(Int32)        o, 
       Var(Int32)        l,
       Var(Int32)        c
     ) =
   (One u) |-> if *v is 
      {
        [ ] then if *file is 
          {
            failure then failure, 
            success(byte) then o <- *o+1;      //increment the offset counter
                               (if byte = '\n' then (l <- *l+1; c <- 0) 
                                               //and if it is a new line increment the line counter
                                               else c <- *c+1);
                               success(byte)   
          }, 
        [h . t] then v <- t; 
                     o <- *o+1; 
                     (if h = '\n' then (l <- *l+1; c <- 0)
                                  else c <- *c+1); 
                     success(h)
     }. 
   

   
define Int8 -> One
   make_ub     // making the 'ub' function 
     (
       Var(List(Int8))   v, 
       Var(Int32)        o, 
       Var(Int32)        l,
       Var(Int32)        c
     ) =
   (Int8 byte) |-> v <- [byte . *v]; 
                   o <- *o-1;
                   (if byte = '\n' then (l <- *l-1; c <- 0)
                                   else c <- *c-1).

   
   
   
public define Stream
   make_stream
     (
       RAddr(Int8)    file
     ) =
   with v = var((List(Int8))[]),     // stack of unput bytes
        o = var((Int32)0),           // offset
        l = var((Int32)1),           // line
        c = var((Int32)0),           // column
   stream(
     make_rb(file,v,o,l,c), 
     (Int8 c) |-> failure, 
     make_ub(v,o,l,c), 
     (One u) |-> *o, 
     (One u) |-> *l,
     (One u) |-> *c
         ). 
   
public define Stream
   make_stream
     (
       RWAddr(Int8)    file
     ) =
   with v = var((List(Int8))[]),     // stack of unput bytes
        o = var((Int32)0),           // offset
        l = var((Int32)1),           // line
        c = var((Int32)0),           // column
   stream(
     make_rb(weaken(file),v,o,l,c), 
     (Int8 c) |-> file <- c, 
     make_ub(v,o,l,c), 
     (One u) |-> *o, 
     (One u) |-> *l,
     (One u) |-> *c
         ). 
   
public define Stream
   make_stream
     (
       WAddr(Int8)    file
     ) =
   stream(
     (One u) |-> failure,
     (Int8 c) |-> file <- c,
     (Int8 c) |-> unique, 
     (One u) |-> 0,
     (One u) |-> 0, 
     (One u) |-> 0
   ).
   
public define Stream
   make_stream
     (
       String s
     ) =
   with i = var((Int32)0), 
        l = var((Int32)1), 
        c = var((Int32)0), 
        b = var((List(Int8))[]), 
        d = var((List(String))[]),
        n = var((Int32)0), 
   stream(
          // reading the next byte:
          (One u) |-> if nth(*i,s) is 
                {
                  failure then failure, 
                  success(byte) then i <- *i+1; 
                                     (if byte = '\n' then (l <- *l+1; c <- 0)
                                                     else c <- *c+1);
                                     success(byte)
                },
   
          // writing a byte
          (Int8 c) |-> 
             b <- [c . *b];
             n <- *n + 1; 
             if *n >= 100
             then 
               (protect
                d <- [implode(reverse(*b)) . *d];
                b <- [];
                n <- 0;
                success(unique)
               )
             else success(unique), 
   
          // unputting a byte:
          (Int8 byte) |-> i <- *i-1;
                          (if byte = '\n' then (l <- *l-1; c <- 0)
                                          else c <- *c-1),
   
          // getting the offset:
          (One u) |-> *i,
   
          // getting the line:
          (One u) |-> *l,
    
          // getting the column
          (One u) |-> *c
         ). 
   
   
   

public define Int32      
   current_offset
     (
       Stream s
     ) =
   of(s)(unique).    
   
   
public define Int32
   current_line
     (
       Stream s
     ) =
   ln(s)(unique). 
   
public define Int32
   current_column
     (
       Stream s
     ) =
   cl(s)(unique). 
   
   
public define Maybe(Int8)
   read_byte
     (
       Stream s
     ) =
   rb(s)(unique). 
   
public define One
   unput_byte
     (
       Int8 c, 
       Stream s
     ) =
   ub(s)(c). 
   
   

   *Description*  'read_text_byte' transforms  a  function of  type  'One ->  Maybe(Int8)'
   reading bytes  in binary mode into  a function of the  same type reading  bytes in text
   mode. By convention all ends of lines are returned as '\n'. 
   
define One -> Maybe(Int8)
   read_text_byte
     (
       One -> Maybe(Int8) rbs,
       Int8 -> One        unput
     ) =
   (One u) |-> 
     if rbs(unique) is 
       {
         failure    then failure, 
         success(c) then 
           if c = '\r'
           then if rbs(unique) is 
             {
               failure     then success('\n'),
               success(d)  then 
                 if d = '\n'
                 then success('\n')
                 else unput(d); success('\n')
             }
           else success(c)
                /* actually: 
                if c = '\n'
                then success('\n')
                else success(c)
                */
       }.
   
   
   
define Maybe(String)
   read_line
     (
       One -> Maybe(Int8)    rtb,       // this function reads bytes in text mode
       List(Int8)            so_far
     ) =
   if rtb(unique) is 
     {
       failure     then if so_far is []
                        then failure 
                        else success(implode(reverse(so_far))), 
       success(c)  then 
         if c = '\n'
         then success(implode(reverse(so_far)))
         else read_line(rtb,[c . so_far])
     }. 
   
public define Maybe(String)
   read_line
     (
       Stream s
     ) =
   read_line(read_text_byte(rb(s),ub(s)),[]). 
   
   
public define One
   skip_blanks
     (
       Stream     s
     ) =
   if rb(s)(unique) is 
     {
       failure then unique, 
       success(c) then 
         if int8_to_int32(c) =< ' '
         then skip_blanks(s)
         else ub(s)(c)
     }. 
   
   
   
define Maybe(String)
   read_symbol
     (
       Stream         s,
       List(Int8)     so_far,
       Int8 -> Bool   is_acceptable_char
     ) =
   if rb(s)(unique) is 
     {
       failure then if so_far is 
         {
           [ ] then failure, 
           [_ . _] then success(implode(reverse(so_far)))
         },
       success(c) then 
         if is_acceptable_char(c)
         then read_symbol(s,[c . so_far],is_acceptable_char)
         else 
           ub(s)(c); 
           if so_far is 
             {
               [ ] then failure, 
               [_ . _] then success(implode(reverse(so_far)))
             }
     }.
   
   
define Bool
   is_symbol_char
     (
       Int8   c
     ) =
   with n = int8_to_int32(c), 
   if n = '_' then true else
   if ('a' =< n & n =< 'z') then true else
   if ('A' =< n & n =< 'Z') then true else
   ('0' =< n & n =< '9').
   
define Bool
   is_integer_char
     (
       Int8   c
     ) =
   with n = int8_to_int32(c), 
   '0' =< n & n =< '9'.
   
   
public define Maybe(String)
   read_symbol
     (
       Stream     s
     ) =
   skip_blanks(s);
   read_symbol(s,[],is_symbol_char).    
   
public define Maybe(String)
   read_integer
     (
       Stream     s
     ) =
   skip_blanks(s);
   read_symbol(s,[],is_integer_char).    
   
   
define List($T)
   read_several
     (
       Stream                   s, 
       Stream -> Maybe($T)      read_one,
       List($T)                 so_far
     ) =
   if read_one(s) is 
     {
       failure then reverse(so_far),
       success(u) then read_several(s,read_one,[u . so_far])
     }.
   
   
   
public define List($T)
   read_several
     (
       Stream                   s, 
       Stream -> Maybe($T)      read_one
     ) =
   read_several(s,read_one,[]).    
   
   
define List($T)
   read_list
     (
       Stream                   s, 
       Stream -> Maybe($T)      read_one,
       List($T)                 so_far
     ) =
   if read_one(s) is 
     {
       failure then 
         skip_blanks(s); 
         if rb(s)(unique) is 
           {
             failure then reverse(so_far),
             success(c) then
               if c = ')'
               then reverse(so_far)
               else (ub(s)(c); reverse(so_far))
           },
       success(u) then read_list(s,read_one,[u . so_far])
     }.
   
public define Maybe(List($T))   
   read_list
     (
       Stream                   s, 
       Stream -> Maybe($T)      read_one
     ) =
   skip_blanks(s); 
   if rb(s)(unique) is 
     {
       failure then failure, 
       success(c) then 
         if c = '('
         then success(read_list(s,read_one,[]))
         else ub(s)(c); failure
     }.
   
   
public define Maybe(One)
   write_byte
     (
       Stream  s, 
       Int8  byte
     ) =
   wb(s)(byte). 

   
   
define Maybe(One)    
   write_string     
     (
       Int8 -> Maybe(One)  wb, 
       String              t,
       Int32               i
     ) =    
   if nth(i,t) is 
     {
       failure then success(unique), 
       success(c) then 
         if wb(c) is 
           {
             failure then failure, 
             success(_) then write_string(wb,t,i+1)
           }
     }.
   
   
public define Maybe(One)    
   write_string
     (
       Stream      s, 
       String      t
     ) =    
   write_string(wb(s),t,0).