@aeriajs/compiler 0.0.7 → 0.0.9

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/dist/codegen.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import type { CompilationOptions } from './compile.js';
2
- import type * as AST from './ast';
1
+ import type * as AST from './ast.js';
2
+ import type { CompilationOptions } from './types.js';
3
3
  export declare const generateCode: (ast: AST.ProgramNode, options: CompilationOptions) => Promise<Record<string, string>>;
package/dist/compile.d.ts CHANGED
@@ -1,21 +1,11 @@
1
- import type * as AST from './ast.js';
1
+ import type { CompilationOptions, CompilationResult } from './types.js';
2
2
  import { Diagnostic } from './diagnostic.js';
3
- export type CompilationResult = {
4
- success: boolean;
5
- ast: AST.ProgramNode;
6
- errors: Diagnostic[];
7
- errorCount: number;
8
- };
9
- export type CompilationOptions = {
10
- outDir: string;
11
- dryRun?: true;
12
- };
13
- export declare const parseAndCheck: (sources: Record<string, string>) => Promise<CompilationResult>;
3
+ export declare const parseAndCheck: (sources: Record<string, string>, options?: Pick<CompilationOptions, "languageServer">) => Promise<CompilationResult>;
14
4
  export declare const generateScaffolding: (options: CompilationOptions) => Promise<string[]>;
15
- export declare const compileFromFiles: (schemaDir: string, options: CompilationOptions) => Promise<{
5
+ export declare const compileFromFiles: (schemaDir: string, options: CompilationOptions) => Promise<CompilationResult | {
16
6
  emittedFiles: Record<string, string>;
17
7
  success: boolean;
18
- ast: AST.ProgramNode;
8
+ ast?: import("./ast.js").ProgramNode;
19
9
  errors: Diagnostic[];
20
10
  errorCount: number;
21
11
  }>;
package/dist/compile.js CHANGED
@@ -40,8 +40,8 @@ const parser_js_1 = require("./parser.js");
40
40
  const semantic_js_1 = require("./semantic.js");
41
41
  const codegen_js_1 = require("./codegen.js");
42
42
  const path = __importStar(require("node:path"));
43
- const fsPromises = __importStar(require("node:fs/promises"));
44
- const parseAndCheck = async (sources) => {
43
+ const fs = __importStar(require("node:fs"));
44
+ const parseAndCheck = async (sources, options = {}) => {
45
45
  const errors = [];
46
46
  let errorCount = 0;
47
47
  let ast;
@@ -49,7 +49,7 @@ const parseAndCheck = async (sources) => {
49
49
  diagnostic_js_1.Diagnostic.currentFile = fileName;
50
50
  const { errors: lexerErrors, tokens } = (0, lexer_js_1.tokenize)(sources[fileName]);
51
51
  const { errors: parserErrors, ast: currentAst } = (0, parser_js_1.parse)(Array.from(tokens));
52
- const { errors: semanticErrors } = await (0, semantic_js_1.analyze)(currentAst);
52
+ const { errors: semanticErrors } = await (0, semantic_js_1.analyze)(currentAst, options);
53
53
  errors.push(...lexerErrors.concat(parserErrors, semanticErrors));
54
54
  errorCount += errors.length;
55
55
  if (!ast) {
@@ -65,14 +65,14 @@ const parseAndCheck = async (sources) => {
65
65
  success: errorCount === 0,
66
66
  errors,
67
67
  errorCount,
68
- ast: ast,
68
+ ast,
69
69
  };
70
70
  };
71
71
  exports.parseAndCheck = parseAndCheck;
72
72
  const generateScaffolding = async (options) => {
73
73
  const directories = [path.join(options.outDir, 'collections')];
74
74
  for (const dir of directories) {
75
- await fsPromises.mkdir(dir, {
75
+ await fs.promises.mkdir(dir, {
76
76
  recursive: true,
77
77
  });
78
78
  }
@@ -80,16 +80,20 @@ const generateScaffolding = async (options) => {
80
80
  };
81
81
  exports.generateScaffolding = generateScaffolding;
82
82
  const compileFromFiles = async (schemaDir, options) => {
83
- const fileList = await fsPromises.readdir(schemaDir);
83
+ const fileList = await fs.promises.readdir(schemaDir);
84
84
  const sources = {};
85
85
  for (const file of fileList) {
86
- const fileContent = await fsPromises.readFile(`${schemaDir}/${file}`);
87
- sources[file] = fileContent.toString();
86
+ sources[file] = await fs.promises.readFile(`${schemaDir}/${file}`, {
87
+ encoding: 'utf-8',
88
+ });
89
+ }
90
+ const result = await (0, exports.parseAndCheck)(sources, options);
91
+ if (!result.ast) {
92
+ return result;
88
93
  }
89
- const parsed = await (0, exports.parseAndCheck)(sources);
90
- const emittedFiles = await (0, codegen_js_1.generateCode)(parsed.ast, options);
94
+ const emittedFiles = await (0, codegen_js_1.generateCode)(result.ast, options);
91
95
  return {
92
- ...parsed,
96
+ ...result,
93
97
  emittedFiles,
94
98
  };
95
99
  };
package/dist/compile.mjs CHANGED
@@ -5,8 +5,8 @@ import { parse } from "./parser.mjs";
5
5
  import { analyze } from "./semantic.mjs";
6
6
  import { generateCode } from "./codegen.mjs";
7
7
  import * as path from "node:path";
8
- import * as fsPromises from "node:fs/promises";
9
- export const parseAndCheck = async (sources) => {
8
+ import * as fs from "node:fs";
9
+ export const parseAndCheck = async (sources, options = {}) => {
10
10
  const errors = [];
11
11
  let errorCount = 0;
12
12
  let ast;
@@ -14,7 +14,7 @@ export const parseAndCheck = async (sources) => {
14
14
  Diagnostic.currentFile = fileName;
15
15
  const { errors: lexerErrors, tokens } = tokenize(sources[fileName]);
16
16
  const { errors: parserErrors, ast: currentAst } = parse(Array.from(tokens));
17
- const { errors: semanticErrors } = await analyze(currentAst);
17
+ const { errors: semanticErrors } = await analyze(currentAst, options);
18
18
  errors.push(...lexerErrors.concat(parserErrors, semanticErrors));
19
19
  errorCount += errors.length;
20
20
  if (!ast) {
@@ -35,23 +35,27 @@ export const parseAndCheck = async (sources) => {
35
35
  export const generateScaffolding = async (options) => {
36
36
  const directories = [path.join(options.outDir, "collections")];
37
37
  for (const dir of directories) {
38
- await fsPromises.mkdir(dir, {
38
+ await fs.promises.mkdir(dir, {
39
39
  recursive: true
40
40
  });
41
41
  }
42
42
  return directories;
43
43
  };
44
44
  export const compileFromFiles = async (schemaDir, options) => {
45
- const fileList = await fsPromises.readdir(schemaDir);
45
+ const fileList = await fs.promises.readdir(schemaDir);
46
46
  const sources = {};
47
47
  for (const file of fileList) {
48
- const fileContent = await fsPromises.readFile(`${schemaDir}/${file}`);
49
- sources[file] = fileContent.toString();
48
+ sources[file] = await fs.promises.readFile(`${schemaDir}/${file}`, {
49
+ encoding: "utf-8"
50
+ });
51
+ }
52
+ const result = await parseAndCheck(sources, options);
53
+ if (!result.ast) {
54
+ return result;
50
55
  }
51
- const parsed = await parseAndCheck(sources);
52
- const emittedFiles = await generateCode(parsed.ast, options);
56
+ const emittedFiles = await generateCode(result.ast, options);
53
57
  return {
54
- ...parsed,
58
+ ...result,
55
59
  emittedFiles
56
60
  };
57
61
  };
package/dist/lexer.d.ts CHANGED
@@ -8,7 +8,7 @@ export declare const CONTRACT_KEYWORDS: readonly ["payload", "query", "response"
8
8
  export declare const TOPLEVEL_KEYWORDS: readonly ["collection", "contract", "functionset"];
9
9
  export declare const MISC_KEYWORDS: readonly ["extends"];
10
10
  export declare const KEYWORDS: Keyword[];
11
- export declare const tokenize: (input: string) => {
11
+ export declare const tokenize: (rawInput: string) => {
12
12
  tokens: Token[];
13
13
  errors: Diagnostic[];
14
14
  };
package/dist/lexer.js CHANGED
@@ -156,7 +156,8 @@ const TOKENS = [
156
156
  valueExtractor: (value) => value.slice(1),
157
157
  },
158
158
  ];
159
- const tokenize = function (input) {
159
+ const tokenize = function (rawInput) {
160
+ const input = rawInput.replace(/\r\n/g, '\n');
160
161
  let index = 0, line = 1, start = 0, end = 0;
161
162
  const tokens = [];
162
163
  const errors = [];
package/dist/lexer.mjs CHANGED
@@ -161,7 +161,8 @@ const TOKENS = [
161
161
  valueExtractor: (value) => value.slice(1)
162
162
  }
163
163
  ];
164
- export const tokenize = function(input) {
164
+ export const tokenize = function(rawInput) {
165
+ const input = rawInput.replace(/\r\n/g, "\n");
165
166
  let index = 0, line = 1, start = 0, end = 0;
166
167
  const tokens = [];
167
168
  const errors = [];
@@ -1,5 +1,6 @@
1
+ import type { CompilationOptions } from './types.js';
1
2
  import { Diagnostic } from './diagnostic.js';
2
3
  import * as AST from './ast.js';
3
- export declare const analyze: (ast: AST.ProgramNode, errors?: Diagnostic[]) => Promise<{
4
+ export declare const analyze: (ast: AST.ProgramNode, options: Pick<CompilationOptions, "languageServer">, errors?: Diagnostic[]) => Promise<{
4
5
  errors: Diagnostic[];
5
6
  }>;
package/dist/semantic.js CHANGED
@@ -38,9 +38,12 @@ const common_1 = require("@aeriajs/common");
38
38
  const parser_js_1 = require("./parser.js");
39
39
  const diagnostic_js_1 = require("./diagnostic.js");
40
40
  const AST = __importStar(require("./ast.js"));
41
- const collectionHasProperty = async (collection, propName) => {
41
+ const collectionHasProperty = async (collection, propName, options) => {
42
42
  let hasProperty = propName in collection.properties;
43
43
  if (!hasProperty) {
44
+ if (options.languageServer) {
45
+ return true;
46
+ }
44
47
  if (collection.extends) {
45
48
  const { packageName, symbolName } = collection.extends;
46
49
  const { [symbolName]: importedCollection } = await Promise.resolve(`${packageName}`).then(s => __importStar(require(s)));
@@ -52,13 +55,13 @@ const collectionHasProperty = async (collection, propName) => {
52
55
  }
53
56
  return hasProperty;
54
57
  };
55
- const analyze = async (ast, errors = []) => {
58
+ const analyze = async (ast, options, errors = []) => {
56
59
  const checkCollectionForeignProperties = async (foreignCollection, property, attributeName) => {
57
60
  if (!property[attributeName]) {
58
61
  return;
59
62
  }
60
63
  for (const foreignPropName of property[attributeName]) {
61
- if (!await collectionHasProperty(foreignCollection, foreignPropName)) {
64
+ if (!await collectionHasProperty(foreignCollection, foreignPropName, options)) {
62
65
  let location;
63
66
  if (property[AST.LOCATION_SYMBOL]) {
64
67
  location = parser_js_1.locationMap.get(property[AST.LOCATION_SYMBOL].attributes[attributeName]);
@@ -74,7 +77,7 @@ const analyze = async (ast, errors = []) => {
74
77
  for (const index in node[attributeName]) {
75
78
  const propName = node[attributeName][index];
76
79
  const symbol = node[AST.LOCATION_SYMBOL].arrays[attributeName][index];
77
- if (!await collectionHasProperty(node, propName)) {
80
+ if (!await collectionHasProperty(node, propName, options)) {
78
81
  const location = parser_js_1.locationMap.get(symbol);
79
82
  errors.push(new diagnostic_js_1.Diagnostic(`collection "${node.name}" hasn't such property "${propName}"`, location));
80
83
  }
package/dist/semantic.mjs CHANGED
@@ -3,9 +3,12 @@ import { isValidCollection } from "@aeriajs/common";
3
3
  import { locationMap } from "./parser.mjs";
4
4
  import { Diagnostic } from "./diagnostic.mjs";
5
5
  import * as AST from "./ast.mjs";
6
- const collectionHasProperty = async (collection, propName) => {
6
+ const collectionHasProperty = async (collection, propName, options) => {
7
7
  let hasProperty = propName in collection.properties;
8
8
  if (!hasProperty) {
9
+ if (options.languageServer) {
10
+ return true;
11
+ }
9
12
  if (collection.extends) {
10
13
  const { packageName, symbolName } = collection.extends;
11
14
  const { [symbolName]: importedCollection } = await import(packageName);
@@ -17,13 +20,13 @@ const collectionHasProperty = async (collection, propName) => {
17
20
  }
18
21
  return hasProperty;
19
22
  };
20
- export const analyze = async (ast, errors = []) => {
23
+ export const analyze = async (ast, options, errors = []) => {
21
24
  const checkCollectionForeignProperties = async (foreignCollection, property, attributeName) => {
22
25
  if (!property[attributeName]) {
23
26
  return;
24
27
  }
25
28
  for (const foreignPropName of property[attributeName]) {
26
- if (!await collectionHasProperty(foreignCollection, foreignPropName)) {
29
+ if (!await collectionHasProperty(foreignCollection, foreignPropName, options)) {
27
30
  let location;
28
31
  if (property[AST.LOCATION_SYMBOL]) {
29
32
  location = locationMap.get(property[AST.LOCATION_SYMBOL].attributes[attributeName]);
@@ -39,7 +42,7 @@ export const analyze = async (ast, errors = []) => {
39
42
  for (const index in node[attributeName]) {
40
43
  const propName = node[attributeName][index];
41
44
  const symbol = node[AST.LOCATION_SYMBOL].arrays[attributeName][index];
42
- if (!await collectionHasProperty(node, propName)) {
45
+ if (!await collectionHasProperty(node, propName, options)) {
43
46
  const location = locationMap.get(symbol);
44
47
  errors.push(new Diagnostic(`collection "${node.name}" hasn't such property "${propName}"`, location));
45
48
  }
@@ -0,0 +1,13 @@
1
+ import type * as AST from './ast.js';
2
+ import { type Diagnostic } from './diagnostic.js';
3
+ export type CompilationResult = {
4
+ success: boolean;
5
+ ast?: AST.ProgramNode;
6
+ errors: Diagnostic[];
7
+ errorCount: number;
8
+ };
9
+ export type CompilationOptions = {
10
+ outDir: string;
11
+ dryRun?: boolean;
12
+ languageServer?: boolean;
13
+ };
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/types.mjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/compiler",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",