types.js 1.4 KB

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