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

94.17% Statements 194/206
93.58% Branches 248/265
100% Functions 17/17
94.17% Lines 194/206

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 3801x   1x       1x 1x 1x 1x 1x 1x 1x             5342x       421638x       543457x       121819x           5342x         5342x 5342x 5342x       421638x 15542x 15542x     421638x 5342x     416296x   416296x 416296x       79291x       79291x       500929x       6x       843276x 10684x   832592x       500929x       421638x       1x               5342x 5342x 5342x   5342x 5342x       121819x   121819x 79291x 79291x   79291x 79291x 79291x 10230x     79291x 79291x 79291x 70426x     79291x 79291x 158x     79291x 79291x 149x 79142x 1419x 77723x 221x 221x 221x 83x 138x 67x 71x 66x 5x 5x       77502x 36x 77466x 27930x 12338x 15592x 90x   90x   90x 58x   32x   15502x 2794x 678x 2116x 252x 1864x 1143x   721x   12708x 90x 36x 54x 22x 32x 32x       12618x 2795x 1757x 1038x 55x 983x 971x   12x   9823x 1813x 25x 1788x 8x 1780x 8x   1772x   8010x 91x 50x 41x 10x 31x 1x   30x   7919x 197x   197x 192x 5x     5x     49536x 4512x 263x   263x   263x     263x   4249x 233x   233x   233x 2x   231x         79291x 56762x   79291x   121819x       5342x 5342x   5342x 421638x 421638x 421638x 421638x 421638x 421638x   421638x   1332x 1332x 420306x     221x 221x 420085x   87x 87x 419998x   36x 36x 419962x     147x 147x 419815x   34x 34x 419781x             87x 87x 419694x         221x 221x 419473x             1325x 1325x 2x   1323x   418148x                               104519x 313629x 108x 108x 313521x   313521x         480x 313041x                     7880x     421638x 5342x       5342x      
import * as Tokens from "./tokens";
import {IFile} from "../../files/_ifile";
import {Position, VirtualPosition} from "../../position";
import {Token} from "./tokens/_token";
import {ILexerResult} 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);
  }
}
 
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): ILexerResult {
    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.prevChar();
      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 === "{") {
          tok = new Tokens.StringTemplateBegin(pos, s);
        } else if (first === "}" && last === "|") {
          tok = new Tokens.StringTemplateEnd(pos, s);
        } else Eif (first === "}" && last === "{") {
          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 === "[") {
          Iif (whiteBefore && whiteAfter) {
            tok = new Tokens.WBracketLeftW(pos, s);
          } else Iif (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 Eif (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 === "@") {
          Iif (whiteBefore && whiteAfter) {
            tok = new Tokens.WAtW(pos, s);
          } else if (whiteBefore) {
            tok = new Tokens.WAt(pos, s);
          } else Iif (whiteAfter) {
            tok = new Tokens.AtW(pos, s);
          } else {
            tok = new Tokens.At(pos, s);
          }
        }
      } else if (s.length === 2) {
        if (s === "->") {
          Iif (whiteBefore && whiteAfter) {
            tok = new Tokens.WInstanceArrowW(pos, s);
          } else Iif (whiteBefore) {
            tok = new Tokens.WInstanceArrow(pos, s);
          } else Iif (whiteAfter) {
            tok = new Tokens.InstanceArrowW(pos, s);
          } else {
            tok = new Tokens.InstanceArrow(pos, s);
          }
        } else if (s === "=>") {
          Iif (whiteBefore && whiteAfter) {
            tok = new Tokens.WStaticArrowW(pos, s);
          } else Iif (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.match(/`/g) || []).length % 2 === 0
          && ahead !== "`") {
// end of ping
        this.add();
        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.match(/'/g) || []).length % 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 Iif (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();
  }
 
}