@fraczak/k 1.3.3 → 1.3.4

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/INTRO.md CHANGED
@@ -129,7 +129,7 @@ names are integers starting from zero. E.g., `{.toto "0", .titi "1",
129
129
  zero? = [(),0] EQ 0;
130
130
  factorial = <
131
131
  zero? 1,
132
- [(dec factorial), ()] TIMES
132
+ [dec factorial, ()] TIMES
133
133
  >;
134
134
  { () x, factorial "x!" }
135
135
 
@@ -137,10 +137,24 @@ names are integers starting from zero. E.g., `{.toto "0", .titi "1",
137
137
 
138
138
  _Codes_ (prefixed by `$`) can be defined by taged union and product. E.g.:
139
139
 
140
- $nat = <nat 1, {} 0>; -- e.g., {{{{} 0} 1} 1}
141
- $pair = {nat x, nat y}; -- e.g., {{{} 0} x, {{{{} 0} 1} 1} y}
140
+ $nat = <nat 1, {} 0>;
141
+ $pair = {nat x, nat y};
142
142
 
143
- suc = $nat {() 1} $nat;
144
- add = $pair <{.x.1 x, .y suc y} add, .y> $nat;
143
+ suc = {$nat 1};
144
+ add = $pair <{.x.1 x, .y suc y} add, .y>;
145
+
146
+ ### Basic extension codes
147
+
148
+ Since _basic extension_ introduces integers, booleans, and strings, there are three
149
+ predefined types: `int`, `bool`, and `string`. A vector product code can also be defined
150
+ by `[ codeExp ]`. All members of the vector are the same code. E.g.,
151
+
152
+ $intVector = [ int ];
153
+ $boolVector = [ bool ];
154
+ $tree = [ tree ];
155
+
156
+ emptyList? = $intVector $[ string ]; -- as only an empty vector can be a vector
157
+ -- of integers and a vector of strings
158
+
145
159
 
146
160
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraczak/k",
3
- "version": "1.3.3",
3
+ "version": "1.3.4",
4
4
  "description": "k-language for JSON-like data transformation",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/parser.jison CHANGED
@@ -2,6 +2,26 @@
2
2
 
3
3
  var s = require("./symbol-table");
4
4
 
5
+ function fromEscString(escString) {
6
+ const isSingleQuoted = escString.startsWith("'");
7
+ let str = escString.substring(1, escString.length - 1);
8
+ if (isSingleQuoted) {
9
+ str = str.replace(/\\'/g, "'");
10
+ } else {
11
+ str = str.replace(/\\"/g, '"');
12
+ }
13
+ str = str.replace(/\\\\/g, '\\');
14
+ str = str.replace(/\\\//g, '/');
15
+ str = str.replace(/\\b/g, '\b');
16
+ str = str.replace(/\\f/g, '\f');
17
+ str = str.replace(/\\n/g, '\n');
18
+ str = str.replace(/\\r/g, '\r');
19
+ str = str.replace(/\\t/g, '\t');
20
+ str = str.replace(/\\u([\dA-F]{4})/gi, (match, p1) => String.fromCharCode(parseInt(p1, 16)));
21
+
22
+ return str;
23
+ }
24
+
5
25
  %}
6
26
 
7
27
  %lex
@@ -24,7 +44,7 @@ var s = require("./symbol-table");
24
44
  ";" return 'SC';
25
45
  ":" return 'COL';
26
46
  "$" return 'DOLLAR';
27
- \"[^\"\n]*\"|\'[^\'\n]*\' return 'STRING'
47
+ \"([^"\\]|\\(.|\n))*\"|\'([^'\\]|\\(.|\n))*\' return 'STRING';
28
48
  [a-zA-Z_][a-zA-Z0-9_?!]* return 'NAME';
29
49
  0|[-]?[1-9][0-9]* return 'INT';
30
50
  <<EOF>> return 'EOF';
@@ -40,11 +60,11 @@ var s = require("./symbol-table");
40
60
  %%
41
61
 
42
62
  name: NAME { $$ = String(yytext); };
43
- str: STRING { $$ = String(yytext).slice(1,-1); };
63
+ str: STRING { $$ = fromEscString(String(yytext)); };
44
64
  int: INT { $$ = parseInt(String(yytext)); };
45
65
 
46
66
  input_with_eof: defs comp EOF {
47
- result = {defs: {rels: s.rels, codes: s.codes}, exp: $2};
67
+ const result = {defs: {rels: s.rels, codes: s.codes}, exp: $2};
48
68
  // console.log(JSON.stringify(result, "", 2));
49
69
  return result;
50
70
  };
package/parser.js CHANGED
@@ -76,6 +76,26 @@
76
76
 
77
77
  var s = require("./symbol-table");
78
78
 
79
+ function fromEscString(escString) {
80
+ const isSingleQuoted = escString.startsWith("'");
81
+ let str = escString.substring(1, escString.length - 1);
82
+ if (isSingleQuoted) {
83
+ str = str.replace(/\\'/g, "'");
84
+ } else {
85
+ str = str.replace(/\\"/g, '"');
86
+ }
87
+ str = str.replace(/\\\\/g, '\\');
88
+ str = str.replace(/\\\//g, '/');
89
+ str = str.replace(/\\b/g, '\b');
90
+ str = str.replace(/\\f/g, '\f');
91
+ str = str.replace(/\\n/g, '\n');
92
+ str = str.replace(/\\r/g, '\r');
93
+ str = str.replace(/\\t/g, '\t');
94
+ str = str.replace(/\\u([\dA-F]{4})/gi, (match, p1) => String.fromCharCode(parseInt(p1, 16)));
95
+
96
+ return str;
97
+ }
98
+
79
99
 
80
100
 
81
101
  var parser = (function(){
@@ -94,14 +114,14 @@ case 1:
94
114
  this.$ = String(yytext);
95
115
  break;
96
116
  case 2:
97
- this.$ = String(yytext).slice(1,-1);
117
+ this.$ = fromEscString(String(yytext));
98
118
  break;
99
119
  case 3:
100
120
  this.$ = parseInt(String(yytext));
101
121
  break;
102
122
  case 4:
103
123
 
104
- result = {defs: {rels: s.rels, codes: s.codes}, exp: $$[$0-1]};
124
+ const result = {defs: {rels: s.rels, codes: s.codes}, exp: $$[$0-1]};
105
125
  // console.log(JSON.stringify(result, "", 2));
106
126
  return result;
107
127
 
@@ -713,7 +733,7 @@ case 15:return 28;
713
733
  break;
714
734
  case 16:return 15;
715
735
  break;
716
- case 17:return 6
736
+ case 17:return 6;
717
737
  break;
718
738
  case 18:return 4;
719
739
  break;
@@ -723,7 +743,7 @@ case 20:return 12;
723
743
  break;
724
744
  }
725
745
  },
726
- rules: [/^(?:[/][*]([*][^/]|[^*])*[*][/])/,/^(?:(\/\/|#|%|--)[^\n]*)/,/^(?:\s+)/,/^(?:<)/,/^(?:\{)/,/^(?:\[)/,/^(?:\()/,/^(?:>)/,/^(?:\})/,/^(?:\])/,/^(?:\))/,/^(?:=)/,/^(?:\.)/,/^(?:,)/,/^(?:;)/,/^(?::)/,/^(?:\$)/,/^(?:"[^\"\n]*"|'[^\'\n]*')/,/^(?:[a-zA-Z_][a-zA-Z0-9_?!]*)/,/^(?:0|[-]?[1-9][0-9]*)/,/^(?:$)/],
746
+ rules: [/^(?:[/][*]([*][^/]|[^*])*[*][/])/,/^(?:(\/\/|#|%|--)[^\n]*)/,/^(?:\s+)/,/^(?:<)/,/^(?:\{)/,/^(?:\[)/,/^(?:\()/,/^(?:>)/,/^(?:\})/,/^(?:\])/,/^(?:\))/,/^(?:=)/,/^(?:\.)/,/^(?:,)/,/^(?:;)/,/^(?::)/,/^(?:\$)/,/^(?:"([^"\\]|\\(.|\n))*"|'([^'\\]|\\(.|\n))*')/,/^(?:[a-zA-Z_][a-zA-Z0-9_?!]*)/,/^(?:0|[-]?[1-9][0-9]*)/,/^(?:$)/],
727
747
  conditions: {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],"inclusive":true}}
728
748
  });
729
749
  return lexer;