# # KEHOME/parser/bselist.icn # Richard H. McCullough Oct/26/2005 Jul/20/2009 Aug/4/2014 Mar/7/2015 $include "yyinclude.h" link deepcopy # BSE is list of strings* with begin,separator,end character # bselist captures list details which are optional in MKR language # insures input text can be recovered exactly (except blanks between symbols) # used for subject,object,value lists # e.g.: value with optional brackets # e.g.: format enclosed by [] or {} # * now includes other types #--------------------------------------------------------------------- # BSE record: order matches parsing order record BSE( bse_list # [sep,begin,blist,end] ) # BSE methods #============ # new_bse(S,B,L,E) # bse_list(x) # bse_sep(x) # bse_begin(x) # bse_blist(x) # bse_end(x) # bse_unparse(x) # bse_writes(fd,x) # bse_delete_sep(x) # bse_insert_sep(x) # bse_copy(x) # use deepcopy() #========================================================# procedure new_bse(S,B,L,E) #========================= # BSE record: S + parse order B,L,E local t,uS,uB,uE,dL static prog,b,colon,yyprefix initial { prog := "new_bse: " b := " " colon := ":" } /S := "," /B := "" /E := "" yyprefix := yylineno||colon||yywordno||b||KFORMAT||b||prog case t := type(L) of { default: { write(&errout,yyprefix,"unexpected input type (",t,")"); fail } ("string"|"integer"|"real"): { L := [L] } # no separator "list": { } # comma separator "set": { } # comma separator "CSV": { } # comma separator "PHRASE": { } # blank separator "NVPHRASE": { } # blank separator "PPHRASE": { } # blank separator "QPHRASE": { } # blank separator } # end case t uS := unparse(S) uB := unparse(B) uE := unparse(E) dL := bse_delete_sep(L,uS) return BSE([uS,uB,dL,uE]) end # BSE procedure bse_copy(x) #==================== return deepcopy(x) end # string procedure bse_unparse(x) #======================= # return input string for x local S,B,L,E local s,y,z,t,vL static prog,b,colon,yyprefix initial { prog := "bse_unparse: " b := " " colon := ":" } yyprefix := yylineno||colon||yywordno||b||KFORMAT||b||prog S := bse_sep(x) B := bse_begin(x) L := unparse(bse_blist(x),S) E := bse_end(x) y := B || L || E if DEBUG==("PARSE"|"UNPARSE") then write(mylog,yyprefix,"y = (",image(y),")") return y end #========================================================# # list procedure bse_list(x) #==================== local t,y static prog,b,colon,yyprefix initial { prog := "bse_list: " b := " " colon := ":" } yyprefix := yylineno||colon||yywordno||b||KFORMAT||b||prog case t := type(x) of { default: { write(&errout,yyprefix,"unxpected input type ("||t||")"); fail } "BSE": { y := x.bse_list } } return y end # string procedure bse_sep(x) #=================== return unparse(x.bse_list[1]) end # string procedure bse_begin(x) #========================= return unparse(x.bse_list[2]) end # list procedure bse_blist(x) #===================== return x.bse_list[3] end # string procedure bse_end(x) #========================= return unparse(x.bse_list[4]) end # integer procedure bse_writes(fd,x) #=========================== local t,S,B,L,E static prog,b,c,colon,yyprefix initial { prog := "bse_writes: " b := " " c := "," colon := ":" } /fd := &output yyprefix := yylineno||colon||yywordno||b||KFORMAT||b||prog case t := type(x) of { default: { write(&errout,yyprefix,"unexpected input type ("||t||")"); fail } "BSE": { writes(fd,bse_unparse(x)) } } # end case t end # list or CSV procedure bse_delete_sep(L,sep) #============================== # delete any separators # e.g.: left from initial parsing local t,i,dL static prog,b,colon,yyprefix initial { prog := "bse_delete_sep: " b := " " colon := ":" } /sep := "," yyprefix := yylineno||colon||yywordno||b||KFORMAT||b||prog case t := type(L) of { default: { write(&errout,yyprefix,"unexpected input type ("||t||")"); fail } "list": { } "CSV": { L := csv_list(L) } } dL := [] every i := 1 to *L do { if unparse(L[i]) ~== sep then put(dL,L[i]) } case t of { "list": { } "CSV": { dL := CSV(dL) } } return dL end procedure bse_insert_sep(L,sep) #============================== # insert separators local t,i,iL static prog,b,colon,yyprefix initial { prog := "bse_insert_sep: " b := " " colon := ":" } /sep := "," yyprefix := yylineno||colon||yywordno||b||KFORMAT||b||prog case t := type(L) of { default: { write(&errout,yyprefix,"unexpected input type ("||t||")"); fail } "list": {} } iL := [] every i := 1 to *L do { put(iL,L[i]) if i < *L then put(iL,sep) } return iL end #