@antv/t8
Version:
T8 is a text visualization solution for unstructured data within the AntV technology stack, and it is a declarative JSON Schema syntax that can be used to describe the content of data interpretation reports.
230 lines (224 loc) • 6.75 kB
JavaScript
;
var clarinet = require('clarinet');
var setValueByPath = require('./setValueByPath.js');
function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var clarinet__namespace = /*#__PURE__*/_interopNamespaceDefault(clarinet);
/**
* Create a new T8 Clarinet Parser instance
* Handles streaming JSON parsing for T8 narrative text specifications
*/
function createT8ClarinetParser() {
// State management using closure
var state = {
parser: new clarinet__namespace.CParser(),
buffer: '',
result: {},
isComplete: false,
error: undefined,
currentValue: null,
currentPath: [],
wrapperStructure: [],
};
/**
* Initialize the clarinet parser and set up event handlers
*/
var initializeParser = function () {
state.parser = new clarinet__namespace.CParser();
setupEventHandlers();
};
/**
* Clear all parser state
*/
var clearState = function () {
state = {
parser: new clarinet__namespace.CParser(),
buffer: '',
result: {},
isComplete: false,
error: undefined,
currentValue: null,
currentPath: [],
wrapperStructure: [],
};
};
/**
* Set up clarinet parser event handlers
*/
var setupEventHandlers = function () {
setupObjectHandlers();
setupArrayHandlers();
setupValueHandlers();
setupErrorHandlers();
};
/**
* Set up object-related event handlers
*/
var setupObjectHandlers = function () {
state.parser.onopenobject = function (key) {
handleObjectOpen(key);
};
state.parser.oncloseobject = function () {
handleObjectClose();
};
state.parser.onkey = function (key) {
state.currentPath.push(key);
};
};
/**
* Set up array-related event handlers
*/
var setupArrayHandlers = function () {
state.parser.onopenarray = function () {
handleArrayOpen();
};
state.parser.onclosearray = function () {
handleArrayClose();
};
};
/**
* Set up value and completion event handlers
*/
var setupValueHandlers = function () {
state.parser.onvalue = function (value) {
handleValue(value);
};
state.parser.onend = function () {
state.isComplete = true;
};
};
/**
* Set up error handling
*/
var setupErrorHandlers = function () {
state.parser.onerror = function (error) {
state.error = error.message;
};
};
/**
* Handle opening of an object
*/
var handleObjectOpen = function (key) {
var _a;
state.wrapperStructure.push('object');
setValueByPath.setValueByPath(state.result, state.currentPath, (_a = {}, _a[key] = undefined, _a));
state.currentPath.push(key);
};
/**
* Handle closing of an object
*/
var handleObjectClose = function () {
state.wrapperStructure.pop();
var currentArrayIndex = state.currentPath.pop();
// If we're inside an array, increment the index
if (state.wrapperStructure[state.wrapperStructure.length - 1] === 'array') {
state.currentPath.push(Number(currentArrayIndex) + 1);
}
};
/**
* Handle opening of an array
*/
var handleArrayOpen = function () {
state.wrapperStructure.push('array');
setValueByPath.setValueByPath(state.result, state.currentPath, []);
state.currentPath.push(0);
};
/**
* Handle closing of an array
*/
var handleArrayClose = function () {
state.wrapperStructure.pop();
state.currentPath.pop();
// Remove the parent key if we're not nested in another array
if (state.wrapperStructure[state.wrapperStructure.length - 1] !== 'array') {
state.currentPath.pop();
}
};
/**
* Handle value assignment
*/
var handleValue = function (value) {
state.currentValue = value;
setValueByPath.setValueByPath(state.result, state.currentPath, state.currentValue);
var currentArrayIndex = state.currentPath.pop();
// If we're inside an array, increment the index for the next element
if (state.wrapperStructure[state.wrapperStructure.length - 1] === 'array') {
state.currentPath.push(Number(currentArrayIndex) + 1);
}
};
// Initialize the parser
initializeParser();
// Return the public API
return {
/**
* Append JSON fragment for parsing
*/
append: function (chunk) {
state.buffer += chunk;
try {
state.parser.write(chunk);
}
catch (error) {
state.error = error instanceof Error ? error.message : 'Unknown error';
}
},
/**
* Get current parse result
*/
getResult: function () { return ({
document: state.result,
isComplete: state.isComplete,
error: state.error,
currentPath: state.currentPath,
}); },
/**
* Reset parser to initial state
*/
reset: function () {
clearState();
initializeParser();
},
/**
* Get current error message
*/
getError: function () { return state.error; },
};
}
// // Create a singleton instance for backward compatibility
// const parser = createT8ClarinetParser();
// /**
// * Parse T8 JSON fragment with streaming support
// */
// export function parseT8WithClarinet(
// fragment: string,
// options: {
// onError?: (error: string) => void;
// onComplete?: (result: T8ClarinetParseResult) => void;
// },
// ): () => void {
// parser.append(fragment);
// const result = parser.getResult();
// if (options.onComplete) {
// options.onComplete(result);
// }
// if (options.onError && result.error) {
// options.onError(result.error);
// }
// return parser.reset;
// }
exports.createT8ClarinetParser = createT8ClarinetParser;
//# sourceMappingURL=t8ClarinetParser.js.map