gcode.js 2.1 KB

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