pinsformat.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/env node
  2. //
  3. // Formatter script for pins_MYPINS.h files
  4. //
  5. // Usage: mffmt [infile] [outfile]
  6. //
  7. // With no parameters convert STDIN to STDOUT
  8. //
  9. const fs = require("fs");
  10. // String lpad / rpad
  11. String.prototype.lpad = function(len, chr) {
  12. if (!len) return this;
  13. if (chr === undefined) chr = ' ';
  14. var s = this+'', need = len - s.length;
  15. if (need > 0) s = new Array(need+1).join(chr) + s;
  16. return s;
  17. };
  18. String.prototype.rpad = function(len, chr) {
  19. if (!len) return this;
  20. if (chr === undefined) chr = ' ';
  21. var s = this+'', need = len - s.length;
  22. if (need > 0) s += new Array(need+1).join(chr);
  23. return s;
  24. };
  25. const mpatt = [ '-?\\d{1,3}', 'P[A-I]\\d+', 'P\\d_\\d+', 'Pin[A-Z]\\d\\b' ],
  26. definePatt = new RegExp(`^\\s*(//)?#define\\s+[A-Z_][A-Z0-9_]+\\s+(${mpatt[0]}|${mpatt[1]}|${mpatt[2]}|${mpatt[3]})\\s*(//.*)?$`, 'gm'),
  27. ppad = [ 3, 4, 5, 5 ],
  28. col_comment = 50,
  29. col_value_rj = col_comment - 3;
  30. var mexpr = [];
  31. for (let m of mpatt) mexpr.push(new RegExp('^' + m + '$'));
  32. const argv = process.argv.slice(2), argc = argv.length;
  33. var src_file = 0, src_name = 'STDIN', dst_file, do_log = false;
  34. if (argc > 0) {
  35. let ind = 0;
  36. if (argv[0] == '-v') { do_log = true; ind++; }
  37. dst_file = src_file = src_name = argv[ind++];
  38. if (ind < argc) dst_file = argv[ind];
  39. }
  40. // Read from file or STDIN until it terminates
  41. const filtered = process_text(fs.readFileSync(src_file).toString());
  42. if (dst_file)
  43. fs.writeFileSync(dst_file, filtered);
  44. else
  45. console.log(filtered);
  46. // Find the pin pattern so non-pin defines can be skipped
  47. function get_pin_pattern(txt) {
  48. var r, m = 0, match_count = [ 0, 0, 0, 0 ];
  49. definePatt.lastIndex = 0;
  50. while ((r = definePatt.exec(txt)) !== null) {
  51. let ind = -1;
  52. if (mexpr.some((p) => {
  53. ind++;
  54. const didmatch = r[2].match(p);
  55. return r[2].match(p);
  56. }) ) {
  57. const m = ++match_count[ind];
  58. if (m >= 5) {
  59. return { match: mpatt[ind], pad:ppad[ind] };
  60. }
  61. }
  62. }
  63. return null;
  64. }
  65. function process_text(txt) {
  66. if (!txt.length) return '(no text)';
  67. const patt = get_pin_pattern(txt);
  68. if (!patt) return txt;
  69. const pindefPatt = new RegExp(`^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(${patt.match})\\s*(//.*)?$`),
  70. noPinPatt = new RegExp(`^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(-1)\\s*(//.*)?$`),
  71. skipPatt1 = new RegExp('^(\\s*(//)?#define)\\s+(AT90USB|USBCON|(BOARD|DAC|FLASH|HAS|IS|USE)_.+|.+_(ADDRESS|AVAILABLE|BAUDRATE|CLOCK|CONNECTION|DEFAULT|FREQ|ITEM|MODULE|NAME|ONLY|PERIOD|RANGE|RATE|SERIAL|SIZE|SPI|STATE|STEP|TIMER))\\s+(.+)\\s*(//.*)?$'),
  72. skipPatt2 = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+(0x[0-9A-Fa-f]+|\d+|.+[a-z].+)\\s*(//.*)?$'),
  73. aliasPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+([A-Z_][A-Z0-9_()]+)\\s*(//.*)?$'),
  74. switchPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s*(//.*)?$'),
  75. undefPatt = new RegExp('^(\\s*(//)?#undef)\\s+([A-Z_][A-Z0-9_]+)\\s*(//.*)?$'),
  76. defPatt = new RegExp('^(\\s*(//)?#define)\\s+([A-Z_][A-Z0-9_]+)\\s+([-_\\w]+)\\s*(//.*)?$'),
  77. condPatt = new RegExp('^(\\s*(//)?#(if|ifn?def|else|elif)(\\s+\\S+)*)\\s+(//.*)$'),
  78. commPatt = new RegExp('^\\s{20,}(//.*)?$');
  79. const col_value_lj = col_comment - patt.pad - 2;
  80. var r, out = '', check_comment_next = false;
  81. txt.split('\n').forEach((line) => {
  82. if (check_comment_next)
  83. check_comment_next = ((r = commPatt.exec(line)) !== null);
  84. if (check_comment_next)
  85. // Comments in column 45
  86. line = ''.rpad(col_comment) + r[1];
  87. else if (skipPatt1.exec(line) !== null) {
  88. //
  89. // #define SKIP_ME
  90. //
  91. if (do_log) console.log("skip:", line);
  92. }
  93. else if ((r = pindefPatt.exec(line)) !== null) {
  94. //
  95. // #define MY_PIN [pin]
  96. //
  97. if (do_log) console.log("pin:", line);
  98. const pinnum = r[4].charAt(0) == 'P' ? r[4] : r[4].lpad(patt.pad);
  99. line = r[1] + ' ' + r[3];
  100. line = line.rpad(col_value_lj) + pinnum;
  101. if (r[5]) line = line.rpad(col_comment) + r[5];
  102. }
  103. else if ((r = noPinPatt.exec(line)) !== null) {
  104. //
  105. // #define MY_PIN -1
  106. //
  107. if (do_log) console.log("pin -1:", line);
  108. line = r[1] + ' ' + r[3];
  109. line = line.rpad(col_value_lj) + '-1';
  110. if (r[5]) line = line.rpad(col_comment) + r[5];
  111. }
  112. else if (skipPatt2.exec(line) !== null) {
  113. //
  114. // #define SKIP_ME
  115. //
  116. if (do_log) console.log("skip:", line);
  117. }
  118. else if ((r = aliasPatt.exec(line)) !== null) {
  119. //
  120. // #define ALIAS OTHER
  121. //
  122. if (do_log) console.log("alias:", line);
  123. line = r[1] + ' ' + r[3];
  124. line += r[4].lpad(col_value_rj + 1 - line.length);
  125. if (r[5]) line = line.rpad(col_comment) + r[5];
  126. }
  127. else if ((r = switchPatt.exec(line)) !== null) {
  128. //
  129. // #define SWITCH
  130. //
  131. if (do_log) console.log("switch:", line);
  132. line = r[1] + ' ' + r[3];
  133. if (r[4]) line = line.rpad(col_comment) + r[4];
  134. check_comment_next = true;
  135. }
  136. else if ((r = defPatt.exec(line)) !== null) {
  137. //
  138. // #define ...
  139. //
  140. if (do_log) console.log("def:", line);
  141. line = r[1] + ' ' + r[3] + ' ';
  142. line += r[4].lpad(col_value_rj + 1 - line.length);
  143. if (r[5]) line = line.rpad(col_comment - 1) + ' ' + r[5];
  144. }
  145. else if ((r = undefPatt.exec(line)) !== null) {
  146. //
  147. // #undef ...
  148. //
  149. if (do_log) console.log("undef:", line);
  150. line = r[1] + ' ' + r[3];
  151. if (r[4]) line = line.rpad(col_comment) + r[4];
  152. }
  153. else if ((r = condPatt.exec(line)) !== null) {
  154. //
  155. // #if ...
  156. //
  157. if (do_log) console.log("cond:", line);
  158. line = r[1].rpad(col_comment) + r[5];
  159. check_comment_next = true;
  160. }
  161. out += line + '\n';
  162. });
  163. return out.replace(/\n\n+/g, '\n\n').replace(/\n\n$/g, '\n');
  164. }