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

96.32% Statements 366/380
92.34% Branches 205/222
100% Functions 16/16
96.32% Lines 366/380

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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5617x 5617x 1x 1x 459050x 459050x 1x 1x 592394x 592394x 1x 1x 133344x 133344x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5617x 5617x 5617x 5617x 1x 1x 459050x 17145x 17145x 17145x 459050x 459050x 5617x 5617x 453433x 453433x 453433x 453433x 453433x 453433x 1x 1x 86180x 86180x 1x 1x 86180x 86180x 1x 1x 545230x 545230x 1x 1x 6x 6x 1x 1x 918100x 11234x 11234x 906866x 906866x 1x 1x 545230x 545230x 1x 1x 459050x 459050x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5617x 5617x 5617x 5617x 5617x 5617x 5617x 1x 1x 133344x 133344x 133344x 86180x 86180x 86180x 86180x 86180x 86180x 10731x 10731x 86180x 86180x 86180x 86180x 76753x 76753x 86180x 86180x 86180x 158x 158x 86180x 86180x 86180x 150x 86180x 1528x 86030x 225x 225x 225x 87x 225x 67x 138x 66x 71x 5x 5x     84502x 40x 84277x 30150x 13673x 30150x 97x   97x   97x 60x 97x 37x 37x 16477x 2945x 695x 2945x 259x 2250x 1196x 1991x 795x 795x 16380x 97x 36x 97x 24x 61x 37x 37x     13435x 2946x 1825x 2946x 57x 1121x 1052x 1064x 12x 12x 13338x 1898x 25x 1898x 8x 1873x 8x 1865x 1857x 1857x 10392x 98x 53x 98x 10x 45x 1x 35x 34x 34x 8494x 219x   219x 214x 219x   5x 5x 5x 219x 84237x 4898x 275x   275x   275x   275x 275x 275x 4898x 252x   252x   252x 2x 252x 250x 250x 252x 4898x 86180x 86180x 61737x 61737x 86180x 86180x 133344x 133344x 1x 1x 5617x 5617x 5617x 5617x 459050x 459050x 459050x 459050x 459050x 459050x 459050x 459050x 1399x 1399x 1399x 459050x 457651x 225x 225x 225x 457651x 129x 129x 129x 457426x 40x 40x 40x 457297x 457257x 148x 148x 148x 457257x 37x 37x 37x 457109x 457072x 457072x 457072x 457072x 457072x 129x 129x 129x 457072x 456943x 456943x 456943x 225x 225x 225x 456943x 456718x 456718x 456718x 456718x 456718x 1392x 1392x 1392x 2x 1392x 1390x 1390x 456718x 455326x 443655x 443655x 443655x 443655x 443655x 443655x 443655x 443655x 443655x 443655x 443655x 443655x 443655x 455326x 115052x 455326x 109x 109x 340274x   340165x 340165x 340165x 340165x 510x 510x 340165x 339655x 328093x 328093x 328093x 328093x 328093x 328093x 328093x 328093x 339655x 8332x 8332x 459050x 459050x 5617x 5617x 459050x 5617x 5617x 5617x 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);
  }
}
 
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.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 if (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 === "[") {
          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.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 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();
  }
 
}