UNPKG

5.13 kBJavaScriptView Raw
1'use strict';
2Object.defineProperty(exports, "__esModule", { value: true });
3var P = require("parsimmon");
4var regex = require("../regex");
5var utils = require("../utils");
6var id = P.regex(regex.label).desc('project name');
7var semver = P.regex(regex.semverV).desc('semver');
8var space = P.string(' ');
9var colon = P.string(':');
10var optColon = P.regex(/:?/);
11var linebreak = P.regex(/\r?\n/).desc('linebreak');
12var lineTrail = P.regex(/[ \t]*\r?\n/).desc('linebreak');
13var tabSpace = P.regex(/[ \t]/).desc('tab or space');
14var optTabSpace = P.regex(/[ \t]*/).desc('tab or space');
15var optComma = P.regex(/,?/);
16var url = P.regex(regex.uri).desc('url');
17var urlBracket = P.string('<').then(url).skip(P.string('>'));
18var bomOpt = P.regex(regex.bomOpt);
19var comment = P.string('//');
20var commentSpace = comment.skip(space);
21var commentTab = comment.skip(tabSpace);
22var comment3 = P.string('///').skip(space);
23var nameUTF = P.regex(regex.nameUTF).desc('name');
24var separatorComma = P.string(',')
25 .then(space.or(optTabSpace
26 .then(linebreak)
27 .then(comment)
28 .then(tabSpace)
29 .then(optTabSpace)));
30var separatorOptComma = P.seq(P.string(','), space)
31 .or(optTabSpace
32 .then(optComma)
33 .then(linebreak)
34 .then(comment)
35 .then(tabSpace)
36 .then(optTabSpace));
37var separatorProject = P.seq(P.string(','), space)
38 .or(optTabSpace
39 .then(optComma)
40 .then(linebreak)
41 .then(comment)
42 .then(tabSpace)
43 .then(P.seq(P.string('Project:'), tabSpace).or(optTabSpace)));
44var untilEndOfLine = P.takeWhile(function (c) {
45 return c !== '\r' &&
46 c !== '\n';
47});
48exports.person = P
49 .seq(
50// name: Grab everything up to the URL bracket
51P.takeWhile(function (c) { return c !== '<'; }).skip(P.string('<')),
52// url: Grab everything between the URL brackets
53P.takeWhile(function (c) { return c !== '>'; }).skip(P.string('>')))
54 .map(function (arr) {
55 return {
56 name: arr[0].trim(),
57 url: arr[1] ? utils.untrail(arr[1]) : null
58 };
59})
60 .skip(optTabSpace);
61exports.label = P
62 .string('// Type definitions for ')
63 .then(untilEndOfLine)
64 .map(function (result) {
65 // Name is everything that is not the version number
66 // Version number is separated from the label by a space
67 // - Expected format is MAJOR.MINOR but authors might deviate from it
68 // - Can be omitted
69 // - Can have leading 'v'
70 // - Can have trailing 'x'
71 // - Can have trailing '+'
72 // - Can be in the middle of the label
73 // - Can indicate multiple versions (e.g. '1.10.x / 2.0.x')
74 var matchVersion = /(?:[\- ])(v?[\d.x+]{2,})/ig;
75 var start = result.length - 1;
76 var end = 0;
77 var match;
78 while ((match = matchVersion.exec(result)) !== null) {
79 if (match.index < start) {
80 start = match.index;
81 }
82 end = matchVersion.lastIndex;
83 }
84 var name = result;
85 var version = null;
86 if (start < end) {
87 var nameStart = result.slice(0, start).trim();
88 version = result.slice(start + 1, end);
89 var nameEnd = result.slice(end).trim();
90 name = nameStart + ' ' + nameEnd;
91 }
92 return {
93 name: name.trim(),
94 version: version
95 };
96})
97 .skip(optTabSpace);
98exports.project = P.string('// Project: ')
99 .then(P.seq(url, separatorProject.then(url).many()))
100 .map(function (arr) {
101 var ret = [];
102 ret.push({
103 url: utils.untrail(arr[0])
104 });
105 arr[1].forEach(function (url) {
106 ret.push({
107 url: utils.untrail(url)
108 });
109 });
110 return ret;
111})
112 .skip(untilEndOfLine);
113var multilineAuthorsSeparator = P.seq(
114// Line can end with optional comma followed by any number of spaces and tabs
115optComma, optTabSpace,
116// Check if there's more authors
117linebreak.notFollowedBy(P.string('// Definitions: ')),
118// Grab the beginning of the line up to the start of the next author
119comment, optTabSpace).map(function (result) { return result.join(''); });
120// Authors can be separated on the same line by a comma followed by any number of spaces and tabs
121var sameLineAuthorsSeparator = P.seq(P.string(','), optTabSpace).map(function (result) { return result.join(''); });
122exports.authors = P.string('// Definitions by: ')
123 .then(P.sepBy(exports.person, P.alt(
124// Multi-line must go first or else same-line will eat its optional comma
125multilineAuthorsSeparator, sameLineAuthorsSeparator)))
126 .skip(optTabSpace);
127exports.repo = P.string('// Definitions: ')
128 .then(untilEndOfLine)
129 .map(function (url) {
130 return {
131 url: utils.untrail(url.replace(/\s/, ''))
132 };
133})
134 .skip(optTabSpace);
135exports.header = bomOpt
136 .then(P.seq(exports.label.skip(linebreak).or(P.of(null)), exports.project.skip(linebreak), exports.authors.skip(linebreak), exports.repo.skip(linebreak)))
137 .skip(P.all)
138 .map(function (arr) {
139 return {
140 label: arr[0],
141 project: arr[1],
142 authors: arr[2],
143 repository: arr[3]
144 };
145});
146//# sourceMappingURL=lax.js.map
\No newline at end of file