All files / src/parsers primitives.js

50% Statements 22/44
25.81% Branches 8/31
50% Functions 5/10
50% Lines 22/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       174x 174x 174x 174x 174x                                       174x                         6x 6x 6x       6x 6x                                       6x 6x     6x       6x 6x 174x   174x 174x       174x           6x      
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) {
    Eif (!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;
    Iif (!name) {
      throw new Error('No name specified for primitive group');
    }
    Iif (!primitives) {
      console.warn(`primitive group "${name}" doesn't have any primitives`);
      primitives = [];
    }
    const items = [];
    for (let item of primitives) {
      Iif (typeof item == 'string') {
        items.push(new Primitive(languageId, item));
      } else Eif (typeof item == 'object') {
        Iif (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);
  }
}