Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.72 KB | None | 0 0
  1. @initialize:python@
  2. @@
  3. import re
  4.  
  5. def fmt_parser(_st):
  6.     # a bunch of (non-% or %%), and then (% followed by non-%).
  7.     REG = re.compile('([^%]|%%)*(%[^%])')
  8.     retval = {}
  9.     pos = 0  # where to start searching for next time
  10.     i = 0
  11.     while True:
  12.         match = REG.match(_st, pos)
  13.         if match is None:
  14.             return retval
  15.         fmt = match.group(2)
  16.         pos = match.end()
  17.         idx = pos - len(fmt)
  18.         retval[i] = { 'idx': idx, 'fmt': fmt }
  19.         i += 1
  20.  
  21. def fmt_replace_by_pos(_str, _idx, _fmt):
  22.     try:
  23.         fmts  = fmt_parser(_str)
  24.         new   = _str
  25.  
  26.         if _idx == -1:
  27.             _idx = [item for item in range(0, len(fmts))]
  28.  
  29.         for _i in _idx:
  30.             f     = fmts[_i]
  31.             idx   = f['idx']
  32.             fmt   = f['fmt']
  33.             fmt_l = len(fmt)
  34.             new   = new[:idx] + _fmt + new[idx + fmt_l:]
  35.        
  36.         return ''.join(new)
  37.     except Exception as e:
  38.         print("** ERROR: Something wrong in fmt_replace_by_pos():\n {}\n".format(str(e)))
  39.  
  40. @r1@
  41. format list fl;
  42. identifier fn;
  43. expression list e;
  44. position p;
  45. @@
  46.  
  47. fn("%@fl@", e@p)
  48.  
  49. @script:python s1@
  50. fl << r1.fl;
  51. fn << r1.fn;
  52. e << r1.e;
  53. p << r1.p;
  54. new_fmt;
  55. to_e;
  56. @@
  57. // Update the %fmt by the position (Position currently hardcode)
  58. new_fmt = fmt_replace_by_pos(coccinelle.fl, { 1, 3, 4 }, "%m")
  59. coccinelle.new_fmt = cocci.make_expr("\"{}\"".format(new_fmt))
  60.  
  61. // Debug
  62. print("* Debug: {}")
  63. _e = str(coccinelle.e).replace(" ", "")
  64. print("\t->old: {}(\"{}\", {})".format(coccinelle.fn, coccinelle.fl, _e))
  65. print("\t->new: {}(\"{}\", {})".format(coccinelle.fn, new_fmt, _e))
  66. print("")
  67.  
  68. @main depends on s1 && r1@
  69. format list r1.fl;
  70. expression s1.new_fmt;
  71. identifier r1.fn;
  72. expression list r1.e;
  73. expression list s1.to_e;
  74. position r1.p;
  75. //struct mydata SMD;
  76. //struct mydata* SMDP;
  77. @@
  78.  
  79.  fn(
  80. -"%@fl@"
  81. +new_fmt
  82. ,
  83. e@p
  84.  );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement