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.

test-events.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. var parseCst = require('./parse-cst.js');
  3. var Document = require('./Document-9b4560a1.js');
  4. require('./PlainValue-ec8e588e.js');
  5. require('./resolveSeq-d03cb037.js');
  6. require('./Schema-88e323a7.js');
  7. require('./warnings-1000a372.js');
  8. function testEvents(src, options) {
  9. const opt = Object.assign({
  10. keepCstNodes: true,
  11. keepNodeTypes: true,
  12. version: '1.2'
  13. }, options);
  14. const docs = parseCst.parse(src).map(cstDoc => new Document.Document(opt).parse(cstDoc));
  15. const errDoc = docs.find(doc => doc.errors.length > 0);
  16. const error = errDoc ? errDoc.errors[0].message : null;
  17. const events = ['+STR'];
  18. try {
  19. for (let i = 0; i < docs.length; ++i) {
  20. const doc = docs[i];
  21. let root = doc.contents;
  22. if (Array.isArray(root)) root = root[0];
  23. const [rootStart, rootEnd] = doc.range || [0, 0];
  24. let e = doc.errors[0] && doc.errors[0].source;
  25. if (e && e.type === 'SEQ_ITEM') e = e.node;
  26. if (e && (e.type === 'DOCUMENT' || e.range.start < rootStart)) throw new Error();
  27. let docStart = '+DOC';
  28. const pre = src.slice(0, rootStart);
  29. const explicitDoc = /---\s*$/.test(pre);
  30. if (explicitDoc) docStart += ' ---';else if (!doc.contents) continue;
  31. events.push(docStart);
  32. addEvents(events, doc, e, root);
  33. if (doc.contents && doc.contents.length > 1) throw new Error();
  34. let docEnd = '-DOC';
  35. if (rootEnd) {
  36. const post = src.slice(rootEnd);
  37. if (/^\.\.\./.test(post)) docEnd += ' ...';
  38. }
  39. events.push(docEnd);
  40. }
  41. } catch (e) {
  42. return {
  43. events,
  44. error: error || e
  45. };
  46. }
  47. events.push('-STR');
  48. return {
  49. events,
  50. error
  51. };
  52. }
  53. function addEvents(events, doc, e, node) {
  54. if (!node) {
  55. events.push('=VAL :');
  56. return;
  57. }
  58. if (e && node.cstNode === e) throw new Error();
  59. let props = '';
  60. let anchor = doc.anchors.getName(node);
  61. if (anchor) {
  62. if (/\d$/.test(anchor)) {
  63. const alt = anchor.replace(/\d$/, '');
  64. if (doc.anchors.getNode(alt)) anchor = alt;
  65. }
  66. props = ` &${anchor}`;
  67. }
  68. if (node.cstNode && node.cstNode.tag) {
  69. const {
  70. handle,
  71. suffix
  72. } = node.cstNode.tag;
  73. props += handle === '!' && !suffix ? ' <!>' : ` <${node.tag}>`;
  74. }
  75. let scalar = null;
  76. switch (node.type) {
  77. case 'ALIAS':
  78. {
  79. let alias = doc.anchors.getName(node.source);
  80. if (/\d$/.test(alias)) {
  81. const alt = alias.replace(/\d$/, '');
  82. if (doc.anchors.getNode(alt)) alias = alt;
  83. }
  84. events.push(`=ALI${props} *${alias}`);
  85. }
  86. break;
  87. case 'BLOCK_FOLDED':
  88. scalar = '>';
  89. break;
  90. case 'BLOCK_LITERAL':
  91. scalar = '|';
  92. break;
  93. case 'PLAIN':
  94. scalar = ':';
  95. break;
  96. case 'QUOTE_DOUBLE':
  97. scalar = '"';
  98. break;
  99. case 'QUOTE_SINGLE':
  100. scalar = "'";
  101. break;
  102. case 'PAIR':
  103. events.push(`+MAP${props}`);
  104. addEvents(events, doc, e, node.key);
  105. addEvents(events, doc, e, node.value);
  106. events.push('-MAP');
  107. break;
  108. case 'FLOW_SEQ':
  109. case 'SEQ':
  110. events.push(`+SEQ${props}`);
  111. node.items.forEach(item => {
  112. addEvents(events, doc, e, item);
  113. });
  114. events.push('-SEQ');
  115. break;
  116. case 'FLOW_MAP':
  117. case 'MAP':
  118. events.push(`+MAP${props}`);
  119. node.items.forEach(({
  120. key,
  121. value
  122. }) => {
  123. addEvents(events, doc, e, key);
  124. addEvents(events, doc, e, value);
  125. });
  126. events.push('-MAP');
  127. break;
  128. default:
  129. throw new Error(`Unexpected node type ${node.type}`);
  130. }
  131. if (scalar) {
  132. const value = node.cstNode.strValue.replace(/\\/g, '\\\\').replace(/\0/g, '\\0').replace(/\x07/g, '\\a').replace(/\x08/g, '\\b').replace(/\t/g, '\\t').replace(/\n/g, '\\n').replace(/\v/g, '\\v').replace(/\f/g, '\\f').replace(/\r/g, '\\r').replace(/\x1b/g, '\\e');
  133. events.push(`=VAL${props} ${scalar}${value}`);
  134. }
  135. }
  136. exports.testEvents = testEvents;