python.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. import { languages } from '../fillers/monaco-editor-core.js';
  6. export var conf = {
  7. comments: {
  8. lineComment: '#',
  9. blockComment: ["'''", "'''"]
  10. },
  11. brackets: [
  12. ['{', '}'],
  13. ['[', ']'],
  14. ['(', ')']
  15. ],
  16. autoClosingPairs: [
  17. { open: '{', close: '}' },
  18. { open: '[', close: ']' },
  19. { open: '(', close: ')' },
  20. { open: '"', close: '"', notIn: ['string'] },
  21. { open: "'", close: "'", notIn: ['string', 'comment'] }
  22. ],
  23. surroundingPairs: [
  24. { open: '{', close: '}' },
  25. { open: '[', close: ']' },
  26. { open: '(', close: ')' },
  27. { open: '"', close: '"' },
  28. { open: "'", close: "'" }
  29. ],
  30. onEnterRules: [
  31. {
  32. beforeText: new RegExp('^\\s*(?:def|class|for|if|elif|else|while|try|with|finally|except|async).*?:\\s*$'),
  33. action: { indentAction: languages.IndentAction.Indent }
  34. }
  35. ],
  36. folding: {
  37. offSide: true,
  38. markers: {
  39. start: new RegExp('^\\s*#region\\b'),
  40. end: new RegExp('^\\s*#endregion\\b')
  41. }
  42. }
  43. };
  44. export var language = {
  45. defaultToken: '',
  46. tokenPostfix: '.python',
  47. keywords: [
  48. // This section is the result of running
  49. // `for k in keyword.kwlist: print(' "' + k + '",')` in a Python REPL,
  50. // though note that the output from Python 3 is not a strict superset of the
  51. // output from Python 2.
  52. 'False',
  53. 'None',
  54. 'True',
  55. 'and',
  56. 'as',
  57. 'assert',
  58. 'async',
  59. 'await',
  60. 'break',
  61. 'class',
  62. 'continue',
  63. 'def',
  64. 'del',
  65. 'elif',
  66. 'else',
  67. 'except',
  68. 'exec',
  69. 'finally',
  70. 'for',
  71. 'from',
  72. 'global',
  73. 'if',
  74. 'import',
  75. 'in',
  76. 'is',
  77. 'lambda',
  78. 'nonlocal',
  79. 'not',
  80. 'or',
  81. 'pass',
  82. 'print',
  83. 'raise',
  84. 'return',
  85. 'try',
  86. 'while',
  87. 'with',
  88. 'yield',
  89. 'int',
  90. 'float',
  91. 'long',
  92. 'complex',
  93. 'hex',
  94. 'abs',
  95. 'all',
  96. 'any',
  97. 'apply',
  98. 'basestring',
  99. 'bin',
  100. 'bool',
  101. 'buffer',
  102. 'bytearray',
  103. 'callable',
  104. 'chr',
  105. 'classmethod',
  106. 'cmp',
  107. 'coerce',
  108. 'compile',
  109. 'complex',
  110. 'delattr',
  111. 'dict',
  112. 'dir',
  113. 'divmod',
  114. 'enumerate',
  115. 'eval',
  116. 'execfile',
  117. 'file',
  118. 'filter',
  119. 'format',
  120. 'frozenset',
  121. 'getattr',
  122. 'globals',
  123. 'hasattr',
  124. 'hash',
  125. 'help',
  126. 'id',
  127. 'input',
  128. 'intern',
  129. 'isinstance',
  130. 'issubclass',
  131. 'iter',
  132. 'len',
  133. 'locals',
  134. 'list',
  135. 'map',
  136. 'max',
  137. 'memoryview',
  138. 'min',
  139. 'next',
  140. 'object',
  141. 'oct',
  142. 'open',
  143. 'ord',
  144. 'pow',
  145. 'print',
  146. 'property',
  147. 'reversed',
  148. 'range',
  149. 'raw_input',
  150. 'reduce',
  151. 'reload',
  152. 'repr',
  153. 'reversed',
  154. 'round',
  155. 'self',
  156. 'set',
  157. 'setattr',
  158. 'slice',
  159. 'sorted',
  160. 'staticmethod',
  161. 'str',
  162. 'sum',
  163. 'super',
  164. 'tuple',
  165. 'type',
  166. 'unichr',
  167. 'unicode',
  168. 'vars',
  169. 'xrange',
  170. 'zip',
  171. '__dict__',
  172. '__methods__',
  173. '__members__',
  174. '__class__',
  175. '__bases__',
  176. '__name__',
  177. '__mro__',
  178. '__subclasses__',
  179. '__init__',
  180. '__import__'
  181. ],
  182. brackets: [
  183. { open: '{', close: '}', token: 'delimiter.curly' },
  184. { open: '[', close: ']', token: 'delimiter.bracket' },
  185. { open: '(', close: ')', token: 'delimiter.parenthesis' }
  186. ],
  187. tokenizer: {
  188. root: [
  189. { include: '@whitespace' },
  190. { include: '@numbers' },
  191. { include: '@strings' },
  192. [/[,:;]/, 'delimiter'],
  193. [/[{}\[\]()]/, '@brackets'],
  194. [/@[a-zA-Z_]\w*/, 'tag'],
  195. [
  196. /[a-zA-Z_]\w*/,
  197. {
  198. cases: {
  199. '@keywords': 'keyword',
  200. '@default': 'identifier'
  201. }
  202. }
  203. ]
  204. ],
  205. // Deal with white space, including single and multi-line comments
  206. whitespace: [
  207. [/\s+/, 'white'],
  208. [/(^#.*$)/, 'comment'],
  209. [/'''/, 'string', '@endDocString'],
  210. [/"""/, 'string', '@endDblDocString']
  211. ],
  212. endDocString: [
  213. [/[^']+/, 'string'],
  214. [/\\'/, 'string'],
  215. [/'''/, 'string', '@popall'],
  216. [/'/, 'string']
  217. ],
  218. endDblDocString: [
  219. [/[^"]+/, 'string'],
  220. [/\\"/, 'string'],
  221. [/"""/, 'string', '@popall'],
  222. [/"/, 'string']
  223. ],
  224. // Recognize hex, negatives, decimals, imaginaries, longs, and scientific notation
  225. numbers: [
  226. [/-?0x([abcdef]|[ABCDEF]|\d)+[lL]?/, 'number.hex'],
  227. [/-?(\d*\.)?\d+([eE][+\-]?\d+)?[jJ]?[lL]?/, 'number']
  228. ],
  229. // Recognize strings, including those broken across lines with \ (but not without)
  230. strings: [
  231. [/'$/, 'string.escape', '@popall'],
  232. [/'/, 'string.escape', '@stringBody'],
  233. [/"$/, 'string.escape', '@popall'],
  234. [/"/, 'string.escape', '@dblStringBody']
  235. ],
  236. stringBody: [
  237. [/[^\\']+$/, 'string', '@popall'],
  238. [/[^\\']+/, 'string'],
  239. [/\\./, 'string'],
  240. [/'/, 'string.escape', '@popall'],
  241. [/\\$/, 'string']
  242. ],
  243. dblStringBody: [
  244. [/[^\\"]+$/, 'string', '@popall'],
  245. [/[^\\"]+/, 'string'],
  246. [/\\./, 'string'],
  247. [/"/, 'string.escape', '@popall'],
  248. [/\\$/, 'string']
  249. ]
  250. }
  251. };