libxmq-lite
The TypeScript
api.
libxmq-lite: a small library for editors that support xmq. It parses xmq (with the same error messages as the C xmq), pretty prints it as xmq, renders it as syntax colored html, converts it to json and splits the source into tokens for syntax coloring. It has no dependencies and needs no DOM. The parsed document is opaque, there is no api to walk or change it (doc/DECISIONS.md D23). Embedded xml (<...> in xmq) is parsed like C does, so a text that is xml is read too. A port of the Java implementation of libxmq (org.libxmq), which gives the same output as the C implementation.
Examples:FormatXmq.ts HighlightXmq.ts ReportError.ts
Functions
parse(xmq, options?)Parse xmq into a document.
function parse(xmq: string, options: ParseOptions = {}): XMQDoc
Parse xmq into a document. A document can hold several nodes at the top, eg a = 1 b = 2, and keeps the entity references (eg ), the comments, the doctype and the namespace prefixes as they were written. Embedded xml (eg a { <b>x</b> }) is parsed like the C xmq parses it: each fragment is an xml document whose root element is added, after the comments and processing instructions before it; its whitespace-only text between elements is dropped (libxml2 NOBLANKS) and a cdata section is text. So xml text (one root element, after an optional xml declaration and doctype, which are dropped, and comments) parses too, eg parse("<car><model>Volvo</model></car>") prints as car { model = Volvo }.
Parameters
xmq- The xmq text.
options- The source name for the error messages.
Returns
- The document.
Throws
XMQParseError- If the xmq cannot be parsed, with the same message as the C xmq. Embedded xml that is not well formed is "error parsing xml" at its <.
parseErrorToString(code)xmqParseErrorToString in C (src/main/c/parts/xmq_internals.c).
function parseErrorToString(code: ParseErrorCode): string
xmqParseErrorToString in C (src/main/c/parts/xmq_internals.c).
print(doc, options?)Print a document as xmq, like xmq FILE to-xmq: pretty printed with the indentation of the options, or with compact: true in the compact form on one line.
function print(doc: XMQDoc, options: PrintOptions = {}): string
Print a document as xmq, like xmq FILE to-xmq: pretty printed with the indentation of the options, or with compact: true in the compact form on one line.
Parameters
doc- The document.
options- The indentation, the escapes and compact, as the to-xmq options of xmq.
Returns
- The xmq text, without a final newline.
Throws
TypeError- If doc is not a document made by parse.
renderHtml(doc, options?)Render a document as syntax colored xmq in html, like xmq FILE render-html --nostyle: the xmq that print gives (the same options), inside a <pre class="xmq xmq_auto">, every part in an element of its kind, eg <xmqEN>car</xmqEN> for an element name, <xmqC> a comment, <xmqQ> a quote.
function renderHtml(doc: XMQDoc, options: RenderHtmlOptions = {}): string
Render a document as syntax colored xmq in html, like xmq FILE render-html --nostyle: the xmq that print gives (the same options), inside a
<pre class="xmq xmq_auto">, every part in an element of its kind, eg <xmqEN>car</xmqEN> for an element name, <xmqC> a comment, <xmqQ> a quote. The colors come from the style sheet of renderHtmlStyle, add it once to the page. With the option page: true the result is a whole html page with the style sheet, like xmq FILE render-html. Parameters
doc- The document.
options- The options of print, and theme, bg, id, class and page.
Returns
- The html, without a final newline.
Throws
Error- If the theme is not valid.
TypeError- If doc is not a document made by parse.
renderHtmlStyle(options?)The css of the html rendering, like xmq render-html --onlystyle: the colors of the theme for the dark and the light background, following the preference of the browser (prefers-color-scheme) for the class xmq_auto, forced for xmq_dark and xmq_light.
function renderHtmlStyle(options: RenderHtmlOptions = {}): string
The css of the html rendering, like xmq render-html --onlystyle: the colors of the theme for the dark and the light background, following the preference of the browser (prefers-color-scheme) for the class xmq_auto, forced for xmq_dark and xmq_light. Put it in a
<style> element of the page that shows the result of renderHtml or tokenizeHtml. Parameters
options- The theme.
Returns
- The css.
Throws
Error- If the theme is not valid.
toJson(doc, options?)Convert a document to a javascript value with the json conventions of xmq, the value of the json that xmq FILE to-json prints: eg car { model = Volvo year = 1999 } gives { _: "car", model: "Volvo", year: 1999 }.
function toJson(doc: XMQDoc, options: JsonOptions = {}): JsonValue | undefined
Convert a document to a javascript value with the json conventions of xmq, the value of the json that xmq FILE to-json prints: eg car { model = Volvo year = 1999 } gives { _: "car", model: "Volvo", year: 1999 }. The root element name is "_", attributes are "_name", repeated names get [n], text among elements is "|", a comment "//". Numbers become numbers, true false null become booleans and null. Where xmq prints text that is not one json value: a comment, text or entity at the top or in an array is an object with its key, eg { "//": "comment" }, and several values at the top (eg a processing instruction before the root element) are an array.
Parameters
doc- The document.
options- onWarning.
Returns
- The value, undefined for an empty document.
Throws
TypeError- If doc is not a document made by parse.
tokenizeHtml(xmq, options?)Syntax color an xmq text without pretty printing it, like xmq FILE tokenize --type=html: the text exactly as it is (its whitespace, comments and quoting), every token in an element of its kind inside a <pre class="xmq xmq_auto">, the same elements as renderHtml.
function tokenizeHtml(xmq: string, options: TokenizeHtmlOptions = {}): string
Syntax color an xmq text without pretty printing it, like xmq FILE tokenize --type=html: the text exactly as it is (its whitespace, comments and quoting), every token in an element of its kind inside a
<pre class="xmq xmq_auto">, the same elements as renderHtml. The colors come from the style sheet of renderHtmlStyle, add it once to the page. Parameters
xmq- The xmq text.
options- The source for the error messages, and theme, bg, id and class.
Returns
- The html.
Throws
XMQParseError- If the xmq cannot be tokenized, the same message as xmq.
Error- If the text is xml, html or json, or the theme is not valid.
tokens(xmq, options?)The tokens of an xmq text, for syntax coloring in an editor: every part of the text in order, whitespace and comments included, with its kind and its position, the same tokens as xmq FILE tokenize --type=debugtokens.
function tokens(xmq: string, options: ParseOptions = {}): XMQToken[]
The tokens of an xmq text, for syntax coloring in an editor: every part of the text in order, whitespace and comments included, with its kind and its position, the same tokens as xmq FILE tokenize --type=debugtokens. The positions start and end are UTF-16 code units (string indexes), the positions of editors; line and column are those of the error messages.
Parameters
xmq- The xmq text.
options- The source name for the error messages.
Returns
- The tokens.
Throws
XMQParseError- If the xmq cannot be tokenized, the same message as xmq. The error has the offset of the error, for a mark in the editor.
HtmlColorOptions
interface HtmlColorOptions
The colors of the html: the options of renderHtml, renderHtmlStyle and tokenizeHtml.
themeThe colors, like --theme= of xmq: a list of NAME=#rrggbb separated by colons, with _B (bold) and _U (underline) after a color, eg "EN=#ff0000:EK=#00ff00_B".
theme?: string;
The colors, like --theme= of xmq: a list of NAME=#rrggbb separated by colons, with _B (bold) and _U (underline) after a color, eg "EN=#ff0000:EK=#00ff00_B". The names are C (comment), Q (quote), E (entity), NS (namespace), EN (element name), EK (element key), EKV (element value), AK (attribute key), AKV (attribute value), CP (compound parentheses), NSD (namespace declaration), UW (unicode whitespace), XSL (the xsl namespace), XML (embedded xml), FG and BG (the text and background of the pre). A theme can start with dark+ or light+ to set the colors of one background only, and two themes can be given separated by a comma. The default colors are those of xmq.
bgThe background: "auto" (the default) follows the preference of the browser (prefers-color-scheme), "dark" and "light" force it (--bg=dark|light), the pre gets the class xmq_auto, xmq_dark or xmq_light.
bg?: "auto" | "dark" | "light";
The background: "auto" (the default) follows the preference of the browser (prefers-color-scheme), "dark" and "light" force it (--bg=dark|light), the pre gets the class xmq_auto, xmq_dark or xmq_light.
idThe id of the pre element (--id=).
id?: string;
The id of the pre element (--id=).
classAn extra class of the pre element (--class=).
class?: string;
An extra class of the pre element (--class=).
JsonOptions
interface JsonOptions
The options of toJson.
onWarningCalled with the warnings that xmq prints on stderr, eg when an element name is lost in a json array.
onWarning?: (message: string) => void;
Called with the warnings that xmq prints on stderr, eg when an element name is lost in a json array. Default: no warnings.
JsonValue
ParseErrorCode
enum ParseErrorCode {
XMQ_ERROR_NONE = 0,
XMQ_ERROR_QUOTE_NOT_CLOSED = 4,
XMQ_ERROR_ENTITY_NOT_CLOSED = 5,
XMQ_ERROR_COMMENT_NOT_CLOSED = 6,
XMQ_ERROR_COMMENT_CLOSED_WITH_TOO_MANY_SLASHES = 7,
XMQ_ERROR_BODY_NOT_CLOSED = 8,
XMQ_ERROR_ATTRIBUTES_NOT_CLOSED = 9,
XMQ_ERROR_COMPOUND_NOT_CLOSED = 10,
XMQ_ERROR_COMPOUND_MAY_NOT_CONTAIN = 11,
XMQ_ERROR_QUOTE_CLOSED_WITH_TOO_MANY_QUOTES = 12,
XMQ_ERROR_UNEXPECTED_CLOSING_BRACE = 13,
XMQ_ERROR_EXPECTED_CONTENT_AFTER_EQUALS = 14,
XMQ_ERROR_UNEXPECTED_TAB = 15,
XMQ_ERROR_INVALID_CHAR = 16,
XMQ_ERROR_BAD_DOCTYPE = 17,
XMQ_ERROR_PARSING_XML = 28,
XMQ_ERROR_VALUE_CANNOT_START_WITH = 30,
XMQ_ERROR_XML_FRAGMENT_NOT_CLOSED = 37,
XMQ_ERROR_TOO_DEEP = 40,
XMQ_ERROR_EXCLUDED_FROM_BUILD = 64,
XMQ_WARNING_QUOTES_NEEDED = 100
}
The parse errors of the xmq parser, the same codes as XMQStatus in the C xmq.h and ParseErrorCode in Java, and the same messages as xmqParseErrorToString in C.
ParseOptions
interface ParseOptions
The options of parse.
sourceWhere the xmq comes from, eg a file name, used in the error messages.
source?: string;
Where the xmq comes from, eg a file name, used in the error messages. Default "-".
PrintOptions
interface PrintOptions
The options of print, like the to-xmq options of the xmq command line.
indentThe number of spaces to indent the content of an element, default 4 (--indent=n).
indent?: number;
The number of spaces to indent the content of an element, default 4 (--indent=n).
escapeNewlinesPrint newlines inside quotes as (--escape-newlines).
escapeNewlines?: boolean;
Print newlines inside quotes as (--escape-newlines).
escapeNon7bitPrint the characters above 126 as character references (--escape-non-7bit).
escapeNon7bit?: boolean;
Print the characters above 126 as character references (--escape-non-7bit).
compactPrint the compact form, on one line with as few spaces as possible, eg car{model=Volvo year=1999} (--compact).
compact?: boolean;
Print the compact form, on one line with as few spaces as possible, eg car{model=Volvo year=1999} (--compact). Newlines are always escaped as .
escapeTabsPrint tabs as 	 (--escape-tabs).
escapeTabs?: boolean;
Print tabs as 	 (--escape-tabs).
preferDoubleQuotesUse double quotes when single and double quotes are equally good (--prefer-double-quotes).
preferDoubleQuotes?: boolean;
Use double quotes when single and double quotes are equally good (--prefer-double-quotes).
RenderHtmlOptions
interface RenderHtmlOptions extends PrintOptions, HtmlColorOptions
The options of renderHtml and renderHtmlStyle, the render-html options of xmq.
pageRender a whole html page with the style sheet, like render-html without --nostyle.
page?: boolean;
Render a whole html page with the style sheet, like render-html without --nostyle. The default is only the pre element (--nostyle).
TokenizeHtmlOptions
interface TokenizeHtmlOptions extends HtmlColorOptions
The options of tokenizeHtml.
sourceWhere the xmq comes from, eg a file name, used in the error messages.
source?: string;
Where the xmq comes from, eg a file name, used in the error messages. Default "-".
XMQDoc
class XMQDoc
An xmq document parsed by parse. It is opaque: print, renderHtml and toJson read it.
XMQParseError
class XMQParseError extends Error
A parse error. The message is the same text as the C xmq prints (generate_state_error_message in C): source:line:col: error: what, followed by the line of the input and a ^ under the error. A warning that C prints before the error (perhaps you need more quotes) is part of the message.
codeThe error code.
readonly code: ParseErrorCode;
The error code.
lineThe line number, the first line is 1.
readonly line: number;
The line number, the first line is 1.
columnThe column in characters (code points), the first column is 1.
readonly column: number;
The column in characters (code points), the first column is 1.
offsetThe position of the error in the xmq text, in UTF-16 code units (a javascript string index, the unit of the positions of editors).
readonly offset: number;
The position of the error in the xmq text, in UTF-16 code units (a javascript string index, the unit of the positions of editors).
sourceWhere the input came from, eg a file name, "stdin" or "-i argument".
readonly source: string;
Where the input came from, eg a file name, "stdin" or "-i argument".
constructor(code, message, line, column, source, offset?)
constructor(code: ParseErrorCode, message: string, line: number, column: number, source: string, offset: number = 0);
XMQToken
interface XMQToken
A token of an xmq text.
kindThe kind of token.
kind: XMQTokenKind;
The kind of token.
startThe position of the first character in the text, in UTF-16 code units (a string index).
start: number;
The position of the first character in the text, in UTF-16 code units (a string index).
endThe position after the last character, in UTF-16 code units.
end: number;
The position after the last character, in UTF-16 code units.
lineThe line of the first character, the first line is 1.
line: number;
The line of the first character, the first line is 1.
columnThe column of the first character in characters (code points), the first column is 1.
column: number;
The column of the first character in characters (code points), the first column is 1.
XMQTokenKind
type XMQTokenKind = "whitespace" | "comment" | "comment_continuation" | "element_ns" | "element_name" | "element_key" | "attr_ns" | "attr_key" | "ns_declaration" | "ns_colon" | "equals" | "apar_left" | "apar_right" | "cpar_left" | "cpar_right" | "brace_left" | "brace_right" | "quote" | "element_value_quote" | "element_value_compound_quote" | "attr_value_quote" | "attr_value_compound_quote" | "entity" | "element_value_entity" | "element_value_compound_entity" | "attr_value_entity" | "attr_value_compound_entity" | "element_value_text" | "attr_value_text" | "embedded_xml";
The kind of a token, the names of xmq tokenize --type=debugtokens (the C token callbacks). The value kinds tell where the text is: an element value (key = value), an attribute value, or inside a compound value ( ... ). An embedded xml fragment (eg <b>x</b>) is one token, embedded_xml.