Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utility for replace imports in grammar file #71

Merged
merged 11 commits into from
Jun 9, 2024
14 changes: 8 additions & 6 deletions docs/architecture/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
title: Framework Architecture
---

Yantrix provides a set of mixed functional and objective APIs, each of them being self-sufficient for a particular application layer.
Yantrix provides a set of mixed functional and objective APIs, each of them being self-sufficient for a particular application layer.
However, when used together they form an all-around framework that manages things like:
* Sync and Async I/O
* Timers, Events and integrations
* Declarative data storage
* Application State management

Being primary a Typescript solution, the intended application is mostly Frontend, working well with libraries like React and Redux.
- Sync and Async I/O
- Timers, Events and integrations
- Declarative data storage
- Application State management

Being primary a Typescript solution, the intended application is mostly Frontend, working well with libraries like React and Redux.
Due to its codegen approach Yantrix could work well in Svelte environment and "dumb" s2s NodeJS integrations.

## APIs relation diagram

```mermaid
erDiagram
DataModel ||..o{ Storage: "updates"
Expand Down
2 changes: 0 additions & 2 deletions docs/contributing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ Contributing
---

First, clone the repo from [GitHub](https://github.com/tfcp68/yantrix)


32 changes: 17 additions & 15 deletions docs/contributing/working_with_repo.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Working with Lerna
---

The repository is organized into packages with help of [Lerna](https://lerna.js.org/) is an open source multi-package repository management tool. It allows developers to manage multiple packages in a single monorepository, including versioning, publishing, dependency management, and building.

## Installation
Expand Down Expand Up @@ -77,10 +78,11 @@ lerna version 1.0.0
```

Once a version is selected, **Lerna**:
* updates **package.json** with the version number
* commits the change
* adds the appropriate version tag (e.g. v1.0.0)
* pushes the commit and the tag to the remote repository.

- updates **package.json** with the version number
- commits the change
- adds the appropriate version tag (e.g. v1.0.0)
- pushes the commit and the tag to the remote repository.

If you want to update the version of a specific package, use the **`--scope`** flag, for example:

Expand All @@ -100,19 +102,19 @@ You can also publish packages to a local **NPM** registry using utilities such a

**Lerna** has many [**options**](https://lerna.js.org/docs/api-reference/commands#options), which allow you to customize the behavior of certain commands. Here're some:

- **`--scope`**: allows you to work only with the specified package.
- **`--ignore`**: allows you to ignore the specified packages.
- **`--concurrency`**: allows you to configure the number of parallel tasks.
- **`--no-private`**: excludes private packages (they are enabled by default).
- **`--since [ref]`**: Include only those packages that were changed after the specified **`ref`** .
- **`--scope`**: allows you to work only with the specified package.
- **`--ignore`**: allows you to ignore the specified packages.
- **`--concurrency`**: allows you to configure the number of parallel tasks.
- **`--no-private`**: excludes private packages (they are enabled by default).
- **`--since [ref]`**: Include only those packages that were changed after the specified **`ref`** .

## Using Lerna with Git

Lerna integrates well with **Git**. It can automatically generate commits and tags for package version changes. You can use the following commands to work with **Git** in **Lerna**:

- **`lerna changed`**: Shows packages that were changed in the last commit.
- **`lerna diff`**: Shows the differences between two versions of the specified package.
- **`lerna exec <command> --since <last-version>`**: Runs the command for all packages that have changed since the latest version.
- **`lerna changed`**: Shows packages that were changed in the last commit.
- **`lerna diff`**: Shows the differences between two versions of the specified package.
- **`lerna exec <command> --since <last-version>`**: Runs the command for all packages that have changed since the latest version.

## How to add/remove/update dependencies in packages?

Expand Down Expand Up @@ -209,9 +211,9 @@ Once you have built your packages, you can use them in other projects by includi

```json
{
"dependencies": {
"my-package": "*"
}
"dependencies": {
"my-package": "*"
}
}
```

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"description": "Event-Driven FSM Framework",
"type": "module",
"scripts": {
"prepare": "pnpm build",
"clean": "lerna exec rimraf dist coverage",
"build": "lerna run build",
"build:affected": "lerna run build --since=origin/main",
Expand Down
3 changes: 2 additions & 1 deletion packages/yantrix-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"format": "prettier --write . && cross-env TIMING=1 eslint --fix src",
"lint": "prettier --check . && cross-env TIMING=1 eslint src",
"prepack": "pnpm lint && pnpm test && pnpm build",
"test": "pnpm run build && vitest run"
"test": "pnpm run build && vitest run",
"export-grammar": "node src/bin/exportCodeToGrammar.js"
},
"description": "Execute jison parser",
"author": "Alexander Uskov <neolord.tmb@gmail.com>",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExpressionTypes } from '../../types/expressions.js';
import { ExpressionTypes } from '../../constants/index.js';

export const primitives = {
string: {
Expand Down
43 changes: 43 additions & 0 deletions packages/yantrix-parser/src/bin/exportCodeToGrammar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fs, { constants } from 'fs';
import path from 'path';

const args = process.argv.slice(2);
const grammarPath = args[0] ?? 'src/grammar/parserGrammar.jison';
const constantsPath = args[1] ?? 'dist/constants/index.js';
const outputPath = args[2] ?? 'src/grammar/parserGrammarWithoutImports.jison';
const grammarFilePath = path.resolve(grammarPath);
const constantsFilePath = path.resolve(constantsPath);
const outputFilePath = path.resolve(outputPath);

/**
* Replaces imports in the grammar file with the content of the constants file
* @param {string} grammarData - The content of the grammar file
* @param {string} constantsData - The content of the constants file
* @returns {string} - The updated grammar data with imports replaced
*/
const replaceImportsInGrammar = (grammarData, constantsData) => {
const importRegex = /import\s+\{[^}]+\}\s+from\s+['"]\.\/index\.js['"];?/;
const exportRegexp = /export/g;

const withoutExports = constantsData.replace(exportRegexp, '');

return grammarData.replace(importRegex, withoutExports);
};

try {
const grammarData = fs.readFileSync(grammarFilePath, 'utf8');
const constantsData = fs.readFileSync(constantsFilePath, 'utf8');
const updatedGrammarData = replaceImportsInGrammar(grammarData, constantsData);
fs.access(outputFilePath, constants.F_OK && constants.R_OK, (err) => {
if (err) {
console.error('File is not writable or does not exist. Start build process first.');
throw new Error(err.message);
} else {
console.log('File exists and is accessible');
fs.writeFileSync(outputFilePath, updatedGrammarData, 'utf8');
console.log('Successfully replaced imports with constants content.');
}
});
} catch (err) {
console.error(`Error: ${err.message}`);
}
15 changes: 14 additions & 1 deletion packages/yantrix-parser/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const ReservedList = ['end note, +INITIAL'];
export const SpecialCharList = [
'!',
'@',
'$',
'$', // Нужны двойные кавычки, т.к при одинарных некорректно импортируется в грамматику
'%',
'^',
'&',
Expand All @@ -34,3 +34,16 @@ export const SpecialCharList = [
'\\',
'|',
];
/**
* This object contains the different types of expressions that can be used in the parser.
*/
export const ExpressionTypes = {
Function: 'function',
StringDeclaration: 'string',
ArrayDeclaration: 'array',
Constant: 'constant',
IntegerDeclaration: 'integer',
DecimalDeclaration: 'decimal',
FunctionProperty: 'FunctionProperty',
Property: 'property',
} as const;
Loading