gcode.js 2.3 KB

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