str_cursor.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
-------------------------> GNU Sather - sourcefile <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute and/or modify it under the terms of the GNU Library General --
-- Public License (LGPL) as published by the Free Software Foundation; --
-- either version 2 of the license, or (at your option) any later version. --
-- This library is distributed in the hope that it will be useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details. --
-- The license text is also available from: Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --
--------------> Please email comments to <bug-sather@gnu.org> <--------------
class STR_CURSOR < $TEXT_CURSOR{CHAR,STR}
class STR_CURSOR < $TEXT_CURSOR{CHAR,STR} is
-- This class models a pointer referring to the successive elements of
-- a list/string. It provides a convenient mechanism to convert string
-- representations of other objects into objects of the appropriate classes.
-- If the expected objext representation was not found then the cursor error
-- state is set appropriately.
-- Version 1.3 Apr 99. Copyright K Hopper, U of Waikato
-- Development History
-- -------------------
-- Date Who By Detail
-- ---- ------ ------
-- 11 Dec 96 kh Original
-- 3 Jan 97 kh Changed to CARD from INT
-- 23 Feb 99 kh Changed to $CHAR
-- 14 Apr 99 kh Revised for V8 of text classes
include TEXT_CURSOR{CHAR,STR} ;
reset_line(val : CARD)
pre ~void(self)
post line_no = val
is
-- This is provided for use when it has been necessary to change to
-- different positions in the string, using set_index. If track needs to be
-- kept of the line number then this routine enables restoration.
line_no := val
end ;
split(ch : CHAR) : FLIST{STR}
pre ~void(self) and (buffer.size > 0)
post (~buffer.contains(ch)
and (result.size = 1)
and (result[0] = buffer))
or (result.size > 1)
is
-- This routine splits the string into a sequence of strings separated
-- at each occurrence of ch.
res : FLIST{STR} := FLIST{STR}::create ;
elt : STR := STR::create ;
loop
until!(is_done) ;
elt := get_upto_char(ch) ;
if (is_done) then
if elt.size > 0 then -- terminating non-empty segment!
res := res.push(elt)
end ;
break!
else
res := res.push(elt) ;
advance -- past the 'ch'
end
end ;
return res
end ;
current_loc_str(cursor_char : CHAR) : STR
pre ~void(self)
and (buffer.size > 0)
post (result[result.size - 1] = cursor_char)
is
-- This routine returns a string which is space-filled up to the
-- cursor_char which when printed/displayed therefore would appear at the
-- current location in the line.
loc_current : CARD := index ;
skip_back_to_line_start ;
cnt : CARD := loc_current - index ;
if index = 0 then
cnt := cnt - 1
end ;
set_index(loc_current) ;
res : STR := STR::create(buffer.index_lib) ;
if cnt > 0 then
res := buffer.index_lib.Space.str.repeat(cnt)
end ;
return res + cursor_char
end ;
-- NOTE The remaining routines in this class are convenience
-- versions of the routines in the various class
-- representation converters.
card : CARD
pre ~void(self)
and (buffer.size > 0)
and item.is_digit(buffer.index_lib)
post (initial(index) < index)
is
-- This routine attempts to read a string of characters which is
-- expected to be in decimal notation.
return CARD::build_based(self,10)
end ;
get_octal : CARD
pre ~void(self)
and (buffer.size > 0)
and item.is_octal_digit(buffer.index_lib)
post initial(index) > index
is
-- This routine attempts to read a string of characters which is
-- expected to be in octal notation WITHOUT any prefix.
return CARD::build_based(self,8)
end ;
octal : CARD
pre ~void(self)
and (buffer.size > 0)
and (buffer.index_lib.card(item) = 0)
post ((result = CARD::nil)
and (initial(index) = index))
or (initial(index) < index)
is
-- This routine attempts to read a string of characters which is
-- expected to be in octal notation prefixed by "0o".
start_index : CARD := index ;
advance ; -- past the zero!
if item = buffer.index_lib.Octal_Prefix.char then
advance
else
retract ;
return CARD::nil
end ;
if item.is_octal_digit(buffer.index_lib) then
return CARD::build_based(self,8)
else
set_index(start_index) ;
return CARD::nil
end
end ;
get_hex : CARD
pre ~void(self)
and (buffer.size > 0)
and item.is_hex_digit(buffer.index_lib)
post initial(index) < index
is
-- This routine attempts to read a string of characters which is
-- expected to be in hexadecimal notation WITHOUT any prefix.
return CARD::build_based(self,16)
end ;
hex : CARD
pre ~void(self)
and (buffer.size > 0)
and (buffer.index_lib.card(item) = 0)
post ((result = CARD::nil)
and (initial(index) = index))
or (initial(index) < index)
is
-- This routine attempts to read a string of characters which is
-- expected to be in hexadecimal notation prefixed by "0x".
start_index : CARD := index ;
advance ; -- past the zero!
if item = buffer.index_lib.Hex_Prefix.char then
advance
else
retract ;
return CARD::nil
end ;
if item.is_hex_digit(buffer.index_lib) then
return CARD::build_based(self,16)
else
set_index(start_index) ;
return CARD::nil
end
end ;
field : FIELD
pre ~void(self)
and (buffer.size > 0)
and item.is_digit(buffer.index_lib)
post (initial(index) < index)
is
-- This routine attempts to read a string of characters which is
-- expected to be in decimal notation.
return FIELD::build_based(self,10)
end ;
int : INT
pre ~void(self)
and (buffer.size > 0)
post ((result = INT::nil)
and (initial(index) = index))
or (initial(index) < index)
is
-- This routine obtains the value of an integer number which, after
-- leading spaces is expected to be next in the string. It is a synonym for
-- get_int
return INT::build(self)
end ;
flt : FLT
pre ~void(self)
and (buffer.size > 0)
post true -- ((result = FLT::quiet_NaN(0.int))
-- and (initial(index) = index))
-- or (initial(index) < index)
is
-- This routine attempts to obtain a floating point number the value of
-- which is returned if successful - otherwise the cursor has not been
-- altered.
return FLT::build(self)
end ;
fltd : FLTD
pre ~void(self)
and (buffer.size > 0)
post true -- ((result = FLTD::quiet_NaN(0.int))
-- and (initial(index) = index))
-- or (initial(index) < index)
is
-- This routine attempts to obtain a double precision floating point
-- number the value of which is returned if successful - otherwise the cursor
-- has not been altered.
return FLTD::build(self)
end ;
bool : BOOL
pre ~void(self)
and (buffer.size > 0)
post true
is
-- This routine expects a boolean value as the next item in the string
-- indicated and returns it, advancing by one or more positions in the string
-- if found.
return BOOL::build(self)
end ;
end ; -- STR_CURSOR