parsers.js 1.3 KB

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