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

96.2% Statements 406/422
94.17% Branches 210/223
100% Functions 20/20
96.2% Lines 406/422

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 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 4221x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7586x 7586x 1x 1x 778254x 778254x 778254x 1x 1x 229115x 229115x 1x 1x 229115x 229115x 1x 1x 2321x 2321x 14600x 4709x 4709x 14600x 2321x 2321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7586x 7586x 7586x 7586x 1x 1x 778254x 29730x 29730x 29730x 778254x 778254x 7586x 7586x 770668x 770668x 770668x 770668x 770668x 770668x 1x 1x 147138x 147138x 1x 1x 147138x 147138x 1x 1x 1817x     1817x 1817x 1x 1x 11x     11x 11x 1x 1x 1556508x 15172x 1556508x 15172x 15172x 1526164x 1526164x 1x 1x 948116x 37652x 37652x 910464x 910464x 1x 1x 778254x 22724x 22724x 755530x 755530x 1x 1x 141812x 141812x 1x 1x 288950x 288950x 1x 1x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 229115x 229115x 229115x 147138x 147138x 147138x 147138x 147138x 141812x 141812x 100946x 100946x 141812x 147138x 147138x 147138x 147138x 131047x 131047x 147138x 147138x 147138x 204x 204x 147138x 147138x 147138x 237x 147138x 2307x 146901x 579x 579x 579x 259x 579x 156x 320x 154x 164x 6x 10x 4x 4x 144594x 64x 144015x 51449x 23580x 51449x 173x   173x   173x 132x 173x 41x 41x 27869x 4789x 884x 4789x 329x 3905x 2027x 3576x 1549x 1549x 27696x 173x 97x 173x 35x 76x 41x 41x     22907x 4791x 2832x 4791x 65x 1959x 1879x 1894x 15x 15x 22734x 3202x 40x 3202x 16x 3162x 9x 3146x 3137x 3137x 17943x 214x 148x 214x 18x 66x 1x 48x 47x 47x 14741x 385x   385x 374x 385x   11x 11x 11x 385x 143951x 8968x 408x   408x 1x 408x   407x 407x 407x 8968x 408x 2x 408x   406x   406x 406x 406x 408x 8968x 147138x 147138x 105828x 105828x 147138x 147138x 229115x 229115x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 7586x 778254x 778254x 778254x 778254x 778254x 778254x 755691x 201182x 755691x 2154x 2154x 2154x 554509x 579x 579x 579x 552355x 153x 153x 153x 551776x 64x 64x 64x 551623x 551559x 232x 232x 232x 551559x 376x 551327x 550951x 408x 550951x 550543x 550543x 796x 796x 550543x 549747x 549747x 12466x 12466x 778254x 60x 60x 60x 22563x 22503x 22503x 22503x 22503x 22503x 153x 153x 153x 1x 153x 152x 152x 22503x 22350x 22350x 22350x 579x 579x 579x 22350x 21771x 21771x 21771x 21771x 21771x 2147x 2147x 2147x 4x 2147x 2143x 2143x 21771x 180x 180x 19624x     778254x 778254x 7586x 7586x 778254x 7586x 7586x 7586x 7586x 7586x
import {IFile} from "../../files/_ifile";
import {Position, VirtualPosition} from "../../position";
import {Token} from "./tokens/_token";
import {IABAPLexerResult} from "./lexer_result";
import {At, AtW, BracketLeft, BracketLeftW, BracketRight, BracketRightW, Comment, Dash, DashW, Identifier, InstanceArrow, InstanceArrowW, ParenLeft, ParenLeftW, ParenRight, ParenRightW, Plus, PlusW, Pragma, Punctuation, StaticArrow, StaticArrowW, StringTemplate, StringTemplateBegin, StringTemplateEnd, StringTemplateMiddle, StringToken, WAt, WAtW, WBracketLeft, WBracketLeftW, WBracketRight, WBracketRightW, WDash, WDashW, WInstanceArrow, WInstanceArrowW, WParenLeft, WParenLeftW, WParenRight, WParenRightW, WPlus, WPlusW, WStaticArrow, WStaticArrowW} from "./tokens";
 
class Buffer {
  private buf: string;
 
  public constructor() {
    this.buf = "";
  }
 
  public add(s: string): string {
    this.buf = this.buf + s;
    return this.buf;
  }
 
  public get(): string {
    return this.buf;
  }
 
  public clear(): void {
    this.buf = "";
  }
 
  public countIsEven(char: string): boolean {
    let count = 0;
    for (let i = 0; i < this.buf.length; i += 1) {
      if (this.buf.charAt(i) === char) {
        count += 1;
      }
    }
    return count % 2 === 0;
  }
}
 
class Stream {
  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) {
      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;
  }
}
 
export class Lexer {
  private readonly ModeNormal: number = 1;
  private readonly ModePing: number = 2;
  private readonly ModeStr: number = 3;
  private readonly ModeTemplate: number = 4;
  private readonly ModeComment: number = 5;
  private readonly ModePragma: number = 6;
 
  private virtual: Position | undefined;
  private tokens: Token[];
  private m: number;
  private stream: Stream;
  private buffer: Buffer;
 
  public run(file: IFile, virtual?: Position): IABAPLexerResult {
    this.virtual = virtual;
    this.tokens = [];
    this.m = this.ModeNormal;
 
    this.process(file.getRaw());
    return {file, tokens: this.tokens};
  }
 
  private add() {
    const s = this.buffer.get().trim();
 
    if (s.length > 0) {
      const col = this.stream.getCol();
      const row = this.stream.getRow();
 
      let whiteBefore = false;
      if (this.stream.getOffset() - s.length >= 0) {
        const prev = this.stream.getRaw().substr(this.stream.getOffset() - s.length, 1);
        if (prev === " " || prev === "\n" || prev === "\t" || prev === ":") {
          whiteBefore = true;
        }
      }
 
      let whiteAfter = false;
      const next = this.stream.nextChar();
      if (next === " " || next === "\n" || next === "\t" || next === ":" || next === "," || next === "." || next === "" || next === "\"") {
        whiteAfter = true;
      }
 
      let pos = new Position(row, col - s.length);
      if (this.virtual) {
        pos = new VirtualPosition(this.virtual, pos.getRow(), pos.getCol());
      }
 
      let tok: Token | undefined = undefined;
      if (this.m === this.ModeComment) {
        tok = new Comment(pos, s);
      } else if (this.m === this.ModePing || this.m === this.ModeStr) {
        tok = new StringToken(pos, s);
      } else if (this.m === this.ModeTemplate) {
        const first = s.charAt(0);
        const last = s.charAt(s.length - 1);
        if (first === "|" && last === "|") {
          tok = new StringTemplate(pos, s);
        } else if (first === "|" && last === "{" && whiteAfter === true) {
          tok = new StringTemplateBegin(pos, s);
        } else if (first === "}" && last === "|" && whiteBefore === true) {
          tok = new StringTemplateEnd(pos, s);
        } else if (first === "}" && last === "{" && whiteAfter === true && whiteBefore === true) {
          tok = new StringTemplateMiddle(pos, s);
        } else {
          tok = new Identifier(pos, s);
        }
      } else if (s.length > 2 && s.substr(0, 2) === "##") {
        tok = new Pragma(pos, s);
      } else if (s.length === 1) {
        if (s === "." || s === ",") {
          tok = new Punctuation(pos, s);
        } else if (s === "[") {
          if (whiteBefore === true && whiteAfter === true) {
            tok = new WBracketLeftW(pos, s);
          } else if (whiteBefore === true) {
            tok = new WBracketLeft(pos, s);
          } else if (whiteAfter === true) {
            tok = new BracketLeftW(pos, s);
          } else {
            tok = new BracketLeft(pos, s);
          }
        } else if (s === "(") {
          if (whiteBefore === true && whiteAfter === true) {
            tok = new WParenLeftW(pos, s);
          } else if (whiteBefore === true) {
            tok = new WParenLeft(pos, s);
          } else if (whiteAfter === true) {
            tok = new ParenLeftW(pos, s);
          } else {
            tok = new ParenLeft(pos, s);
          }
        } else if (s === "]") {
          if (whiteBefore === true && whiteAfter === true) {
            tok = new WBracketRightW(pos, s);
          } else if (whiteBefore === true) {
            tok = new WBracketRight(pos, s);
          } else if (whiteAfter === true) {
            tok = new BracketRightW(pos, s);
          } else {
            tok = new BracketRight(pos, s);
          }
        } else if (s === ")") {
          if (whiteBefore === true && whiteAfter === true) {
            tok = new WParenRightW(pos, s);
          } else if (whiteBefore === true) {
            tok = new WParenRight(pos, s);
          } else if (whiteAfter === true) {
            tok = new ParenRightW(pos, s);
          } else {
            tok = new ParenRight(pos, s);
          }
        } else if (s === "-") {
          if (whiteBefore === true && whiteAfter === true) {
            tok = new WDashW(pos, s);
          } else if (whiteBefore === true) {
            tok = new WDash(pos, s);
          } else if (whiteAfter === true) {
            tok = new DashW(pos, s);
          } else {
            tok = new Dash(pos, s);
          }
        } else if (s === "+") {
          if (whiteBefore === true && whiteAfter === true) {
            tok = new WPlusW(pos, s);
          } else if (whiteBefore === true) {
            tok = new WPlus(pos, s);
          } else if (whiteAfter === true) {
            tok = new PlusW(pos, s);
          } else {
            tok = new Plus(pos, s);
          }
        } else if (s === "@") {
          if (whiteBefore === true && whiteAfter === true) {
            tok = new WAtW(pos, s);
          } else if (whiteBefore === true) {
            tok = new WAt(pos, s);
          } else if (whiteAfter === true) {
            tok = new AtW(pos, s);
          } else {
            tok = new At(pos, s);
          }
        }
      } else if (s.length === 2) {
        if (s === "->") {
          if (whiteBefore === true && whiteAfter === true) {
            tok = new WInstanceArrowW(pos, s);
          } else if (whiteBefore === true) {
            tok = new WInstanceArrow(pos, s);
          } else if (whiteAfter === true) {
            tok = new InstanceArrowW(pos, s);
          } else {
            tok = new InstanceArrow(pos, s);
          }
        } else if (s === "=>") {
          if (whiteBefore === true && whiteAfter === true) {
            tok = new WStaticArrowW(pos, s);
          } else if (whiteBefore === true) {
            tok = new WStaticArrow(pos, s);
          } else if (whiteAfter === true) {
            tok = new StaticArrowW(pos, s);
          } else {
            tok = new StaticArrow(pos, s);
          }
        }
      }
 
      if (tok === undefined) {
        tok = new Identifier(pos, s);
      }
      this.tokens.push(tok);
    }
    this.buffer.clear();
  }
 
  private process(raw: string) {
    this.stream = new Stream(raw.replace(/\r/g, ""));
    this.buffer = new Buffer();
 
    const splits: {[name: string]: boolean} = {};
    splits[" "] = true;
    splits[":"] = true;
    splits["."] = true;
    splits[","] = true;
    splits["-"] = true;
    splits["+"] = true;
    splits["("] = true;
    splits[")"] = true;
    splits["["] = true;
    splits["]"] = true;
    splits["\t"] = true;
    splits["\n"] = true;
 
    const bufs: {[name: string]: boolean} = {};
    bufs["."] = true;
    bufs[","] = true;
    bufs[":"] = true;
    bufs["("] = true;
    bufs[")"] = true;
    bufs["["] = true;
    bufs["]"] = true;
    bufs["+"] = true;
    bufs["@"] = true;
 
    for (;;) {
      const current = this.stream.currentChar();
      const buf = this.buffer.add(current);
      const ahead = this.stream.nextChar();
      const aahead = this.stream.nextNextChar();
 
      if (this.m === this.ModeNormal) {
        if (splits[ahead]) {
          this.add();
        } else if (ahead === "'") {
// start string
          this.add();
          this.m = this.ModeStr;
        } else if (ahead === "|" || ahead === "}") {
// start template
          this.add();
          this.m = this.ModeTemplate;
        } else if (ahead === "`") {
// start ping
          this.add();
          this.m = this.ModePing;
        } else if (aahead === "##") {
// start pragma
          this.add();
          this.m = this.ModePragma;
        } else if (ahead === "\""
            || (ahead === "*" && current === "\n")) {
// start comment
          this.add();
          this.m = this.ModeComment;
        } else if (ahead === "@" && buf.trim().length === 0) {
          this.add();
        } else if (aahead === "->"
            || aahead === "=>") {
          this.add();
        } else if (current === ">"
            && ahead !== " "
            && (this.stream.prevChar() === "-" || this.stream.prevChar() === "=")) {
// arrows
          this.add();
        } else if (buf.length === 1
            && (bufs[buf]
            || (buf === "-" && ahead !== ">"))) {
          this.add();
        }
      } else if (this.m === this.ModePragma && (ahead === "," || ahead === ":" || ahead === "." || ahead === " " || ahead === "\n")) {
// end of pragma
        this.add();
        this.m = this.ModeNormal;
      } else if (this.m === this.ModePing
          && buf.length > 1
          && current === "`"
          && aahead !== "``"
          && ahead !== "`"
          && this.buffer.countIsEven("`")) {
// end of ping
        this.add();
        if (ahead === `"`) {
          this.m = this.ModeComment;
        } else {
          this.m = this.ModeNormal;
        }
      } else if (this.m === this.ModeTemplate
          && buf.length > 1
          && (current === "|" || current === "{")
          && (this.stream.prevChar() !== "\\" || this.stream.prevPrevChar() === "\\\\")) {
// end of template
        this.add();
        this.m = this.ModeNormal;
      } else if (this.m === this.ModeStr
          && current === "'"
          && buf.length > 1
          && aahead !== "''"
          && ahead !== "'"
          && this.buffer.countIsEven("'")) {
// end of string
        this.add();
        if (ahead === "\"") {
          this.m = this.ModeComment;
        } else {
          this.m = this.ModeNormal;
        }
      } else if (ahead === "\n" && this.m !== this.ModeTemplate) {
        this.add();
        this.m = this.ModeNormal;
      } else if (this.m === this.ModeTemplate && current === "\n") {
        this.add();
      }
 
      if (!this.stream.advance()) {
        break;
      }
    }
 
    this.add();
  }
 
}