All files / src/cds cds_lexer.ts

99.03% Statements 205/207
97.53% Branches 79/81
100% Functions 8/8
99.03% Lines 205/207

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 2081x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 585x 585x 1x 1x 89674x 89674x 89674x 89674x 1x 1x 89656x 89656x 89656x 1x 1x 90241x 90241x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 585x 585x 585x 585x 31529x 16971x 16971x 24x 16971x 16947x 16947x 16971x 31529x 31529x 585x 585x 585x 585x 585x 1x 1x 1x 585x 585x 585x 585x 585x 585x 585x 585x 585x 585x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 2528x 2528x 4x 4x 4x 2528x 3x 3x 3x 2524x 1x 1x 1x 2521x 389x 389x 389x 2528x 2528x 87128x 87128x 89656x 126x 126x     126x 1x 1x 126x 16x 16x 16x 126x 126x 87002x 87002x 89656x 726x 23x 23x 726x 703x 703x 703x 89656x 18x 18x 18x 18x 86276x 6x 6x 6x 6x 6x 86275x 86275x 89656x 256x 3x 256x 9x 9x 256x 89656x 9x 9x 9x 9x 9x 9x 86010x 86010x 89656x 389x 389x 389x 389x 89656x 16x 16x 16x 16x 89656x 89656x 16689x 16689x 89656x 2709x 2709x 2709x 2709x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 89656x 5228x 5228x 5228x 89656x 224x 224x 224x 224x 89656x 60755x 60755x 89656x 89656x 585x 585x 585x 585x 1x  
import {Comment, Identifier} from "../abap/1_lexer/tokens";
import {AbstractToken} from "../abap/1_lexer/tokens/abstract_token";
import {IFile} from "../files/_ifile";
import {Position} from "../position";
 
// todo: Keywords must be all uppercase, all lowercase, or in lowercase with an
// uppercase initial letter. Other mixes of uppercase and lowercase are not allowed
 
class Stream {
  private buffer: string;
 
  public constructor(buffer: string) {
    this.buffer = buffer;
  }
 
  public takeNext(): string {
    const next = this.buffer.substring(0, 1);
    this.buffer = this.buffer.substring(1);
    return next;
  }
 
  public peekNext(): string {
    const next = this.buffer.substring(0, 1);
    return next;
  }
 
  public length(): number {
    return this.buffer.length;
  }
}
 
enum Mode {
  Default,
  String,
  DoubleQuoteString,
  SingleLineComment,
  MultiLineComment,
}
 
class Result {
  private readonly result: AbstractToken[] = [];
 
  public add(text: string, row: number, col: number, mode: Mode): string {
    if (text.length > 0) {
      if (mode === Mode.SingleLineComment
          && (text.startsWith("--") || text.startsWith("//"))) {
        this.result.push(new Comment(new Position(row, col - text.length), text));
      } else {
        this.result.push(new Identifier(new Position(row, col), text));
      }
    }
    return "";
  }
 
  public get() {
    return this.result;
  }
}
 
export class CDSLexer {
  public static run(file: IFile): AbstractToken[] {
    const result = new Result();
    let mode = Mode.Default;
    let row = 1;
    let col = 1;
    let build = "";
 
    const stream = new Stream(file.getRaw().replace(/\r/g, "").replace(/\u00a0/g, " ").replace(/\u000b/g, " "));
 
    let next = "";
    while (stream.length() > 0) {
      const prev = next;
      next = stream.takeNext();
      const nextNext = stream.peekNext();
      col++;
 
// string handling
      if (mode === Mode.String) {
        build += next;
        if (next === "'" && nextNext === "'") {
          // escaped single quote (doubled), continue string
          build += stream.takeNext();
          col++;
        } else if (next === "\\" && nextNext === "\\") {
          // escaped backslash (e.g. '\\' in ltrim/rtrim calls), consume both chars
          build += stream.takeNext();
          col++;
        } else if (next === "\\" && nextNext === "'") {
          // backslash-escaped single quote, continue string
          build += stream.takeNext();
          col++;
        } else if (next === "'") {
          build = result.add(build, row, col, mode);
          mode = Mode.Default;
        }
        continue;
      }
 
// double-quote string handling
      if (mode === Mode.DoubleQuoteString) {
        build += next;
        if (next === "\\" && nextNext === "\"") {
          build += stream.takeNext();
          col++;
        } else if (next === "\"" && nextNext === "\"") {
          build += stream.takeNext();
          col++;
        } else if (next === "\"") {
          build = result.add(build, row, col, mode);
          mode = Mode.Default;
        }
        continue;
      }
 
// single line comment handling
      if (mode === Mode.SingleLineComment) {
        if (next === "\n") {
          build = result.add(build, row, col, mode);
          mode = Mode.Default;
        } else {
          build += next;
          continue;
        }
      } else if (mode === Mode.Default && next === "/" && nextNext === "/") {
        mode = Mode.SingleLineComment;
        build = result.add(build, row, col, mode);
        build += next;
        continue;
      } else if (mode === Mode.Default && next === "-" && nextNext === "-") {
        mode = Mode.SingleLineComment;
        build = result.add(build, row, col, mode);
        build += next;
        continue;
      }
 
// multi line comment handling
      if (mode === Mode.MultiLineComment) {
        if (next === "\n") {
          row++;
        } else if (prev === "*" && next === "/") {
          mode = Mode.Default;
        }
        continue;
      } else if (mode === Mode.Default && next === "/" && nextNext === "*") {
        mode = Mode.MultiLineComment;
        build = result.add(build, row, col, mode);
        stream.takeNext(); // consume the '*' so it doesn't become prev for '*/' detection
        col++;
        continue;
      }
 
      switch (next) {
        case "'":
          build = result.add(build, row, col, mode);
          mode = Mode.String;
          build += next;
          break;
        case "\"":
          build = result.add(build, row, col, mode);
          mode = Mode.DoubleQuoteString;
          build += next;
          break;
        case " ":
        case "\t":
          build = result.add(build, row, col, mode);
          break;
        case "\n":
          build = result.add(build, row, col, mode);
          row++;
          col = 0;
          break;
        case ";":
        case ":":
        case ",":
        case ".":
        case "{":
        case "}":
        case "(":
        case ")":
        case "[":
        case "]":
        case "!":
        case "=":
        case "<":
        case ">":
        case "+":
        case "-":
        case "*":
        case "/":
          build = result.add(build, row, col, mode);
          result.add(next, row, col, mode);
          break;
        case "@":
          // @ starts a new annotation; flush current token and start building with @
          result.add(build, row, col, mode);
          build = "@";
          break;
        default:
          build += next;
          break;
      }
    }
 
    result.add(build, row, col, mode);
    return result.get();
  }
}