All files / parser vsltokenizer.js

81.08% Statements 30/37
60% Branches 6/10
80% Functions 12/15
85.71% Lines 30/35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192          1171x   1x 160x     1x   1x       1x       1x   1x                         1x 1x     22x 5x   5x   5x   5x     5x                     258x 258x   135x 135x         8x 8x                                                                                                                                                                                                             258x   8x 8x       16x 16x 8x     258x 258x        
import Tokenizer from './tokenizer';
import VSLScope from './vslscope';
import VSLTokenType from './vsltokentype';
 
function noop () {}
function passThrough (_, match) { return match; }
function strip (character) {
    return function transform (_, match) {
        return match.replace(character, '');
    };
}
const strip_ = strip('_');
function slice (start, end) {
    return function transform (_, match) {
        return match.slice(start, end);
    };
}
const slice1 = slice(1);
 
// from https://github.com/iamakulov/unescape-js/blob/master/src/index.js
 
const jsEscapeRegex = /\\(u\{([0-9A-Fa-f]+)\}|u([0-9A-Fa-f]{4})|x([0-9A-Fa-f]{2})|([1-7][0-7]{0,2}|[0-7]{2,3})|(['"tbrnfv0\\]))/g;
 
const usualEscapeSequences = {
    '0': '\0',
    'b': '\b',
    'f': '\f',
    'n': '\n',
    'r': '\r',
    't': '\t',
    'v': '\v',
    '\'': '\'',
    '"': '"',
    '\\': '\\'
};
 
const fromHex = (str) => String.fromCodePoint(parseInt(str, 16));
const fromOct = (str) => String.fromCodePoint(parseInt(str, 8));
 
function unescapeString(_, match) {
    return match.slice(1, -1).replace(jsEscapeRegex, (_, __, varHex, longHex, shortHex, octal, specialCharacter) => {
        Iif (varHex !== undefined)
            return fromHex(varHex);
        Iif (longHex !== undefined)
            return fromHex(longHex);
        Iif (shortHex !== undefined)
            return fromHex(shortHex);
        Iif (octal !== undefined)
            return fromOct(octal);
        else
            return usualEscapeSequences[specialCharacter];
    });
}
 
/**
 * VSL-specific Tokenizer
 * 
 * This defines tokens for VSL. For further information see {@link Tokenizer}
 */
export default class VSLTokenizer extends Tokenizer {
    constructor () {
        let tokenMatchers = Array(VSLScope.MAX);
        tokenMatchers[VSLScope.Normal] = [
            ['(?:\\s|\\\\\\n)*[\r\n](?:\\s|\\\\\\n)*', (self, match) => {
                self.newline(match.match(/(?:\r|\n|\r\n)/).length, match.match(/[ \t\v\f]*$/)[0].length - 1);
                return '\n';
            }],
            ['(?:\\s|\\\\\\n)+', noop],
            ['//[^\r\n]+', noop],
            ['/\\*', self => {
                self.variables.commentDepth++;
                self.begin(VSLScope.Comment);
            }, null],
            ['"(?:\\\\["bfnrt\\/\\\\]|\\\\u[a-fA-F0-9]{4}|[^"\\\\])*"', unescapeString, VSLTokenType.String],
            ["'(?:\\\\['bfnrt\\/\\\\]|\\\\u[a-fA-F0-9]{4}|[^'\\\\])*'", unescapeString, VSLTokenType.String],
            ['\\$+[0-9]+', passThrough, VSLTokenType.SpecialArgument],
            ['\\$[a-zA-Z_][a-zA-Z0-9_]*', slice1, VSLTokenType.SpecialIdentifier],
            ['\\.[0-9_]+', strip_, VSLTokenType.Decimal],
            ['[0-9][0-9_]*\\.[0-9_]+', strip_, VSLTokenType.Decimal],
            ['(?:[1-5]?[0-9]|6[0-2])b[0-9a-zA-Z_]*', strip_, VSLTokenType.Integer],
            ['[0-9][0-9_]*', strip_, VSLTokenType.Integer],
            ['/[^\/\*]([^\\/\r\n]|\\[^\r\n])+/[gmixc]*', passThrough, VSLTokenType.Regex],
            ['\\.\\.\\.', passThrough],
            ['\\.\\.', passThrough],
            ['\\.', passThrough],
            ['->', passThrough],
            ['=>', passThrough],
            ['~>', passThrough],
            [':>', passThrough],
            ['@@@', passThrough],
            ['@@', passThrough],
            ['@', passThrough],
            [';', passThrough],
            [':', passThrough],
            [',', passThrough],
            ['\\+=', passThrough],
            ['-=', passThrough],
            ['\\*\\*=', passThrough],
            ['\\*=', passThrough],
            ['/=', passThrough],
            ['%=', passThrough],
            ['\\+', passThrough],
            ['-', passThrough],
            ['/', passThrough],
            ['%', passThrough],
            ['\\*\\*', passThrough],
            ['\\*', passThrough],
            ['<<=', passThrough],
            ['>>=', passThrough],
            ['<<', passThrough],
            ['>>', passThrough],
            ['==', passThrough],
            ['!=', passThrough],
            ['<>', passThrough],
            ['<=>', passThrough],
            ['<=', passThrough],
            ['>=', passThrough],
            ['<', passThrough],
            ['>', passThrough],
            ['=', passThrough],
            [':=', passThrough],
            ['&=', passThrough],
            ['\\|=', passThrough],
            ['\\^=', passThrough],
            ['&&', passThrough],
            ['\\|\\|', passThrough],
            ['!', passThrough],
            ['&', passThrough],
            ['\\|', passThrough],
            ['~', passThrough],
            ['\\^', passThrough],
            ['\\?', passThrough],
            ['and', passThrough],
            ['or', passThrough],
            ['not', passThrough],
            ['xor', passThrough],
            ['::', passThrough],
            ['{', passThrough],
            ['}', passThrough],
            ['\\(', passThrough],
            ['\\)', passThrough],
            ['\\[', passThrough],
            ['\\]', passThrough],
            
            ['var', passThrough],
            ['let', passThrough],
            ['final', passThrough],
            ['const', passThrough],
 
            ['static', passThrough],
            ['lazy', passThrough],
 
            ['public', passThrough],
            ['private', passThrough],
            ['readonly', passThrough],
            ['internal', passThrough],
 
            ['is', passThrough],
 
            ['function', passThrough],           
            ['func', passThrough],
            ['fn', passThrough],
            ['class', passThrough],
            ['struct', passThrough],
            ['interface', passThrough],
            ['enum', passThrough],
            ['typealias', passThrough],
            
            ['if', passThrough],
            ['for', passThrough],
            ['while', passThrough],
            
            ['[a-zA-Z_][a-zA-Z0-9_]*', passThrough, VSLTokenType.Identifier]
        ];
        tokenMatchers[VSLScope.Comment] = [
            ['/\\*', self => {
                self.variables.commentDepth++;
                self.begin(VSLScope.Comment);
            }],
            ['(?:[^/*]|\\*[^/]|/[^*])+', noop],
            ['\\*/', self => {
                self.variables.commentDepth--;
                if (!self.variables.commentDepth)
                    self.begin(VSLScope.Normal);
            }]
        ];
        super(tokenMatchers, VSLScope.Normal);
        this.variables = {
            commentDepth: 0
        };
    }
}