l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

gcode.js (2328B)


      1 /*
      2  Language: G-code (ISO 6983)
      3  Contributors: Adam Joseph Cook <adam.joseph.cook@gmail.com>
      4  Description: G-code syntax highlighter for Fanuc and other common CNC machine tool controls.
      5  Website: https://www.sis.se/api/document/preview/911952/
      6  */
      7 
      8 function gcode(hljs) {
      9     var GCODE_IDENT_RE = '[A-Z_][A-Z0-9_.]*';
     10     var GCODE_CLOSE_RE = '\\%';
     11     var GCODE_KEYWORDS = {
     12       $pattern: GCODE_IDENT_RE,
     13       keyword: 'IF DO WHILE ENDWHILE CALL ENDIF SUB ENDSUB GOTO REPEAT ENDREPEAT ' +
     14       'EQ LT GT NE GE LE OR XOR'
     15     };
     16     var GCODE_START = {
     17         className: 'meta',
     18         begin: '([O])([0-9]+)'
     19     };
     20     var GCODE_CODE = [
     21         hljs.C_LINE_COMMENT_MODE,
     22         hljs.C_BLOCK_COMMENT_MODE,
     23         hljs.COMMENT(/\(/, /\)/),
     24         hljs.inherit(hljs.C_NUMBER_MODE, {begin: '([-+]?([0-9]*\\.?[0-9]+\\.?))|' + hljs.C_NUMBER_RE}),
     25         hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null}),
     26         hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null}),
     27         {
     28             className: 'name',
     29             begin: '([G])([0-9]+\\.?[0-9]?)'
     30         },
     31         {
     32             className: 'name',
     33             begin: '([M])([0-9]+\\.?[0-9]?)'
     34         },
     35         {
     36             className: 'attr',
     37             begin: '(VC|VS|#)',
     38             end: '(\\d+)'
     39         },
     40         {
     41             className: 'attr',
     42             begin: '(VZOFX|VZOFY|VZOFZ)'
     43         },
     44         {
     45             className: 'built_in',
     46             begin: '(ATAN|ABS|ACOS|ASIN|SIN|COS|EXP|FIX|FUP|ROUND|LN|TAN)(\\[)',
     47             end: '([-+]?([0-9]*\\.?[0-9]+\\.?))(\\])'
     48         },
     49         {
     50             className: 'symbol',
     51             variants: [
     52                 {
     53                     begin: 'N', end: '\\d+',
     54                     illegal: '\\W'
     55                 }
     56             ]
     57         }
     58     ];
     59 
     60     return {
     61         name: 'G-code (ISO 6983)',
     62         aliases: ['nc'],
     63         // Some implementations (CNC controls) of G-code are interoperable with uppercase and lowercase letters seamlessly.
     64         // However, most prefer all uppercase and uppercase is customary.
     65         case_insensitive: true,
     66         keywords: GCODE_KEYWORDS,
     67         contains: [
     68             {
     69                 className: 'meta',
     70                 begin: GCODE_CLOSE_RE
     71             },
     72             GCODE_START
     73         ].concat(GCODE_CODE)
     74     };
     75 }
     76 
     77 module.exports = gcode;