cddl 0.2.1 → 0.3.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.
- package/.nvmrc +1 -0
- package/README.md +5 -5
- package/bin/cddl.js +3 -1
- package/build/ast.d.ts +14 -14
- package/build/ast.d.ts.map +1 -1
- package/build/ast.js +2 -5
- package/build/cli/commands/repl.d.ts +3 -3
- package/build/cli/commands/repl.d.ts.map +1 -1
- package/build/cli/commands/repl.js +13 -20
- package/build/cli/commands/validate.d.ts +4 -4
- package/build/cli/commands/validate.d.ts.map +1 -1
- package/build/cli/commands/validate.js +14 -19
- package/build/cli/constants.d.ts +1 -1
- package/build/cli/constants.js +1 -4
- package/build/cli/index.d.ts +7 -3
- package/build/cli/index.d.ts.map +1 -1
- package/build/cli/index.js +12 -12
- package/build/constants.d.ts +4 -0
- package/build/constants.d.ts.map +1 -1
- package/build/constants.js +8 -6
- package/build/index.d.ts +6 -4
- package/build/index.d.ts.map +1 -1
- package/build/index.js +16 -17
- package/build/lexer.d.ts +7 -1
- package/build/lexer.d.ts.map +1 -1
- package/build/lexer.js +81 -50
- package/build/parser.d.ts +6 -4
- package/build/parser.d.ts.map +1 -1
- package/build/parser.js +126 -79
- package/build/tokens.d.ts +2 -2
- package/build/tokens.d.ts.map +1 -1
- package/build/tokens.js +2 -5
- package/build/transform/ts.d.ts +3 -0
- package/build/transform/ts.d.ts.map +1 -0
- package/build/transform/ts.js +113 -0
- package/build/types.d.ts +5 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +1 -0
- package/build/utils.d.ts +1 -1
- package/build/utils.d.ts.map +1 -1
- package/build/utils.js +11 -18
- package/examples/webdriver/local.cddl +846 -0
- package/examples/webdriver/remote.cddl +830 -0
- package/package.json +30 -26
- package/vitest.config.ts +16 -0
package/build/lexer.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { Tokens } from './tokens.js';
|
|
2
|
+
import { isLetter, isAlphabeticCharacter, isDigit, hasSpecialNumberCharacter } from './utils.js';
|
|
3
|
+
import { WHITESPACE_CHARACTERS } from './constants.js';
|
|
4
|
+
export default class Lexer {
|
|
5
|
+
input;
|
|
6
|
+
position = 0;
|
|
7
|
+
readPosition = 0;
|
|
8
|
+
ch = 0;
|
|
7
9
|
constructor(source) {
|
|
8
|
-
this.position = 0;
|
|
9
|
-
this.readPosition = 0;
|
|
10
|
-
this.ch = 0;
|
|
11
10
|
this.input = source;
|
|
12
11
|
this.readChar();
|
|
13
12
|
}
|
|
@@ -21,89 +20,122 @@ class Lexer {
|
|
|
21
20
|
this.position = this.readPosition;
|
|
22
21
|
this.readPosition++;
|
|
23
22
|
}
|
|
23
|
+
getLocation() {
|
|
24
|
+
const position = this.position - 2;
|
|
25
|
+
const sourceLines = this.input.split('\n');
|
|
26
|
+
const sourceLineLength = sourceLines.map((l) => l.length);
|
|
27
|
+
let i = 0;
|
|
28
|
+
for (const [line, lineLength] of Object.entries(sourceLineLength)) {
|
|
29
|
+
i += lineLength + 1;
|
|
30
|
+
const lineBegin = i - lineLength;
|
|
31
|
+
if (i > position) {
|
|
32
|
+
const lineBegin = i - lineLength;
|
|
33
|
+
return {
|
|
34
|
+
line: parseInt(line, 10),
|
|
35
|
+
position: position - lineBegin + 1
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return { line: 0, position: 0 };
|
|
40
|
+
}
|
|
41
|
+
getLine(lineNumber) {
|
|
42
|
+
return this.input.split('\n')[lineNumber];
|
|
43
|
+
}
|
|
44
|
+
getLocationInfo() {
|
|
45
|
+
const loc = this.getLocation();
|
|
46
|
+
const line = loc ? this.getLine(loc.line) : '';
|
|
47
|
+
let locationInfo = line + '\n';
|
|
48
|
+
locationInfo += ' '.repeat(loc?.position || 0) + '^\n';
|
|
49
|
+
locationInfo += ' '.repeat(loc?.position || 0) + '|\n';
|
|
50
|
+
return locationInfo;
|
|
51
|
+
}
|
|
24
52
|
nextToken() {
|
|
25
53
|
let token;
|
|
26
54
|
this.skipWhitespace();
|
|
27
55
|
const Literal = String.fromCharCode(this.ch);
|
|
28
56
|
switch (this.ch) {
|
|
29
57
|
case '='.charCodeAt(0):
|
|
30
|
-
token = { Type:
|
|
58
|
+
token = { Type: Tokens.ASSIGN, Literal };
|
|
31
59
|
break;
|
|
32
60
|
case '('.charCodeAt(0):
|
|
33
|
-
token = { Type:
|
|
61
|
+
token = { Type: Tokens.LPAREN, Literal };
|
|
34
62
|
break;
|
|
35
63
|
case ')'.charCodeAt(0):
|
|
36
|
-
token = { Type:
|
|
64
|
+
token = { Type: Tokens.RPAREN, Literal };
|
|
37
65
|
break;
|
|
38
66
|
case '{'.charCodeAt(0):
|
|
39
|
-
token = { Type:
|
|
67
|
+
token = { Type: Tokens.LBRACE, Literal };
|
|
40
68
|
break;
|
|
41
69
|
case '}'.charCodeAt(0):
|
|
42
|
-
token = { Type:
|
|
70
|
+
token = { Type: Tokens.RBRACE, Literal };
|
|
43
71
|
break;
|
|
44
72
|
case '['.charCodeAt(0):
|
|
45
|
-
token = { Type:
|
|
73
|
+
token = { Type: Tokens.LBRACK, Literal };
|
|
46
74
|
break;
|
|
47
75
|
case ']'.charCodeAt(0):
|
|
48
|
-
token = { Type:
|
|
76
|
+
token = { Type: Tokens.RBRACK, Literal };
|
|
49
77
|
break;
|
|
50
78
|
case '<'.charCodeAt(0):
|
|
51
|
-
token = { Type:
|
|
79
|
+
token = { Type: Tokens.LT, Literal };
|
|
52
80
|
break;
|
|
53
81
|
case '>'.charCodeAt(0):
|
|
54
|
-
token = { Type:
|
|
82
|
+
token = { Type: Tokens.GT, Literal };
|
|
55
83
|
break;
|
|
56
84
|
case '+'.charCodeAt(0):
|
|
57
|
-
token = { Type:
|
|
85
|
+
token = { Type: Tokens.PLUS, Literal };
|
|
58
86
|
break;
|
|
59
87
|
case ','.charCodeAt(0):
|
|
60
|
-
token = { Type:
|
|
88
|
+
token = { Type: Tokens.COMMA, Literal };
|
|
61
89
|
break;
|
|
62
90
|
case '.'.charCodeAt(0):
|
|
63
|
-
token = { Type:
|
|
91
|
+
token = { Type: Tokens.DOT, Literal };
|
|
64
92
|
break;
|
|
65
93
|
case ':'.charCodeAt(0):
|
|
66
|
-
token = { Type:
|
|
94
|
+
token = { Type: Tokens.COLON, Literal };
|
|
67
95
|
break;
|
|
68
96
|
case '?'.charCodeAt(0):
|
|
69
|
-
token = { Type:
|
|
97
|
+
token = { Type: Tokens.QUEST, Literal };
|
|
70
98
|
break;
|
|
71
99
|
case '/'.charCodeAt(0):
|
|
72
|
-
token = { Type:
|
|
100
|
+
token = { Type: Tokens.SLASH, Literal };
|
|
73
101
|
break;
|
|
74
102
|
case '*'.charCodeAt(0):
|
|
75
|
-
token = { Type:
|
|
103
|
+
token = { Type: Tokens.ASTERISK, Literal };
|
|
76
104
|
break;
|
|
77
105
|
case '^'.charCodeAt(0):
|
|
78
|
-
token = { Type:
|
|
106
|
+
token = { Type: Tokens.CARET, Literal };
|
|
79
107
|
break;
|
|
80
108
|
case '#'.charCodeAt(0):
|
|
81
|
-
token = { Type:
|
|
109
|
+
token = { Type: Tokens.HASH, Literal };
|
|
82
110
|
break;
|
|
83
111
|
case '~'.charCodeAt(0):
|
|
84
|
-
token = { Type:
|
|
112
|
+
token = { Type: Tokens.TILDE, Literal };
|
|
85
113
|
break;
|
|
86
114
|
case '"'.charCodeAt(0):
|
|
87
|
-
token = { Type:
|
|
115
|
+
token = { Type: Tokens.STRING, Literal: this.readString() };
|
|
88
116
|
break;
|
|
89
117
|
case ';'.charCodeAt(0):
|
|
90
|
-
token = { Type:
|
|
118
|
+
token = { Type: Tokens.COMMENT, Literal: this.readComment() };
|
|
91
119
|
break;
|
|
92
120
|
case 0:
|
|
93
|
-
token = { Type:
|
|
121
|
+
token = { Type: Tokens.EOF, Literal: '' };
|
|
94
122
|
break;
|
|
95
123
|
default: {
|
|
96
|
-
if (
|
|
97
|
-
return { Type:
|
|
124
|
+
if (isAlphabeticCharacter(Literal)) {
|
|
125
|
+
return { Type: Tokens.IDENT, Literal: this.readIdentifier() };
|
|
98
126
|
}
|
|
99
|
-
else if (
|
|
127
|
+
else if (
|
|
128
|
+
// positive number
|
|
129
|
+
isDigit(Literal) ||
|
|
130
|
+
// negative number
|
|
131
|
+
(this.ch === Tokens.MINUS.charCodeAt(0) && isDigit(this.input[this.readPosition]))) {
|
|
100
132
|
const numberOrFloat = this.readNumberOrFloat();
|
|
101
133
|
return {
|
|
102
|
-
Type: numberOrFloat.includes(
|
|
134
|
+
Type: numberOrFloat.includes(Tokens.DOT) ? Tokens.FLOAT : Tokens.NUMBER,
|
|
103
135
|
Literal: numberOrFloat
|
|
104
136
|
};
|
|
105
137
|
}
|
|
106
|
-
token = { Type:
|
|
138
|
+
token = { Type: Tokens.ILLEGAL, Literal: '' };
|
|
107
139
|
}
|
|
108
140
|
}
|
|
109
141
|
this.readChar();
|
|
@@ -117,16 +149,16 @@ class Lexer {
|
|
|
117
149
|
*/
|
|
118
150
|
while (
|
|
119
151
|
// a letter (a-z, A-Z)
|
|
120
|
-
|
|
152
|
+
isLetter(String.fromCharCode(this.ch)) ||
|
|
121
153
|
// a digit (0-9)
|
|
122
|
-
|
|
154
|
+
isDigit(String.fromCharCode(this.ch)) ||
|
|
123
155
|
// and special characters (-, _, @, ., $)
|
|
124
156
|
[
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
157
|
+
Tokens.MINUS.charCodeAt(0),
|
|
158
|
+
Tokens.UNDERSCORE.charCodeAt(0),
|
|
159
|
+
Tokens.ATSIGN.charCodeAt(0),
|
|
160
|
+
Tokens.DOT.charCodeAt(0),
|
|
161
|
+
Tokens.DOLLAR.charCodeAt(0)
|
|
130
162
|
].includes(this.ch)) {
|
|
131
163
|
this.readChar();
|
|
132
164
|
}
|
|
@@ -142,7 +174,7 @@ class Lexer {
|
|
|
142
174
|
readString() {
|
|
143
175
|
const position = this.position;
|
|
144
176
|
this.readChar(); // eat "
|
|
145
|
-
while (this.ch && String.fromCharCode(this.ch) !==
|
|
177
|
+
while (this.ch && String.fromCharCode(this.ch) !== Tokens.QUOT) {
|
|
146
178
|
this.readChar(); // eat any character until "
|
|
147
179
|
}
|
|
148
180
|
return this.input.slice(position + 1, this.position).trim();
|
|
@@ -155,19 +187,19 @@ class Lexer {
|
|
|
155
187
|
*/
|
|
156
188
|
while (
|
|
157
189
|
// a number
|
|
158
|
-
|
|
190
|
+
isDigit(String.fromCharCode(this.ch)) ||
|
|
159
191
|
// a special character, e.g. ".", "x" and "b"
|
|
160
|
-
|
|
192
|
+
hasSpecialNumberCharacter(this.ch)) {
|
|
161
193
|
/**
|
|
162
194
|
* ensure we respect ranges, e.g. 0..10
|
|
163
195
|
* so break after the second dot and adjust read position
|
|
164
196
|
*/
|
|
165
|
-
if (
|
|
197
|
+
if (hasSpecialNumberCharacter(this.ch) && foundSpecialCharacter) {
|
|
166
198
|
this.position--;
|
|
167
199
|
this.readPosition--;
|
|
168
200
|
break;
|
|
169
201
|
}
|
|
170
|
-
if (
|
|
202
|
+
if (hasSpecialNumberCharacter(this.ch)) {
|
|
171
203
|
foundSpecialCharacter = true;
|
|
172
204
|
}
|
|
173
205
|
this.readChar(); // eat any character until a non digit or a 2nd dot
|
|
@@ -175,9 +207,8 @@ class Lexer {
|
|
|
175
207
|
return this.input.slice(position, this.position).trim();
|
|
176
208
|
}
|
|
177
209
|
skipWhitespace() {
|
|
178
|
-
while (
|
|
210
|
+
while (WHITESPACE_CHARACTERS.includes(String.fromCharCode(this.ch))) {
|
|
179
211
|
this.readChar();
|
|
180
212
|
}
|
|
181
213
|
}
|
|
182
214
|
}
|
|
183
|
-
exports.default = Lexer;
|
package/build/parser.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import Lexer from './lexer';
|
|
2
|
-
import { Token } from './tokens';
|
|
3
|
-
import { Assignment } from './ast';
|
|
1
|
+
import Lexer from './lexer.js';
|
|
2
|
+
import { Token } from './tokens.js';
|
|
3
|
+
import { Assignment } from './ast.js';
|
|
4
4
|
export default class Parser {
|
|
5
|
+
#private;
|
|
5
6
|
l: Lexer;
|
|
6
7
|
curToken: Token;
|
|
7
8
|
peekToken: Token;
|
|
8
|
-
constructor(
|
|
9
|
+
constructor(filePath: string);
|
|
9
10
|
private nextToken;
|
|
10
11
|
private parseAssignments;
|
|
11
12
|
private parseAssignmentValue;
|
|
@@ -25,5 +26,6 @@ export default class Parser {
|
|
|
25
26
|
*/
|
|
26
27
|
private parseComment;
|
|
27
28
|
parse(): Assignment[];
|
|
29
|
+
private parserError;
|
|
28
30
|
}
|
|
29
31
|
//# sourceMappingURL=parser.d.ts.map
|
package/build/parser.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,MAAM,YAAY,CAAA;AAE9B,OAAO,EAAE,KAAK,EAAU,MAAM,aAAa,CAAC;AAG5C,OAAO,EAE2C,UAAU,EAE3D,MAAM,UAAU,CAAA;AAKjB,MAAM,CAAC,OAAO,OAAO,MAAM;;IAEvB,CAAC,EAAE,KAAK,CAAC;IAET,QAAQ,EAAE,KAAK,CAAa;IAC5B,SAAS,EAAE,KAAK,CAAa;gBAEhB,QAAQ,EAAE,MAAM;IAQ7B,OAAO,CAAC,SAAS;IAMjB,OAAO,CAAC,gBAAgB;IA8BxB,OAAO,CAAC,oBAAoB;IA2R5B,OAAO,CAAC,wBAAwB;IAahC;;;;OAIG;IACH,OAAO,CAAC,WAAW;IAoBnB,OAAO,CAAC,iBAAiB;IAazB,OAAO,CAAC,iBAAiB;IA8GzB,OAAO,CAAC,kBAAkB;IAwC1B,OAAO,CAAC,gBAAgB;IA4DxB;;OAEG;IACH,OAAO,CAAC,YAAY;IAUpB,KAAK;IAuBL,OAAO,CAAC,WAAW;CAKtB"}
|