Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. module.exports = parse;
  2. function parse(str) {
  3. return new Parser(str).parse();
  4. }
  5. function Parser(str) {
  6. this.str = str;
  7. }
  8. Parser.prototype.skip = function(m){
  9. this.str = this.str.slice(m[0].length);
  10. };
  11. Parser.prototype.comma = function(){
  12. var m = /^, */.exec(this.str);
  13. if (!m) return;
  14. this.skip(m);
  15. return { type: 'comma', string: ',' };
  16. };
  17. Parser.prototype.ident = function(){
  18. var m = /^([\w-]+) */.exec(this.str);
  19. if (!m) return;
  20. this.skip(m);
  21. return {
  22. type: 'ident',
  23. string: m[1]
  24. }
  25. };
  26. Parser.prototype.int = function(){
  27. var m = /^((\d+)(\S+)?) */.exec(this.str);
  28. if (!m) return;
  29. this.skip(m);
  30. var n = ~~m[2];
  31. var u = m[3];
  32. return {
  33. type: 'number',
  34. string: m[1],
  35. unit: u || '',
  36. value: n
  37. }
  38. };
  39. Parser.prototype.float = function(){
  40. var m = /^(((?:\d+)?\.\d+)(\S+)?) */.exec(this.str);
  41. if (!m) return;
  42. this.skip(m);
  43. var n = parseFloat(m[2]);
  44. var u = m[3];
  45. return {
  46. type: 'number',
  47. string: m[1],
  48. unit: u || '',
  49. value: n
  50. }
  51. };
  52. Parser.prototype.number = function(){
  53. return this.float() || this.int();
  54. };
  55. Parser.prototype.double = function(){
  56. var m = /^"([^"]*)" */.exec(this.str);
  57. if (!m) return m;
  58. this.skip(m);
  59. return {
  60. type: 'string',
  61. quote: '"',
  62. string: '"' + m[1] + '"',
  63. value: m[1]
  64. }
  65. };
  66. Parser.prototype.single = function(){
  67. var m = /^'([^']*)' */.exec(this.str);
  68. if (!m) return m;
  69. this.skip(m);
  70. return {
  71. type: 'string',
  72. quote: "'",
  73. string: "'" + m[1] + "'",
  74. value: m[1]
  75. }
  76. };
  77. Parser.prototype.string = function(){
  78. return this.single() || this.double();
  79. };
  80. Parser.prototype.value = function(){
  81. return this.number()
  82. || this.ident()
  83. || this.string()
  84. || this.comma();
  85. };
  86. Parser.prototype.parse = function(){
  87. var vals = [];
  88. while (this.str.length) {
  89. var obj = this.value();
  90. if (!obj) throw new Error('failed to parse near `' + this.str.slice(0, 10) + '...`');
  91. vals.push(obj);
  92. }
  93. return vals;
  94. };