gcode.js 2.1 KB

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