PreviousUpNext

15.4.985  src/lib/src/string-to-list.pkg

## string-to-list.pkg

# Compiled by:
#     src/lib/std/standard.lib



###             "I wish to work miracles."
###
###                    -- Leonardo da Vinci



package   string_to_list
: (weak)  String_To_List                                                # String_To_List        is from   src/lib/src/string-to-list.api
{

    # Given an expected initial string, a separator, a terminating
    # string, and an item scanning function, return a function that
    # scans a string for a list of items.  Whitespace is ignored.
    # If the input string has the incorrect syntax, then the exception
    # ScanList is raised with the position of the first error.          # Except that no exception is visibly defined or raised in this file:  Maybe it happens in number_string?
    #
    fun string_to_list { first, between, last, from_string } getc stream
        =
        {   skip_ws   =  number_string::skip_ws  getc;
            scan_item =  from_string getc;

            fun eat ""
                    =>
                    (fn stream =  (TRUE, skip_ws stream));

                eat s
                    =>
                    eat'
                    where
                        n = size s;

                        fun is_prefix (i, stream)
                            =
                            if   (i == n)
                                 THE stream;
                            else
                                 case (getc stream)
                                   
                                     THE (c, stream)
                                         =>
                                         if (string::get (s, i) == c)   is_prefix (i+1, stream);
                                         else                           NULL;                fi;

                                     NULL => NULL;
                                  esac;
                            fi;

                        fun eat' stream
                            =
                            case (is_prefix (0, skip_ws stream))
                              
                                 THE stream => (TRUE,  stream);
                                 NULL       => (FALSE, stream);
                            esac;
                    end;
            end;

            is_first   = eat  first;
            is_between = eat  between;
            is_last    = eat  last;

            fun scan (stream, l)
                =
                case (is_between stream)
                  
                     (TRUE, stream)
                         =>
                         case (scan_item stream)
                           
                              THE (x, stream) =>  scan (stream, x ! l);
                              NULL            =>  NULL;
                         esac;

                     (FALSE, stream)
                         =>
                         case (is_last stream)
                           
                              (TRUE,  stream) =>  THE (reverse l, stream);
                              (FALSE, stream) =>  NULL;
                         esac;
                esac;


            case (is_first stream)
              
                 (TRUE, stream)
                     =>
                     case (is_last stream)
                       
                          (TRUE,  stream)
                              =>
                              THE ([], stream);

                          (FALSE, stream)
                              =>
                              case (scan_item stream)
                                
                                   THE (x, stream) =>  scan (stream, [x]);
                                   NULL          =>  NULL;
                              esac;
                     esac;

                 (FALSE, i)
                     =>
                     NULL;
            esac;
        };                              # fun scan 
};                                      # package string_to_list


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext