PageRenderTime 19ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/parse/mini_parser_buffer.e

http://github.com/tybor/Liberty
Specman e | 206 lines | 138 code | 27 blank | 41 comment | 2 complexity | 6601e69d2725c68ef72359043167de4f MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4class MINI_PARSER_BUFFER
  5   --
  6   -- Useful and efficient mini parsing buffer.
  7   -- From the user point of view, an object of this class can be considered
  8   -- as the combination of a STRING to be parsed with an extra INTEGER
  9   -- index to memorize the current position in this STRING.
 10   --
 11
 12insert
 13   STRING_HANDLER
 14      redefine
 15         default_create
 16      end
 17
 18create {ANY}
 19   default_create
 20
 21feature {ANY}
 22   initialize_with (string: ABSTRACT_STRING) is
 23         -- Initialize the `Current' buffer interning `string'.
 24      require
 25         string /= Void
 26      do
 27         storage := string.intern
 28         current_index := storage.lower
 29         last_error := Void
 30      ensure
 31         count = string.count
 32         last_error = Void
 33      end
 34
 35   next is
 36         -- Move the `current_index' forward by one.
 37      require
 38         not end_reached
 39      do
 40         current_index := current_index + 1
 41      ensure
 42         current_index = 1 + old current_index
 43      end
 44
 45   skip_separators is
 46         -- Skip all separators by using `is_separator' of class CHARACTER.
 47      local
 48         stop: BOOLEAN
 49      do
 50         from
 51         until
 52            stop
 53         loop
 54            if current_index > upper then
 55               stop := True
 56            elseif storage.item(current_index).is_separator then
 57               current_index := current_index + 1
 58            else
 59               stop := True
 60            end
 61         end
 62      end
 63
 64   last_error: PARSE_ERROR
 65         -- Can be used to memorize an error message.
 66
 67   set_last_error (error: like last_error) is
 68      do
 69         last_error := error
 70      ensure
 71         last_error = error
 72      end
 73
 74   set_last_error_message (error_message: STRING) is
 75      do
 76         create last_error.make(current_index, error_message, last_error)
 77      ensure
 78         last_error.message = error_message
 79      end
 80
 81   set_current_index (new_index: like current_index) is
 82         -- To be able to move (usually back) in the buffer
 83      require
 84         new_index.in_range(lower, upper + 1)
 85      do
 86         current_index := new_index
 87      ensure
 88         current_index = new_index
 89      end
 90
 91feature {ANY} -- Memo
 92   set_memory (a_memory: like memory) is
 93      require
 94         a_memory /= Void
 95      do
 96         memory := a_memory
 97      ensure
 98         memory = a_memory
 99      end
100
101   memo: INTEGER is
102      do
103         Result := memory.memo(Current)
104      end
105
106   restore (a_memo: like memo) is
107      do
108         memory.restore(a_memo, Current)
109      end
110
111   memory: MINI_PARSER_MEMORY
112
113feature {ANY} -- Queries
114   lower: INTEGER is
115      do
116         Result := storage.lower
117      end
118
119   upper: INTEGER is
120         -- Maximum valid index in storage.
121      do
122         Result := storage.upper
123      end
124
125   count: INTEGER is
126         -- The length of the `Current' buffer which is also the maximum valid index.
127      do
128         Result := storage.count
129      end
130
131   capacity: INTEGER is
132         -- Of `storage'.
133      do
134         Result := storage.capacity
135      end
136
137   current_index: INTEGER
138         -- Index of the current character.
139
140   current_character: CHARACTER is
141         -- The current character (the one at `current_index').
142      require
143         current_index.in_range(lower, upper + 1)
144      do
145         Result := storage.item(current_index)
146      end
147
148   end_reached: BOOLEAN is
149         -- Is the end of the buffer reached?
150      do
151         Result := current_index > upper
152         if Result then
153            marked := True
154         end
155      ensure
156         Result = (current_index > upper)
157         Result implies marked
158      end
159
160   marked: BOOLEAN
161
162   clear_mark is
163      do
164         marked := False
165      ensure
166         not marked
167      end
168
169feature {}
170   storage: FIXED_STRING
171         -- The `storage' area to be parsed.
172
173   default_create is
174      do
175         memory := default_memory
176      end
177
178   default_memory: DEFAULT_MINI_PARSER_MEMORY is
179      once
180         create Result
181      end
182
183invariant
184   memory /= Void
185
186end -- class MINI_PARSER_BUFFER
187--
188-- Copyright (c) 2009 by all the people cited in the AUTHORS file.
189--
190-- Permission is hereby granted, free of charge, to any person obtaining a copy
191-- of this software and associated documentation files (the "Software"), to deal
192-- in the Software without restriction, including without limitation the rights
193-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
194-- copies of the Software, and to permit persons to whom the Software is
195-- furnished to do so, subject to the following conditions:
196--
197-- The above copyright notice and this permission notice shall be included in
198-- all copies or substantial portions of the Software.
199--
200-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
201-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
202-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
203-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
204-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
205-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
206-- THE SOFTWARE.