@faubulous/mentor-rdf-serializers 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +582 -0
- package/dist/base-serializer.d.ts +63 -0
- package/dist/base-serializer.d.ts.map +1 -0
- package/dist/base-serializer.js +144 -0
- package/dist/base-serializer.js.map +1 -0
- package/dist/base-token-formatter.d.ts +225 -0
- package/dist/base-token-formatter.d.ts.map +1 -0
- package/dist/base-token-formatter.js +521 -0
- package/dist/base-token-formatter.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/jsonld/serializer.d.ts +99 -0
- package/dist/jsonld/serializer.d.ts.map +1 -0
- package/dist/jsonld/serializer.js +282 -0
- package/dist/jsonld/serializer.js.map +1 -0
- package/dist/n3/formatter.d.ts +105 -0
- package/dist/n3/formatter.d.ts.map +1 -0
- package/dist/n3/formatter.js +444 -0
- package/dist/n3/formatter.js.map +1 -0
- package/dist/n3/serializer.d.ts +103 -0
- package/dist/n3/serializer.d.ts.map +1 -0
- package/dist/n3/serializer.js +134 -0
- package/dist/n3/serializer.js.map +1 -0
- package/dist/nquads/formatter.d.ts +44 -0
- package/dist/nquads/formatter.d.ts.map +1 -0
- package/dist/nquads/formatter.js +116 -0
- package/dist/nquads/formatter.js.map +1 -0
- package/dist/nquads/serializer.d.ts +38 -0
- package/dist/nquads/serializer.d.ts.map +1 -0
- package/dist/nquads/serializer.js +70 -0
- package/dist/nquads/serializer.js.map +1 -0
- package/dist/ntriples/formatter.d.ts +44 -0
- package/dist/ntriples/formatter.d.ts.map +1 -0
- package/dist/ntriples/formatter.js +116 -0
- package/dist/ntriples/formatter.js.map +1 -0
- package/dist/ntriples/serializer.d.ts +38 -0
- package/dist/ntriples/serializer.d.ts.map +1 -0
- package/dist/ntriples/serializer.js +79 -0
- package/dist/ntriples/serializer.js.map +1 -0
- package/dist/sorting/index.d.ts +5 -0
- package/dist/sorting/index.d.ts.map +1 -0
- package/dist/sorting/index.js +5 -0
- package/dist/sorting/index.js.map +1 -0
- package/dist/sorting/strategies.d.ts +46 -0
- package/dist/sorting/strategies.d.ts.map +1 -0
- package/dist/sorting/strategies.js +259 -0
- package/dist/sorting/strategies.js.map +1 -0
- package/dist/sparql/formatter.d.ts +103 -0
- package/dist/sparql/formatter.d.ts.map +1 -0
- package/dist/sparql/formatter.js +764 -0
- package/dist/sparql/formatter.js.map +1 -0
- package/dist/token-serializer.d.ts +96 -0
- package/dist/token-serializer.d.ts.map +1 -0
- package/dist/token-serializer.js +456 -0
- package/dist/token-serializer.js.map +1 -0
- package/dist/trig/formatter.d.ts +99 -0
- package/dist/trig/formatter.d.ts.map +1 -0
- package/dist/trig/formatter.js +418 -0
- package/dist/trig/formatter.js.map +1 -0
- package/dist/trig/serializer.d.ts +39 -0
- package/dist/trig/serializer.d.ts.map +1 -0
- package/dist/trig/serializer.js +170 -0
- package/dist/trig/serializer.js.map +1 -0
- package/dist/turtle/formatter.d.ts +56 -0
- package/dist/turtle/formatter.d.ts.map +1 -0
- package/dist/turtle/formatter.js +325 -0
- package/dist/turtle/formatter.js.map +1 -0
- package/dist/turtle/serializer.d.ts +87 -0
- package/dist/turtle/serializer.d.ts.map +1 -0
- package/dist/turtle/serializer.js +343 -0
- package/dist/turtle/serializer.js.map +1 -0
- package/dist/types.d.ts +323 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +14 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +157 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +446 -0
- package/dist/utils.js.map +1 -0
- package/package.json +73 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { escapeIri, escapeLocalName, escapeString, findPrefix, isDecimal, isDouble, isInteger, mergeOptions, needsLongString, RDF_TYPE, XSD_BOOLEAN, XSD_DECIMAL, XSD_DOUBLE, XSD_INTEGER, XSD_STRING } from './utils.js';
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for RDF serializers.
|
|
4
|
+
* Provides common functionality for serializing terms and literals.
|
|
5
|
+
*/
|
|
6
|
+
export class BaseSerializer {
|
|
7
|
+
/**
|
|
8
|
+
* Whether this serializer supports prefixed names.
|
|
9
|
+
*/
|
|
10
|
+
supportsPrefixes = false;
|
|
11
|
+
/**
|
|
12
|
+
* Whether this serializer supports the 'a' shorthand for rdf:type.
|
|
13
|
+
*/
|
|
14
|
+
supportsRdfTypeShorthand = false;
|
|
15
|
+
/**
|
|
16
|
+
* Whether this serializer supports RDF 1.2 features (triple terms, reifiers, annotations).
|
|
17
|
+
*/
|
|
18
|
+
supportsRdf12 = false;
|
|
19
|
+
/**
|
|
20
|
+
* Serializes quads with formatting, returning detailed result.
|
|
21
|
+
*/
|
|
22
|
+
format(quads, options) {
|
|
23
|
+
return {
|
|
24
|
+
output: this.serialize(quads, options)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Serializes a named node (IRI).
|
|
29
|
+
*/
|
|
30
|
+
serializeNamedNode(node, options) {
|
|
31
|
+
// Check for rdf:type shorthand
|
|
32
|
+
if (this.supportsRdfTypeShorthand && options.useRdfTypeShorthand && node.value === RDF_TYPE) {
|
|
33
|
+
return 'a';
|
|
34
|
+
}
|
|
35
|
+
// Try to use a prefix
|
|
36
|
+
if (this.supportsPrefixes && Object.keys(options.prefixes).length > 0) {
|
|
37
|
+
const prefixMatch = findPrefix(node.value, options.prefixes);
|
|
38
|
+
if (prefixMatch) {
|
|
39
|
+
const escapedLocalName = escapeLocalName(prefixMatch.localName);
|
|
40
|
+
return `${prefixMatch.prefix}:${escapedLocalName}`;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Fall back to full IRI
|
|
44
|
+
return `<${escapeIri(node.value)}>`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Serializes a blank node.
|
|
48
|
+
*/
|
|
49
|
+
serializeBlankNode(node, _options) {
|
|
50
|
+
return `_:${node.value}`;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Serializes a literal value.
|
|
54
|
+
*/
|
|
55
|
+
serializeLiteral(literal, options) {
|
|
56
|
+
const value = literal.value;
|
|
57
|
+
const datatype = literal.datatype?.value;
|
|
58
|
+
const language = literal.language;
|
|
59
|
+
// Handle language-tagged literals
|
|
60
|
+
if (language) {
|
|
61
|
+
const escapedValue = escapeString(value);
|
|
62
|
+
return `"${escapedValue}"@${language}`;
|
|
63
|
+
}
|
|
64
|
+
// Handle boolean shortcuts (for Turtle-family formats)
|
|
65
|
+
if (this.supportsPrefixes && datatype === XSD_BOOLEAN) {
|
|
66
|
+
if (value === 'true' || value === '1') {
|
|
67
|
+
return 'true';
|
|
68
|
+
}
|
|
69
|
+
if (value === 'false' || value === '0') {
|
|
70
|
+
return 'false';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Handle numeric shortcuts (for Turtle-family formats)
|
|
74
|
+
if (this.supportsPrefixes) {
|
|
75
|
+
if (datatype === XSD_INTEGER && isInteger(value)) {
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
if (datatype === XSD_DECIMAL && isDecimal(value)) {
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
if (datatype === XSD_DOUBLE && isDouble(value)) {
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Check if we need long string quoting
|
|
86
|
+
const useLongString = this.supportsPrefixes && needsLongString(value);
|
|
87
|
+
const quote = useLongString ? '"""' : '"';
|
|
88
|
+
const escapedValue = escapeString(value, useLongString);
|
|
89
|
+
// xsd:string is the default, no need to serialize it
|
|
90
|
+
if (!datatype || datatype === XSD_STRING) {
|
|
91
|
+
return `${quote}${escapedValue}${quote}`;
|
|
92
|
+
}
|
|
93
|
+
// Serialize with datatype
|
|
94
|
+
const datatypeStr = this.serializeNamedNode({ termType: 'NamedNode', value: datatype, equals: () => false }, options);
|
|
95
|
+
return `${quote}${escapedValue}${quote}^^${datatypeStr}`;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Serializes a variable (SPARQL).
|
|
99
|
+
*/
|
|
100
|
+
serializeVariable(variable, _options) {
|
|
101
|
+
return `?${variable.value}`;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Serializes an RDF 1.2 Triple Term.
|
|
105
|
+
* Syntax: <<( subject predicate object )>>
|
|
106
|
+
*/
|
|
107
|
+
serializeTripleTerm(tripleTerm, options) {
|
|
108
|
+
if (!this.supportsRdf12) {
|
|
109
|
+
throw new Error('Triple terms are not supported in this format');
|
|
110
|
+
}
|
|
111
|
+
const subject = this.serializeTerm(tripleTerm.subject, options);
|
|
112
|
+
const predicate = this.serializeTerm(tripleTerm.predicate, options);
|
|
113
|
+
const object = this.serializeTerm(tripleTerm.object, options);
|
|
114
|
+
return `<<( ${subject} ${predicate} ${object} )>>`;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Serializes any RDF term.
|
|
118
|
+
*/
|
|
119
|
+
serializeTerm(term, options) {
|
|
120
|
+
switch (term.termType) {
|
|
121
|
+
case 'NamedNode':
|
|
122
|
+
return this.serializeNamedNode(term, options);
|
|
123
|
+
case 'BlankNode':
|
|
124
|
+
return this.serializeBlankNode(term, options);
|
|
125
|
+
case 'Literal':
|
|
126
|
+
return this.serializeLiteral(term, options);
|
|
127
|
+
case 'Variable':
|
|
128
|
+
return this.serializeVariable(term, options);
|
|
129
|
+
case 'DefaultGraph':
|
|
130
|
+
return '';
|
|
131
|
+
case 'TripleTerm':
|
|
132
|
+
return this.serializeTripleTerm(term, options);
|
|
133
|
+
default:
|
|
134
|
+
throw new Error(`Unknown term type: ${term.termType}`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Gets merged options with defaults.
|
|
139
|
+
*/
|
|
140
|
+
getOptions(options) {
|
|
141
|
+
return mergeOptions(options);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=base-serializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-serializer.js","sourceRoot":"","sources":["../src/base-serializer.ts"],"names":[],"mappings":"AAUA,OAAO,EAEH,SAAS,EACT,eAAe,EACf,YAAY,EACZ,UAAU,EACV,SAAS,EACT,QAAQ,EACR,SAAS,EAET,YAAY,EACZ,eAAe,EACf,QAAQ,EACR,WAAW,EACX,WAAW,EACX,UAAU,EACV,WAAW,EACX,UAAU,EACb,MAAM,YAAY,CAAC;AAEpB;;;GAGG;AACH,MAAM,OAAgB,cAAc;IAGhC;;OAEG;IACO,gBAAgB,GAAY,KAAK,CAAC;IAE5C;;OAEG;IACO,wBAAwB,GAAY,KAAK,CAAC;IAEpD;;OAEG;IACO,aAAa,GAAY,KAAK,CAAC;IAYzC;;OAEG;IACH,MAAM,CAAC,KAAiC,EAAE,OAA2B;QACjE,OAAO;YACH,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC;SACzC,CAAC;IACN,CAAC;IAED;;OAEG;IACO,kBAAkB,CACxB,IAAe,EACf,OAAoC;QAEpC,+BAA+B;QAC/B,IAAI,IAAI,CAAC,wBAAwB,IAAI,OAAO,CAAC,mBAAmB,IAAI,IAAI,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC1F,OAAO,GAAG,CAAC;QACf,CAAC;QAED,sBAAsB;QACtB,IAAI,IAAI,CAAC,gBAAgB,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACpE,MAAM,WAAW,GAAG,UAAU,CAAC,IAAI,CAAC,KAAK,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;YAC7D,IAAI,WAAW,EAAE,CAAC;gBACd,MAAM,gBAAgB,GAAG,eAAe,CAAC,WAAW,CA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a,CAAC,UAAU,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QACpE,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAE9D,OAAO,OAAO,OAAO,IAAI,SAAS,IAAI,MAAM,MAAM,CAAC;IACvD,CAAC;IAED;;OAEG;IACO,aAAa,CACnB,IAAsB,EACtB,OAAoC;QAEpC,QAAQ,IAAI,CAAC,QAAQ,EAAE,CAAC;YACpB,KAAK,WAAW;gBACZ,OAAO,IAAI,CAAC,kBAAkB,CAAC,IAAiB,EAAE,OAAO,CAAC,CAAC;YAC/D,KAAK,WAAW;gBACZ,OAAO,IAAI,CAAC,kBAAkB,CAAC,IAAiB,EAAE,OAAO,CAAC,CAAC;YAC/D,KAAK,SAAS;gBACV,OAAO,IAAI,CAAC,gBAAgB,CAAC,IAAe,EAAE,OAAO,CAAC,CAAC;YAC3D,KAAK,UAAU;gBACX,OAAO,IAAI,CAAC,iBAAiB,CAAC,IAAgB,EAAE,OAAO,CAAC,CAAC;YAC7D,KAAK,cAAc;gBACf,OAAO,EAAE,CAAC;YACd,KAAK,YAAY;gBACb,OAAO,IAAI,CAAC,mBAAmB,CAAC,IAAkB,EAAE,OAAO,CAAC,CAAC;YACjE;gBACI,MAAM,IAAI,KAAK,CAAC,sBAAuB,IAAa,CAAC,QAAQ,EAAE,CAAC,CAAC;QACzE,CAAC;IACL,CAAC;IAED;;OAEG;IACO,UAAU,CAAC,OAA2B;QAC5C,OAAO,YAAY,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;CACJ"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import type { IToken } from 'chevrotain';
|
|
2
|
+
import type { SerializerOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Metadata attached to each token during the analysis pass.
|
|
5
|
+
* This captures source-layout information for the emit pass.
|
|
6
|
+
*/
|
|
7
|
+
export interface TokenAnnotation {
|
|
8
|
+
/** Source had a newline between this token and the previous non-WS token. */
|
|
9
|
+
sourceNewlineBefore: boolean;
|
|
10
|
+
/** Source had a blank line (2+ newlines) between this token and the previous non-WS token. */
|
|
11
|
+
sourceBlankLineBefore: boolean;
|
|
12
|
+
/** Number of source newlines between this token and the previous non-WS token. */
|
|
13
|
+
sourceLineGap: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A token together with its analysis annotation.
|
|
17
|
+
*/
|
|
18
|
+
export interface AnnotatedToken {
|
|
19
|
+
token: IToken;
|
|
20
|
+
annotation: TokenAnnotation;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Scope entry representing a nesting level.
|
|
24
|
+
*/
|
|
25
|
+
export interface Scope {
|
|
26
|
+
/** Type of scope. */
|
|
27
|
+
type: 'curly' | 'bracket' | 'paren';
|
|
28
|
+
/** Whether the block is formatted inline (no line breaks). */
|
|
29
|
+
isInline: boolean;
|
|
30
|
+
/** Whether the block spans multiple source lines. */
|
|
31
|
+
isMultiLine: boolean;
|
|
32
|
+
/** The indent level when this scope was entered. */
|
|
33
|
+
indentLevel: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Base formatting context with fields shared by all token-based formatters.
|
|
37
|
+
* Subclasses extend this to add format-specific state.
|
|
38
|
+
*/
|
|
39
|
+
export interface BaseFormatterContext {
|
|
40
|
+
/** Accumulated output parts. */
|
|
41
|
+
parts: string[];
|
|
42
|
+
/** Current indent nesting level. */
|
|
43
|
+
indentLevel: number;
|
|
44
|
+
/** Current line length (for wrapping). */
|
|
45
|
+
currentLineLength: number;
|
|
46
|
+
/** The scope stack. */
|
|
47
|
+
scopeStack: Scope[];
|
|
48
|
+
/** A newline should be emitted before the next token. */
|
|
49
|
+
needsNewline: boolean;
|
|
50
|
+
/** A blank line should be emitted before the next token. */
|
|
51
|
+
needsBlankLine: boolean;
|
|
52
|
+
/** A space should be emitted before the next token. */
|
|
53
|
+
needsSpace: boolean;
|
|
54
|
+
/** Whether the pending newline originated from source positions. */
|
|
55
|
+
sourceNewline: boolean;
|
|
56
|
+
lastToken: IToken | null;
|
|
57
|
+
lastNonWsToken: IToken | null;
|
|
58
|
+
lastWasNewline: boolean;
|
|
59
|
+
lastWasComment: boolean;
|
|
60
|
+
/** Whether we are inside a prefix/base declaration. */
|
|
61
|
+
inPrefix: boolean;
|
|
62
|
+
/** Triple position counter (0=subject, 1=predicate, 2=object). */
|
|
63
|
+
triplePosition: number;
|
|
64
|
+
/** Last-seen subject image (for blank line insertion). */
|
|
65
|
+
lastSubject: string | null;
|
|
66
|
+
/** Whether the current statement should be rendered on a single line. */
|
|
67
|
+
inlineStatement: boolean;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Options common to all token-based formatters.
|
|
71
|
+
*/
|
|
72
|
+
export interface BaseFormatterOptions extends SerializerOptions {
|
|
73
|
+
/** Add a space before punctuation (. and ;). Default: false. */
|
|
74
|
+
spaceBeforePunctuation?: boolean;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Annotates a token array with source-layout metadata.
|
|
78
|
+
*
|
|
79
|
+
* This is the "analysis pass" that records where the source had newlines
|
|
80
|
+
* and blank lines. The emit pass can then decide whether to honour them.
|
|
81
|
+
*
|
|
82
|
+
* @param tokens The main token stream (no WS / comment groups).
|
|
83
|
+
* @param comments Comment tokens extracted from lexer groups.
|
|
84
|
+
* @returns An array of annotated tokens (comments merged in order) with
|
|
85
|
+
* annotations describing source-layout between adjacent tokens.
|
|
86
|
+
*/
|
|
87
|
+
export declare function annotateTokens(tokens: IToken[], comments?: IToken[]): AnnotatedToken[];
|
|
88
|
+
/**
|
|
89
|
+
* Abstract base class for all token-based RDF/SPARQL formatters.
|
|
90
|
+
*
|
|
91
|
+
* Provides:
|
|
92
|
+
* - Two-pass architecture: annotateTokens (analysis) + emit (subclass).
|
|
93
|
+
* - A unified scope stack for { }, [ ], ( ).
|
|
94
|
+
* - Output building helpers (addPart, getIndent, line length tracking).
|
|
95
|
+
* - Shared handlers for brackets, punctuation, comments, spacing.
|
|
96
|
+
* - Source newline / blank line preservation helpers.
|
|
97
|
+
*
|
|
98
|
+
* Subclasses override hooks (e.g. handleOpenBrace, handleMajorClause) to add
|
|
99
|
+
* syntax-specific behaviour.
|
|
100
|
+
*/
|
|
101
|
+
export declare abstract class BaseTokenFormatter<TContext extends BaseFormatterContext = BaseFormatterContext, TOptions extends BaseFormatterOptions = BaseFormatterOptions> {
|
|
102
|
+
/** Merge user options with defaults. Must be implemented by subclasses. */
|
|
103
|
+
protected abstract getOptions(options?: TOptions): Required<TOptions>;
|
|
104
|
+
/** Create the initial context. Subclasses extend the base context. */
|
|
105
|
+
protected abstract createContext(opts: Required<TOptions>): TContext;
|
|
106
|
+
/**
|
|
107
|
+
* Format a token's image (e.g. keyword casing).
|
|
108
|
+
* Default: return unchanged.
|
|
109
|
+
*/
|
|
110
|
+
protected formatTokenValue(token: IToken, _opts: Required<TOptions>): string;
|
|
111
|
+
protected isKeyword(token: IToken): boolean;
|
|
112
|
+
protected isLowercaseOnly(token: IToken): boolean;
|
|
113
|
+
protected isTermToken(token: IToken): boolean;
|
|
114
|
+
protected isFunctionKeyword(token: IToken): boolean;
|
|
115
|
+
protected isMajorClauseKeyword(token: IToken): boolean;
|
|
116
|
+
protected isNewlineKeyword(token: IToken): boolean;
|
|
117
|
+
protected isOpeningBracket(token: IToken): boolean;
|
|
118
|
+
protected isClosingBracket(token: IToken): boolean;
|
|
119
|
+
/** Push a new scope. */
|
|
120
|
+
protected pushScope(ctx: TContext, type: Scope['type'], isInline: boolean, isMultiLine: boolean): void;
|
|
121
|
+
/** Pop the current scope. Returns it, or undefined if the stack is empty. */
|
|
122
|
+
protected popScope(ctx: TContext): Scope | undefined;
|
|
123
|
+
/** The current innermost scope, or undefined. */
|
|
124
|
+
protected currentScope(ctx: TContext): Scope | undefined;
|
|
125
|
+
/** Whether we are inside a bracket scope (blank-node property list). */
|
|
126
|
+
protected inBracketScope(ctx: TContext): boolean;
|
|
127
|
+
/** Append text to the output and track line length. */
|
|
128
|
+
protected addPart(ctx: TContext, text: string, lineEnd: string, forceNewline?: boolean): void;
|
|
129
|
+
/** Get indentation string for a level. */
|
|
130
|
+
protected getIndent(level: number, indent: string): string;
|
|
131
|
+
/** Check if a wrap is needed. */
|
|
132
|
+
protected shouldWrap(ctx: TContext, nextLength: number, maxLineWidth: number): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Apply the source-layout annotation to the context flags.
|
|
135
|
+
*
|
|
136
|
+
* Call this early in the per-token loop, before any formatting decisions.
|
|
137
|
+
* It sets `needsNewline`, `needsBlankLine`, and `sourceNewline` based on
|
|
138
|
+
* what the source had, but only if the formatter hasn't already decided
|
|
139
|
+
* on something stronger.
|
|
140
|
+
*/
|
|
141
|
+
protected applySourceLayout(ctx: TContext, ann: TokenAnnotation): void;
|
|
142
|
+
/**
|
|
143
|
+
* Scans forward from `startIndex` to the statement-terminating period
|
|
144
|
+
* (at nesting depth 0) and determines whether the source had the
|
|
145
|
+
* entire statement on a single line.
|
|
146
|
+
*
|
|
147
|
+
* @returns The approximate inline character-length of the statement,
|
|
148
|
+
* or `-1` if the source contained newlines within the statement
|
|
149
|
+
* (meaning the author intended multi-line layout).
|
|
150
|
+
*
|
|
151
|
+
* Nested brackets `[]` and parentheses `()` are traversed but their
|
|
152
|
+
* tokens are counted towards the total length. Comments force multi-line.
|
|
153
|
+
*/
|
|
154
|
+
protected calculateStatementLength(tokens: IToken[], startIndex: number): number;
|
|
155
|
+
/**
|
|
156
|
+
* Evaluates whether the current statement (starting at `startIndex`)
|
|
157
|
+
* should be rendered inline, and sets `ctx.inlineStatement` accordingly.
|
|
158
|
+
*
|
|
159
|
+
* Call this when a subject is detected (triplePosition === 0).
|
|
160
|
+
*
|
|
161
|
+
* @param ctx The formatting context.
|
|
162
|
+
* @param tokens The full token stream.
|
|
163
|
+
* @param startIndex Index of the subject token.
|
|
164
|
+
* @param indent The indent string for one level.
|
|
165
|
+
* @param maxLineWidth The configured maximum line width (0 = no limit).
|
|
166
|
+
*/
|
|
167
|
+
protected detectInlineStatement(ctx: TContext, tokens: IToken[], startIndex: number, indent: string, maxLineWidth: number): void;
|
|
168
|
+
/**
|
|
169
|
+
* Handles an opening square bracket `[` (blank-node property list).
|
|
170
|
+
*
|
|
171
|
+
* Pushes a 'bracket' scope, increments indent.
|
|
172
|
+
*/
|
|
173
|
+
protected handleOpenBracket(ctx: TContext, token: IToken, lineEnd: string, indent: string, prettyPrint: boolean): void;
|
|
174
|
+
/**
|
|
175
|
+
* Handles a closing square bracket `]`.
|
|
176
|
+
*
|
|
177
|
+
* Pops the 'bracket' scope and places `]` on its own line at the outer indent.
|
|
178
|
+
*/
|
|
179
|
+
protected handleCloseBracket(ctx: TContext, token: IToken, lineEnd: string, indent: string, prettyPrint: boolean): void;
|
|
180
|
+
/**
|
|
181
|
+
* Handles a semicolon `;` (predicate-object list separator).
|
|
182
|
+
*
|
|
183
|
+
* Inside bracket scopes: continues at the same indent.
|
|
184
|
+
* Outside: adds one extra indent for predicate continuations.
|
|
185
|
+
*/
|
|
186
|
+
protected handleSemicolon(ctx: TContext, lineEnd: string, indent: string, prettyPrint: boolean, spaceBeforePunctuation: boolean): void;
|
|
187
|
+
/**
|
|
188
|
+
* Handles a period `.` (statement terminator).
|
|
189
|
+
*/
|
|
190
|
+
protected handlePeriod(ctx: TContext, lineEnd: string, prettyPrint: boolean, spaceBeforePunctuation: boolean): void;
|
|
191
|
+
/**
|
|
192
|
+
* Handles a comma `,` (object-list separator).
|
|
193
|
+
*/
|
|
194
|
+
protected handleComma(ctx: TContext, lineEnd: string): void;
|
|
195
|
+
/**
|
|
196
|
+
* Handles a comment token.
|
|
197
|
+
*/
|
|
198
|
+
protected handleComment(ctx: TContext, comment: IToken, lineEnd: string, indent: string): void;
|
|
199
|
+
/**
|
|
200
|
+
* Handles generic token spacing (newlines and spaces before a token).
|
|
201
|
+
*
|
|
202
|
+
* This is the "default" spacing logic. Subclasses may override or call
|
|
203
|
+
* super for specific token types (e.g. datatype, lang tags, function calls).
|
|
204
|
+
*/
|
|
205
|
+
protected handleTokenSpacing(ctx: TContext, token: IToken, lineEnd: string, indent: string): void;
|
|
206
|
+
/**
|
|
207
|
+
* Handles prefix/base IRI completion.
|
|
208
|
+
*/
|
|
209
|
+
protected handlePrefixIri(ctx: TContext, value: string, lineEnd: string, indent: string, prettyPrint: boolean): void;
|
|
210
|
+
/**
|
|
211
|
+
* Checks if a parenthesis block (starting at `(` at index) contains
|
|
212
|
+
* source newlines between the open and close parens.
|
|
213
|
+
*/
|
|
214
|
+
protected isParenBlockMultiLine(tokens: IToken[], openIndex: number): boolean;
|
|
215
|
+
/**
|
|
216
|
+
* Returns a base context with all shared fields initialised.
|
|
217
|
+
* Subclasses call this and spread into their own context literal.
|
|
218
|
+
*/
|
|
219
|
+
protected createBaseContext(): Omit<BaseFormatterContext, never>;
|
|
220
|
+
/**
|
|
221
|
+
* Helper: merges base options.
|
|
222
|
+
*/
|
|
223
|
+
protected mergeBaseOptions(options?: SerializerOptions): Required<SerializerOptions>;
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=base-token-formatter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-token-formatter.d.ts","sourceRoot":"","sources":["../src/base-token-formatter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAa,MAAM,YAAY,CAAC;AAGpD,OAAO,KAAK,EAAE,iBAAiB,EAAuB,MAAM,YAAY,CAAC;AAOzE;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC5B,6EAA6E;IAC7E,mBAAmB,EAAE,OAAO,CAAC;IAC7B,8FAA8F;IAC9F,qBAAqB,EAAE,OAAO,CAAC;IAC/B,kFAAkF;IAClF,aAAa,EAAE,MAAM,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,eAA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