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

97.41% Statements 339/348
96.26% Branches 206/214
100% Functions 3/3
97.41% Lines 339/348

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 3491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12032x 12032x 12032x 12032x 12032x 12032x 12032x 1x 1x 391123x 391123x 242614x 242614x 242614x 242614x 242614x 242614x 233736x 233736x 166914x 166914x 233736x 242614x 242614x 242614x 242614x 242614x 214196x 214196x 242614x 242614x 242614x 282x 282x 242614x 242614x 242614x 346x 242614x 3138x 242268x 695x 695x 695x 289x 695x 193x 406x 190x 213x 18x 23x 5x 5x 239130x 84x 238435x 82371x 36992x 82371x 341x 6x 341x   335x 280x 335x 55x 55x 45379x 7839x 1310x 7839x 555x 6529x 3153x 5974x 2821x 2821x 45038x 341x 209x 341x 75x 132x 52x 57x 5x 5x 37199x 7841x 4356x 7841x 91x 3485x 3351x 3394x 43x 43x 36858x 4864x 99x 4864x 32x 4765x 10x 4733x 4723x 4723x 29017x 546x 228x 546x 241x 318x 2x 77x 75x 75x 24153x 2748x   2748x 2734x 2748x   14x 14x 14x 2748x 238351x 15791x 653x   653x 1x 653x   652x 652x 652x 15791x 528x 2x 528x   526x   526x 526x 526x 528x 15791x 242614x 242614x 346x 346x 346x 346x 346x 219x 219x 346x 242614x 242614x 175439x 175439x 242614x 242614x 391123x 391123x 1x 1x 12032x 12032x 12032x 12032x 12032x 1292603x 1292603x 1292603x 1292603x 1292603x 1292603x 1261672x 343805x 1261672x 2912x 2912x 2912x 917867x 697x 697x 697x 914955x 229x 229x 229x 914258x 84x 84x 84x 914029x 913945x 340x 340x 340x 913945x 2736x 913605x 528x 910869x 910341x 910341x 1106x 1106x 910341x 909235x 909235x 22478x 22478x 1292603x 30931x 78x 78x 78x 30931x 30853x 30853x 30853x 30853x 30853x 229x 229x 226x 226x 229x 229x 229x 2x 229x 227x 227x 30853x 30624x 30624x 30624x 697x 697x 694x 694x 697x 30624x 29927x 29927x 1x 29927x 29926x 29926x 29926x 29926x 29926x 2905x 2905x 2905x 4x 2905x 2901x 2901x 29926x 272x 272x 27021x     1292603x 1292603x 12032x 12032x 1292603x 12032x 12032x 12032x 1x 1x  
import {IFile} from "../../files/_ifile";
import {Position} from "../../position";
import {VirtualPosition} from "../../virtual_position";
import {AbstractToken} from "./tokens/abstract_token";
import {IABAPLexerResult} from "./lexer_result";
import {At, AtW, AssociationName, 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";
import {LexerBuffer} from "./lexer_buffer";
import {LexerStream} from "./lexer_stream";
 
const ModeNormal: number = 1;
const ModePing: number = 2;
const ModeStr: number = 3;
const ModeTemplate: number = 4;
const ModeComment: number = 5;
const ModePragma: number = 6;
 
// character codes, used for integer comparisons in the hot loop
const EOF = -1;             // no character (start/end of file)
const CH_TAB = 9;           // "\t"
const CH_NL = 10;           // "\n"
const CH_SPACE = 32;        // " "
const CH_DQUOTE = 34;       // "\""
const CH_HASH = 35;         // "#"
const CH_QUOTE = 39;        // "'"
const CH_STAR = 42;         // "*"
const CH_DASH = 45;         // "-"
const CH_COMMA = 44;        // ","
const CH_DOT = 46;          // "."
const CH_COLON = 58;        // ":"
const CH_EQUALS = 61;       // "="
const CH_GT = 62;           // ">"
const CH_AT = 64;           // "@"
const CH_BACKSLASH = 92;    // "\\"
const CH_BACKTICK = 96;     // "`"
const CH_LBRACE = 123;      // "{"
const CH_PIPE = 124;        // "|"
const CH_RBRACE = 125;      // "}"
 
// characters that terminate the current token
const SPLITS = new Set<number>([
  CH_SPACE, CH_COLON, CH_DOT, CH_COMMA, CH_DASH, 43 /* + */, 40 /* ( */,
  41 /* ) */, 91 /* [ */, 93 /* ] */, CH_BACKSLASH, CH_TAB, CH_NL]);
 
// single characters that are emitted as their own token
const BUFS = new Set<number>([
  CH_DOT, CH_COMMA, CH_COLON, 40 /* ( */, 41 /* ) */, 91 /* [ */,
  93 /* ] */, 43 /* + */, CH_AT]);
 
// characters treated as separators after ping literals and string templates.
// Single-quoted text literals always end at their closing quote.
const AFTER_LITERAL = new Set<number>([
  EOF, CH_SPACE, CH_TAB, CH_NL, CH_DOT, CH_COMMA, CH_COLON, CH_DQUOTE,
  40 /* ( */, 41 /* ) */, 91 /* [ */, 93 /* ] */]);
 
export class Lexer {
 
  private virtual: Position | undefined;
  private tokens: AbstractToken[];
  private m: number;
  private stream: LexerStream;
  private buffer: LexerBuffer;
 
  public run(file: IFile, virtual?: Position): IABAPLexerResult {
    this.virtual = virtual;
    this.tokens = [];
    this.m = 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;
      const beforeOffset = this.stream.getOffset() - s.length;
      if (beforeOffset >= 0) {
        const prev = this.stream.charCodeAt(beforeOffset);
        if (prev === CH_SPACE || prev === CH_NL || prev === CH_TAB || prev === CH_COLON) {
          whiteBefore = true;
        }
      }
 
      let whiteAfter = false;
      const next = this.stream.nextChar();
      if (next === CH_SPACE || next === CH_NL || next === CH_TAB || next === CH_COLON
          || next === CH_COMMA || next === CH_DOT || next === EOF || next === CH_DQUOTE) {
        whiteAfter = true;
      }
 
      let pos = new Position(row, col - s.length);
      if (this.virtual) {
        pos = new VirtualPosition(this.virtual, pos.getRow(), pos.getCol());
      }
 
      let tok: AbstractToken | undefined = undefined;
      if (this.m === ModeComment) {
        tok = new Comment(pos, s);
      } else if (this.m === ModePing || this.m === ModeStr) {
        tok = new StringToken(pos, s);
      } else if (this.m === 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 && this.m === ModeNormal && s.charAt(0) === "\\") {
        const adj = this.stream.nextChar() === EOF ? 1 : 0;
        const prevOffset = this.stream.getOffset() - s.length - adj;
        const prevChar = this.stream.charCodeAt(prevOffset);
        const whiteBeforeBackslash = prevChar === CH_SPACE || prevChar === CH_NL || prevChar === CH_TAB || prevChar === CH_COLON;
        if (!whiteBeforeBackslash) {
          tok = new AssociationName(pos, s);
        }
      }
 
      if (tok === undefined) {
        tok = new Identifier(pos, s);
      }
      this.tokens.push(tok);
    }
    this.buffer.clear();
  }
 
  private process(raw: string) {
    const stream = new LexerStream(raw.replace(/\r/g, ""));
    this.stream = stream;
    this.buffer = new LexerBuffer(stream.getRaw());
 
    for (;;) {
      const current = stream.currentChar();
      this.buffer.add(stream.getOffset());
      const ahead = stream.nextChar();
      const aahead = stream.nextNextChar();
 
      if (this.m === ModeNormal) {
        if (SPLITS.has(ahead)) {
          this.add();
        } else if (ahead === CH_QUOTE) {
// start string
          this.add();
          this.m = ModeStr;
        } else if (ahead === CH_PIPE || ahead === CH_RBRACE) {
// start template
          this.add();
          this.m = ModeTemplate;
        } else if (ahead === CH_BACKTICK) {
// start ping
          this.add();
          this.m = ModePing;
        } else if (ahead === CH_HASH && aahead === CH_HASH) {
// start pragma
          this.add();
          this.m = ModePragma;
        } else if (ahead === CH_DQUOTE
            || (ahead === CH_STAR && current === CH_NL)) {
// start comment
          this.add();
          this.m = ModeComment;
        } else if (ahead === CH_AT && this.buffer.get().trim().length === 0) {
          this.add();
        } else if ((ahead === CH_DASH || ahead === CH_EQUALS) && aahead === CH_GT) {
          this.add();
        } else if (current === CH_GT
            && ahead !== CH_SPACE
            && (stream.prevChar() === CH_DASH || stream.prevChar() === CH_EQUALS)) {
// arrows
          this.add();
        } else if (this.buffer.length() === 1
            && (BUFS.has(current)
            || (current === CH_DASH && ahead !== CH_GT))) {
          this.add();
        }
      } else if (this.m === ModePragma && (ahead === CH_COMMA || ahead === CH_COLON
        || ahead === CH_DOT || ahead === CH_SPACE || ahead === CH_NL)) {
// end of pragma
        this.add();
        this.m = ModeNormal;
      } else if (this.m === ModePing
          && this.buffer.length() > 1
          && current === CH_BACKTICK
          && !(ahead === CH_BACKTICK && aahead === CH_BACKTICK)
          && ahead !== CH_BACKTICK
          && this.buffer.countIsEven(CH_BACKTICK)) {
// end of ping
        if (AFTER_LITERAL.has(ahead)) {
          this.add();
        }
// else: no separator after the literal, keep the buffer so the following
// characters are glued onto the token, the parser will report the error
        if (ahead === CH_DQUOTE) {
          this.m = ModeComment;
        } else {
          this.m = ModeNormal;
        }
      } else if (this.m === ModeTemplate
          && this.buffer.length() > 1
          && (current === CH_PIPE || current === CH_LBRACE)
          && (stream.prevChar() !== CH_BACKSLASH || (stream.prevPrevChar() === CH_BACKSLASH && stream.prevChar() === CH_BACKSLASH))) {
// end of template
        if (current === CH_LBRACE || AFTER_LITERAL.has(ahead)) {
          this.add();
        }
        this.m = ModeNormal;
      } else if (this.m === ModeTemplate
          && ahead === CH_RBRACE
          && current !== CH_BACKSLASH) {
        this.add();
      } else if (this.m === ModeStr
          && current === CH_QUOTE
          && this.buffer.length() > 1
          && !(ahead === CH_QUOTE && aahead === CH_QUOTE)
          && ahead !== CH_QUOTE
          && this.buffer.countIsEven(CH_QUOTE)) {
// end of string
        this.add();
        if (ahead === CH_DQUOTE) {
          this.m = ModeComment;
        } else {
          this.m = ModeNormal;
        }
      } else if (ahead === CH_NL && this.m !== ModeTemplate) {
        this.add();
        this.m = ModeNormal;
      } else if (this.m === ModeTemplate && current === CH_NL) {
        this.add();
      }
 
      if (!stream.advance()) {
        break;
      }
    }
 
    this.add();
  }
 
}