@cnrs/hel 0.4.0 → 0.5.1
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 +21 -5
- package/package.json +1 -1
- package/src/ast2str.js +1 -0
- package/src/index.js +6 -6
- package/src/str2ast.js +3 -1
- package/src/str2fun.js +1 -0
package/README.md
CHANGED
|
@@ -28,8 +28,8 @@ For example, combined with [Heimdall.js](https://gitlab.huma-num.fr/datasphere/h
|
|
|
28
28
|
|
|
29
29
|
## Why should I use it?
|
|
30
30
|
|
|
31
|
-
Well really
|
|
32
|
-
Anyways, it's up to you. ¯\\_(ツ)_/¯
|
|
31
|
+
Well really y̴̖̿̓ö̶͕͕͝ǘ̶͖͍ ̵̺̫́s̵̼͝h̷͓͂ò̸̠ü̸͍̈́ḽ̷̋͊ḏ̷̽ṅ̷̘'̷̺́̈́ṭ̶̬́͋,̸͔̥̾ ̴̪̌ô̶̤̇r̴̳̅̀ ̶͚̫͒̕H̶̹̿͜e̵̛̞̒l̵̖͗ ̵̪͓͘m̷̰̦̾a̵̠̱̽y̴͙̝͑ ̷͈̙͌d̵̯͝ř̷̟̓ͅa̷̟͖͗i̸͔̮̎n̶̗̠͂͋ ̶̢̦̿̕y̶̖̼̎o̵̖͉͐̎ú̴̧̕r̶͙̳͆ ̸̟͈͋s̶̰̫̿̀o̵̲͕̐ṳ̸̠̋͠l̷͔̫͘ ̴̦̇̐ ̶̧̤̑̂̃͐ä̵̡̙́̀n̵̞͓̬̅d̷̛͕̞̈̂͘ ̸̧͖̘̘̀t̵͖̓̿̆͂ą̵̳̘͉̿͂̈̎k̴̳̘̖̉͆͠ě̴̠̫ ̴̛̞̏̈́̕i̷̬̜̻͗́͒̽͜t̸̘̻̓ ̴̮̎̇̓a̶̦̪̰͈̍̍͝l̶͕̣̙̆̓ő̴̭͊͌͑ǹ̵̡͖̼̗̚g̷͖̙̤̐̂ ̷̝̞̣͑͜ẗ̸̯́h̶̙̒̊e̶̟͓̖͆̄ͅ ̴̣̰͋t̸̙̭́ẅ̷̨̹́ͅẻ̵̗͍̑l̶̪̏͊͌v̴̼̣̾͑͜͠é̷͇͈̥̣́̇ ̸͈̤̱̗͌̃́r̶̬̼͓̕͝ͅi̷̪̎v̵͍͎̦͂͊̓e̴̗̠͖̍̅͝r̸̡̩̘͝s̷̢̝̞͆ ̸̮̒̎̏̕a̴̳̖̔́̕ḷ̷̨͇͙͌͂̓l̴͖̺̤͓͌̓ ̶̢̩̑̃̅t̷̤͙̙̅̓h̸̢̙̰̋ḙ̶͎̰̀ ̵̢̪̿̍w̶͈͚̺̋̃á̵͈̑̅y̷͇̍ ̷̧̠͍̈́̾͑͝t̴̨̨͕̪̎͋̂ȏ̸̳ͅ ̴̡͓̀̔̓̐t̵͖̖̤̽h̴̞͑́ȇ̵̹̰̫̞ ̸̧̻̻̻̌͗H̷̪̜̐́a̴͎̜͖̞͒̋̉̃l̵͖̀̿́͂ḽ̸͛͌ş̶̜̤͐͌ ̴̧̜̞̊̂o̴̥͚̩̐͜f̶̤̦́̔ ̵̳̰̈́͌ṫ̶̡̩͈h̸̳̳̯̏̈́́͒ḙ̶̣̈͊̎̕ ̴̢͓͇̬̍̽D̷̩̋̈́͝͝e̶̲̥̲̐̀͠a̶̪̐d̷͙̉͑ ̸̣̪̥͠f̷̨͈̰̎̿̓ͅỏ̶͓̬͉̘̀͝r̸̫͍̘̪̍͑̋̕ ̸̱̤̎a̵̺̻̿́ṅ̸̢̡̮̹͘̕ ̴̳̻̮̣̊̍̔e̷͚̠̜̠̽t̵̮͚͈̙̋ę̶̭͔͕͛r̶̼̿̋̕n̷̞̼͈̻̒ḭ̴͚̔͛͊͊ͅṱ̴̣͆͘y̸̤̝̘̲͝ ̴̧͓̩̈́̍͐̀ö̵́́̽͠ͅf̴̼͎̣̂̔̔ ̷̨̝͋̕s̸̫͐͝u̴̦͇̙̤̓f̷̥̙̆f̴̡̲̠̽́̎ě̶̡͍̀r̸̛̹͎̭͔̽i̷̖͕͋̚n̴͓̊́͜ģ̶͉̭̈́̅̌̇.̶̝̓̕ ̵̝̣̞͋̋̽̚
|
|
32
|
+
Anyways, what was I saying? Oh well, it's up to you I guess. ¯\\_(ツ)_/¯
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
|
|
@@ -47,12 +47,28 @@ Once installed, you can start using it with a few simple calls:
|
|
|
47
47
|
import { * as hel } from '@cnrs/hel';
|
|
48
48
|
|
|
49
49
|
const query = '!(type != "cat") & (subtype == "bobcat" | danger > "high")';
|
|
50
|
-
const filter = hel.
|
|
51
|
-
const tree = hel.
|
|
52
|
-
const equivalent_query = hel.
|
|
50
|
+
const filter = hel.fromStringToFilter(query); // filter function
|
|
51
|
+
const tree = hel.fromStringToTree(query); // Abstract Syntax Tree (AST)
|
|
52
|
+
const equivalent_query = hel.fromTreeToString(tree); // equivalent to query
|
|
53
53
|
```
|
|
54
54
|
|
|
55
55
|
|
|
56
|
+
The equivalences between the HEL query language syntax and the corresponding [AST (Abstract Syntax Tree)](https://en.wikipedia.org/wiki/Abstract_syntax_tree) JavaScript nodes :
|
|
57
|
+
|
|
58
|
+
| Name | Syntax | AST node | Atom? | Notes |
|
|
59
|
+
| --------- | ------------- | -------------------- | ----- | ------------------------------------------ |
|
|
60
|
+
| Entity |`eid VALUE` |`{ eid: 'VALUE'}` | No | An item of entity ID `VALUE`:String. |
|
|
61
|
+
| Attribute |`aid VALUE` |`{ aid: 'VALUE'}` | Yes | A metadata of attribute ID `VALUE`:String. |
|
|
62
|
+
| Property |`pid VALUE` |`{ pid: 'VALUE'}` | Yes | A metadata of property ID `VALUE`:String. |
|
|
63
|
+
| Number |`VALUE` |`VALUE` | Yes | A litteral numeric equals to `VALUE`. |
|
|
64
|
+
| String |`"VALUE"` |`"VALUE"` | Yes | A litteral string equals to `VALUE`. |
|
|
65
|
+
| Relation |`ATOM OP ATOM` |`{ left: ATOM, operator: OP, right: ATOM}`| No | A relation between a left and a right `ATOM`, using a binary relationnal operator `OP`. |
|
|
66
|
+
| And |`N¹ & N² & …` |`{ and: [ N¹, N², …]}`| No | True if all nodes `N¹`,`N²`,… are true. |
|
|
67
|
+
| Or |`N¹ \| N² \| …`|`{ or: [ N¹, N², …]}` | No | True if any node `N¹`,`N²`,… is true. |
|
|
68
|
+
| Not |`!( N )` |`{ not: N }` | No | The negated value of node `N`. |
|
|
69
|
+
| Parentheses |`( N )` |`N` | No | Used for priority between And and Or. |
|
|
70
|
+
|
|
71
|
+
|
|
56
72
|
|
|
57
73
|
## Is it documented?
|
|
58
74
|
|
package/package.json
CHANGED
package/src/ast2str.js
CHANGED
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { toString as
|
|
2
|
-
import { toAst as
|
|
3
|
-
import { toFunction as
|
|
1
|
+
import { toString as fromTreeToString } from './ast2str.js';
|
|
2
|
+
import { toAst as fromStringToTree } from './str2ast.js';
|
|
3
|
+
import { toFunction as fromStringToFilter } from './str2fun.js';
|
|
4
4
|
|
|
5
5
|
import { operators as ops } from './tokens.js';
|
|
6
6
|
|
|
@@ -12,8 +12,8 @@ for (const [key, token] of Object.entries(ops)) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
fromTreeToString,
|
|
16
|
+
fromStringToTree,
|
|
17
|
+
fromStringToFilter,
|
|
18
18
|
operators,
|
|
19
19
|
};
|
package/src/str2ast.js
CHANGED
|
@@ -71,7 +71,8 @@ class StringToAstVisitor extends CstVisitor {
|
|
|
71
71
|
if (context.attribute) return this.visit(context.attribute);
|
|
72
72
|
if (context.property) return this.visit(context.property);
|
|
73
73
|
if (context.Integer) return parseInt(context.Integer[0].image);
|
|
74
|
-
|
|
74
|
+
const value = context.String[0].image;
|
|
75
|
+
return value.substring(1, value.length-1);
|
|
75
76
|
}
|
|
76
77
|
|
|
77
78
|
relationalOperator(context) {
|
|
@@ -101,6 +102,7 @@ class StringToAstVisitor extends CstVisitor {
|
|
|
101
102
|
const visitor = new StringToAstVisitor(); // this is stateless, so Singleton
|
|
102
103
|
|
|
103
104
|
export function toAst(input) {
|
|
105
|
+
if (!input) return {}; // accepts ""
|
|
104
106
|
const result = tokenize(input);
|
|
105
107
|
parser.input = result.tokens; // resets the parser's internal state
|
|
106
108
|
const cst = parser.statement(); // semantics actions are now defined
|
package/src/str2fun.js
CHANGED
|
@@ -129,6 +129,7 @@ class StringToFunctionVisitor extends CstVisitor {
|
|
|
129
129
|
const visitor = new StringToFunctionVisitor(); // this is stateless, so Singleton
|
|
130
130
|
|
|
131
131
|
export function toFunction(input) {
|
|
132
|
+
if (!input) return (item) => true; // accepts ""
|
|
132
133
|
const result = tokenize(input);
|
|
133
134
|
parser.input = result.tokens; // resets the parser's internal state
|
|
134
135
|
const cst = parser.statement(); // semantics actions are now defined
|