@fraczak/k 2.1.6 → 3.0.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/parser.jison CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  import s from "./symbol-table.mjs";
4
4
 
5
+ function getToken(yytext,yy,lstack) {
6
+ const yylloc = yy.lexer.yylloc;
7
+ const loc = lstack[lstack.length - 1]; // { first_line: 5, last_line: 5, first_column: 2, last_column: 3 };
8
+ const start = {line: loc.first_line, column: loc.first_column + 1};
9
+ const end = {line: start.line, column: start.column + yytext.length};
10
+ const result = {start,end,value: String(yytext)};
11
+ // console.log(result);
12
+ return result;
13
+ }
14
+
5
15
  function fromEscString(escString) {
6
16
  const isSingleQuoted = escString.startsWith("'");
7
17
  let str = escString.substring(1, escString.length - 1);
@@ -27,9 +37,9 @@ function fromEscString(escString) {
27
37
  %lex
28
38
 
29
39
  %%
30
- [/][*]([*][^/]|[^*])*[*][/] /* c-comment */
31
- ("//"|"#"|"%"|"--")[^\n]* /* one line comment */
32
- \s+ /* blanks */
40
+ [/][*]([*][^/]|[^*])*[*][/] /* c-comment */
41
+ ("//"|"#"|"%"|"--")[^\n]* /* one line comment */
42
+ \s+ /* blanks */
33
43
  "<" return 'LA';
34
44
  "{" return 'LC';
35
45
  "[" return 'LB';
@@ -39,11 +49,13 @@ function fromEscString(escString) {
39
49
  "]" return 'RB';
40
50
  ")" return 'RP';
41
51
  "=" return 'EQ';
52
+ "..." return 'DOTS';
42
53
  "." return 'DOT';
43
54
  "," return 'COMMA';
44
55
  ";" return 'SC';
45
56
  ":" return 'COL';
46
57
  "$" return 'DOLLAR';
58
+ "?" return 'QMARK';
47
59
  "|" return 'PIPE';
48
60
  "^" return 'CARET';
49
61
  \"([^"\\]|\\(.|\n))*\"|\'([^'\\]|\\(.|\n))*\' return 'STRING';
@@ -54,41 +66,58 @@ function fromEscString(escString) {
54
66
  /lex
55
67
 
56
68
  %token NAME STRING INT
57
- %token LA LC LB LP RA RP RB RC EQ DOT COMMA SC COL DOLLAR PIPE CARET
69
+ %token LA LC LB LP RA RP RB RC EQ DOT COMMA SC COL DOLLAR PIPE CARET QMARK DOTS
58
70
  %token EOF
59
71
 
60
72
  %start input_with_eof
61
73
 
62
74
  %%
63
75
 
64
- name: NAME { $$ = String(yytext); };
65
- str: STRING { $$ = fromEscString(String(yytext)); };
66
- int: INT { $$ = parseInt(String(yytext)); };
76
+ name: NAME { $$ = getToken(yytext,yy,_$); };
77
+ str: STRING { $$ = getToken(yytext,yy,_$); $$.value = fromEscString($$.value);};
78
+ int: INT { $$ = getToken(yytext,yy,_$); $$.value = parseInt($$.value); };
79
+ la: LA { $$ = getToken(yytext,yy,_$); };
80
+ lc: LC { $$ = getToken(yytext,yy,_$); };
81
+ lb: LB { $$ = getToken(yytext,yy,_$); };
82
+ lp: LP { $$ = getToken(yytext,yy,_$); };
83
+ ra: RA { $$ = getToken(yytext,yy,_$); };
84
+ rc: RC { $$ = getToken(yytext,yy,_$); };
85
+ rb: RB { $$ = getToken(yytext,yy,_$); };
86
+ rp: RP { $$ = getToken(yytext,yy,_$); };
87
+ eq: EQ { $$ = getToken(yytext,yy,_$); };
88
+ dots: DOTS { $$ = getToken(yytext,yy,_$); };
89
+ dot: DOT { $$ = getToken(yytext,yy,_$); };
90
+ comma: COMMA { $$ = getToken(yytext,yy,_$); };
91
+ sc: SC { $$ = getToken(yytext,yy,_$); };
92
+ col: COL { $$ = getToken(yytext,yy,_$); };
93
+ dollar: DOLLAR { $$ = getToken(yytext,yy,_$); };
94
+ qmark: QMARK { $$ = getToken(yytext,yy,_$); };
67
95
 
68
96
  input_with_eof: defs comp EOF {
69
97
  const result = {defs: {rels: s.rels, codes: s.codes}, exp: $2};
70
98
  // console.log(JSON.stringify(result, "", 2));
99
+ // process.exit(0);
71
100
  return result;
72
101
  };
73
102
 
74
103
  defs: { }
75
- | defs name EQ comp SC { s.add_rel($2,$4); }
76
- | defs DOLLAR name EQ codeDef SC { s.add_code($3,$5); }
104
+ | defs name eq comp sc { s.add_rel($2.value,$4); }
105
+ | defs dollar name eq codeDef SC { s.add_code($3.value,$5); }
77
106
  ;
78
107
 
79
108
  code
80
- : name { $$ = {code: "ref", ref: $1}; }
109
+ : name { $$ = { code: "ref", ref: $1.value, start: $1.start, end: $1.end}; }
81
110
  | codeDef { $$ = $1; }
82
111
  ;
83
112
 
84
113
  codeDef
85
- : LC labelled_codes RC { $$ = {code: "product", product: $2}; }
86
- | LB code RB { $$ = {code: "vector", vector: s.as_ref($2)}; }
87
- | LA labelled_codes RA { $$ = {code: "union", union: $2}; }
114
+ : lc labelled_codes rc { $$ = { code: "product", product: $2, start: $1.start, end: $3.end }; }
115
+ | lb code rb { $$ = { code: "vector", vector: s.as_ref($2), start: $1.start, end: $3.end }; }
116
+ | la labelled_codes ra { $$ = { code: "union", union: $2, start: $1.start, end: $3.end }; }
88
117
  ;
89
118
 
90
119
  labelled_codes
91
- : { $$ = {} }
120
+ : { $$ = {}; }
92
121
  | non_empty_labelled_codes { $$ = $1.reduce((r, lc) => {
93
122
  r[lc.label] = s.as_ref(lc.code);
94
123
  return r }
@@ -97,39 +126,77 @@ labelled_codes
97
126
 
98
127
  non_empty_labelled_codes
99
128
  : code_label { $$ = [$1]; }
100
- | non_empty_labelled_codes COMMA code_label
129
+ | non_empty_labelled_codes comma code_label
101
130
  { $$ = [].concat($1,$3); }
102
131
  ;
103
132
 
104
133
  code_label
105
- : code name { $$ = {label: $2, code: $1}; }
106
- | code str { $$ = {label: $2, code: $1}; }
107
- | code int { $$ = {label: $2, code: $1}; }
108
- | name COL code { $$ = {label: $1, code: $3}; }
109
- | str COL code { $$ = {label: $1, code: $3}; }
110
- | int COL code { $$ = {label: $1, code: $3}; }
134
+ : code name { $$ = {label: $2.value, code: $1}; }
135
+ | code str { $$ = {label: $2.value, code: $1}; }
136
+ | code int { $$ = {label: $2.value, code: $1}; }
137
+ | name col code { $$ = {label: $1.value, code: $3}; }
138
+ | str col code { $$ = {label: $1.value, code: $3}; }
139
+ | int col code { $$ = {label: $1.value, code: $3}; }
140
+ ;
141
+
142
+ filter
143
+ : name { $$ = { type: "name", name: $1.value, start: $1.start, end: $1.end}; }
144
+ | dollar code { $$ = { type: "code", code: s.as_ref($2), start: $1.start, end: $2.end}; }
145
+ | lp labelled_filters rp { $$ = { open: $2.open, fields: $2.fields, start: $1.start, end: $3.end}; }
146
+ | la labelled_filters ra { $$ = { type: "union", open: $2.open, fields: $2.fields, start: $1.start, end: $3.end}; }
147
+ | lc labelled_filters rc { $$ = { type: "product", open: $2.open, fields: $2.fields, start: $1.start, end: $3.end}; }
148
+ | lb filter rb { $$ = { type: "vector", vector: $2, start: $1.start, end: $3.end }; }
149
+ ;
150
+
151
+ labelled_filters
152
+ : { $$ = { fields: {}}; }
153
+ | non_empty_labelled_filters { $$ = $1.reduce((r, lc) => {
154
+ if (lc.dots) {
155
+ r.open = true;
156
+ } else {
157
+ r.fields[lc.label] = lc.filter;
158
+ }
159
+ return r }
160
+ , { fields: {} }); }
161
+ ;
162
+
163
+ non_empty_labelled_filters
164
+ : filter_label { $$ = [$1]; }
165
+ | non_empty_labelled_filters comma filter_label
166
+ { $$ = [].concat($1,$3); }
167
+ ;
168
+
169
+ filter_label
170
+ : dots { $$ = {dots: true }; }
171
+ | filter name { $$ = {label: $2.value, filter: $1}; }
172
+ | filter str { $$ = {label: $2.value, filter: $1}; }
173
+ | filter int { $$ = {label: $2.value, filter: $1}; }
174
+ | name col filter { $$ = {label: $1.value, filter: $3}; }
175
+ | str col filter { $$ = {label: $1.value, filter: $3}; }
176
+ | int col filter { $$ = {label: $1.value, filter: $3}; }
111
177
  ;
112
178
 
113
179
  comp
114
180
  : exp { $$ = $1; }
115
- | comp exp { $$ = s.comp($1, $2); }
116
- | comp CARET { $$ = {op: 'caret', caret: $1}; }
181
+ | comp exp { $$ = {...s.comp($1, $2), start: $1.start, end:$2.end}; }
182
+ | comp CARET { $$ = {op: 'caret', caret: $1, start: $1.start, end:$2.end}; }
117
183
  ;
118
184
 
119
185
  exp
120
- : LC labelled RC { $$ = $2; }
121
- | LB list RB { $$ = {op: "vector", vector: $2}; }
122
- | LA list RA { $$ = s.union($2); }
123
- | name { $$ = {op: "ref", ref: $1}; }
124
- | LP RP { $$ = s.identity; }
125
- | LP comp RP { $$ = $2; }
126
- | str { $$ = {op: "str", str: $1}; }
127
- | int { $$ = {op: "int", int: $1}; }
128
- | DOT int { $$ = {op: "dot", dot: $2}; }
129
- | DOT str { $$ = {op: "dot", dot: $2}; }
130
- | DOT name { $$ = {op: "dot", dot: $2}; }
131
- | DOLLAR code { $$ = {op: "code", code: s.as_ref($2)}; }
132
- | PIPE { $$ = {op: "pipe"}; }
186
+ : lc labelled rc { $$ = {...$2, start: $1.start, end: $3.end}; }
187
+ | lb list rb { $$ = {op: "vector", vector: $2, start: $1.start, end: $3.end}; }
188
+ | la list ra { $$ = {...s.union($2), start: $1.start, end: $3.end}; }
189
+ | name { $$ = {op: "ref", ref: $1.value, start: $1.start, end: $1.end}; }
190
+ | lp rp { $$ = {...s.identity, start: $1.start, end: $2.end}; }
191
+ | lp comp rp { $$ = {...$2, start: $1.start, end: $3.end }; }
192
+ | str { $$ = {op: "str", str: $1.value, start: $1.start, end: $1.end }; }
193
+ | int { $$ = {op: "int", int: $1.value, start: $1.start, end: $1.end }; }
194
+ | dot int { $$ = {op: "dot", dot: $2.value, start: $1.start, end: $2.end }; }
195
+ | dot str { $$ = {op: "dot", dot: $2.value, start: $1.start, end: $2.end }; }
196
+ | dot name { $$ = {op: "dot", dot: $2.value, start: $1.start, end: $2.end }; }
197
+ | dollar code { $$ = {op: "code", code: s.as_ref($2), start: $1.start, end: $2.end}; }
198
+ | qmark filter { $$ = {op: "filter", filter: $2, start: $1.start, end:$2.end}; }
199
+ | PIPE { $$ = {op: "pipe", start: $1.start, end: $1.end}; }
133
200
  ;
134
201
 
135
202
  labelled
@@ -140,17 +207,17 @@ labelled
140
207
  non_empty_labelled
141
208
  : comp_label
142
209
  { $$ = {op: "product", product: [$1]}; }
143
- | non_empty_labelled COMMA comp_label
210
+ | non_empty_labelled comma comp_label
144
211
  { $1.product = [].concat($1.product,$3); $$ = $1; }
145
212
  ;
146
213
 
147
214
  comp_label
148
- : comp name { $$ = {label: $2, exp: $1}; }
149
- | comp str { $$ = {label: $2, exp: $1}; }
150
- | comp int { $$ = {label: $2, exp: $1}; }
151
- | name COL comp { $$ = {label: $1, exp: $3}; }
152
- | str COL comp { $$ = {label: $1, exp: $3}; }
153
- | int COL comp { $$ = {label: $1, exp: $3}; }
215
+ : comp name { $$ = {label: $2.value, exp: $1}; }
216
+ | comp str { $$ = {label: $2.value, exp: $1}; }
217
+ | comp int { $$ = {label: $2.value, exp: $1}; }
218
+ | name col comp { $$ = {label: $1.value, exp: $3}; }
219
+ | str col comp { $$ = {label: $1.value, exp: $3}; }
220
+ | int col comp { $$ = {label: $1.value, exp: $3}; }
154
221
  ;
155
222
 
156
223
  list
@@ -160,7 +227,7 @@ list
160
227
 
161
228
  non_empty_list
162
229
  : comp { $$ = [$1]; }
163
- | non_empty_list COMMA comp { $$ = [].concat($1,$3); }
230
+ | non_empty_list comma comp { $$ = [].concat($1,$3); }
164
231
  ;
165
232
 
166
233
  %%