All files / PARSE/2-Parse TargetDirectives.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 2/2
100% Lines 9/9

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                          5933x 5933x 128x 128x 18x     110x       220x 110x             5933x          
/**
 * ADR-049: the one reader of a file's `#pragma` lines.
 *
 * The grammar lexes each pragma as one token, so the directive is always one
 * line; this splits that token into its key and values. Nothing else reads
 * pragma text -- not the run-target resolution, not the test harness -- so a
 * pragma cannot be understood two ways.
 */
import type * as Parser from "./grammar/CNextParser";
import type ITargetDirective from "../../types/ITargetDirective";
 
class TargetDirectives {
  static read(tree: Parser.ProgramContext): ITargetDirective[] {
    const directives: ITargetDirective[] = [];
    for (const directive of tree.preprocessorDirective()) {
      const pragma = directive.pragmaDirective();
      if (!pragma) {
        continue;
      }
      // "#", optional blanks, "pragma", then blank-separated words
      const words = pragma
        .getText()
        .replace(/^#\s*pragma\s+/, "")
        .split(/\s+/)
        .filter((word) => word.length > 0);
      directives.push({
        key: words[0],
        values: words.slice(1),
        line: pragma.start!.line,
        column: pragma.start!.column,
      });
    }
    return directives;
  }
}
 
export default TargetDirectives;