language.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import type * as monacoType from 'monaco-editor/esm/vs/editor/editor.api';
  2. interface CloudWatchLanguage extends monacoType.languages.IMonarchLanguage {
  3. keywords: string[];
  4. operators: string[];
  5. builtinFunctions: string[];
  6. }
  7. export const SELECT = 'SELECT';
  8. export const FROM = 'FROM';
  9. export const WHERE = 'WHERE';
  10. export const GROUP = 'GROUP';
  11. export const ORDER = 'ORDER';
  12. export const BY = 'BY';
  13. export const DESC = 'DESC';
  14. export const ASC = 'ASC';
  15. export const LIMIT = 'LIMIT';
  16. export const WITH = 'WITH';
  17. export const SCHEMA = 'SCHEMA';
  18. export const KEYWORDS = [SELECT, FROM, WHERE, GROUP, ORDER, BY, DESC, ASC, LIMIT, WITH, SCHEMA];
  19. export const STATISTICS = ['AVG', 'COUNT', 'MAX', 'MIN', 'SUM'];
  20. export const AND = 'AND';
  21. export const LOGICAL_OPERATORS = [AND];
  22. export const EQUALS = '=';
  23. export const NOT_EQUALS = '!=';
  24. export const COMPARISON_OPERATORS = [EQUALS, NOT_EQUALS];
  25. export const language: CloudWatchLanguage = {
  26. defaultToken: '',
  27. tokenPostfix: '.sql',
  28. ignoreCase: true,
  29. brackets: [
  30. { open: '[', close: ']', token: 'delimiter.square' },
  31. { open: '(', close: ')', token: 'delimiter.parenthesis' },
  32. ],
  33. keywords: KEYWORDS,
  34. operators: LOGICAL_OPERATORS,
  35. builtinFunctions: STATISTICS,
  36. tokenizer: {
  37. root: [
  38. [/\$[a-zA-Z0-9-_]+/, 'variable'],
  39. { include: '@comments' },
  40. { include: '@whitespace' },
  41. { include: '@numbers' },
  42. { include: '@strings' },
  43. { include: '@complexIdentifiers' },
  44. [/[;,.]/, 'delimiter'],
  45. [/[()]/, '@brackets'],
  46. [
  47. /[\w@#$]+/,
  48. {
  49. cases: {
  50. '@keywords': 'keyword',
  51. '@operators': 'operator',
  52. '@builtinFunctions': 'predefined',
  53. '@default': 'identifier',
  54. },
  55. },
  56. ],
  57. [/[=!%&+\-*/|~^]/, 'operator'], // TODO: strip these options
  58. ],
  59. whitespace: [[/\s+/, 'white']],
  60. comments: [[/--+.*/, 'comment']],
  61. comment: [
  62. [/[^*/]+/, 'comment'],
  63. [/./, 'comment'],
  64. ],
  65. numbers: [
  66. [/0[xX][0-9a-fA-F]*/, 'number'],
  67. [/[$][+-]*\d*(\.\d*)?/, 'number'],
  68. [/((\d+(\.\d*)?)|(\.\d+))([eE][\-+]?\d+)?/, 'number'],
  69. ],
  70. strings: [
  71. [/N'/, { token: 'string', next: '@string' }],
  72. [/'/, { token: 'string', next: '@string' }],
  73. [/"/, { token: 'type', next: '@string_double' }],
  74. ],
  75. string: [
  76. [/[^']+/, 'string'],
  77. [/''/, 'string'],
  78. [/'/, { token: 'string', next: '@pop' }],
  79. ],
  80. string_double: [
  81. [/[^\\"]+/, 'type'],
  82. [/"/, 'type', '@pop'],
  83. ],
  84. complexIdentifiers: [
  85. [/\[/, { token: 'identifier.quote', next: '@bracketedIdentifier' }],
  86. [/"/, { token: 'identifier.quote', next: '@quotedIdentifier' }],
  87. ],
  88. bracketedIdentifier: [
  89. [/[^\]]+/, 'identifier'],
  90. [/]]/, 'identifier'],
  91. [/]/, { token: 'identifier.quote', next: '@pop' }],
  92. ],
  93. quotedIdentifier: [
  94. [/[^"]+/, 'identifier'],
  95. [/""/, 'identifier'],
  96. [/"/, { token: 'identifier.quote', next: '@pop' }],
  97. ],
  98. },
  99. };
  100. export const conf: monacoType.languages.LanguageConfiguration = {
  101. comments: {
  102. lineComment: '--',
  103. blockComment: ['/*', '*/'],
  104. },
  105. brackets: [
  106. ['{', '}'],
  107. ['[', ']'],
  108. ['(', ')'],
  109. ],
  110. autoClosingPairs: [
  111. { open: '{', close: '}' },
  112. { open: '[', close: ']' },
  113. { open: '(', close: ')' },
  114. { open: '"', close: '"' },
  115. { open: "'", close: "'" },
  116. ],
  117. surroundingPairs: [
  118. { open: '{', close: '}' },
  119. { open: '[', close: ']' },
  120. { open: '(', close: ')' },
  121. { open: '"', close: '"' },
  122. { open: "'", close: "'" },
  123. ],
  124. };