|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import { TextRange } from './TextRange'; |
| 5 | + |
| 6 | +export enum TokenKind { |
| 7 | + // One or more spaces/tabs |
| 8 | + Spaces, |
| 9 | + // A single newline sequence such as CRLF or LF |
| 10 | + NewLine, |
| 11 | + // An unrecognized character |
| 12 | + Other, |
| 13 | + // The end of the input string |
| 14 | + EndOfInput |
| 15 | +} |
| 16 | + |
| 17 | +export class Token { |
| 18 | + public readonly kind: TokenKind; |
| 19 | + public readonly range: TextRange; |
| 20 | + |
| 21 | + public constructor(kind: TokenKind, range: TextRange) { |
| 22 | + this.kind = kind; |
| 23 | + this.range = range; |
| 24 | + } |
| 25 | + |
| 26 | + public toString(): string { |
| 27 | + return this.range.toString(); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export class Tokenizer { |
| 32 | + public readonly input: TextRange; |
| 33 | + private _currentIndex: number; |
| 34 | + |
| 35 | + constructor(input: TextRange | string) { |
| 36 | + if (typeof(input) === 'string') { |
| 37 | + this.input = TextRange.fromString(input); |
| 38 | + } else { |
| 39 | + this.input = input; |
| 40 | + } |
| 41 | + this._currentIndex = this.input.pos; |
| 42 | + } |
| 43 | + |
| 44 | + public get currentIndex(): number { |
| 45 | + return this._currentIndex; |
| 46 | + } |
| 47 | + |
| 48 | + public getToken(): Token { |
| 49 | + const input: TextRange = this.input; |
| 50 | + |
| 51 | + const startIndex: number = this._currentIndex; |
| 52 | + let c: string | undefined = this._get(); |
| 53 | + |
| 54 | + // Reached end of input yet? |
| 55 | + if (c === undefined) { |
| 56 | + return new Token(TokenKind.EndOfInput, TextRange.empty); |
| 57 | + } |
| 58 | + |
| 59 | + // Is it a sequence of whitespace? |
| 60 | + if (/[ \t]/.test(c)) { |
| 61 | + |
| 62 | + while (Tokenizer._isSpace(this._peek())) { |
| 63 | + this._get(); |
| 64 | + } |
| 65 | + |
| 66 | + return new Token(TokenKind.Spaces, input.getNewRange(startIndex, this._currentIndex)); |
| 67 | + } |
| 68 | + |
| 69 | + // Is it a newline? |
| 70 | + if (c === '\r') { |
| 71 | + if (this._peek() === '\n') { |
| 72 | + this._get(); |
| 73 | + } |
| 74 | + return new Token(TokenKind.NewLine, input.getNewRange(startIndex, this._currentIndex)); |
| 75 | + } else if (c === '\n') { |
| 76 | + return new Token(TokenKind.NewLine, input.getNewRange(startIndex, this._currentIndex)); |
| 77 | + } |
| 78 | + |
| 79 | + // Otherwise treat it as an "other" character |
| 80 | + return new Token(TokenKind.Other, input.getNewRange(startIndex, this._currentIndex)); |
| 81 | + } |
| 82 | + |
| 83 | + public getTokens(): Token[] { |
| 84 | + const tokens: Token[] = []; |
| 85 | + let token: Token = this.getToken(); |
| 86 | + while (token.kind !== TokenKind.EndOfInput) { |
| 87 | + tokens.push(token); |
| 88 | + token = this.getToken(); |
| 89 | + } |
| 90 | + return tokens; |
| 91 | + } |
| 92 | + |
| 93 | + private _get(): string | undefined { |
| 94 | + if (this._currentIndex >= this.input.end) { |
| 95 | + return undefined; |
| 96 | + } |
| 97 | + return this.input.buffer[this._currentIndex++]; |
| 98 | + } |
| 99 | + |
| 100 | + private _peek(): string | undefined { |
| 101 | + if (this._currentIndex >= this.input.end) { |
| 102 | + return undefined; |
| 103 | + } |
| 104 | + return this.input.buffer[this._currentIndex]; |
| 105 | + } |
| 106 | + |
| 107 | + private static _isSpace(c: string | undefined): boolean { |
| 108 | + return c === ' ' || c === '\t'; |
| 109 | + } |
| 110 | +} |
0 commit comments