depends-txt
Version:
A parser module for TeX Live's DEPENDS.txt file format
77 lines • 2.32 kB
JavaScript
export const Event = {
Name: 'Name',
Directive: 'Directive',
WhiteSpace: 'WhiteSpace',
Comment: 'Comment',
NewLine: 'NewLine',
Error: 'Error',
};
export const Directive = {
Hard: 'hard',
Soft: 'soft',
Package: 'package',
};
export const Severity = {
Error: true,
Warning: false,
Info: undefined,
};
export function* validate(event) {
const { type, value, position } = event;
if (type in RULES) {
const { re, ...rule } = RULES[type];
for (const match of value.matchAll(re)) {
const place = structuredClone(position);
const [start, end] = match.indices[0];
place.start.column += start;
place.start.offset += start;
place.end.column += end;
place.end.offset += end;
yield { ...structuredClone(rule), actual: escape(match[0]), place };
}
}
}
const RULES = {
[Event.Name]: {
ruleId: 'illegal-character-in-package-name',
reason: 'this character is not allowed in package names',
fatal: Severity.Warning,
expected: [
'lowercase ASCII letters',
'ASCII numbers',
`dash (${escape('-')})`,
`underscore (${escape('_')})`,
],
re: /[^\w\-]/dgv,
},
[Event.WhiteSpace]: {
ruleId: 'illegal-whitespace-character',
reason: 'this character cannot be used as a whitespace character',
fatal: Severity.Warning,
expected: [
`space (${escape(' ')})`,
`tab (${escape('\t')})`,
],
re: /[^ \t]/dgv,
},
[Event.NewLine]: {
ruleId: 'illegal-newline-character',
reason: 'this character cannot be used as a newline sequence',
fatal: Severity.Warning,
expected: [
`LF (${escape('\n')})`,
`CRLF (${escape('\r\n')})`,
],
re: /^(?!\r?\n).*/dgv,
},
};
/** Represents the given string as a sequence of Unicode code points. */
function escape(input) {
return [...input]
.map((ch) => {
return String.raw `\u{${ch.codePointAt(0).toString(16).toUpperCase().padStart(4, '0')}}`;
})
.join('');
}
/* eslint @typescript-eslint/prefer-readonly-parameter-types: off */
//# sourceMappingURL=events.js.map