• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/jsdoc/c-transpiler.js

  1 /*
  2  * c-transpiler.js - transpile C to JS while retaining line numbers.
  3  *
  4  * Copyright (C) 2023 Jo-Philipp Wich <jo@mein.io>
  5  *
  6  * Permission to use, copy, modify, and/or distribute this software for any
  7  * purpose with or without fee is hereby granted, provided that the above
  8  * copyright notice and this permission notice appear in all copies.
  9  *
 10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 17  */
 18 
 19 'use strict';
 20 
 21 function isCommentStart(source, offset) {
 22         if (source[offset++] != '\n')
 23                 return false;
 24 
 25         while (source[offset] == ' ' || source[offset] == '\t')
 26                 offset++;
 27 
 28         return (
 29                 source[offset++] == '/' &&
 30                 source[offset++] == '*' &&
 31                 source[offset++] == '*'
 32         );
 33 }
 34 
 35 exports.handlers = {
 36   beforeParse: function(e) {
 37         if (!e.filename.match(/\.(c|h)$/))
 38                 return;
 39 
 40         let chunks = [ { start: 0, end: -1, comment: false } ];
 41         let chunk = chunks[0];
 42         let i = 0;
 43 
 44         for (i = 0; i < e.source.length; i++) {
 45                 if (!chunk.comment && isCommentStart(e.source, i)) {
 46                         chunk.end = i;
 47                         chunk = { start: i, end: -1, comment: true };
 48                         chunks.push(chunk);
 49                         i += 3;
 50                 }
 51                 else if (chunk.comment && e.source[i] == '*' && e.source[i+1] == '/') {
 52                         chunk.end = i + 1;
 53                         chunk = { start: i + 1, end: -1, comment: false };
 54                         chunks.push(chunk);
 55                         i += 1;
 56                 }
 57         }
 58 
 59         chunk.end = i;
 60 
 61         let source = '';
 62 
 63         for (chunk of chunks) {
 64                 if (chunk.comment)
 65                         source += e.source.substring(chunk.start, chunk.end);
 66                 else
 67                         source += e.source.substring(chunk.start, chunk.end).replace(/(^|\n)/g, '$1//');
 68         }
 69 
 70         e.source = source;
 71   }
 72 };

This page was automatically generated by LXR 0.3.1.  •  OpenWrt