1 | static new AtomicInteger nlParse_count; // how often did we parse something? |
2 | |
3 | static Lisp nlParse(S s) { |
4 | ret nlParse(s, true); |
5 | } |
6 | |
7 | static Lisp nlParse(S s, boolean unbracket) { |
8 | ret nlParse(codeTokens(snlTok(s)), unbracket, false, null); |
9 | } |
10 | |
11 | static Lisp nlParse(S s, boolean unbracket, boolean explicitBrackets) { |
12 | ret nlParse(codeTokens(snlTok(s)), unbracket, explicitBrackets, null); |
13 | } |
14 | |
15 | static Lisp nlParse(L<S> tok) { |
16 | ret nlParse(tok, true, false, null); |
17 | } |
18 | |
19 | // unbracket refers to outermost brackets |
20 | // explicitBrackets refers to inner brackets |
21 | static Lisp nlParse(L<S> tok, boolean unbracket, final boolean explicitBrackets, L<S> warnings) { |
22 | nlParse_count.incrementAndGet(); |
23 | |
24 | class Entry { |
25 | int i; |
26 | Lisp tree; |
27 | S wrapper; |
28 | |
29 | *(int *i) { |
30 | tree = lisp("[]"); |
31 | } |
32 | |
33 | *(int *i, S bracket) { |
34 | tree = lisp("[]"); |
35 | wrapper = bracket; |
36 | } |
37 | |
38 | Lisp wrapped() { |
39 | Lisp t = explicitBrackets ? tree : nlUnbracket(tree); |
40 | ret wrapper == null ? t : lisp(wrapper, t); |
41 | } |
42 | } |
43 | |
44 | new L<Entry> stack; |
45 | stack.add(new Entry(0)); |
46 | |
47 | for (int i = 0; i < l(tok); i++) { |
48 | S t = tok.get(i); |
49 | if (eq(t, "[") || eq(t, "(")) { |
50 | stack.add(new Entry(i, eq(t, "(") ? "()" : null)); |
51 | } else if (eq(t, "]") || eq(t, ")")) { |
52 | if (l(stack) == 1) |
53 | warn("too many closing brackets", warnings); |
54 | else { |
55 | Entry e = popLast(stack); |
56 | /*if (!bracketsMatch(tok.get(e.i), t)) |
57 | warn("non-matching brackets");*/ |
58 | last(stack).tree.add(e.wrapped()); |
59 | } |
60 | } else |
61 | last(stack).tree.add(t); |
62 | } |
63 | |
64 | while (l(stack) > 1) { |
65 | warn("too many opening brackets", warnings); |
66 | Entry e = popLast(stack); |
67 | last(stack).tree.add(e.wrapped()); |
68 | } |
69 | |
70 | Lisp result = last(stack).wrapped(); |
71 | ret unbracket ? nlUnbracket(result) : result; |
72 | } |
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1002839 |
Snippet name: | nlParse |
Eternal ID of this version: | #1002839/1 |
Text MD5: | 5f482de23f9ca0569efe9f8bd4866911 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-04-10 23:06:33 |
Source code size: | 1905 bytes / 72 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 740 / 911 |
Referenced in: | [show references] |