routeros.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Language: MikroTik RouterOS script
  3. Author: Ivan Dementev <ivan_div@mail.ru>
  4. Description: Scripting host provides a way to automate some router maintenance tasks by means of executing user-defined scripts bounded to some event occurrence
  5. Website: https://wiki.mikrotik.com/wiki/Manual:Scripting
  6. */
  7. // Colors from RouterOS terminal:
  8. // green - #0E9A00
  9. // teal - #0C9A9A
  10. // purple - #99069A
  11. // light-brown - #9A9900
  12. function routeros(hljs) {
  13. const STATEMENTS = 'foreach do while for if from to step else on-error and or not in';
  14. // Global commands: Every global command should start with ":" token, otherwise it will be treated as variable.
  15. const GLOBAL_COMMANDS = 'global local beep delay put len typeof pick log time set find environment terminal error execute parse resolve toarray tobool toid toip toip6 tonum tostr totime';
  16. // Common commands: Following commands available from most sub-menus:
  17. const COMMON_COMMANDS = 'add remove enable disable set get print export edit find run debug error info warning';
  18. const LITERALS = 'true false yes no nothing nil null';
  19. const OBJECTS = 'traffic-flow traffic-generator firewall scheduler aaa accounting address-list address align area bandwidth-server bfd bgp bridge client clock community config connection console customer default dhcp-client dhcp-server discovery dns e-mail ethernet filter firmware gps graphing group hardware health hotspot identity igmp-proxy incoming instance interface ip ipsec ipv6 irq l2tp-server lcd ldp logging mac-server mac-winbox mangle manual mirror mme mpls nat nd neighbor network note ntp ospf ospf-v3 ovpn-server page peer pim ping policy pool port ppp pppoe-client pptp-server prefix profile proposal proxy queue radius resource rip ripng route routing screen script security-profiles server service service-port settings shares smb sms sniffer snmp snooper socks sstp-server system tool tracking type upgrade upnp user-manager users user vlan secret vrrp watchdog web-access wireless pptp pppoe lan wan layer7-protocol lease simple raw';
  20. const VAR = {
  21. className: 'variable',
  22. variants: [
  23. { begin: /\$[\w\d#@][\w\d_]*/ },
  24. { begin: /\$\{(.*?)\}/ }
  25. ]
  26. };
  27. const QUOTE_STRING = {
  28. className: 'string',
  29. begin: /"/,
  30. end: /"/,
  31. contains: [
  32. hljs.BACKSLASH_ESCAPE,
  33. VAR,
  34. {
  35. className: 'variable',
  36. begin: /\$\(/,
  37. end: /\)/,
  38. contains: [ hljs.BACKSLASH_ESCAPE ]
  39. }
  40. ]
  41. };
  42. const APOS_STRING = {
  43. className: 'string',
  44. begin: /'/,
  45. end: /'/
  46. };
  47. return {
  48. name: 'MikroTik RouterOS script',
  49. aliases: [ 'mikrotik' ],
  50. case_insensitive: true,
  51. keywords: {
  52. $pattern: /:?[\w-]+/,
  53. literal: LITERALS,
  54. keyword: STATEMENTS + ' :' + STATEMENTS.split(' ').join(' :') + ' :' + GLOBAL_COMMANDS.split(' ').join(' :')
  55. },
  56. contains: [
  57. { // illegal syntax
  58. variants: [
  59. { // -- comment
  60. begin: /\/\*/,
  61. end: /\*\//
  62. },
  63. { // Stan comment
  64. begin: /\/\//,
  65. end: /$/
  66. },
  67. { // HTML tags
  68. begin: /<\//,
  69. end: />/
  70. }
  71. ],
  72. illegal: /./
  73. },
  74. hljs.COMMENT('^#', '$'),
  75. QUOTE_STRING,
  76. APOS_STRING,
  77. VAR,
  78. // attribute=value
  79. {
  80. // > is to avoid matches with => in other grammars
  81. begin: /[\w-]+=([^\s{}[\]()>]+)/,
  82. relevance: 0,
  83. returnBegin: true,
  84. contains: [
  85. {
  86. className: 'attribute',
  87. begin: /[^=]+/
  88. },
  89. {
  90. begin: /=/,
  91. endsWithParent: true,
  92. relevance: 0,
  93. contains: [
  94. QUOTE_STRING,
  95. APOS_STRING,
  96. VAR,
  97. {
  98. className: 'literal',
  99. begin: '\\b(' + LITERALS.split(' ').join('|') + ')\\b'
  100. },
  101. {
  102. // Do not format unclassified values. Needed to exclude highlighting of values as built_in.
  103. begin: /("[^"]*"|[^\s{}[\]]+)/ }
  104. /*
  105. {
  106. // IPv4 addresses and subnets
  107. className: 'number',
  108. variants: [
  109. {begin: IPADDR_wBITMASK+'(,'+IPADDR_wBITMASK+')*'}, //192.168.0.0/24,1.2.3.0/24
  110. {begin: IPADDR+'-'+IPADDR}, // 192.168.0.1-192.168.0.3
  111. {begin: IPADDR+'(,'+IPADDR+')*'}, // 192.168.0.1,192.168.0.34,192.168.24.1,192.168.0.1
  112. ]
  113. },
  114. {
  115. // MAC addresses and DHCP Client IDs
  116. className: 'number',
  117. begin: /\b(1:)?([0-9A-Fa-f]{1,2}[:-]){5}([0-9A-Fa-f]){1,2}\b/,
  118. },
  119. */
  120. ]
  121. }
  122. ]
  123. },
  124. {
  125. // HEX values
  126. className: 'number',
  127. begin: /\*[0-9a-fA-F]+/
  128. },
  129. {
  130. begin: '\\b(' + COMMON_COMMANDS.split(' ').join('|') + ')([\\s[(\\]|])',
  131. returnBegin: true,
  132. contains: [
  133. {
  134. className: 'built_in', // 'function',
  135. begin: /\w+/
  136. }
  137. ]
  138. },
  139. {
  140. className: 'built_in',
  141. variants: [
  142. { begin: '(\\.\\./|/|\\s)((' + OBJECTS.split(' ').join('|') + ');?\\s)+' },
  143. {
  144. begin: /\.\./,
  145. relevance: 0
  146. }
  147. ]
  148. }
  149. ]
  150. };
  151. }
  152. module.exports = routeros;