Skip to content

Commit f1e6c15

Browse files
author
Evgeny Poberezkin
committed
Merge pull request #2 from JSONScript/macros
Macros
2 parents 03938a2 + 96c2cf8 commit f1e6c15

17 files changed

+814
-20
lines changed

LANGUAGE.md

+284-7
Large diffs are not rendered by default.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ JSONScript is created to manage scripted execution in remote systems to avoid th
1212

1313
JSONScript:
1414

15-
- uses JSON as its representaion format for both data and control structures, being similar to lisp (homoiconic)
15+
- uses JSON as its representaion format for both data and control structures, being similar to lisp (homoiconic).
1616
- defines simple control structures.
17-
- is asynchronous and concurrent without the need to use any special keywords
17+
- is asynchronous and concurrent without the need to use any special keywords.
1818
- actual processing in the remote system is done synchronously or asynchronously by the functions and objects supplied to JSONScript interpreter by the host environment.
1919

2020

SCHEMA.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# JSONScript Schema
22

3-
JSONScript uses JSON-Schema standard both for the validation schemas and for the schema that defines evaluation process.
3+
JSONScript uses JSON-Schema standard both for the validation schemas and for the schemas that define macro expansion and evaluation process.
44

5-
[JSONScript schema](http://www.json-script.com/schema/schema.json#) the schema that does not validate scalar keywords in instructions (keyword values can be scripts and have to be validated when the script is evaluated).
5+
[JSONScript schema](http://www.json-script.com/schema/schema.json#) - the schema for JSONScript that does not validate scalar keywords in instructions (keyword values can be scripts and have to be validated when the script is evaluated).
66

7-
[JSONScript strict schema](http://www.json-script.com/schema/schema_strict.json#) the schema that validates scalar keywords in instructions.
7+
[JSONScript strict schema](http://www.json-script.com/schema/schema_strict.json#) - the schema for JSONScript that validates scalar keywords in instructions.
88

9-
[JSONScript evaluation schema](http://www.json-script.com/schema/evaluate.json#) this schema defines evalution process. It can be used by implementations to evaluate scripts. It contains non-standard keywords.
9+
[Macro expansion schema](http://www.json-script.com/schema/expand_macros.json#) - this schema defines macro expansion process. It can be used by implementations to expand macros in the scripts before their evaluation. It contains non-standard keyword `expandJsMacro`.
1010

11-
[Instruction definition schema](http://www.json-script.com/schema/instruction.json#) the schema for instruction defnitions. The definitions of both standard and user-defined instructions should be valid according to this schema.
11+
[Evaluation schema](http://www.json-script.com/schema/evaluate.json#) - this schema defines evalution process. It can be used by implementations to evaluate scripts. It contains non-standard keywords.
12+
13+
[Instruction definition schema](http://www.json-script.com/schema/instruction.json#) - the schema for instruction defnitions. The definitions of both standard and user-defined instructions should be valid according to this schema.
14+
15+
[Macro definition schema](http://www.json-script.com/schema/macro.json#) - the schema for macro definition. The definitions of both standard and user-defined macros should be valid according to this schema.

macros/calc.json

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "calc",
3+
"description": "short calculations syntax",
4+
"rules": [
5+
{
6+
"description": "calculation",
7+
"pattern": {
8+
"\\$([+\\-*/=!<>&\\|^]{1,2})": 1
9+
},
10+
"script": {
11+
"$exec": "calc",
12+
"$method": { "$1": {
13+
"+": "add",
14+
"-": "subtract",
15+
"*": "multiply",
16+
"/": "divide",
17+
"==": "equal",
18+
"!=": "notEqual",
19+
">": "greater",
20+
">=": "greaterEqual",
21+
"<": "lesser",
22+
"<=": "lesserEqual",
23+
"&&": "and",
24+
"||": "or",
25+
"^^": "xor",
26+
"!": "not"
27+
} },
28+
"$args": 1
29+
}
30+
}
31+
]
32+
}

macros/call.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "call",
3+
"description": "short syntax for function call",
4+
"rules": [
5+
{
6+
"description": "call named function with arguments",
7+
"pattern": {
8+
"^\\$\\#(.+)$": 1
9+
},
10+
"script": {
11+
"$call": "$1",
12+
"$args": 1
13+
}
14+
}
15+
]
16+
}

macros/exec.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "exec",
3+
"description": "short syntax for executor call",
4+
"rules": [
5+
{
6+
"description": "executor call with method",
7+
"pattern": {
8+
"^\\$\\$([^\\.]+)\\.([^\\.]+)$": 1
9+
},
10+
"script": {
11+
"$exec": "$1",
12+
"$method": "$2",
13+
"$args": 1
14+
}
15+
},
16+
{
17+
"description": "executor call without method",
18+
"pattern": {
19+
"^\\$\\$([^\\.]+)$": 1
20+
},
21+
"script": {
22+
"$exec": "$1",
23+
"$args": 1
24+
}
25+
}
26+
]
27+
}

macros/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
module.exports = [
4+
require('./exec.json'),
5+
require('./call.json'),
6+
require('./calc.json')
7+
];

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonscript",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "Platform independent asynchronous and concurrent scripting language using JSON format",
55
"main": "index.js",
66
"scripts": {

schema/expand_macros.json

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"id": "http://www.json-script.com/schema/expand_macros.json#",
3+
"$schema": "https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#",
4+
"title": "JSONScript macro expansion schema",
5+
"description": "Schema with custom keywords that expands macros in JSON script.",
6+
"switch": [
7+
{
8+
"if": {
9+
"type": "object"
10+
},
11+
"then": {
12+
"allOf": [
13+
{
14+
"anyOf": [
15+
{
16+
"expandJsMacro": {
17+
"description": "executor call with method",
18+
"pattern": {
19+
"^\\$\\$([^\\.]+)\\.([^\\.]+)$": 1
20+
},
21+
"script": {
22+
"$exec": "$1",
23+
"$method": "$2",
24+
"$args": 1
25+
}
26+
}
27+
},
28+
{
29+
"expandJsMacro": {
30+
"description": "executor call without method",
31+
"pattern": {
32+
"^\\$\\$([^\\.]+)$": 1
33+
},
34+
"script": {
35+
"$exec": "$1",
36+
"$args": 1
37+
}
38+
}
39+
},
40+
{
41+
"expandJsMacro": {
42+
"description": "call named function with arguments",
43+
"pattern": {
44+
"^\\$\\#(.+)$": 1
45+
},
46+
"script": {
47+
"$call": "$1",
48+
"$args": 1
49+
}
50+
}
51+
},
52+
{
53+
"expandJsMacro": {
54+
"description": "calculation",
55+
"pattern": {
56+
"\\$([+\\-*/=!<>&\\|^]{1,2})": 1
57+
},
58+
"script": {
59+
"$exec": "calc",
60+
"$method": {
61+
"$1": {
62+
"+": "add",
63+
"-": "subtract",
64+
"*": "multiply",
65+
"/": "divide",
66+
"==": "equal",
67+
"!=": "notEqual",
68+
">": "greater",
69+
">=": "greaterEqual",
70+
"<": "lesser",
71+
"<=": "lesserEqual",
72+
"&&": "and",
73+
"||": "or",
74+
"^^": "xor",
75+
"!": "not"
76+
}
77+
},
78+
"$args": 1
79+
}
80+
}
81+
},
82+
{
83+
"additionalProperties": {
84+
"$ref": "#"
85+
}
86+
}
87+
]
88+
},
89+
{
90+
"additionalProperties": {
91+
"$ref": "#"
92+
}
93+
}
94+
]
95+
}
96+
},
97+
{
98+
"if": {
99+
"type": "array"
100+
},
101+
"then": {
102+
"items": {
103+
"$ref": "#"
104+
}
105+
}
106+
}
107+
]
108+
}

schema/expand_macros.json.dot

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"id": "http://www.json-script.com/schema/expand_macros.json#",
3+
"$schema": "https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#",
4+
"title": "JSONScript macro expansion schema",
5+
"description": "Schema with custom keywords that expands macros in JSON script.",
6+
"switch": [
7+
{
8+
"if": { "type": "object" },
9+
"then": {
10+
"allOf": [
11+
{
12+
"anyOf": [
13+
{{~ it.macros:macro }}
14+
{{~ macro.rules:rule }}
15+
{ "expandJsMacro": {{= JSON.stringify(rule) }} },
16+
{{~}}
17+
{{~}}
18+
{ "additionalProperties": { "$ref": "#" } }
19+
]
20+
},
21+
{ "additionalProperties": { "$ref": "#" } }
22+
]
23+
}
24+
},
25+
{
26+
"if": { "type": "array" },
27+
"then": { "items": { "$ref": "#" } }
28+
}
29+
]
30+
}

schema/macro.json

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"id": "http://www.json-script.com/schema/macro.json#",
3+
"$schema": "https://raw.githubusercontent.com/epoberezkin/ajv/master/lib/refs/json-schema-v5.json#",
4+
"description": "schema for macro definition",
5+
"type": "object",
6+
"required": [ "name", "description", "rules" ],
7+
"additionalProperties": false,
8+
"properties": {
9+
"name": {
10+
"decription": "macro name",
11+
"$ref": "#nonEmptyString"
12+
},
13+
"description": {
14+
"$ref": "#nonEmptyString"
15+
},
16+
"rules": {
17+
"description": "macro rules",
18+
"type": "array",
19+
"minItems": 1,
20+
"items": {
21+
"type": "object",
22+
"required": [ "description", "pattern", "script" ],
23+
"additionalProperties": false,
24+
"properties": {
25+
"description": { "$ref": "#nonEmptyString" },
26+
"pattern": {
27+
"type": "object",
28+
"minProperties": 1,
29+
"maxProperties": 1,
30+
"additionalProperties": {
31+
"description": "property itself should be a valid regular expression",
32+
"type": "integer"
33+
}
34+
},
35+
"script": {
36+
"type": "object",
37+
"additionalProperties": false,
38+
"patternProperties": {
39+
"^\\$[a-z]+$": {
40+
"anyOf": [
41+
{
42+
"description": "this string referes to the partial match in macro pattern",
43+
"type": "string",
44+
"pattern": "^\\$[1-9]$"
45+
},
46+
{
47+
"description": "object with a single property that refers to the match; the value of the property is a substitution map",
48+
"type": "object",
49+
"minProperties": 1,
50+
"maxProperties": 1,
51+
"additionalProperties": false,
52+
"patternProperties": {
53+
"^\\$[1-9]$": {
54+
"type": "object",
55+
"additionalProperties": {
56+
"type": "string"
57+
}
58+
}
59+
}
60+
},
61+
{
62+
"description": "this number referes to the value in the macro",
63+
"type": "integer"
64+
},
65+
{
66+
"description": "any valid JSONScript"
67+
}
68+
]
69+
}
70+
}
71+
}
72+
}
73+
}
74+
}
75+
},
76+
"definitions": {
77+
"nonEmptyString": {
78+
"id": "#nonEmptyString",
79+
"type": "string",
80+
"minLength": 1
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)