PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/xml/internal/xml_parser_tools.e

http://github.com/tybor/Liberty
Specman e | 268 lines | 226 code | 20 blank | 22 comment | 21 complexity | 4e7836e31eef81570cc7df9e309dc075 MD5 | raw file
  1-- See the Copyright notice at the end of this file.
  2--
  3deferred class XML_PARSER_TOOLS
  4
  5insert
  6   UNICODE_CHARACTERS
  7
  8feature {ANY}
  9   line: INTEGER
 10      do
 11         Result := buffer.line
 12      end
 13
 14   column: INTEGER
 15      do
 16         Result := buffer.column
 17      end
 18
 19feature {}
 20   buffer: UNICODE_PARSER_BUFFER
 21      deferred
 22      end
 23
 24   skip_blanks
 25      require
 26         buffer.is_connected
 27      do
 28         from
 29         until
 30            buffer.end_of_input or else not is_separator(current_character)
 31         loop
 32            buffer.next
 33         end
 34      end
 35
 36   end_of_input: BOOLEAN
 37      require
 38         buffer.is_connected
 39      do
 40         Result := buffer.end_of_input
 41      end
 42
 43   next
 44      require
 45         buffer.is_connected
 46         not end_of_input
 47      do
 48         buffer.next
 49      end
 50
 51   previous
 52      require
 53         buffer.is_connected
 54         buffer.index > 0
 55      do
 56         buffer.previous
 57      end
 58
 59   current_character: INTEGER
 60      require
 61         buffer.is_connected
 62         not end_of_input
 63      do
 64         Result := buffer.code
 65      end
 66
 67   skip (character: CHARACTER): BOOLEAN
 68      require
 69         buffer.is_connected
 70      do
 71         if not end_of_input then
 72            skip_blanks
 73            if current_character = character.code then
 74               Result := True
 75               next
 76            end
 77         end
 78      end
 79
 80   skip2 (char1, char2: CHARACTER): BOOLEAN
 81      require
 82         buffer.is_connected
 83      do
 84         if skip(char1) then
 85            if skip(char2) then
 86               Result := True
 87            else
 88               previous
 89            end
 90         end
 91      end
 92
 93   skip_word (word: STRING): BOOLEAN
 94      require
 95         buffer.is_connected
 96         not word.is_empty
 97      local
 98         w: INTEGER; p: UNICODE_PARSER_POSITION
 99      do
100         p := buffer.save_position
101         skip_blanks
102         from
103            w := word.lower
104            Result := True
105         until
106            not Result or else w > word.upper
107         loop
108            Result := skip(word.item(w))
109            w := w + 1
110         end
111         if not Result then
112            buffer.restore_position(p)
113         end
114      end
115
116   is_identifier_start (unicode: INTEGER): BOOLEAN
117      require
118         buffer.is_connected
119      do
120         Result := is_letter(unicode)
121         if not Result then
122            inspect
123               unicode
124            when 95, 58 then --  '_', ':'
125               Result := True
126            else
127               check not Result end
128            end
129         end
130      end
131
132   is_identifier_part (unicode: INTEGER): BOOLEAN
133      require
134         buffer.is_connected
135      do
136         Result := is_letter(unicode) or else is_digit(unicode) or else is_combining_char(unicode) or else is_extender(unicode)
137         if not Result then
138            inspect
139               unicode
140            when 95, 58, 45, 46 then -- '_', ':', '-', '.'
141               Result := True
142            else
143               check not Result end
144            end
145         end
146      end
147
148   read_identifier: UNICODE_STRING
149      require
150         buffer.is_connected
151      local
152         done: BOOLEAN
153      do
154         skip_blanks
155         from
156            if end_of_input then
157               done := True
158            else
159               Result := once U""
160               Result.clear_count
161               if is_identifier_start(current_character) then
162                  Result.extend(current_character)
163                  next
164               else
165                  done := True
166               end
167            end
168         until
169            done
170         loop
171            if end_of_input then
172               done := True
173            else
174               if is_identifier_part(current_character) then
175                  Result.extend(current_character)
176                  next
177               else
178                  done := True
179               end
180            end
181         end
182         if Result.is_empty then
183            Result := Void
184         end
185      end
186
187   read_string: UNICODE_STRING
188      require
189         buffer.is_connected
190         current_character = '%''.code or else current_character = '"'.code
191      local
192         done: BOOLEAN; limit: INTEGER; p: UNICODE_PARSER_POSITION
193      do
194         p := buffer.save_position
195         from
196            Result := once U""
197            Result.clear_count
198            limit := current_character
199            next
200         until
201            done
202         loop
203            if end_of_input then
204               Result := Void
205               buffer.restore_position(p)
206               done := True
207            elseif current_character = limit then
208               next
209               done := True
210            else
211               Result.extend(current_character)
212               next
213            end
214         end
215      end
216
217   read_identifier_as_string: STRING
218      require
219         buffer.is_connected
220      local
221         s: UNICODE_STRING
222      do
223         s := read_identifier
224         if s /= Void then
225            Result := once ""
226            Result.clear_count
227            s.utf8_encode_in(Result)
228         end
229      end
230
231   read_string_as_string: STRING
232      require
233         buffer.is_connected
234      local
235         s: UNICODE_STRING
236      do
237         s := read_string
238         if s /= Void then
239            Result := once ""
240            Result.clear_count
241            s.utf8_encode_in(Result)
242         end
243      end
244
245invariant
246   readable_url: (buffer /= Void and then buffer.is_connected) implies buffer.url.read
247
248end -- class XML_PARSER_TOOLS
249--
250-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
251--
252-- Permission is hereby granted, free of charge, to any person obtaining a copy
253-- of this software and associated documentation files (the "Software"), to deal
254-- in the Software without restriction, including without limitation the rights
255-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
256-- copies of the Software, and to permit persons to whom the Software is
257-- furnished to do so, subject to the following conditions:
258--
259-- The above copyright notice and this permission notice shall be included in
260-- all copies or substantial portions of the Software.
261--
262-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
263-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
264-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
265-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
266-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
267-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
268-- THE SOFTWARE.