parsers.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. }