message = cluster is create, value, origin, destination, parse % Overview: A message contains a message to be delivered across a % network, a node of origin, and a node of destination. rep = record[mval, morg, mdes: string] create = proc(v, og, d: string) returns(cvt) % effects: Returns a message whose message part is v, origin % is "og," and destination is "d." return(rep${mval: v, morg: og, mdes: d}) end create value = proc(m: cvt) returns(string) % effects: Returns the message part of m. return(rep$get_mval(m)) end value origin = proc(m: cvt) returns(string) % effects: Returns the origin part of m. return(rep$get_morg(m)) end origin destination = proc(m: cvt) returns(string) % effects: Returns the destination part of m. return(rep$get_mdes(m)) end destination parse = iter(mstream: stream) yields(message) % effects: Takes in a stream in the format described below % and parses it into nodes which it then yields. % % The stream has the following format: A message is % described by a unique identifier naming the message, followed % by a sorce node and destination node, seperated by any % number of spaces or tabs. Each message appears on its % own line in the stream. A line beginning with "%" is % a comment and is ignored. If a line is not in % the proper format, an error message is sent to % primary_output and parsing is resumed at the next line. tab = '\t' space = ' ' comment = '%' line: int:= 0 l: string while ~stream$empty(mstream) do l := stream$getl(mstream) line := line + 1 if l[1] = comment then continue end except when bounds: continue end % parse into appropriate values temp: array[string]:= array[string]$new() array[string]$addh(temp, "") i: int:= 1 for c: char in string$chars(l) do if c = tab cor c = space then if ~string$empty(temp[i]) then i:=i + 1 array[string]$addh(temp, "") end else temp[i]:=string$concat(temp[i], string$c2s(c)) end end if string$empty(temp[i]) then array[string]$remh(temp) i:=i - 1 end if i > 3 then error("Too many words", line) continue end if i < 3 then if i = 0 then continue end error("Too few words",line) continue end yield(create(temp[1], temp[2], temp[3])) end end parse error = proc(m: string, line: int) % effects: Prints error message "m" to error_output and indicates % the line number of the error stream$putl(stream$error_output(), "Message file error: " || m) stream$putl(stream$error_output(), " Line: " || int$unparse(line)) stream$putl(stream$error_output(), " Continuing parsing at line " || int$unparse(line + 1)) end error end message