|
| 1 | +import type { languages } from 'monaco-editor' |
| 2 | + |
| 3 | +export const conf: languages.LanguageConfiguration = { |
| 4 | + comments: { |
| 5 | + lineComment: '//', |
| 6 | + }, |
| 7 | + brackets: [['(', ')']], |
| 8 | + autoClosingPairs: [ |
| 9 | + { open: '(', close: ')' }, |
| 10 | + { open: '"', close: '"', notIn: ['string'] }, |
| 11 | + { open: '`', close: '`', notIn: ['string'] }, |
| 12 | + ], |
| 13 | + surroundingPairs: [ |
| 14 | + { open: '(', close: ')' }, |
| 15 | + { open: '"', close: '"' }, |
| 16 | + { open: '`', close: '`' }, |
| 17 | + ], |
| 18 | +} |
| 19 | + |
| 20 | +export const language: languages.IMonarchLanguage = { |
| 21 | + defaultToken: '', |
| 22 | + tokenPostfix: '.go.mod', |
| 23 | + |
| 24 | + keywords: ['module', 'require', 'replace', 'exclude', 'go', 'toolchain'], |
| 25 | + operators: ['=>'], |
| 26 | + |
| 27 | + tokenizer: { |
| 28 | + root: [ |
| 29 | + // Comments |
| 30 | + [/\/\/.*$/, 'comment'], |
| 31 | + |
| 32 | + // Multi-Line Directive |
| 33 | + [ |
| 34 | + /^(\w+)(\s*)(\()/, |
| 35 | + [ |
| 36 | + { cases: { '@keywords': 'keyword', '@default': 'identifier' }, next: '@directiveArgs' }, |
| 37 | + 'white', |
| 38 | + 'delimiter.parenthesis', |
| 39 | + ], |
| 40 | + ], |
| 41 | + |
| 42 | + // Single-Line Directive |
| 43 | + [ |
| 44 | + /^(\w+)(\s*)(.*)$/, |
| 45 | + [{ cases: { '@keywords': 'keyword', '@default': 'identifier' } }, 'white', { token: '', next: '@arguments' }], |
| 46 | + ], |
| 47 | + |
| 48 | + // Invalid |
| 49 | + [/.*$/, 'invalid'], |
| 50 | + ], |
| 51 | + |
| 52 | + directiveArgs: [{ include: '@arguments' }, [/\)/, { token: 'delimiter.parenthesis', next: '@pop' }]], |
| 53 | + |
| 54 | + arguments: [ |
| 55 | + // Comments |
| 56 | + [/\/\/.*$/, 'comment'], |
| 57 | + |
| 58 | + // Double Quoted String |
| 59 | + [/"([^"\\]|\\.)*$/, 'string.invalid'], // Non-terminated string |
| 60 | + [/"/, { token: 'string.quote', next: '@doubleString' }], |
| 61 | + |
| 62 | + // Raw Quoted String |
| 63 | + [/`/, { token: 'string.quote', next: '@rawString' }], |
| 64 | + |
| 65 | + // Operator |
| 66 | + [/=>/, 'operator'], |
| 67 | + |
| 68 | + // Semver Version |
| 69 | + [ |
| 70 | + /v(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?(?:\+[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)*)?/, |
| 71 | + 'number', |
| 72 | + ], |
| 73 | + |
| 74 | + // Unquoted String |
| 75 | + [/([^\s/]|\/(?!\/))+/, 'string'], |
| 76 | + |
| 77 | + // Whitespace |
| 78 | + [/\s+/, 'white'], |
| 79 | + |
| 80 | + // End of Line |
| 81 | + [/$/, '', '@pop'], |
| 82 | + ], |
| 83 | + |
| 84 | + doubleString: [ |
| 85 | + // Escaped Characters |
| 86 | + { include: '@stringEscapedChar' }, |
| 87 | + |
| 88 | + // Placeholders |
| 89 | + { include: '@stringPlaceholder' }, |
| 90 | + |
| 91 | + // Regular String Content |
| 92 | + [/[^\\%"']+/, 'string'], |
| 93 | + |
| 94 | + // Escape at End of Line |
| 95 | + [/\\$/, 'string.escape'], |
| 96 | + |
| 97 | + // Closing Quote |
| 98 | + [/"/, { token: 'string.quote', next: '@pop' }], |
| 99 | + |
| 100 | + // Invalid Escape |
| 101 | + [/\\./, 'string.escape.invalid'], |
| 102 | + ], |
| 103 | + |
| 104 | + rawString: [ |
| 105 | + // Placeholders |
| 106 | + { include: '@stringPlaceholder' }, |
| 107 | + |
| 108 | + // Regular String Content |
| 109 | + [/[^%`]+/, 'string'], |
| 110 | + |
| 111 | + // Percent Sign Not Followed by Placeholder |
| 112 | + [/%%/, 'string'], |
| 113 | + [/%/, 'string'], |
| 114 | + |
| 115 | + // Closing Backtick |
| 116 | + [/`/, { token: 'string.quote', next: '@pop' }], |
| 117 | + ], |
| 118 | + |
| 119 | + stringEscapedChar: [ |
| 120 | + [/\\([0-7]{3}|[abfnrtv\\"']|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/, 'string.escape'], |
| 121 | + [/\\./, 'invalid'], |
| 122 | + ], |
| 123 | + |
| 124 | + stringPlaceholder: [ |
| 125 | + [/%(\[\d+])?([+#\-0\x20]{0,2}((\d+|\*)?(\.?(?:\d+|\*|\[\d+]\*?)?\[\d+]?)?))?[vT%tbcdoqxXUeEfFgGsp]/, 'variable'], |
| 126 | + ], |
| 127 | + }, |
| 128 | +} |
0 commit comments