Front end of the Slack clone application.

doctrine.js 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*
  2. Copyright (C) 2012-2014 Yusuke Suzuki <utatane.tea@gmail.com>
  3. Copyright (C) 2014 Dan Tao <daniel.tao@gmail.com>
  4. Copyright (C) 2013 Andrew Eisenberg <andrew@eisenberg.as>
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  13. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  16. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. (function () {
  24. 'use strict';
  25. var typed,
  26. utility,
  27. isArray,
  28. jsdoc,
  29. esutils,
  30. hasOwnProperty;
  31. esutils = require('esutils');
  32. isArray = require('isarray');
  33. typed = require('./typed');
  34. utility = require('./utility');
  35. function sliceSource(source, index, last) {
  36. return source.slice(index, last);
  37. }
  38. hasOwnProperty = (function () {
  39. var func = Object.prototype.hasOwnProperty;
  40. return function hasOwnProperty(obj, name) {
  41. return func.call(obj, name);
  42. };
  43. }());
  44. function shallowCopy(obj) {
  45. var ret = {}, key;
  46. for (key in obj) {
  47. if (obj.hasOwnProperty(key)) {
  48. ret[key] = obj[key];
  49. }
  50. }
  51. return ret;
  52. }
  53. function isASCIIAlphanumeric(ch) {
  54. return (ch >= 0x61 /* 'a' */ && ch <= 0x7A /* 'z' */) ||
  55. (ch >= 0x41 /* 'A' */ && ch <= 0x5A /* 'Z' */) ||
  56. (ch >= 0x30 /* '0' */ && ch <= 0x39 /* '9' */);
  57. }
  58. function isParamTitle(title) {
  59. return title === 'param' || title === 'argument' || title === 'arg';
  60. }
  61. function isReturnTitle(title) {
  62. return title === 'return' || title === 'returns';
  63. }
  64. function isProperty(title) {
  65. return title === 'property' || title === 'prop';
  66. }
  67. function isNameParameterRequired(title) {
  68. return isParamTitle(title) || isProperty(title) ||
  69. title === 'alias' || title === 'this' || title === 'mixes' || title === 'requires';
  70. }
  71. function isAllowedName(title) {
  72. return isNameParameterRequired(title) || title === 'const' || title === 'constant';
  73. }
  74. function isAllowedNested(title) {
  75. return isProperty(title) || isParamTitle(title);
  76. }
  77. function isTypeParameterRequired(title) {
  78. return isParamTitle(title) || isReturnTitle(title) ||
  79. title === 'define' || title === 'enum' ||
  80. title === 'implements' || title === 'this' ||
  81. title === 'type' || title === 'typedef' || isProperty(title);
  82. }
  83. // Consider deprecation instead using 'isTypeParameterRequired' and 'Rules' declaration to pick when a type is optional/required
  84. // This would require changes to 'parseType'
  85. function isAllowedType(title) {
  86. return isTypeParameterRequired(title) || title === 'throws' || title === 'const' || title === 'constant' ||
  87. title === 'namespace' || title === 'member' || title === 'var' || title === 'module' ||
  88. title === 'constructor' || title === 'class' || title === 'extends' || title === 'augments' ||
  89. title === 'public' || title === 'private' || title === 'protected';
  90. }
  91. function trim(str) {
  92. return str.replace(/^\s+/, '').replace(/\s+$/, '');
  93. }
  94. function unwrapComment(doc) {
  95. // JSDoc comment is following form
  96. // /**
  97. // * .......
  98. // */
  99. // remove /**, */ and *
  100. var BEFORE_STAR = 0,
  101. STAR = 1,
  102. AFTER_STAR = 2,
  103. index,
  104. len,
  105. mode,
  106. result,
  107. ch;
  108. doc = doc.replace(/^\/\*\*?/, '').replace(/\*\/$/, '');
  109. index = 0;
  110. len = doc.length;
  111. mode = BEFORE_STAR;
  112. result = '';
  113. while (index < len) {
  114. ch = doc.charCodeAt(index);
  115. switch (mode) {
  116. case BEFORE_STAR:
  117. if (esutils.code.isLineTerminator(ch)) {
  118. result += String.fromCharCode(ch);
  119. } else if (ch === 0x2A /* '*' */) {
  120. mode = STAR;
  121. } else if (!esutils.code.isWhiteSpace(ch)) {
  122. result += String.fromCharCode(ch);
  123. mode = AFTER_STAR;
  124. }
  125. break;
  126. case STAR:
  127. if (!esutils.code.isWhiteSpace(ch)) {
  128. result += String.fromCharCode(ch);
  129. }
  130. mode = esutils.code.isLineTerminator(ch) ? BEFORE_STAR : AFTER_STAR;
  131. break;
  132. case AFTER_STAR:
  133. result += String.fromCharCode(ch);
  134. if (esutils.code.isLineTerminator(ch)) {
  135. mode = BEFORE_STAR;
  136. }
  137. break;
  138. }
  139. index += 1;
  140. }
  141. return result.replace(/\s+$/, '');
  142. }
  143. // JSDoc Tag Parser
  144. (function (exports) {
  145. var Rules,
  146. index,
  147. lineNumber,
  148. length,
  149. source,
  150. recoverable,
  151. sloppy,
  152. strict;
  153. function advance() {
  154. var ch = source.charCodeAt(index);
  155. index += 1;
  156. if (esutils.code.isLineTerminator(ch) && !(ch === 0x0D /* '\r' */ && source.charCodeAt(index) === 0x0A /* '\n' */)) {
  157. lineNumber += 1;
  158. }
  159. return String.fromCharCode(ch);
  160. }
  161. function scanTitle() {
  162. var title = '';
  163. // waste '@'
  164. advance();
  165. while (index < length && isASCIIAlphanumeric(source.charCodeAt(index))) {
  166. title += advance();
  167. }
  168. return title;
  169. }
  170. function seekContent() {
  171. var ch, waiting, last = index;
  172. waiting = false;
  173. while (last < length) {
  174. ch = source.charCodeAt(last);
  175. if (esutils.code.isLineTerminator(ch) && !(ch === 0x0D /* '\r' */ && source.charCodeAt(last + 1) === 0x0A /* '\n' */)) {
  176. waiting = true;
  177. } else if (waiting) {
  178. if (ch === 0x40 /* '@' */) {
  179. break;
  180. }
  181. if (!esutils.code.isWhiteSpace(ch)) {
  182. waiting = false;
  183. }
  184. }
  185. last += 1;
  186. }
  187. return last;
  188. }
  189. // type expression may have nest brace, such as,
  190. // { { ok: string } }
  191. //
  192. // therefore, scanning type expression with balancing braces.
  193. function parseType(title, last) {
  194. var ch, brace, type, direct = false;
  195. // search '{'
  196. while (index < last) {
  197. ch = source.charCodeAt(index);
  198. if (esutils.code.isWhiteSpace(ch)) {
  199. advance();
  200. } else if (ch === 0x7B /* '{' */) {
  201. advance();
  202. break;
  203. } else {
  204. // this is direct pattern
  205. direct = true;
  206. break;
  207. }
  208. }
  209. if (direct) {
  210. return null;
  211. }
  212. // type expression { is found
  213. brace = 1;
  214. type = '';
  215. while (index < last) {
  216. ch = source.charCodeAt(index);
  217. if (esutils.code.isLineTerminator(ch)) {
  218. advance();
  219. } else {
  220. if (ch === 0x7D /* '}' */) {
  221. brace -= 1;
  222. if (brace === 0) {
  223. advance();
  224. break;
  225. }
  226. } else if (ch === 0x7B /* '{' */) {
  227. brace += 1;
  228. }
  229. type += advance();
  230. }
  231. }
  232. if (brace !== 0) {
  233. // braces is not balanced
  234. return utility.throwError('Braces are not balanced');
  235. }
  236. if (isParamTitle(title)) {
  237. return typed.parseParamType(type);
  238. }
  239. return typed.parseType(type);
  240. }
  241. function scanIdentifier(last) {
  242. var identifier;
  243. if (!esutils.code.isIdentifierStart(source.charCodeAt(index))) {
  244. return null;
  245. }
  246. identifier = advance();
  247. while (index < last && esutils.code.isIdentifierPart(source.charCodeAt(index))) {
  248. identifier += advance();
  249. }
  250. return identifier;
  251. }
  252. function skipWhiteSpace(last) {
  253. while (index < last && (esutils.code.isWhiteSpace(source.charCodeAt(index)) || esutils.code.isLineTerminator(source.charCodeAt(index)))) {
  254. advance();
  255. }
  256. }
  257. function parseName(last, allowBrackets, allowNestedParams) {
  258. var name = '', useBrackets;
  259. skipWhiteSpace(last);
  260. if (index >= last) {
  261. return null;
  262. }
  263. if (allowBrackets && source.charCodeAt(index) === 0x5B /* '[' */) {
  264. useBrackets = true;
  265. name = advance();
  266. }
  267. if (!esutils.code.isIdentifierStart(source.charCodeAt(index))) {
  268. return null;
  269. }
  270. name += scanIdentifier(last);
  271. if (allowNestedParams) {
  272. if (source.charCodeAt(index) === 0x3A /* ':' */ && (
  273. name === 'module' ||
  274. name === 'external' ||
  275. name === 'event')) {
  276. name += advance();
  277. name += scanIdentifier(last);
  278. }
  279. if(source.charCodeAt(index) === 0x5B /* '[' */ && source.charCodeAt(index + 1) === 0x5D /* ']' */){
  280. name += advance();
  281. name += advance();
  282. }
  283. while (source.charCodeAt(index) === 0x2E /* '.' */ ||
  284. source.charCodeAt(index) === 0x23 /* '#' */ ||
  285. source.charCodeAt(index) === 0x7E /* '~' */) {
  286. name += advance();
  287. name += scanIdentifier(last);
  288. }
  289. }
  290. if (useBrackets) {
  291. // do we have a default value for this?
  292. if (source.charCodeAt(index) === 0x3D /* '=' */) {
  293. // consume the '='' symbol
  294. name += advance();
  295. var bracketDepth = 1;
  296. // scan in the default value
  297. while (index < last) {
  298. if (source.charCodeAt(index) === 0x5B /* '[' */) {
  299. bracketDepth++;
  300. } else if (source.charCodeAt(index) === 0x5D /* ']' */ &&
  301. --bracketDepth === 0) {
  302. break;
  303. }
  304. name += advance();
  305. }
  306. }
  307. if (index >= last || source.charCodeAt(index) !== 0x5D /* ']' */) {
  308. // we never found a closing ']'
  309. return null;
  310. }
  311. // collect the last ']'
  312. name += advance();
  313. }
  314. return name;
  315. }
  316. function skipToTag() {
  317. while (index < length && source.charCodeAt(index) !== 0x40 /* '@' */) {
  318. advance();
  319. }
  320. if (index >= length) {
  321. return false;
  322. }
  323. utility.assert(source.charCodeAt(index) === 0x40 /* '@' */);
  324. return true;
  325. }
  326. function TagParser(options, title) {
  327. this._options = options;
  328. this._title = title;
  329. this._tag = {
  330. title: title,
  331. description: null
  332. };
  333. if (this._options.lineNumbers) {
  334. this._tag.lineNumber = lineNumber;
  335. }
  336. this._last = 0;
  337. // space to save special information for title parsers.
  338. this._extra = { };
  339. }
  340. // addError(err, ...)
  341. TagParser.prototype.addError = function addError(errorText) {
  342. var args = Array.prototype.slice.call(arguments, 1),
  343. msg = errorText.replace(
  344. /%(\d)/g,
  345. function (whole, index) {
  346. utility.assert(index < args.length, 'Message reference must be in range');
  347. return args[index];
  348. }
  349. );
  350. if (!this._tag.errors) {
  351. this._tag.errors = [];
  352. }
  353. if (strict) {
  354. utility.throwError(msg);
  355. }
  356. this._tag.errors.push(msg);
  357. return recoverable;
  358. };
  359. TagParser.prototype.parseType = function () {
  360. // type required titles
  361. if (isTypeParameterRequired(this._title)) {
  362. try {
  363. this._tag.type = parseType(this._title, this._last);
  364. if (!this._tag.type) {
  365. if (!isParamTitle(this._title) && !isReturnTitle(this._title)) {
  366. if (!this.addError('Missing or invalid tag type')) {
  367. return false;
  368. }
  369. }
  370. }
  371. } catch (error) {
  372. this._tag.type = null;
  373. if (!this.addError(error.message)) {
  374. return false;
  375. }
  376. }
  377. } else if (isAllowedType(this._title)) {
  378. // optional types
  379. try {
  380. this._tag.type = parseType(this._title, this._last);
  381. } catch (e) {
  382. //For optional types, lets drop the thrown error when we hit the end of the file
  383. }
  384. }
  385. return true;
  386. };
  387. TagParser.prototype._parseNamePath = function (optional) {
  388. var name;
  389. name = parseName(this._last, sloppy && isParamTitle(this._title), true);
  390. if (!name) {
  391. if (!optional) {
  392. if (!this.addError('Missing or invalid tag name')) {
  393. return false;
  394. }
  395. }
  396. }
  397. this._tag.name = name;
  398. return true;
  399. };
  400. TagParser.prototype.parseNamePath = function () {
  401. return this._parseNamePath(false);
  402. };
  403. TagParser.prototype.parseNamePathOptional = function () {
  404. return this._parseNamePath(true);
  405. };
  406. TagParser.prototype.parseName = function () {
  407. var assign, name;
  408. // param, property requires name
  409. if (isAllowedName(this._title)) {
  410. this._tag.name = parseName(this._last, sloppy && isParamTitle(this._title), isAllowedNested(this._title));
  411. if (!this._tag.name) {
  412. if (!isNameParameterRequired(this._title)) {
  413. return true;
  414. }
  415. // it's possible the name has already been parsed but interpreted as a type
  416. // it's also possible this is a sloppy declaration, in which case it will be
  417. // fixed at the end
  418. if (isParamTitle(this._title) && this._tag.type && this._tag.type.name) {
  419. this._extra.name = this._tag.type;
  420. this._tag.name = this._tag.type.name;
  421. this._tag.type = null;
  422. } else {
  423. if (!this.addError('Missing or invalid tag name')) {
  424. return false;
  425. }
  426. }
  427. } else {
  428. name = this._tag.name;
  429. if (name.charAt(0) === '[' && name.charAt(name.length - 1) === ']') {
  430. // extract the default value if there is one
  431. // example: @param {string} [somebody=John Doe] description
  432. assign = name.substring(1, name.length - 1).split('=');
  433. if (assign[1]) {
  434. this._tag['default'] = assign[1];
  435. }
  436. this._tag.name = assign[0];
  437. // convert to an optional type
  438. if (this._tag.type && this._tag.type.type !== 'OptionalType') {
  439. this._tag.type = {
  440. type: 'OptionalType',
  441. expression: this._tag.type
  442. };
  443. }
  444. }
  445. }
  446. }
  447. return true;
  448. };
  449. TagParser.prototype.parseDescription = function parseDescription() {
  450. var description = trim(sliceSource(source, index, this._last));
  451. if (description) {
  452. if ((/^-\s+/).test(description)) {
  453. description = description.substring(2);
  454. }
  455. this._tag.description = description;
  456. }
  457. return true;
  458. };
  459. TagParser.prototype.parseKind = function parseKind() {
  460. var kind, kinds;
  461. kinds = {
  462. 'class': true,
  463. 'constant': true,
  464. 'event': true,
  465. 'external': true,
  466. 'file': true,
  467. 'function': true,
  468. 'member': true,
  469. 'mixin': true,
  470. 'module': true,
  471. 'namespace': true,
  472. 'typedef': true
  473. };
  474. kind = trim(sliceSource(source, index, this._last));
  475. this._tag.kind = kind;
  476. if (!hasOwnProperty(kinds, kind)) {
  477. if (!this.addError('Invalid kind name \'%0\'', kind)) {
  478. return false;
  479. }
  480. }
  481. return true;
  482. };
  483. TagParser.prototype.parseAccess = function parseAccess() {
  484. var access;
  485. access = trim(sliceSource(source, index, this._last));
  486. this._tag.access = access;
  487. if (access !== 'private' && access !== 'protected' && access !== 'public') {
  488. if (!this.addError('Invalid access name \'%0\'', access)) {
  489. return false;
  490. }
  491. }
  492. return true;
  493. };
  494. TagParser.prototype.parseVariation = function parseVariation() {
  495. var variation, text;
  496. text = trim(sliceSource(source, index, this._last));
  497. variation = parseFloat(text, 10);
  498. this._tag.variation = variation;
  499. if (isNaN(variation)) {
  500. if (!this.addError('Invalid variation \'%0\'', text)) {
  501. return false;
  502. }
  503. }
  504. return true;
  505. };
  506. TagParser.prototype.ensureEnd = function () {
  507. var shouldBeEmpty = trim(sliceSource(source, index, this._last));
  508. if (shouldBeEmpty) {
  509. if (!this.addError('Unknown content \'%0\'', shouldBeEmpty)) {
  510. return false;
  511. }
  512. }
  513. return true;
  514. };
  515. TagParser.prototype.epilogue = function epilogue() {
  516. var description;
  517. description = this._tag.description;
  518. // un-fix potentially sloppy declaration
  519. if (isParamTitle(this._title) && !this._tag.type && description && description.charAt(0) === '[') {
  520. this._tag.type = this._extra.name;
  521. if (!this._tag.name) {
  522. this._tag.name = undefined;
  523. }
  524. if (!sloppy) {
  525. if (!this.addError('Missing or invalid tag name')) {
  526. return false;
  527. }
  528. }
  529. }
  530. return true;
  531. };
  532. Rules = {
  533. // http://usejsdoc.org/tags-access.html
  534. 'access': ['parseAccess'],
  535. // http://usejsdoc.org/tags-alias.html
  536. 'alias': ['parseNamePath', 'ensureEnd'],
  537. // http://usejsdoc.org/tags-augments.html
  538. 'augments': ['parseType', 'parseNamePathOptional', 'ensureEnd'],
  539. // http://usejsdoc.org/tags-constructor.html
  540. 'constructor': ['parseType', 'parseNamePathOptional', 'ensureEnd'],
  541. // Synonym: http://usejsdoc.org/tags-constructor.html
  542. 'class': ['parseType', 'parseNamePathOptional', 'ensureEnd'],
  543. // Synonym: http://usejsdoc.org/tags-extends.html
  544. 'extends': ['parseType', 'parseNamePathOptional', 'ensureEnd'],
  545. // http://usejsdoc.org/tags-deprecated.html
  546. 'deprecated': ['parseDescription'],
  547. // http://usejsdoc.org/tags-global.html
  548. 'global': ['ensureEnd'],
  549. // http://usejsdoc.org/tags-inner.html
  550. 'inner': ['ensureEnd'],
  551. // http://usejsdoc.org/tags-instance.html
  552. 'instance': ['ensureEnd'],
  553. // http://usejsdoc.org/tags-kind.html
  554. 'kind': ['parseKind'],
  555. // http://usejsdoc.org/tags-mixes.html
  556. 'mixes': ['parseNamePath', 'ensureEnd'],
  557. // http://usejsdoc.org/tags-mixin.html
  558. 'mixin': ['parseNamePathOptional', 'ensureEnd'],
  559. // http://usejsdoc.org/tags-member.html
  560. 'member': ['parseType', 'parseNamePathOptional', 'ensureEnd'],
  561. // http://usejsdoc.org/tags-method.html
  562. 'method': ['parseNamePathOptional', 'ensureEnd'],
  563. // http://usejsdoc.org/tags-module.html
  564. 'module': ['parseType', 'parseNamePathOptional', 'ensureEnd'],
  565. // Synonym: http://usejsdoc.org/tags-method.html
  566. 'func': ['parseNamePathOptional', 'ensureEnd'],
  567. // Synonym: http://usejsdoc.org/tags-method.html
  568. 'function': ['parseNamePathOptional', 'ensureEnd'],
  569. // Synonym: http://usejsdoc.org/tags-member.html
  570. 'var': ['parseType', 'parseNamePathOptional', 'ensureEnd'],
  571. // http://usejsdoc.org/tags-name.html
  572. 'name': ['parseNamePath', 'ensureEnd'],
  573. // http://usejsdoc.org/tags-namespace.html
  574. 'namespace': ['parseType', 'parseNamePathOptional', 'ensureEnd'],
  575. // http://usejsdoc.org/tags-private.html
  576. 'private': ['parseType', 'parseDescription'],
  577. // http://usejsdoc.org/tags-protected.html
  578. 'protected': ['parseType', 'parseDescription'],
  579. // http://usejsdoc.org/tags-public.html
  580. 'public': ['parseType', 'parseDescription'],
  581. // http://usejsdoc.org/tags-readonly.html
  582. 'readonly': ['ensureEnd'],
  583. // http://usejsdoc.org/tags-requires.html
  584. 'requires': ['parseNamePath', 'ensureEnd'],
  585. // http://usejsdoc.org/tags-since.html
  586. 'since': ['parseDescription'],
  587. // http://usejsdoc.org/tags-static.html
  588. 'static': ['ensureEnd'],
  589. // http://usejsdoc.org/tags-summary.html
  590. 'summary': ['parseDescription'],
  591. // http://usejsdoc.org/tags-this.html
  592. 'this': ['parseNamePath', 'ensureEnd'],
  593. // http://usejsdoc.org/tags-todo.html
  594. 'todo': ['parseDescription'],
  595. // http://usejsdoc.org/tags-typedef.html
  596. 'typedef': ['parseType', 'parseNamePathOptional'],
  597. // http://usejsdoc.org/tags-variation.html
  598. 'variation': ['parseVariation'],
  599. // http://usejsdoc.org/tags-version.html
  600. 'version': ['parseDescription']
  601. };
  602. TagParser.prototype.parse = function parse() {
  603. var i, iz, sequences, method;
  604. // empty title
  605. if (!this._title) {
  606. if (!this.addError('Missing or invalid title')) {
  607. return null;
  608. }
  609. }
  610. // Seek to content last index.
  611. this._last = seekContent(this._title);
  612. if (hasOwnProperty(Rules, this._title)) {
  613. sequences = Rules[this._title];
  614. } else {
  615. // default sequences
  616. sequences = ['parseType', 'parseName', 'parseDescription', 'epilogue'];
  617. }
  618. for (i = 0, iz = sequences.length; i < iz; ++i) {
  619. method = sequences[i];
  620. if (!this[method]()) {
  621. return null;
  622. }
  623. }
  624. return this._tag;
  625. };
  626. function parseTag(options) {
  627. var title, parser, tag;
  628. // skip to tag
  629. if (!skipToTag()) {
  630. return null;
  631. }
  632. // scan title
  633. title = scanTitle();
  634. // construct tag parser
  635. parser = new TagParser(options, title);
  636. tag = parser.parse();
  637. // Seek global index to end of this tag.
  638. while (index < parser._last) {
  639. advance();
  640. }
  641. return tag;
  642. }
  643. //
  644. // Parse JSDoc
  645. //
  646. function scanJSDocDescription(preserveWhitespace) {
  647. var description = '', ch, atAllowed;
  648. atAllowed = true;
  649. while (index < length) {
  650. ch = source.charCodeAt(index);
  651. if (atAllowed && ch === 0x40 /* '@' */) {
  652. break;
  653. }
  654. if (esutils.code.isLineTerminator(ch)) {
  655. atAllowed = true;
  656. } else if (atAllowed && !esutils.code.isWhiteSpace(ch)) {
  657. atAllowed = false;
  658. }
  659. description += advance();
  660. }
  661. return preserveWhitespace ? description : trim(description);
  662. }
  663. function parse(comment, options) {
  664. var tags = [], tag, description, interestingTags, i, iz;
  665. if (options === undefined) {
  666. options = {};
  667. }
  668. if (typeof options.unwrap === 'boolean' && options.unwrap) {
  669. source = unwrapComment(comment);
  670. } else {
  671. source = comment;
  672. }
  673. // array of relevant tags
  674. if (options.tags) {
  675. if (isArray(options.tags)) {
  676. interestingTags = { };
  677. for (i = 0, iz = options.tags.length; i < iz; i++) {
  678. if (typeof options.tags[i] === 'string') {
  679. interestingTags[options.tags[i]] = true;
  680. } else {
  681. utility.throwError('Invalid "tags" parameter: ' + options.tags);
  682. }
  683. }
  684. } else {
  685. utility.throwError('Invalid "tags" parameter: ' + options.tags);
  686. }
  687. }
  688. length = source.length;
  689. index = 0;
  690. lineNumber = 0;
  691. recoverable = options.recoverable;
  692. sloppy = options.sloppy;
  693. strict = options.strict;
  694. description = scanJSDocDescription(options.preserveWhitespace);
  695. while (true) {
  696. tag = parseTag(options);
  697. if (!tag) {
  698. break;
  699. }
  700. if (!interestingTags || interestingTags.hasOwnProperty(tag.title)) {
  701. tags.push(tag);
  702. }
  703. }
  704. return {
  705. description: description,
  706. tags: tags
  707. };
  708. }
  709. exports.parse = parse;
  710. }(jsdoc = {}));
  711. exports.version = utility.VERSION;
  712. exports.parse = jsdoc.parse;
  713. exports.parseType = typed.parseType;
  714. exports.parseParamType = typed.parseParamType;
  715. exports.unwrapComment = unwrapComment;
  716. exports.Syntax = shallowCopy(typed.Syntax);
  717. exports.Error = utility.DoctrineError;
  718. exports.type = {
  719. Syntax: exports.Syntax,
  720. parseType: typed.parseType,
  721. parseParamType: typed.parseParamType,
  722. stringify: typed.stringify
  723. };
  724. }());
  725. /* vim: set sw=4 ts=4 et tw=80 : */