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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { splitSpace, isSpace } from '../../util.js';
  2. const isQuoted = (s) => s && s.startsWith('"') && s.endsWith('"');
  3. /**
  4. * Splits remaining `spec.lines[].tokens.description` into `name` and `descriptions` tokens,
  5. * and populates the `spec.name`
  6. */
  7. export default function nameTokenizer() {
  8. const typeEnd = (num, { tokens }, i) => tokens.type === '' ? num : i;
  9. return (spec) => {
  10. // look for the name in the line where {type} ends
  11. const { tokens } = spec.source[spec.source.reduce(typeEnd, 0)];
  12. const source = tokens.description.trimLeft();
  13. const quotedGroups = source.split('"');
  14. // if it starts with quoted group, assume it is a literal
  15. if (quotedGroups.length > 1 &&
  16. quotedGroups[0] === '' &&
  17. quotedGroups.length % 2 === 1) {
  18. spec.name = quotedGroups[1];
  19. tokens.name = `"${quotedGroups[1]}"`;
  20. [tokens.postName, tokens.description] = splitSpace(source.slice(tokens.name.length));
  21. return spec;
  22. }
  23. let brackets = 0;
  24. let name = '';
  25. let optional = false;
  26. let defaultValue;
  27. // assume name is non-space string or anything wrapped into brackets
  28. for (const ch of source) {
  29. if (brackets === 0 && isSpace(ch))
  30. break;
  31. if (ch === '[')
  32. brackets++;
  33. if (ch === ']')
  34. brackets--;
  35. name += ch;
  36. }
  37. if (brackets !== 0) {
  38. spec.problems.push({
  39. code: 'spec:name:unpaired-brackets',
  40. message: 'unpaired brackets',
  41. line: spec.source[0].number,
  42. critical: true,
  43. });
  44. return spec;
  45. }
  46. const nameToken = name;
  47. if (name[0] === '[' && name[name.length - 1] === ']') {
  48. optional = true;
  49. name = name.slice(1, -1);
  50. const parts = name.split('=');
  51. name = parts[0].trim();
  52. if (parts[1] !== undefined)
  53. defaultValue = parts.slice(1).join('=').trim();
  54. if (name === '') {
  55. spec.problems.push({
  56. code: 'spec:name:empty-name',
  57. message: 'empty name',
  58. line: spec.source[0].number,
  59. critical: true,
  60. });
  61. return spec;
  62. }
  63. if (defaultValue === '') {
  64. spec.problems.push({
  65. code: 'spec:name:empty-default',
  66. message: 'empty default value',
  67. line: spec.source[0].number,
  68. critical: true,
  69. });
  70. return spec;
  71. }
  72. // has "=" and is not a string, except for "=>"
  73. if (!isQuoted(defaultValue) && /=(?!>)/.test(defaultValue)) {
  74. spec.problems.push({
  75. code: 'spec:name:invalid-default',
  76. message: 'invalid default value syntax',
  77. line: spec.source[0].number,
  78. critical: true,
  79. });
  80. return spec;
  81. }
  82. }
  83. spec.optional = optional;
  84. spec.name = name;
  85. tokens.name = nameToken;
  86. if (defaultValue !== undefined)
  87. spec.default = defaultValue;
  88. [tokens.postName, tokens.description] = splitSpace(source.slice(tokens.name.length));
  89. return spec;
  90. };
  91. }