|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { readFileSync } from 'fs'; |
| 4 | +import { join } from 'path'; |
| 5 | +import * as assert from 'assert'; |
| 6 | + |
| 7 | +interface syntaxFile { |
| 8 | + 'repository': { |
| 9 | + 'function-declarations': { |
| 10 | + 'patterns': [ |
| 11 | + { |
| 12 | + 'match':string |
| 13 | + } |
| 14 | + ] |
| 15 | + } |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +/** |
| 21 | + * Converts a POSIX regular expression string into a format |
| 22 | + * that JavaScript can use. This is because vscode parses |
| 23 | + * syntax files using POSIX but JavaScript can't support it. |
| 24 | + * @param {string} s - A string to be used as a regular expression |
| 25 | + */ |
| 26 | +const regex_from_posix = function (s: string): string { |
| 27 | + const mappings = [ |
| 28 | + ['[:alnum:]', 'a-zA-Z0-9'], |
| 29 | + ['[:alpha:]', 'a-zA-Z'] |
| 30 | + ]; |
| 31 | + let s2 = s; |
| 32 | + mappings.forEach((el) => { |
| 33 | + s2 = s2.replace(el[0], el[1]); |
| 34 | + }); |
| 35 | + return s2; |
| 36 | +}; |
| 37 | + |
| 38 | + |
| 39 | +const extension_root: string = join(__dirname, '..', '..', '..'); |
| 40 | + |
| 41 | +const r_syntax_file: string = join(extension_root, 'syntax', 'r.json'); |
| 42 | +console.log(r_syntax_file); |
| 43 | +const rsyntax_raw = readFileSync(r_syntax_file) as unknown; |
| 44 | +const rsyntax: syntaxFile = JSON.parse(rsyntax_raw as string) as syntaxFile; |
| 45 | + |
| 46 | +const function_pattern: string = rsyntax.repository['function-declarations'].patterns[0].match; |
| 47 | +const function_pattern_fixed: string = regex_from_posix(function_pattern); |
| 48 | + |
| 49 | + |
| 50 | +suite('Syntax Highlighting', () => { |
| 51 | + |
| 52 | + test('function-declarations - basic match', () => { |
| 53 | + const re = new RegExp(function_pattern_fixed); |
| 54 | + const line = 'x <- function(x) {'; |
| 55 | + const match = re.exec(line); |
| 56 | + assert.strictEqual(match[3], 'function'); |
| 57 | + }); |
| 58 | + |
| 59 | + test('function-declarations - extra spacing', () => { |
| 60 | + const re = new RegExp(function_pattern_fixed); |
| 61 | + const line = 'x <- function (x) {'; |
| 62 | + const match = re.exec(line); |
| 63 | + assert.strictEqual(match[3], 'function'); |
| 64 | + }); |
| 65 | + |
| 66 | + test('function-declarations - false function', () => { |
| 67 | + const re = new RegExp(function_pattern_fixed); |
| 68 | + const line = 'x <- functions'; |
| 69 | + const match = re.exec(line); |
| 70 | + assert.strictEqual(match, null); |
| 71 | + }); |
| 72 | + |
| 73 | +}); |
0 commit comments