gcode.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. function gcode(hljs) {
  8. const GCODE_IDENT_RE = '[A-Z_][A-Z0-9_.]*';
  9. const GCODE_CLOSE_RE = '%';
  10. const GCODE_KEYWORDS = {
  11. $pattern: GCODE_IDENT_RE,
  12. keyword: 'IF DO WHILE ENDWHILE CALL ENDIF SUB ENDSUB GOTO REPEAT ENDREPEAT '
  13. + 'EQ LT GT NE GE LE OR XOR'
  14. };
  15. const GCODE_START = {
  16. className: 'meta',
  17. begin: '([O])([0-9]+)'
  18. };
  19. const NUMBER = hljs.inherit(hljs.C_NUMBER_MODE, { begin: '([-+]?((\\.\\d+)|(\\d+)(\\.\\d*)?))|' + hljs.C_NUMBER_RE });
  20. const GCODE_CODE = [
  21. hljs.C_LINE_COMMENT_MODE,
  22. hljs.C_BLOCK_COMMENT_MODE,
  23. hljs.COMMENT(/\(/, /\)/),
  24. NUMBER,
  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. contains: [ NUMBER ],
  48. end: '\\]'
  49. },
  50. {
  51. className: 'symbol',
  52. variants: [
  53. {
  54. begin: 'N',
  55. end: '\\d+',
  56. illegal: '\\W'
  57. }
  58. ]
  59. }
  60. ];
  61. return {
  62. name: 'G-code (ISO 6983)',
  63. aliases: [ 'nc' ],
  64. // Some implementations (CNC controls) of G-code are interoperable with uppercase and lowercase letters seamlessly.
  65. // However, most prefer all uppercase and uppercase is customary.
  66. case_insensitive: true,
  67. keywords: GCODE_KEYWORDS,
  68. contains: [
  69. {
  70. className: 'meta',
  71. begin: GCODE_CLOSE_RE
  72. },
  73. GCODE_START
  74. ].concat(GCODE_CODE)
  75. };
  76. }
  77. module.exports = gcode;