All files / src/abap/1_lexer lexer_stream.ts

95.12% Statements 78/82
91.66% Branches 22/24
100% Functions 11/11
95.12% Lines 78/82

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 831x 1x 1x 1x 1x 1x 1x 8178x 8178x 8178x 8178x 1x 1x 857894x 32974x 32974x 32974x 857894x 857894x 8178x 8178x 8178x 849716x 849716x 849716x 849716x 849716x 849716x 1x 1x 161410x 161410x 1x 1x 161410x 161410x 1x 1x 1952x     1952x 1952x 1x 1x 12x     12x 12x 1x 1x 1715788x 16356x 1715788x 16356x 16356x 1683076x 1683076x 1x 1x 1043804x 40601x 40601x 1003203x 1003203x 1x 1x 857894x 24500x 24500x 833394x 833394x 1x 1x 155732x 155732x 1x 1x 317142x 317142x 1x  
export class LexerStream {
  private readonly raw: string;
  private offset = -1;
  private row: number;
  private col: number;
 
  public constructor(raw: string) {
    this.raw = raw;
    this.row = 0;
    this.col = 0;
  }
 
  public advance(): boolean {
    if (this.currentChar() === "\n") {
      this.col = 1;
      this.row = this.row + 1;
    }
 
    if (this.offset === this.raw.length) {
      this.col = this.col - 1;
      return false;
    }
 
    this.col = this.col + 1;
 
    this.offset = this.offset + 1;
    return true;
  }
 
  public getCol(): number {
    return this.col;
  }
 
  public getRow(): number {
    return this.row;
  }
 
  public prevChar(): string {
    if (this.offset - 1 < 0) {
      return "";
    }
    return this.raw.substr(this.offset - 1, 1);
  }
 
  public prevPrevChar(): string {
    if (this.offset - 2 < 0) {
      return "";
    }
    return this.raw.substr(this.offset - 2, 2);
  }
 
  public currentChar(): string {
    if (this.offset < 0) {
      return "\n"; // simulate newline at start of file to handle star(*) comments
    } else if (this.offset >= this.raw.length) {
      return "";
    }
    return this.raw.substr(this.offset, 1);
  }
 
  public nextChar(): string {
    if (this.offset + 2 > this.raw.length) {
      return "";
    }
    return this.raw.substr(this.offset + 1, 1);
  }
 
  public nextNextChar(): string {
    if (this.offset + 3 > this.raw.length) {
      return this.nextChar();
    }
    return this.raw.substr(this.offset + 1, 2);
  }
 
  public getRaw(): string {
    return this.raw;
  }
 
  public getOffset() {
    return this.offset;
  }
}