lua.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. export var conf = {
  6. comments: {
  7. lineComment: '--',
  8. blockComment: ['--[[', ']]']
  9. },
  10. brackets: [
  11. ['{', '}'],
  12. ['[', ']'],
  13. ['(', ')']
  14. ],
  15. autoClosingPairs: [
  16. { open: '{', close: '}' },
  17. { open: '[', close: ']' },
  18. { open: '(', close: ')' },
  19. { open: '"', close: '"' },
  20. { open: "'", close: "'" }
  21. ],
  22. surroundingPairs: [
  23. { open: '{', close: '}' },
  24. { open: '[', close: ']' },
  25. { open: '(', close: ')' },
  26. { open: '"', close: '"' },
  27. { open: "'", close: "'" }
  28. ]
  29. };
  30. export var language = {
  31. defaultToken: '',
  32. tokenPostfix: '.lua',
  33. keywords: [
  34. 'and',
  35. 'break',
  36. 'do',
  37. 'else',
  38. 'elseif',
  39. 'end',
  40. 'false',
  41. 'for',
  42. 'function',
  43. 'goto',
  44. 'if',
  45. 'in',
  46. 'local',
  47. 'nil',
  48. 'not',
  49. 'or',
  50. 'repeat',
  51. 'return',
  52. 'then',
  53. 'true',
  54. 'until',
  55. 'while'
  56. ],
  57. brackets: [
  58. { token: 'delimiter.bracket', open: '{', close: '}' },
  59. { token: 'delimiter.array', open: '[', close: ']' },
  60. { token: 'delimiter.parenthesis', open: '(', close: ')' }
  61. ],
  62. operators: [
  63. '+',
  64. '-',
  65. '*',
  66. '/',
  67. '%',
  68. '^',
  69. '#',
  70. '==',
  71. '~=',
  72. '<=',
  73. '>=',
  74. '<',
  75. '>',
  76. '=',
  77. ';',
  78. ':',
  79. ',',
  80. '.',
  81. '..',
  82. '...'
  83. ],
  84. // we include these common regular expressions
  85. symbols: /[=><!~?:&|+\-*\/\^%]+/,
  86. escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  87. // The main tokenizer for our languages
  88. tokenizer: {
  89. root: [
  90. // identifiers and keywords
  91. [
  92. /[a-zA-Z_]\w*/,
  93. {
  94. cases: {
  95. '@keywords': { token: 'keyword.$0' },
  96. '@default': 'identifier'
  97. }
  98. }
  99. ],
  100. // whitespace
  101. { include: '@whitespace' },
  102. // keys
  103. [/(,)(\s*)([a-zA-Z_]\w*)(\s*)(:)(?!:)/, ['delimiter', '', 'key', '', 'delimiter']],
  104. [/({)(\s*)([a-zA-Z_]\w*)(\s*)(:)(?!:)/, ['@brackets', '', 'key', '', 'delimiter']],
  105. // delimiters and operators
  106. [/[{}()\[\]]/, '@brackets'],
  107. [
  108. /@symbols/,
  109. {
  110. cases: {
  111. '@operators': 'delimiter',
  112. '@default': ''
  113. }
  114. }
  115. ],
  116. // numbers
  117. [/\d*\.\d+([eE][\-+]?\d+)?/, 'number.float'],
  118. [/0[xX][0-9a-fA-F_]*[0-9a-fA-F]/, 'number.hex'],
  119. [/\d+?/, 'number'],
  120. // delimiter: after number because of .\d floats
  121. [/[;,.]/, 'delimiter'],
  122. // strings: recover on non-terminated strings
  123. [/"([^"\\]|\\.)*$/, 'string.invalid'],
  124. [/'([^'\\]|\\.)*$/, 'string.invalid'],
  125. [/"/, 'string', '@string."'],
  126. [/'/, 'string', "@string.'"]
  127. ],
  128. whitespace: [
  129. [/[ \t\r\n]+/, ''],
  130. [/--\[([=]*)\[/, 'comment', '@comment.$1'],
  131. [/--.*$/, 'comment']
  132. ],
  133. comment: [
  134. [/[^\]]+/, 'comment'],
  135. [
  136. /\]([=]*)\]/,
  137. {
  138. cases: {
  139. '$1==$S2': { token: 'comment', next: '@pop' },
  140. '@default': 'comment'
  141. }
  142. }
  143. ],
  144. [/./, 'comment']
  145. ],
  146. string: [
  147. [/[^\\"']+/, 'string'],
  148. [/@escapes/, 'string.escape'],
  149. [/\\./, 'string.escape.invalid'],
  150. [
  151. /["']/,
  152. {
  153. cases: {
  154. '$#==$S2': { token: 'string', next: '@pop' },
  155. '@default': 'string'
  156. }
  157. }
  158. ]
  159. ]
  160. }
  161. };