home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / tokenstream.st < prev    next >
Text File  |  1992-02-15  |  3KB  |  125 lines

  1. "======================================================================
  2. |
  3. |   Token stream Method Definitions
  4. |
  5. |   A token stream is a stream that's defined in terms of a string.
  6. |   It basically parses off whitespace separated tokens as substrings
  7. |   and returns them (next).  If the entire contents of the string is 
  8. |   requested, it is returned as an Array containing the individual strings.
  9. |
  10.  ======================================================================"
  11.  
  12.  
  13. "======================================================================
  14. |
  15. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  16. | Written by Steve Byrne.
  17. |
  18. | This file is part of GNU Smalltalk.
  19. |
  20. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  21. | under the terms of the GNU General Public License as published by the Free
  22. | Software Foundation; either version 1, or (at your option) any later version.
  23. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  24. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  25. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  26. | details.
  27. | You should have received a copy of the GNU General Public License along with
  28. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  29. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  30. |
  31.  ======================================================================"
  32.  
  33.  
  34. "
  35. |     Change Log
  36. | ============================================================================
  37. | Author       Date       Change 
  38. | sbb         16 Mar 91      Class creation now separate statement.
  39. |
  40. | sbyrne     14 May 90      removed isWhiteSpace:;replaced uses with Character
  41. |              isSeparator.
  42. |
  43. | sbyrne     19 Sep 89      Changed to use real method categories.
  44. |
  45. | sbyrne     12 Jul 89      created.
  46. |
  47. "
  48.  
  49. Stream subclass: #TokenStream
  50.        instanceVariableNames: 'charStream'
  51.        classVariableNames: ''
  52.        poolDictionaries:''
  53.        category: nil
  54. !
  55.  
  56. TokenStream comment:
  57. 'I am not a typical part of the Smalltalk kernel class hierarchy.\n\
  58. I operate on a stream of characters and return distinct \n\
  59. (whitespace-delimited) groups of characters.' !
  60.  
  61.  
  62. !TokenStream class methodsFor: 'instance creation'!
  63.  
  64. on: aString
  65.     ^self onStream: (ReadStream on: aString)
  66. !
  67.  
  68. onStream: aStream
  69.     ^self new setStream: aStream
  70. !!
  71.  
  72.  
  73.  
  74. !TokenStream methodsFor: 'basic'!
  75.  
  76. next
  77.     | char tokStream |
  78.     self atEnd ifTrue: [ ^nil ]. "has the nice side effect of skipping
  79.                                   leading white space."
  80.     tokStream _ WriteStream on: (String new: 1).
  81.     [ char _ charStream peek.
  82.       (char notNil) and: [ (char isSeparator) not ] ]
  83.         whileTrue: [ tokStream nextPut: (charStream next) ].
  84.     ^tokStream contents
  85. !
  86.  
  87. atEnd
  88.     | char |
  89.     [ char _ charStream peek.
  90.       char isNil ] whileFalse:
  91.         [ (char isSeparator) ifFalse: [ ^false ].
  92.      charStream next ].
  93.     ^true
  94. !
  95.  
  96. do: aBlock
  97.     [ self atEnd ] whileFalse:
  98.         [ aBlock value: self next ]
  99. !
  100.  
  101. contents
  102.     | arrayStream |
  103.     arrayStream _ WriteStream on: (Array new: 0).
  104.     self do: [ :aToken | arrayStream nextPut: aToken ].
  105.     ^arrayStream contents
  106. !!
  107.  
  108.  
  109.  
  110. !TokenStream methodsFor: 'write methods'!
  111.  
  112. nextPut: anObject
  113.     self shouldNotImplement
  114. !!
  115.  
  116.  
  117.  
  118. !TokenStream methodsFor: 'private'!
  119.  
  120. setStream: aStream
  121.     charStream _ aStream
  122. !!
  123.