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

97.19% Statements 381/392
95.25% Branches 221/232
100% Functions 18/18
97.19% Lines 381/392

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 3921x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6858x 6858x 1x 1x 656971x 656971x 1x 1x 848940x 848940x 1x 1x 191969x 191969x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6858x 6858x 6858x 6858x 1x 1x 656971x 24726x 24726x 24726x 656971x 656971x 6858x 6858x 650113x 650113x 650113x 650113x 650113x 650113x 1x 1x 123607x 123607x 1x 1x 123607x 123607x 1x 1x 656971x 656971x 1x 1x 11x 11x 1x 1x 1313942x 13716x 13716x 1300226x 1300226x 1x 1x 780578x 780578x 1x 1x 656971x 656971x 1x 1x 123607x 123607x 1x 1x 123607x 123607x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6858x 6858x 6858x 6858x 6858x 6858x 6858x 1x 1x 191969x 191969x 191969x 123607x 123607x 123607x 123607x 123607x 123607x 84690x 84690x 123607x 123607x 123607x 123607x 110066x 110066x 123607x 123607x 123607x 158x 158x 123607x 123607x 123607x 190x 123607x 1986x 123417x 474x 474x 474x 204x 474x 131x 270x 129x 139x 6x 10x 4x 4x 121431x 56x 120957x 43151x 19489x 43151x 158x   158x   158x 119x 158x 39x 39x 23662x 4070x 840x 4070x 295x 3230x 1684x 2935x 1251x 1251x 23504x 158x 84x 158x 35x 74x 39x 39x     19434x 4072x 2448x 4072x 64x 1624x 1547x 1560x 13x 13x 19276x 2769x 37x 2769x 15x 2732x 9x 2717x 2708x 2708x 15204x 177x 120x 177x 18x 57x 1x 39x 38x 38x 12435x 306x   306x 297x 306x   9x 9x 9x 306x 120901x 7512x 348x   348x 1x 348x   347x 347x 347x 7512x 332x 2x 332x   330x   330x 330x 330x 332x 7512x 123607x 123607x 89022x 89022x 123607x 123607x 191969x 191969x 1x 1x 6858x 6858x 6858x 6858x 656971x 656971x 656971x 656971x 656971x 656971x 656971x 656971x 1838x 1838x 1838x 656971x 655133x 474x 474x 474x 655133x 148x 148x 148x 654659x 56x 56x 56x 654511x 654455x 185x 185x 185x 654455x 52x 52x 52x 654270x 654218x 654218x 654218x 654218x 654218x 148x 148x 148x 1x 148x 147x 147x 654218x 654070x 654070x 654070x 474x 474x 474x 654070x 653596x 653596x 653596x 653596x 653596x 1831x 1831x 1831x 4x 1831x 1827x 1827x 653596x 651765x 636254x 636254x 636254x 636254x 636254x 636254x 636254x 636254x 636254x 636254x 636254x 636254x 636254x 651765x 168180x 651765x 137x 137x 483585x   483448x 483448x 483448x 483448x 662x 662x 483448x 482786x 467412x 467412x 467412x 467412x 467412x 467412x 467412x 467412x 482786x 10926x 10926x 656971x 656971x 6858x 6858x 656971x 6858x 6858x 6858x 1x 1x
import * as Tokens from "./tokens";
import {IFile} from "../../files/_ifile";
import {Position, VirtualPosition} from "../../position";
import {Token} from "./tokens/_token";
import {IABAPLexerResult} from "./lexer_result";
 
enum Mode {
  Normal,
  Ping,
  Str,
  Template,
  Comment,
  Pragma,
}
 
class Buffer {
  private buf: string;
 
  public constructor() {
    this.buf = "";
  }
 
  public add(s: string): void {
    this.buf = this.buf + s;
  }
 
  public get(): string {
    return this.buf;
  }
 
  public clear(): void {
    this.buf = "";
  }
}
 
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 {
    return this.raw.substr(this.offset - 1, 1);
  }
 
  public prevPrevChar(): string {
    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
    }
    return this.raw.substr(this.offset, 1);
  }
 
  public nextChar(): string {
    return this.raw.substr(this.offset + 1, 1);
  }
 
  public nextNextChar(): string {
    return this.raw.substr(this.offset + 1, 2);
  }
 
  public getRaw(): string {
    return this.raw;
  }
 
  public getOffset() {
    return this.offset;
  }
}
 
export class Lexer {
  private static virtual: Position | undefined;
  private static tokens: Token[];
  private static m: Mode;
  private static stream: Stream;
  private static buffer: Buffer;
 
  public static run(file: IFile, virtual?: Position): IABAPLexerResult {
    this.virtual = virtual;
    this.tokens = [];
    this.m = Mode.Normal;
 
    this.process(file.getRaw());
    return {file, tokens: this.tokens};
  }
 
  private static add() {
    const s = this.buffer.get().trim();
 
    if (s.length > 0) {
      const col = this.stream.getCol();
      const row = this.stream.getRow();
 
      let whiteBefore = false;
      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 === Mode.Comment) {
        tok = new Tokens.Comment(pos, s);
      } else if (this.m === Mode.Ping || this.m === Mode.Str) {
        tok = new Tokens.String(pos, s);
      } else if (this.m === Mode.Template) {
        const first = s.charAt(0);
        const last = s.charAt(s.length - 1);
        if (first === "|" && last === "|") {
          tok = new Tokens.StringTemplate(pos, s);
        } else if (first === "|" && last === "{" && whiteAfter === true) {
          tok = new Tokens.StringTemplateBegin(pos, s);
        } else if (first === "}" && last === "|" && whiteBefore === true) {
          tok = new Tokens.StringTemplateEnd(pos, s);
        } else if (first === "}" && last === "{" && whiteAfter === true && whiteBefore === true) {
          tok = new Tokens.StringTemplateMiddle(pos, s);
        } else {
          tok = new Tokens.Identifier(pos, s);
        }
      } else if (s.substr(0, 2) === "##") {
        tok = new Tokens.Pragma(pos, s);
      } else if (s.length === 1) {
        if (s === "." || s === ",") {
          tok = new Tokens.Punctuation(pos, s);
        } else if (s === "[") {
          if (whiteBefore && whiteAfter) {
            tok = new Tokens.WBracketLeftW(pos, s);
          } else if (whiteBefore) {
            tok = new Tokens.WBracketLeft(pos, s);
          } else if (whiteAfter) {
            tok = new Tokens.BracketLeftW(pos, s);
          } else {
            tok = new Tokens.BracketLeft(pos, s);
          }
        } else if (s === "(") {
          if (whiteBefore && whiteAfter) {
            tok = new Tokens.WParenLeftW(pos, s);
          } else if (whiteBefore) {
            tok = new Tokens.WParenLeft(pos, s);
          } else if (whiteAfter) {
            tok = new Tokens.ParenLeftW(pos, s);
          } else {
            tok = new Tokens.ParenLeft(pos, s);
          }
        } else if (s === "]") {
          if (whiteBefore && whiteAfter) {
            tok = new Tokens.WBracketRightW(pos, s);
          } else if (whiteBefore) {
            tok = new Tokens.WBracketRight(pos, s);
          } else if (whiteAfter) {
            tok = new Tokens.BracketRightW(pos, s);
          } else {
            tok = new Tokens.BracketRight(pos, s);
          }
        } else if (s === ")") {
          if (whiteBefore && whiteAfter) {
            tok = new Tokens.WParenRightW(pos, s);
          } else if (whiteBefore) {
            tok = new Tokens.WParenRight(pos, s);
          } else if (whiteAfter) {
            tok = new Tokens.ParenRightW(pos, s);
          } else {
            tok = new Tokens.ParenRight(pos, s);
          }
        } else if (s === "-") {
          if (whiteBefore && whiteAfter) {
            tok = new Tokens.WDashW(pos, s);
          } else if (whiteBefore) {
            tok = new Tokens.WDash(pos, s);
          } else if (whiteAfter) {
            tok = new Tokens.DashW(pos, s);
          } else {
            tok = new Tokens.Dash(pos, s);
          }
        } else if (s === "+") {
          if (whiteBefore && whiteAfter) {
            tok = new Tokens.WPlusW(pos, s);
          } else if (whiteBefore) {
            tok = new Tokens.WPlus(pos, s);
          } else if (whiteAfter) {
            tok = new Tokens.PlusW(pos, s);
          } else {
            tok = new Tokens.Plus(pos, s);
          }
        } else if (s === "@") {
          if (whiteBefore && whiteAfter) {
            tok = new Tokens.WAtW(pos, s);
          } else if (whiteBefore) {
            tok = new Tokens.WAt(pos, s);
          } else if (whiteAfter) {
            tok = new Tokens.AtW(pos, s);
          } else {
            tok = new Tokens.At(pos, s);
          }
        }
      } else if (s.length === 2) {
        if (s === "->") {
          if (whiteBefore && whiteAfter) {
            tok = new Tokens.WInstanceArrowW(pos, s);
          } else if (whiteBefore) {
            tok = new Tokens.WInstanceArrow(pos, s);
          } else if (whiteAfter) {
            tok = new Tokens.InstanceArrowW(pos, s);
          } else {
            tok = new Tokens.InstanceArrow(pos, s);
          }
        } else if (s === "=>") {
          if (whiteBefore && whiteAfter) {
            tok = new Tokens.WStaticArrowW(pos, s);
          } else if (whiteBefore) {
            tok = new Tokens.WStaticArrow(pos, s);
          } else if (whiteAfter) {
            tok = new Tokens.StaticArrowW(pos, s);
          } else {
            tok = new Tokens.StaticArrow(pos, s);
          }
        }
      }
 
      if (tok === undefined) {
        tok = new Tokens.Identifier(pos, s);
      }
      this.tokens.push(tok);
    }
    this.buffer.clear();
  }
 
  private static process(raw: string) {
    this.stream = new Stream(raw.replace(/\r/g, ""));
    this.buffer = new Buffer();
 
    for (;;) {
      const current = this.stream.currentChar();
      this.buffer.add(current);
      const buf = this.buffer.get();
      const ahead = this.stream.nextChar();
      const aahead = this.stream.nextNextChar();
      const prev = this.stream.prevChar();
 
      if (ahead === "'" && this.m === Mode.Normal) {
// start string
        this.add();
        this.m = Mode.Str;
      } else if ((ahead === "|" || ahead === "}")
          && this.m === Mode.Normal) {
// start template
        this.add();
        this.m = Mode.Template;
      } else if (ahead === "`" && this.m === Mode.Normal) {
// start ping
        this.add();
        this.m = Mode.Ping;
      } else if (aahead === "##" && this.m === Mode.Normal) {
// start pragma
        this.add();
        this.m = Mode.Pragma;
      } else if ((ahead === "\"" || (ahead === "*" && current === "\n"))
          && this.m === Mode.Normal) {
// start comment
        this.add();
        this.m = Mode.Comment;
      } else if (this.m === Mode.Pragma && (ahead === "," || ahead === ":" || ahead === "." || ahead === " " || ahead === "\n")) {
// end of pragma
        this.add();
        this.m = Mode.Normal;
      } else if (this.m === Mode.Ping
          && buf.length > 1
          && current === "`"
          && aahead !== "``"
          && (buf.split("`").length - 1) % 2 === 0
          && ahead !== "`") {
// end of ping
        this.add();
        if (ahead === `"`) {
          this.m = Mode.Comment;
        } else {
          this.m = Mode.Normal;
        }
      } else if (this.m === Mode.Template
          && buf.length > 1
          && (current === "|" || current === "{")
          && (prev !== "\\" || this.stream.prevPrevChar() === "\\\\")) {
// end of template
        this.add();
        this.m = Mode.Normal;
      } else if (this.m === Mode.Str
          && current === "'"
          && buf.length > 1
          && aahead !== "''"
          && (buf.split("'").length - 1) % 2 === 0
          && ahead !== "'") {
// end of string
        this.add();
        if (ahead === "\"") {
          this.m = Mode.Comment;
        } else {
          this.m = Mode.Normal;
        }
      } else if (this.m === Mode.Normal
          && (ahead === " "
          || ahead === ":"
          || ahead === "."
          || ahead === ","
          || ahead === "-"
          || ahead === "+"
          || ahead === "("
          || ahead === ")"
          || ahead === "["
          || ahead === "]"
          || (ahead === "@" && buf.trim().length === 0)
          || aahead === "->"
          || aahead === "=>"
          || ahead === "\t"
          || ahead === "\n")) {
        this.add();
      } else if (ahead === "\n" && this.m !== Mode.Template) {
        this.add();
        this.m = Mode.Normal;
      } else if (this.m === Mode.Template && current === "\n") {
        this.add();
      } else if (current === ">"
          && (prev === "-" || prev === "=")
          && ahead !== " "
          && this.m === Mode.Normal) {
// arrows
        this.add();
      } else if (this.m === Mode.Normal
          && (buf === "."
          || buf === ","
          || buf === ":"
          || buf === "("
          || buf === ")"
          || buf === "["
          || buf === "]"
          || buf === "+"
          || buf === "@"
          || (buf === "-" && ahead !== ">"))) {
        this.add();
      }
 
      if (!this.stream.advance()) {
        break;
      }
    }
 
    this.add();
  }
 
}