types.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { LanguageService } from "../services/languageService";
  2. import { BigNumber } from 'bignumber.js'
  3. export const Types = Object.freeze({
  4. INTEGER: {value: "int", ord: 0},
  5. REAL: {value: "real", ord: 1},
  6. STRING: {value: "string", ord: 2},
  7. BOOLEAN: {value: "bool", ord: 3},
  8. VOID: {value: "void", ord: 4},
  9. ARRAY: {value: 'array', ord: 5},
  10. UNDEFINED: {value: 'undefined', ord: 6},
  11. ALL: {value: 'all', ord: 7}
  12. });
  13. export function toInt (str) {
  14. return new BigNumber(str);
  15. }
  16. export function toString (str) {
  17. let value = str.replace(/^"/, '');
  18. value = value.replace(/"$/, '');
  19. value = value.replace(/\\b/g, "\b");
  20. value = value.replace(/\\t/g, "\t");
  21. value = value.replace(/\\n/g, "\n");
  22. value = value.replace(/\\r/g, "\r");
  23. value = value.replace(/\\\"/g, "\"");
  24. value = value.replace(/\\\'/g, "\'");
  25. value = value.replace(/\\\\/g, "\\");
  26. return value;
  27. }
  28. export function toReal (value) {
  29. return new BigNumber(value);
  30. }
  31. export function toBool (str) {
  32. const val = "'" + str + "'";
  33. const lexer = LanguageService.getCurrentLexer();
  34. const instance = new lexer(null);
  35. if (instance.literalNames[lexer.RK_TRUE] === val) {
  36. return true;
  37. } else if (instance.literalNames[lexer.RK_FALSE] === val) {
  38. return false;
  39. } else {
  40. // TODO: better error message
  41. throw new Error(str + "not a valid boolean");
  42. }
  43. }