gcode.js 2.0 KB

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