All files / src/parsers primitives.js

95.45% Statements 42/44
96.77% Branches 30/31
80% Functions 8/10
95.45% Lines 42/44

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 951x       8761x 8761x 8761x 8761x 8761x       28x       2x 1x         2x 1x         8709x                         322x 322x 322x       306x 301x   5x 5x 22x   3x 19x     2x 2x 1x       5x       319x 319x 1x   318x 1x 1x   318x 318x 8765x 48x 8717x 8716x   8x   8708x     1x     317x      
import {getLanguage} from '../languages';
 
export class Primitive {
  constructor(languageId, name, {argumentTypes, returnType}={}) {
    this.languageId = languageId;
    this.parser = languageId ? getLanguage(languageId).getParser() : null;
    this.name = name;
    this.argumentTypes = argumentTypes || [];
    this.returnType = returnType;
  }
 
  toString() {
    return this.name;
  }
 
  getASTNode() {
    if (this.parser && this.parser.getASTNodeForPrimitive) {
      return this.parser.getASTNodeForPrimitive(this);
    }
  }
 
  getLiteralNode() {
    if (this.parser && this.parser.getLiteralNodeForPrimitive) {
      return this.parser.getLiteralNodeForPrimitive(this);
    }
  }
 
  static fromConfig(languageId, config) {
    return new Primitive(
      languageId,
      config.name,
      {
        argumentTypes: config.argumentTypes,
        returnType: config.returnType,
      }
    );
  }
}
 
export class PrimitiveGroup {
  constructor(languageId, name, primitives) {
    this.languageId = languageId;
    this.name = name;
    this.primitives = primitives;
  }
 
  filter(search) {
    if (!search) {
      return this;
    }
    let result = [];
    for (let primitive of this.primitives) {
      if (primitive.name.toLowerCase().indexOf(search.toLowerCase()) >= 0) {
        // let's display the entire group and/or primitive
        result.push(primitive);
      } else if (primitive instanceof PrimitiveGroup) {
        // it's a group with a name that doesn't match
        // let's see if child primitives/groups match
        let filtered = primitive.filter(search);
        if (filtered.primitives.length > 0) {
          result.push(filtered);
        }
      }
    }
    return new PrimitiveGroup(this.languageId, this.name, result);
  }
 
  static fromConfig(languageId, config) {
    var {name, primitives} = config;
    if (!name) {
      throw new Error('No name specified for primitive group');
    }
    if (!primitives) {
      console.warn(`primitive group "${name}" doesn't have any primitives`);
      primitives = [];
    }
    const items = [];
    for (let item of primitives) {
      if (typeof item == 'string') {
        items.push(new Primitive(languageId, item));
      } else if (typeof item == 'object') {
        if (item.primitives) {
          // it's a group
          items.push(PrimitiveGroup.fromConfig(languageId, item));
        } else {
          items.push(Primitive.fromConfig(languageId, item));
        }
      } else {
        throw new Error(`Unable to understand config object of type ${typeof item}`);
      }
    }
    return new PrimitiveGroup(languageId, name, items);
  }
}