langium-tools 0.1.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.
Files changed (61) hide show
  1. package/README.md +1171 -0
  2. package/dist/base/arrays.d.ts +6 -0
  3. package/dist/base/arrays.js +34 -0
  4. package/dist/base/document-errors.d.ts +56 -0
  5. package/dist/base/document-errors.js +198 -0
  6. package/dist/base/document-issues.d.ts +178 -0
  7. package/dist/base/document-issues.js +306 -0
  8. package/dist/base/index.d.ts +4 -0
  9. package/dist/base/index.js +4 -0
  10. package/dist/base/marker-parser.d.ts +36 -0
  11. package/dist/base/marker-parser.js +51 -0
  12. package/dist/base/string.d.ts +89 -0
  13. package/dist/base/string.js +104 -0
  14. package/dist/generator/generated-content-manager.d.ts +207 -0
  15. package/dist/generator/generated-content-manager.js +242 -0
  16. package/dist/generator/index.d.ts +2 -0
  17. package/dist/generator/index.js +2 -0
  18. package/dist/generator/tools.d.ts +9 -0
  19. package/dist/generator/tools.js +12 -0
  20. package/dist/index.d.ts +4 -0
  21. package/dist/index.js +4 -0
  22. package/dist/lang/index.d.ts +1 -0
  23. package/dist/lang/index.js +1 -0
  24. package/dist/lang/java/common.d.ts +1 -0
  25. package/dist/lang/java/common.js +3 -0
  26. package/dist/lang/java/generator-tools.d.ts +37 -0
  27. package/dist/lang/java/generator-tools.js +48 -0
  28. package/dist/lang/java/import-manager.d.ts +61 -0
  29. package/dist/lang/java/import-manager.js +88 -0
  30. package/dist/lang/java/index.d.ts +3 -0
  31. package/dist/lang/java/index.js +3 -0
  32. package/dist/testing/common.d.ts +1 -0
  33. package/dist/testing/common.js +5 -0
  34. package/dist/testing/generator-testing.d.ts +56 -0
  35. package/dist/testing/generator-testing.js +184 -0
  36. package/dist/testing/index.d.ts +4 -0
  37. package/dist/testing/index.js +4 -0
  38. package/dist/testing/matchers.d.ts +209 -0
  39. package/dist/testing/matchers.js +306 -0
  40. package/dist/testing/parser-tools.d.ts +19 -0
  41. package/dist/testing/parser-tools.js +18 -0
  42. package/package.json +59 -0
  43. package/src/base/arrays.ts +40 -0
  44. package/src/base/document-issues.ts +430 -0
  45. package/src/base/index.ts +4 -0
  46. package/src/base/marker-parser.ts +96 -0
  47. package/src/base/string.ts +117 -0
  48. package/src/generator/generated-content-manager.ts +368 -0
  49. package/src/generator/index.ts +2 -0
  50. package/src/generator/tools.ts +14 -0
  51. package/src/index.ts +5 -0
  52. package/src/lang/index.ts +1 -0
  53. package/src/lang/java/common.ts +4 -0
  54. package/src/lang/java/generator-tools.ts +59 -0
  55. package/src/lang/java/import-manager.ts +98 -0
  56. package/src/lang/java/index.ts +3 -0
  57. package/src/testing/common.ts +5 -0
  58. package/src/testing/generator-testing.ts +266 -0
  59. package/src/testing/index.ts +4 -0
  60. package/src/testing/matchers.ts +503 -0
  61. package/src/testing/parser-tools.ts +34 -0
@@ -0,0 +1,6 @@
1
+ declare global {
2
+ interface Array<T> {
3
+ sortBy<K extends keyof T, V>(key: K | ((item: T) => V)): this;
4
+ }
5
+ }
6
+ export declare function arraySortBy<T, K extends keyof T, V>(list: T[], key: K | ((item: T) => V)): T[];
@@ -0,0 +1,34 @@
1
+ Array.prototype.sortBy = function (key) {
2
+ return arraySortBy(this, key);
3
+ };
4
+ export function arraySortBy(list, key) {
5
+ const comparator = typeof key === 'function'
6
+ ? key
7
+ : (item) => item[key];
8
+ return list.sort((a, b) => {
9
+ const valA = comparator(a);
10
+ const valB = comparator(b);
11
+ // Handle undefined or null values by moving them to the end, nulls then undefines
12
+ if (valA === undefined && valB === null)
13
+ return 1;
14
+ if (valB === undefined && valA === null)
15
+ return -1;
16
+ if (valA === undefined || valA === null)
17
+ return 1;
18
+ if (valB === undefined || valB === null)
19
+ return -1;
20
+ if (typeof valA === 'number' && typeof valB === 'number') {
21
+ return valA - valB;
22
+ }
23
+ if (typeof valA === 'string' && typeof valB === 'string') {
24
+ return valA.localeCompare(valB);
25
+ }
26
+ // Fallback for other types like Dates, objects with custom compare methods, etc.
27
+ if (valA > valB)
28
+ return 1;
29
+ if (valA < valB)
30
+ return -1;
31
+ return 0;
32
+ });
33
+ }
34
+ ;
@@ -0,0 +1,56 @@
1
+ import { LangiumDocument } from "langium";
2
+ import { DiagnosticSeverity } from "vscode-languageserver";
3
+ export declare enum DocumentIssueSource {
4
+ LEXER = "LEXER",
5
+ PARSER = "PARSER",
6
+ VALIDATION = "VALIDATION"
7
+ }
8
+ export declare enum DocumentIssueSeverity {
9
+ ERROR = "ERROR",
10
+ WARNING = "WARNING",
11
+ INFORMATION = "INFO",
12
+ HINT = "HINT",
13
+ UNKNOWN = "-"
14
+ }
15
+ export type DocumentIssue = {
16
+ source: DocumentIssueSource;
17
+ severity: DocumentIssueSeverity;
18
+ message: string;
19
+ startOffset?: number;
20
+ startLine?: number;
21
+ startColumn?: number;
22
+ endOffset?: number;
23
+ endLine?: number;
24
+ endColumn?: number;
25
+ };
26
+ export type DocumentIssueSummary = {
27
+ countTotal: number;
28
+ countErrors: number;
29
+ countNonErrors: number;
30
+ summary: string;
31
+ message: string;
32
+ };
33
+ export interface GetDocumentIssuesParams {
34
+ skipNonErrorDiagnostics?: boolean;
35
+ skipLexerErrors?: boolean;
36
+ skipParserErrors?: boolean;
37
+ skipValidation?: boolean;
38
+ }
39
+ /**
40
+ * Return summary and details of errors and other issues in a langium document.
41
+ *
42
+ * @param document The document to check for issues
43
+ * @param params parameter object
44
+ * @returns A summary of the issues in the document
45
+ */
46
+ export declare function getDocumentIssues(document: LangiumDocument, params?: GetDocumentIssuesParams): Array<DocumentIssue>;
47
+ /**
48
+ * Return summary and details of errors and other issues in a langium document.
49
+ *
50
+ * @param document The document to check for issues
51
+ * @param params parameter object
52
+ * @returns A summary of the issues in the document
53
+ */
54
+ export declare function getDocumentIssueSummary(document: LangiumDocument, params?: GetDocumentIssuesParams): DocumentIssueSummary;
55
+ export declare function documentIssueToString(issue: DocumentIssue): string;
56
+ export declare function severityToString(severity: DiagnosticSeverity | number | undefined): string;
@@ -0,0 +1,198 @@
1
+ import { DiagnosticSeverity } from "vscode-languageserver";
2
+ export var DocumentIssueSource;
3
+ (function (DocumentIssueSource) {
4
+ DocumentIssueSource["LEXER"] = "LEXER";
5
+ DocumentIssueSource["PARSER"] = "PARSER";
6
+ DocumentIssueSource["VALIDATION"] = "VALIDATION";
7
+ })(DocumentIssueSource || (DocumentIssueSource = {}));
8
+ export var DocumentIssueSeverity;
9
+ (function (DocumentIssueSeverity) {
10
+ DocumentIssueSeverity["ERROR"] = "ERROR";
11
+ DocumentIssueSeverity["WARNING"] = "WARNING";
12
+ DocumentIssueSeverity["INFORMATION"] = "INFO";
13
+ DocumentIssueSeverity["HINT"] = "HINT";
14
+ DocumentIssueSeverity["UNKNOWN"] = "-";
15
+ })(DocumentIssueSeverity || (DocumentIssueSeverity = {}));
16
+ function mapSeverity(severity) {
17
+ if (!severity) {
18
+ return DocumentIssueSeverity.UNKNOWN;
19
+ }
20
+ switch (severity) {
21
+ case DiagnosticSeverity.Error:
22
+ return DocumentIssueSeverity.ERROR;
23
+ case DiagnosticSeverity.Warning:
24
+ return DocumentIssueSeverity.WARNING;
25
+ case DiagnosticSeverity.Information:
26
+ return DocumentIssueSeverity.INFORMATION;
27
+ case DiagnosticSeverity.Hint:
28
+ return DocumentIssueSeverity.HINT;
29
+ default:
30
+ throw new Error(`Unknown DiagnosticSeverity: ${severity}`);
31
+ }
32
+ }
33
+ /**
34
+ * Return summary and details of errors and other issues in a langium document.
35
+ *
36
+ * @param document The document to check for issues
37
+ * @param params parameter object
38
+ * @returns A summary of the issues in the document
39
+ */
40
+ export function getDocumentIssues(document, params) {
41
+ var _a;
42
+ const { skipNonErrorDiagnostics = false, skipLexerErrors = false, skipParserErrors = false, skipValidation = false, } = params || {};
43
+ const issues = [];
44
+ if (!skipLexerErrors) {
45
+ document.parseResult.lexerErrors.forEach((lexerIssue) => {
46
+ issues.push({
47
+ source: DocumentIssueSource.LEXER,
48
+ message: lexerIssue.message,
49
+ severity: DocumentIssueSeverity.ERROR,
50
+ startOffset: lexerIssue.offset,
51
+ startLine: lexerIssue.line,
52
+ startColumn: lexerIssue.column
53
+ });
54
+ });
55
+ }
56
+ if (!skipParserErrors) {
57
+ document.parseResult.parserErrors.forEach((parserError) => {
58
+ issues.push({
59
+ source: DocumentIssueSource.PARSER,
60
+ severity: DocumentIssueSeverity.ERROR,
61
+ message: parserError.message,
62
+ startOffset: parserError.token.startOffset,
63
+ startLine: parserError.token.startLine,
64
+ startColumn: parserError.token.startColumn,
65
+ endOffset: parserError.token.endOffset,
66
+ endLine: parserError.token.endLine,
67
+ endColumn: parserError.token.endColumn
68
+ });
69
+ });
70
+ }
71
+ if (!skipValidation) {
72
+ (_a = document.diagnostics) === null || _a === void 0 ? void 0 : _a.forEach((diagnostic) => {
73
+ if (!skipNonErrorDiagnostics || diagnostic.severity === DiagnosticSeverity.Error) {
74
+ issues.push({
75
+ source: DocumentIssueSource.VALIDATION,
76
+ severity: mapSeverity(diagnostic.severity),
77
+ message: diagnostic.message,
78
+ startLine: diagnostic.range.start.line,
79
+ startColumn: diagnostic.range.start.character,
80
+ endLine: diagnostic.range.end.line,
81
+ endColumn: diagnostic.range.end.character,
82
+ });
83
+ }
84
+ });
85
+ }
86
+ return issues;
87
+ }
88
+ /**
89
+ * Return summary and details of errors and other issues in a langium document.
90
+ *
91
+ * @param document The document to check for issues
92
+ * @param params parameter object
93
+ * @returns A summary of the issues in the document
94
+ */
95
+ export function getDocumentIssueSummary(document, params) {
96
+ // TODO Use it
97
+ // const issues = getDocumentIssues(document, params)
98
+ const skipNonErrorDiagnostics = (params === null || params === void 0 ? void 0 : params.skipNonErrorDiagnostics) || false;
99
+ const skipLexerErrors = (params === null || params === void 0 ? void 0 : params.skipLexerErrors) || false;
100
+ const skipParserErrors = (params === null || params === void 0 ? void 0 : params.skipParserErrors) || false;
101
+ const skipValidation = (params === null || params === void 0 ? void 0 : params.skipValidation) || false;
102
+ const lexerErrors = skipLexerErrors ? [] : document.parseResult.lexerErrors;
103
+ const parserErrors = skipParserErrors ? [] : document.parseResult.parserErrors;
104
+ const diagnostics = skipValidation ? [] : document.diagnostics || [];
105
+ let countErrors = 0;
106
+ let countNonErrors = 0;
107
+ let errorString = '';
108
+ const summary = [];
109
+ if (lexerErrors.length > 0) {
110
+ summary.push(`${lexerErrors.length} lexer error(s)`);
111
+ errorString += 'Lexer errors:\n';
112
+ for (const error of lexerErrors) {
113
+ errorString += `Error${at(error.line, error.column)} - ${error.message}\n`;
114
+ countErrors++;
115
+ }
116
+ }
117
+ if (parserErrors.length > 0) {
118
+ summary.push(`${parserErrors.length} parser error(s)`);
119
+ errorString += 'Parser errors:\n';
120
+ for (const error of parserErrors) {
121
+ errorString += `Error${at(error.token.startLine, error.token.startColumn, error.token.endLine, error.token.endColumn)} - ${error.message}\n`;
122
+ countErrors++;
123
+ }
124
+ }
125
+ let diagnosticsToShow;
126
+ const diagnosticsErrors = diagnostics.filter(d => d.severity === DiagnosticSeverity.Error);
127
+ if (diagnosticsErrors.length > 0) {
128
+ summary.push(`${diagnosticsErrors.length} error diagnostic(s)`);
129
+ }
130
+ if (!skipNonErrorDiagnostics) {
131
+ const diagnosticsNonErrorCount = diagnostics.length - diagnosticsErrors.length;
132
+ if (diagnosticsNonErrorCount > 0) {
133
+ summary.push(`${diagnosticsNonErrorCount} non-error diagnostic(s)`);
134
+ }
135
+ countNonErrors += diagnosticsNonErrorCount;
136
+ countErrors += diagnosticsErrors.length;
137
+ diagnosticsToShow = diagnostics;
138
+ }
139
+ else {
140
+ countErrors += diagnosticsErrors.length;
141
+ diagnosticsToShow = diagnosticsErrors;
142
+ }
143
+ if (diagnosticsToShow.length > 0) {
144
+ errorString += 'Diagnostics:\n';
145
+ for (const diagnostic of diagnosticsToShow) {
146
+ errorString += `${severityToString(diagnostic.severity)}${at(diagnostic.range.start.line, diagnostic.range.start.character, diagnostic.range.end.line, diagnostic.range.end.character)} - ${diagnostic.message}\n`;
147
+ }
148
+ }
149
+ if (summary.length === 0) {
150
+ summary.push('No errors');
151
+ }
152
+ const summaryString = summary.join(', ');
153
+ if (errorString.length > 0) {
154
+ errorString = `${errorString}\n${summaryString}`;
155
+ }
156
+ return {
157
+ countTotal: countErrors + countNonErrors,
158
+ countErrors: countErrors,
159
+ countNonErrors: countNonErrors,
160
+ summary: summaryString,
161
+ message: errorString
162
+ };
163
+ }
164
+ export function documentIssueToString(issue) {
165
+ return `${issue.severity}${at(issue.startLine, issue.startColumn, issue.endLine, issue.endColumn)} - ${issue.message}`;
166
+ }
167
+ export function severityToString(severity) {
168
+ switch (severity) {
169
+ case DiagnosticSeverity.Error:
170
+ return 'Error';
171
+ case DiagnosticSeverity.Warning:
172
+ return 'Warning';
173
+ case DiagnosticSeverity.Information:
174
+ return 'Information';
175
+ case DiagnosticSeverity.Hint:
176
+ return 'Hint';
177
+ default:
178
+ return 'Unknown';
179
+ }
180
+ }
181
+ function at(startLine, startColumn, endLine, endColumn) {
182
+ let result = '';
183
+ if (startLine !== undefined && !isNaN(startLine)) {
184
+ result += ` at ${startLine + 1}`;
185
+ if (startColumn !== undefined && !isNaN(startColumn)) {
186
+ result += `:${startColumn + 1}`;
187
+ if (endLine !== undefined && !isNaN(endLine) && endColumn !== undefined && !isNaN(endColumn)) {
188
+ if (startLine !== endLine) {
189
+ result += `-${endLine + 1}:${endColumn + 1}`;
190
+ }
191
+ else {
192
+ result += `-${endColumn + 1}`;
193
+ }
194
+ }
195
+ }
196
+ }
197
+ return result;
198
+ }
@@ -0,0 +1,178 @@
1
+ import "./string.js";
2
+ import { LangiumDocument } from "langium";
3
+ import { DiagnosticSeverity } from "vscode-languageserver";
4
+ /**
5
+ * Represents the source of a document issue.
6
+ *
7
+ * - `LEXER`: Issues originating from the lexer (tokenization errors).
8
+ * - `PARSER`: Issues originating from the parser (syntax errors).
9
+ * - `VALIDATION`: Issues originating from validation (semantic errors).
10
+ */
11
+ export declare enum DocumentIssueSource {
12
+ LEXER = "LEXER",
13
+ PARSER = "PARSER",
14
+ VALIDATION = "VALIDATION"
15
+ }
16
+ /**
17
+ * Represents the severity level of a document issue.
18
+ *
19
+ * - `ERROR`: Critical issue that prevents further processing.
20
+ * - `WARNING`: Issue that may lead to problems but does not prevent processing.
21
+ * - `INFORMATION`: Informational message.
22
+ * - `HINT`: Suggestion to improve the document.
23
+ * - `UNKNOWN`: Severity is unknown.
24
+ */
25
+ export declare enum DocumentIssueSeverity {
26
+ ERROR = "ERROR",
27
+ WARNING = "WARNING",
28
+ INFORMATION = "INFO",
29
+ HINT = "HINT",
30
+ UNKNOWN = "-"
31
+ }
32
+ /**
33
+ * Represents an issue in a document, such as a syntax error or a validation issue.
34
+ * The issue can be associated with a specific source, such as the lexer, parser, or validation.
35
+ */
36
+ export type DocumentIssue = {
37
+ /** The source of the issue (lexer, parser, or validation). */
38
+ source: DocumentIssueSource;
39
+ /** The severity level of the issue. */
40
+ severity: DocumentIssueSeverity;
41
+ /** The message describing the issue. */
42
+ message: string;
43
+ /**
44
+ * 0-based absolute position of the beginning of the marker
45
+ */
46
+ startOffset?: number;
47
+ /**
48
+ * 0-based line number of the beginning of the marker
49
+ */
50
+ startLine?: number;
51
+ /**
52
+ * 0-based column of the beginning of the marker
53
+ */
54
+ startColumn?: number;
55
+ /**
56
+ * 0-based absolute position of the character _after_ the end of the marker.
57
+ * (0-based position of the last character + 1)
58
+ *
59
+ * This allows representation of the 0-length markers as "startPos == endPos".
60
+ * In this case endLine and endColumn should be ignored.
61
+ */
62
+ endOffset?: number;
63
+ /**
64
+ * 0-based line of the end of the marker
65
+ */
66
+ endLine?: number;
67
+ /**
68
+ * 0-based Position of the character _after_ the end of the marker.
69
+ * (1-based position of the last character + 1)
70
+ */
71
+ endColumn?: number;
72
+ };
73
+ /**
74
+ * Represents a summary of document issues.
75
+ *
76
+ * Contains counts of errors and non-error issues, as well as formatted summary and message strings.
77
+ */
78
+ export type DocumentIssueSummary = {
79
+ /** Total number of issues found in the document. */
80
+ countTotal: number;
81
+ /** Number of issues with severity `ERROR`. */
82
+ countErrors: number;
83
+ /** Number of issues with severity other than `ERROR`. */
84
+ countNonErrors: number;
85
+ /** A brief summary string of the issues. */
86
+ summary: string;
87
+ /** A detailed message string listing all issues. */
88
+ message: string;
89
+ };
90
+ /**
91
+ * Parameters for filtering document issues.
92
+ */
93
+ export interface GetDocumentIssuesParams {
94
+ /**
95
+ * If `true`, non-error diagnostics (warnings, information, hints) are skipped.
96
+ * Defaults to `false`.
97
+ */
98
+ skipNonErrorDiagnostics?: boolean;
99
+ /**
100
+ * If `true`, lexer errors are skipped.
101
+ * Defaults to `false`.
102
+ */
103
+ skipLexerErrors?: boolean;
104
+ /**
105
+ * If `true`, parser errors are skipped.
106
+ * Defaults to `false`.
107
+ */
108
+ skipParserErrors?: boolean;
109
+ /**
110
+ * If `true`, validation issues are skipped.
111
+ * Defaults to `false`.
112
+ */
113
+ skipValidation?: boolean;
114
+ }
115
+ /**
116
+ * Retrieves all issues from a Langium document, optionally filtering based on provided parameters.
117
+ *
118
+ * @param document - The Langium document to extract issues from.
119
+ * @param params - Optional parameters to filter issues.
120
+ * @returns An array of `DocumentIssue` representing the issues in the document.
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * const issues = getDocumentIssues(document, { skipNonErrorDiagnostics: true });
125
+ * ```
126
+ */
127
+ export declare function getDocumentIssues(document: LangiumDocument, params?: GetDocumentIssuesParams): DocumentIssue[];
128
+ /**
129
+ * Generates a summary of issues in a Langium document.
130
+ *
131
+ * @param document - The Langium document to summarize issues from.
132
+ * @param params - Optional parameters to filter issues.
133
+ * @returns A `DocumentIssueSummary` containing counts and formatted messages.
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * const summary = getDocumentIssueSummary(document);
138
+ * console.log(summary.summary);
139
+ * console.log(summary.message);
140
+ * ```
141
+ */
142
+ export declare function getDocumentIssueSummary(document: LangiumDocument, params?: GetDocumentIssuesParams): DocumentIssueSummary;
143
+ /**
144
+ * Generates a summary of issues from a list of `DocumentIssue`.
145
+ *
146
+ * @param issues - An array of `DocumentIssue`.
147
+ * @returns A `DocumentIssueSummary` containing counts and formatted messages.
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * const issues: DocumentIssue[] = [ ... ];
152
+ * const summary = getDocumentIssueSummaryFromIssues(issues);
153
+ * console.log(summary.summary);
154
+ * console.log(summary.message);
155
+ * ```
156
+ */
157
+ export declare function getDocumentIssueSummaryFromIssues(issues: DocumentIssue[]): DocumentIssueSummary;
158
+ /**
159
+ * Converts a `DocumentIssue` to a formatted string.
160
+ *
161
+ * @param issue - The `DocumentIssue` to format.
162
+ * @returns A string representation of the issue.
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * const issue: DocumentIssue = { ... };
167
+ * const issueStr = documentIssueToString(issue);
168
+ * console.log(issueStr);
169
+ * ```
170
+ */
171
+ export declare function documentIssueToString(issue: DocumentIssue): string;
172
+ /**
173
+ * Converts a `DiagnosticSeverity` to a string representation.
174
+ *
175
+ * @param severity - The `DiagnosticSeverity` to convert.
176
+ * @returns A string representing the severity.
177
+ */
178
+ export declare function severityToString(severity: DiagnosticSeverity | number | undefined): string;