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.
Files changed (45) hide show
  1. package/.nvmrc +1 -0
  2. package/README.md +5 -5
  3. package/bin/cddl.js +3 -1
  4. package/build/ast.d.ts +14 -14
  5. package/build/ast.d.ts.map +1 -1
  6. package/build/ast.js +2 -5
  7. package/build/cli/commands/repl.d.ts +3 -3
  8. package/build/cli/commands/repl.d.ts.map +1 -1
  9. package/build/cli/commands/repl.js +13 -20
  10. package/build/cli/commands/validate.d.ts +4 -4
  11. package/build/cli/commands/validate.d.ts.map +1 -1
  12. package/build/cli/commands/validate.js +14 -19
  13. package/build/cli/constants.d.ts +1 -1
  14. package/build/cli/constants.js +1 -4
  15. package/build/cli/index.d.ts +7 -3
  16. package/build/cli/index.d.ts.map +1 -1
  17. package/build/cli/index.js +12 -12
  18. package/build/constants.d.ts +4 -0
  19. package/build/constants.d.ts.map +1 -1
  20. package/build/constants.js +8 -6
  21. package/build/index.d.ts +6 -4
  22. package/build/index.d.ts.map +1 -1
  23. package/build/index.js +16 -17
  24. package/build/lexer.d.ts +7 -1
  25. package/build/lexer.d.ts.map +1 -1
  26. package/build/lexer.js +81 -50
  27. package/build/parser.d.ts +6 -4
  28. package/build/parser.d.ts.map +1 -1
  29. package/build/parser.js +126 -79
  30. package/build/tokens.d.ts +2 -2
  31. package/build/tokens.d.ts.map +1 -1
  32. package/build/tokens.js +2 -5
  33. package/build/transform/ts.d.ts +3 -0
  34. package/build/transform/ts.d.ts.map +1 -0
  35. package/build/transform/ts.js +113 -0
  36. package/build/types.d.ts +5 -0
  37. package/build/types.d.ts.map +1 -0
  38. package/build/types.js +1 -0
  39. package/build/utils.d.ts +1 -1
  40. package/build/utils.d.ts.map +1 -1
  41. package/build/utils.js +11 -18
  42. package/examples/webdriver/local.cddl +846 -0
  43. package/examples/webdriver/remote.cddl +830 -0
  44. package/package.json +30 -26
  45. package/vitest.config.ts +16 -0
package/build/lexer.js CHANGED
@@ -1,13 +1,12 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tokens_1 = require("./tokens");
4
- const utils_1 = require("./utils");
5
- const constants_1 = require("./constants");
6
- class Lexer {
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: tokens_1.Tokens.ASSIGN, Literal };
58
+ token = { Type: Tokens.ASSIGN, Literal };
31
59
  break;
32
60
  case '('.charCodeAt(0):
33
- token = { Type: tokens_1.Tokens.LPAREN, Literal };
61
+ token = { Type: Tokens.LPAREN, Literal };
34
62
  break;
35
63
  case ')'.charCodeAt(0):
36
- token = { Type: tokens_1.Tokens.RPAREN, Literal };
64
+ token = { Type: Tokens.RPAREN, Literal };
37
65
  break;
38
66
  case '{'.charCodeAt(0):
39
- token = { Type: tokens_1.Tokens.LBRACE, Literal };
67
+ token = { Type: Tokens.LBRACE, Literal };
40
68
  break;
41
69
  case '}'.charCodeAt(0):
42
- token = { Type: tokens_1.Tokens.RBRACE, Literal };
70
+ token = { Type: Tokens.RBRACE, Literal };
43
71
  break;
44
72
  case '['.charCodeAt(0):
45
- token = { Type: tokens_1.Tokens.LBRACK, Literal };
73
+ token = { Type: Tokens.LBRACK, Literal };
46
74
  break;
47
75
  case ']'.charCodeAt(0):
48
- token = { Type: tokens_1.Tokens.RBRACK, Literal };
76
+ token = { Type: Tokens.RBRACK, Literal };
49
77
  break;
50
78
  case '<'.charCodeAt(0):
51
- token = { Type: tokens_1.Tokens.LT, Literal };
79
+ token = { Type: Tokens.LT, Literal };
52
80
  break;
53
81
  case '>'.charCodeAt(0):
54
- token = { Type: tokens_1.Tokens.GT, Literal };
82
+ token = { Type: Tokens.GT, Literal };
55
83
  break;
56
84
  case '+'.charCodeAt(0):
57
- token = { Type: tokens_1.Tokens.PLUS, Literal };
85
+ token = { Type: Tokens.PLUS, Literal };
58
86
  break;
59
87
  case ','.charCodeAt(0):
60
- token = { Type: tokens_1.Tokens.COMMA, Literal };
88
+ token = { Type: Tokens.COMMA, Literal };
61
89
  break;
62
90
  case '.'.charCodeAt(0):
63
- token = { Type: tokens_1.Tokens.DOT, Literal };
91
+ token = { Type: Tokens.DOT, Literal };
64
92
  break;
65
93
  case ':'.charCodeAt(0):
66
- token = { Type: tokens_1.Tokens.COLON, Literal };
94
+ token = { Type: Tokens.COLON, Literal };
67
95
  break;
68
96
  case '?'.charCodeAt(0):
69
- token = { Type: tokens_1.Tokens.QUEST, Literal };
97
+ token = { Type: Tokens.QUEST, Literal };
70
98
  break;
71
99
  case '/'.charCodeAt(0):
72
- token = { Type: tokens_1.Tokens.SLASH, Literal };
100
+ token = { Type: Tokens.SLASH, Literal };
73
101
  break;
74
102
  case '*'.charCodeAt(0):
75
- token = { Type: tokens_1.Tokens.ASTERISK, Literal };
103
+ token = { Type: Tokens.ASTERISK, Literal };
76
104
  break;
77
105
  case '^'.charCodeAt(0):
78
- token = { Type: tokens_1.Tokens.CARET, Literal };
106
+ token = { Type: Tokens.CARET, Literal };
79
107
  break;
80
108
  case '#'.charCodeAt(0):
81
- token = { Type: tokens_1.Tokens.HASH, Literal };
109
+ token = { Type: Tokens.HASH, Literal };
82
110
  break;
83
111
  case '~'.charCodeAt(0):
84
- token = { Type: tokens_1.Tokens.TILDE, Literal };
112
+ token = { Type: Tokens.TILDE, Literal };
85
113
  break;
86
114
  case '"'.charCodeAt(0):
87
- token = { Type: tokens_1.Tokens.STRING, Literal: this.readString() };
115
+ token = { Type: Tokens.STRING, Literal: this.readString() };
88
116
  break;
89
117
  case ';'.charCodeAt(0):
90
- token = { Type: tokens_1.Tokens.COMMENT, Literal: this.readComment() };
118
+ token = { Type: Tokens.COMMENT, Literal: this.readComment() };
91
119
  break;
92
120
  case 0:
93
- token = { Type: tokens_1.Tokens.EOF, Literal: '' };
121
+ token = { Type: Tokens.EOF, Literal: '' };
94
122
  break;
95
123
  default: {
96
- if (utils_1.isAlphabeticCharacter(Literal)) {
97
- return { Type: tokens_1.Tokens.IDENT, Literal: this.readIdentifier() };
124
+ if (isAlphabeticCharacter(Literal)) {
125
+ return { Type: Tokens.IDENT, Literal: this.readIdentifier() };
98
126
  }
99
- else if (utils_1.isDigit(Literal)) {
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(tokens_1.Tokens.DOT) ? tokens_1.Tokens.FLOAT : tokens_1.Tokens.NUMBER,
134
+ Type: numberOrFloat.includes(Tokens.DOT) ? Tokens.FLOAT : Tokens.NUMBER,
103
135
  Literal: numberOrFloat
104
136
  };
105
137
  }
106
- token = { Type: tokens_1.Tokens.ILLEGAL, Literal: '' };
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
- utils_1.isLetter(String.fromCharCode(this.ch)) ||
152
+ isLetter(String.fromCharCode(this.ch)) ||
121
153
  // a digit (0-9)
122
- utils_1.isDigit(String.fromCharCode(this.ch)) ||
154
+ isDigit(String.fromCharCode(this.ch)) ||
123
155
  // and special characters (-, _, @, ., $)
124
156
  [
125
- tokens_1.Tokens.MINUS.charCodeAt(0),
126
- tokens_1.Tokens.UNDERSCORE.charCodeAt(0),
127
- tokens_1.Tokens.ATSIGN.charCodeAt(0),
128
- tokens_1.Tokens.DOT.charCodeAt(0),
129
- tokens_1.Tokens.DOLLAR.charCodeAt(0)
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) !== tokens_1.Tokens.QUOT) {
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
- utils_1.isDigit(String.fromCharCode(this.ch)) ||
190
+ isDigit(String.fromCharCode(this.ch)) ||
159
191
  // a special character, e.g. ".", "x" and "b"
160
- utils_1.hasSpecialNumberCharacter(this.ch)) {
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 (utils_1.hasSpecialNumberCharacter(this.ch) && foundSpecialCharacter) {
197
+ if (hasSpecialNumberCharacter(this.ch) && foundSpecialCharacter) {
166
198
  this.position--;
167
199
  this.readPosition--;
168
200
  break;
169
201
  }
170
- if (utils_1.hasSpecialNumberCharacter(this.ch)) {
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 (constants_1.WHITESPACE_CHARACTERS.includes(String.fromCharCode(this.ch))) {
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(l: Lexer);
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
@@ -1 +1 @@
1
- {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,SAAS,CAAA;AAE3B,OAAO,EAAE,KAAK,EAAU,MAAM,UAAU,CAAC;AAGzC,OAAO,EAE2C,UAAU,EAE3D,MAAM,OAAO,CAAA;AAKd,MAAM,CAAC,OAAO,OAAO,MAAM;IACvB,CAAC,EAAE,KAAK,CAAC;IAET,QAAQ,EAAE,KAAK,CAAa;IAC5B,SAAS,EAAE,KAAK,CAAa;gBAEhB,CAAC,EAAE,KAAK;IAOrB,OAAO,CAAC,SAAS;IAMjB,OAAO,CAAC,gBAAgB;IA8BxB,OAAO,CAAC,oBAAoB;IA4O5B,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;CAsBR"}
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"}