Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 2x 2x 24x 2x 22x 16x 16x 16x 6x 6x 6x 22x 22x 42x 2x 40x 27x 18x 27x 10x 13x 3x 3x 13x 23x 23x 12x 23x 23x 11x 9x 9x 23x 2x | import {CommentCallback, Parser, ParserOptions, PrefixCallback, Quad} from 'n3';
import { EventEmitter } from "events";
export type GroupedQuadsCallback = Function; //function (Error|undefined, Quad[][]):void;
export type GroupParserCallbacks = {
onGroupedQuads: GroupedQuadsCallback;
onComment: CommentCallback;
onPrefix: PrefixCallback;
}
export default class GroupedParser {
private _parser: Parser;
constructor(options: ParserOptions) {
this._parser = new Parser(options);
}
/**
*
*/
parse(input: string | EventEmitter, groupCallback?: GroupParserCallbacks|GroupedQuadsCallback): void{
// The second parameter accepts an object { onGrouped: ..., onPrefix: ..., onComment: ...}
// As a second and third parameter it still accepts a separate quadCallback and prefixCallback for backward compatibility as well
let onGroupedQuads: GroupedQuadsCallback, onPrefix: PrefixCallback, onComment: CommentCallback;
if (groupCallback && (
(groupCallback as GroupParserCallbacks).onGroupedQuads !== undefined ||
(groupCallback as GroupParserCallbacks).onComment !== undefined ||
(groupCallback as GroupParserCallbacks).onPrefix !== undefined )) {
onGroupedQuads = (groupCallback as GroupParserCallbacks).onGroupedQuads;
onPrefix = (groupCallback as GroupParserCallbacks).onPrefix;
onComment = (groupCallback as GroupParserCallbacks).onComment;
}
else if (groupCallback) {
onGroupedQuads = groupCallback as GroupedQuadsCallback;
onComment = () => {};
}
let groups:Map<string, Quad[]> = new Map();
this._parser.parse(input, {
onQuad: (error, quad) => {
if (error) {
onGroupedQuads(error);
}
else if (quad) {
groups.forEach(group => {
group.push(quad);
});
// If however there were no groups...
if (groups.size === 0) {
// Output a single quad group
onGroupedQuads(null,[quad]);
}
} else {
// This is the end, just output the remaining groups if they weren’t closed
groups.forEach( (group, key) => {
onGroupedQuads(null, group);
groups.delete(key);
});
onGroupedQuads(null, null);
}
},
onComment: (comment: string) => {
// Parse comment and try to find a start pragma
const foundBegin = comment.match(/\s*@group begin\s*(.*)\s*/);
// When begin is found multiple times, it is going to reset the current set of quads.
if (foundBegin && foundBegin[1] !== undefined) {
// If the group is named, then add it to the map
groups.set(foundBegin[1], []);
}
// Parse comment to find an end -- a second time the group is closed without opening it is just ignored
const foundEnd = comment.match(/\s*@group end\s*(.*)\s*/);
if (foundEnd && foundEnd[1] !== undefined) {
if (groups.has(foundEnd[1])){
onGroupedQuads(null, groups.get(foundEnd[1]));
groups.delete(foundEnd[1]);
}
}
onComment(comment);
},
onPrefix: onPrefix!
});
}
} |