SS.k 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // import as re.*
  2. \d re
  3. \l ngnk-libs/regex/re.k
  4. \d .
  5. /helpers
  6. / e exclusive
  7. slice: {[l;s;e] s_(e-1)#l};
  8. listize: {1_(,/(";",)'{"(",x,")"}'x)};
  9. split: {[l;di] idx: (-1,&(di~'l)),(#l); {slice[x;y[0]+1;y[1]+1]}[l]'(idx@(2':!#idx))};
  10. s2n: {[s] `i$s;}; // string to number
  11. parseidx: {core: slice[x;1;#x]; split[core;";"]};
  12. /:parseidx["[1;2]"];
  13. /:split["1;2;3";";"];
  14. tstkey: "States/Events"; / key indicating start of transition section
  15. cstkey: "Events"; / key indicating start of events condition section
  16. astkey: "Actions"; / key indicating start of actions section
  17. sectionkeys: (tstkey;cstkey;astkey);
  18. lines: 0: "SS.csv";
  19. /csv parser
  20. parsel: {[l] comi: &l=",";comi+:1;r: 2':!#comi;r: comi@r;r: (1 2#(0;r[0;0])),r;slice[l].'r};
  21. csv: parsel'lines;
  22. kw:: "self";
  23. rep:: "t";
  24. deps:: (#csv;#csv[0])#0;
  25. fnr:{[l;k;rp] e:(#l)-(#k); r:!e; idx: &({y~x}[;k]'({slice[y;x;x+(#z)+1]}[;l;k]'r)); l{[last;x;y;z;r]left:x#last; right:(x+(#z))_last; (left,r,right;right)}[;;l;k;rp]/(|idx)};
  26. rd: {[r] deps[0;0]::1; r[0]}; // register dep
  27. dd: {[r] $[r[1;0]~"[";rd[r];r[0]]}; // detect dep
  28. / :{[l;kw;rep] r: fnr[l;kw;rep]; $[(@r)=`C;r;dd[r]]}[;kw;rep]''csv;
  29. / :deps;
  30. / initialize data
  31. / f:<`"compiled_ss.k";
  32. / f 0: kcode;
  33. para:`0:
  34. testcases:{para"search ",z;`0:$x z;para"match ",z;`0:$y z}
  35. test:{para"---";para"regex: ", x;testcases[re.match[re.scmp@x];re.match[re.cmp@x]]'y;};
  36. cases:("abc";"xxxabbc";"abbbcyyy";"xxxabbbbcyyy");
  37. test["ab+c";cases]