gcode.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, {
  20. begin: '([-+]?((\\.\\d+)|(\\d+)(\\.\\d*)?))|' + hljs.C_NUMBER_RE
  21. });
  22. const GCODE_CODE = [
  23. hljs.C_LINE_COMMENT_MODE,
  24. hljs.C_BLOCK_COMMENT_MODE,
  25. hljs.COMMENT(/\(/, /\)/),
  26. NUMBER,
  27. hljs.inherit(hljs.APOS_STRING_MODE, {
  28. illegal: null
  29. }),
  30. hljs.inherit(hljs.QUOTE_STRING_MODE, {
  31. illegal: null
  32. }),
  33. {
  34. className: 'name',
  35. begin: '([G])([0-9]+\\.?[0-9]?)'
  36. },
  37. {
  38. className: 'name',
  39. begin: '([M])([0-9]+\\.?[0-9]?)'
  40. },
  41. {
  42. className: 'attr',
  43. begin: '(VC|VS|#)',
  44. end: '(\\d+)'
  45. },
  46. {
  47. className: 'attr',
  48. begin: '(VZOFX|VZOFY|VZOFZ)'
  49. },
  50. {
  51. className: 'built_in',
  52. begin: '(ATAN|ABS|ACOS|ASIN|SIN|COS|EXP|FIX|FUP|ROUND|LN|TAN)(\\[)',
  53. contains: [
  54. NUMBER
  55. ],
  56. end: '\\]'
  57. },
  58. {
  59. className: 'symbol',
  60. variants: [
  61. {
  62. begin: 'N',
  63. end: '\\d+',
  64. illegal: '\\W'
  65. }
  66. ]
  67. }
  68. ];
  69. return {
  70. name: 'G-code (ISO 6983)',
  71. aliases: ['nc'],
  72. // Some implementations (CNC controls) of G-code are interoperable with uppercase and lowercase letters seamlessly.
  73. // However, most prefer all uppercase and uppercase is customary.
  74. case_insensitive: true,
  75. keywords: GCODE_KEYWORDS,
  76. contains: [
  77. {
  78. className: 'meta',
  79. begin: GCODE_CLOSE_RE
  80. },
  81. GCODE_START
  82. ].concat(GCODE_CODE)
  83. };
  84. }
  85. module.exports = gcode;