forked from kframework/javascript-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk.patch
216 lines (206 loc) · 9.26 KB
/
k.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
diff --git a/include/builtins/string.k b/include/builtins/string.k
index 7ea0100..9bd8f40 100644
--- a/include/builtins/string.k
+++ b/include/builtins/string.k
@@ -97,7 +97,7 @@ module STRING-HOOKS
rule replace(Source:K, _, _, 0) => Source
rule replaceAll(Source:K, ToReplace:K, Replacement:K) => replace(Source, ToReplace, Replacement, countAllOccurences(Source, ToReplace))
- rule ltrim(S:String) => replaceFirst(S, " ", "") when findString(S, " ", 0) ==Int 0
+ rule ltrim(S:String) => ltrim(replaceFirst(S, " ", "")) when findString(S, " ", 0) ==Int 0
rule ltrim(S:String) => S when findString(S, " ", 0) =/=Int 0
rule ltrim("") => ""
diff --git a/include/io/uris.k b/include/io/uris.k
index 4f54b6d..211f20d 100644
--- a/include/io/uris.k
+++ b/include/io/uris.k
@@ -23,7 +23,8 @@ module URIS
| "#lstat" "(" String ")" [function, hook(#IO:#lstat)]
| "#opendir" "(" String ")" [function, hook(#IO:#opendir)]
- syntax KItem ::= "#parse" "(" String "," String ")" [function, klabel(#parse), hook(#K-PARSER:#parse)]
+ syntax KItem ::= "#parse" "(" String "," String ")" [function, klabel(#parse), hook(#K-PARSER:#parse)]
+ syntax KItem ::= "#parseExternal" "(" String "," String ")" [function, klabel(#parseExternal), hook(#K-PARSER:#parseExternal)]
rule #open(S:String) => #open(S:String, "rw")
rule #open(S:String, Mode:String) => #string2Int((#tcpRequest("open\x01" +String S +String "\x01" +String Mode +String "\x01"))::K) when Mode ==String "r" orBool Mode ==String "w" orBool Mode ==String "rw"
@@ -124,7 +125,8 @@ module URIS
// ------------------------------
// Dynamic parsing
- rule #parse(StringToParse:String, Sort:String) => 'String2K(#tcpRequest("parse\x01" +String Sort +String "\x01" +String StringToParse +String "\x01"))::K
+ rule #parse (StringToParse:String, Sort:String) => 'String2K(#tcpRequest("parse\x01" +String Sort +String "\x01" +String StringToParse +String "\x01"))::K
+ rule #parseExternal(StringToParse:String, Sort:String) => 'String2K(#tcpRequest("parseExternal\x01" +String Sort +String "\x01" +String StringToParse +String "\x01"))::K
syntax K ::= String2K(String) [function, hook(#K-PARSER:string2k)]
syntax Bag ::= String2Bag(String) [function, hook(#K-PARSER:string2bag)]
@@ -133,4 +135,7 @@ module URIS
rule 'String2K(#noparse) => #noparse [simplification]
+ syntax String ::= "#external" "(" String ")" [function, klabel(#external), hook(#K-PARSER:#external)]
+ rule #external(Cmd:String) => #tcpRequest("external\x01" +String Cmd +String "\x01")
+
endmodule
diff --git a/src/javasources/KTool/src/org/kframework/krun/ioserver/commands/CommandExternal.java b/src/javasources/KTool/src/org/kframework/krun/ioserver/commands/CommandExternal.java
new file mode 100644
index 0000000..2092a2f
--- /dev/null
+++ b/src/javasources/KTool/src/org/kframework/krun/ioserver/commands/CommandExternal.java
@@ -0,0 +1,60 @@
+package org.kframework.krun.ioserver.commands;
+
+import org.kframework.backend.maude.MaudeFilter;
+import org.kframework.kil.Term;
+import org.kframework.kil.loader.Context;
+import org.kframework.kil.visitors.exceptions.TransformerException;
+import org.kframework.krun.K;
+import org.kframework.krun.RunProcess;
+import org.kframework.krun.api.io.FileSystem;
+import org.kframework.utils.errorsystem.KException;
+import org.kframework.utils.errorsystem.KException.ExceptionType;
+import org.kframework.utils.errorsystem.KException.KExceptionGroup;
+
+import java.net.Socket;
+import java.util.logging.Logger;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class CommandExternal extends Command {
+
+ private String cmd;
+ protected Context context;
+
+ public CommandExternal(String[] args, Socket socket, Logger logger, Context context, FileSystem fs) {
+ super(args, socket, logger, fs);
+ this.context = context;
+
+ cmd = args[1];
+ }
+
+ public void run() {
+ try {
+ RunProcess rp = new RunProcess();
+
+ List<String> tokens = new ArrayList<>();
+ tokens.add("node");
+ tokens.add("-p");
+ tokens.add("-e");
+ tokens.add(cmd);
+ //for (String c : tokens) { System.out.println(c); }
+ Map<String, String> environment = new HashMap<>();
+ rp.execute(environment, tokens.toArray(new String[tokens.size()]));
+
+ if (rp.getExitCode() != 0) {
+ throw new TransformerException(new KException(ExceptionType.ERROR, KExceptionGroup.CRITICAL, "External returned a non-zero exit code: " + rp.getExitCode() + "\nStdout:\n" + rp.getStdout() + "\nStderr:\n" + rp.getErr()));
+ }
+
+ String result = rp.getStdout() != null ? rp.getStdout() : "";
+
+ succeed(result.trim());
+
+ } catch (TransformerException e) {
+ //fail("noparse");
+ fail("failed: " + e.getMessage());
+ }
+ }
+}
diff --git a/src/javasources/KTool/src/org/kframework/krun/ioserver/commands/CommandParseExternal.java b/src/javasources/KTool/src/org/kframework/krun/ioserver/commands/CommandParseExternal.java
new file mode 100644
index 0000000..b9f9745
--- /dev/null
+++ b/src/javasources/KTool/src/org/kframework/krun/ioserver/commands/CommandParseExternal.java
@@ -0,0 +1,39 @@
+package org.kframework.krun.ioserver.commands;
+
+import org.kframework.backend.maude.MaudeFilter;
+import org.kframework.kil.Term;
+import org.kframework.kil.loader.Context;
+import org.kframework.kil.visitors.exceptions.TransformerException;
+import org.kframework.krun.K;
+import org.kframework.krun.RunProcess;
+import org.kframework.krun.api.io.FileSystem;
+
+import java.net.Socket;
+import java.util.logging.Logger;
+
+public class CommandParseExternal extends Command {
+
+ private String stringToParse;
+ private String sort;
+ protected Context context;
+
+ public CommandParseExternal(String[] args, Socket socket, Logger logger, Context context, FileSystem fs) {
+ super(args, socket, logger, fs);
+ this.context = context;
+
+ sort = args[1];
+ stringToParse = args[2];
+ }
+
+ public void run() {
+ try {
+ RunProcess rp = new RunProcess();
+ Term kast = rp.runParser(K.getProgramParser(), stringToParse, true, sort, context);
+ MaudeFilter mf = new MaudeFilter(context);
+ kast.accept(mf);
+ succeed(mf.getResult().toString());
+ } catch (TransformerException e) {
+ fail("noparse");
+ }
+ }
+}
diff --git a/src/javasources/KTool/src/org/kframework/krun/ioserver/main/IOServer.java b/src/javasources/KTool/src/org/kframework/krun/ioserver/main/IOServer.java
index e920d19..a904e8b 100644
--- a/src/javasources/KTool/src/org/kframework/krun/ioserver/main/IOServer.java
+++ b/src/javasources/KTool/src/org/kframework/krun/ioserver/main/IOServer.java
@@ -191,6 +191,12 @@ public class IOServer {
if (command.equals("parse")) {
return new CommandParse(args, socket, logger, context, fs);
}
+ if (command.equals("parseExternal")) {
+ return new CommandParseExternal(args, socket, logger, context, fs);
+ }
+ if (command.equals("external")) {
+ return new CommandExternal(args, socket, logger, context, fs);
+ }
return new CommandUnknown(args, socket, logger, fs); //, (long) 0);
}
diff --git a/src/javasources/KTool/src/org/kframework/utils/StringUtil.java b/src/javasources/KTool/src/org/kframework/utils/StringUtil.java
index f2090d7..e6712bb 100644
--- a/src/javasources/KTool/src/org/kframework/utils/StringUtil.java
+++ b/src/javasources/KTool/src/org/kframework/utils/StringUtil.java
@@ -111,15 +111,24 @@ public class StringUtil {
} else if (str.charAt(i + 1) == 't') {
sb.append('\t');
i++;
+ } else if (str.charAt(i + 1) == 'f') {
+ sb.append(' ');
+ i++;
} else if (str.charAt(i + 1) == 'x') {
String arg = str.substring(i + 2, i + 4);
sb.append((char)Integer.parseInt(arg, 16));
i += 3;
} else if (str.charAt(i + 1) == 'u') {
String arg = str.substring(i + 2, i + 6);
+ if (arg.equals("00A0") || arg.equals("00a0")) {
+ sb.append(' ');
+ } else if (arg.equals("2028") || arg.equals("2029")) {
+ sb.append('\n');
+ } else {
int codePoint = Integer.parseInt(arg, 16);
StringUtil.throwIfSurrogatePair(codePoint);
sb.append((char)codePoint);
+ }
i += 5;
} else if (str.charAt(i + 1) == 'U') {
String arg = str.substring(i + 2, i + 10);
diff --git a/src/javasources/parsers/Concrete/syntax/KBuiltinsBasic.sdf b/src/javasources/parsers/Concrete/syntax/KBuiltinsBasic.sdf
index 963e291..edb427b 100644
--- a/src/javasources/parsers/Concrete/syntax/KBuiltinsBasic.sdf
+++ b/src/javasources/parsers/Concrete/syntax/KBuiltinsBasic.sdf
@@ -20,6 +20,9 @@ lexical syntax
"\"" StringCharDz* "\"" -> DzDzSTRING
~[\"\n\r\\] -> StringCharDz
[\\]~[\n\r] -> StringCharDz
+ "'" StringChar1Dz* "'" -> DzDzSTRING
+ ~[\'\n\r\\] -> StringChar1Dz
+ [\\]~[\n\r] -> StringChar1Dz
%% KLabel definition + restrictions lower
"'" KLabelCharDz+ -> KLabelDz