]> git.kianting.info Git - clo/blob - src/index.js
add basic function
[clo] / src / index.js
1 "use strict";
2 var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9 }) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12 }));
13 var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15 }) : function(o, v) {
16 o["default"] = v;
17 });
18 var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24 };
25 var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27 };
28 Object.defineProperty(exports, "__esModule", { value: true });
29 exports.matchAny = exports.tkTreeToSExp = void 0;
30 var fs = require('fs');
31 const js_tokens_1 = __importDefault(require("js-tokens"));
32 const util = __importStar(require("util"));
33 /**
34 *
35 * # REPRESENTATION
36 */
37 /**
38 * convert a `tkTree` AST to S-expr string
39 * @param t the `tkTree`
40 * @returns S-expr String
41 */
42 function tkTreeToSExp(t) {
43 var str = "";
44 if (Array.isArray(t)) {
45 let strArray = t.map((x) => tkTreeToSExp(x));
46 str = "(" + strArray.join(" ") + ")";
47 }
48 else {
49 if (t === undefined) {
50 str = "%undefined";
51 }
52 else {
53 str = t.value;
54 }
55 }
56 return str;
57 }
58 exports.tkTreeToSExp = tkTreeToSExp;
59 /**inspect the inner of the representation. */
60 let repr = (x) => { return util.inspect(x, { depth: null }); };
61 /**
62 *
63 * # PARSER UNITS
64 */
65 function toSome(x) {
66 return { _tag: "Some", value: x };
67 }
68 /**
69 * like `m ==> f` in ocaml
70 * @param m matchee wrapped
71 * @param f matching function
72 * @returns wrapped result
73 */
74 function thenDo(m, f) {
75 if (m._tag == "None") {
76 return m;
77 }
78 else {
79 var a = f(m.value);
80 if (a._tag == "Some") {
81 a.value.ast = m.value.ast.concat(a.value.ast);
82 }
83 return a;
84 }
85 }
86 /**
87 *
88 * @param m : the `TokenPair` to be consumed.
89 * @returns if the length of `m.remained` >= 1; consumes the matchee by 1 token
90 * and wraps it in `Some`,
91 * otherwise, returns `None`.
92 */
93 function matchAny(m) {
94 if (m.remained.length >= 1) {
95 return {
96 _tag: "Some", value: {
97 matched: m.matched.concat(m.remained[0]),
98 remained: m.remained.slice(1),
99 ast: [m.remained[0]],
100 }
101 };
102 }
103 else {
104 return { _tag: "None" };
105 }
106 }
107 exports.matchAny = matchAny;
108 /**
109 * like `f1 | f2` in regex
110 * @param f1 the first tried function
111 * @param f2 the second tried function
112 * @returns wrapped result
113 */
114 function orDo(f1, f2) {
115 return (x) => {
116 let res1 = f1(x);
117 if (res1._tag == "Some") {
118 return res1;
119 }
120 else {
121 let res2 = f2(x);
122 return res2;
123 }
124 };
125 }
126 /**
127 * like regex [^c]
128 * @param f input token function. one token only.
129 * @returns combined finction
130 */
131 function notDo(f) {
132 return (x) => {
133 let res1 = f(x);
134 if (res1._tag == "Some") {
135 return { _tag: "None" };
136 }
137 else {
138 let res2 = matchAny(x);
139 return res2;
140 }
141 };
142 }
143 function matchToken(typeName, value) {
144 return (t) => {
145 let headToken = t.remained[0];
146 if (headToken.type != typeName) {
147 return { _tag: "None" };
148 }
149 else {
150 if (value === undefined || value == headToken.value) {
151 let newTokenPair = {
152 matched: t.matched.concat(headToken),
153 remained: t.remained.slice(1),
154 ast: [headToken]
155 };
156 return { _tag: "Some", value: newTokenPair };
157 }
158 else {
159 return { _tag: "None" };
160 }
161 }
162 ;
163 };
164 }
165 ;
166 /**
167 *
168 * # TEST
169 */
170 const tokens = Array.from((0, js_tokens_1.default)(`import; foo from\t 'bar';
171 import * as util from 'util';
172
173
174 花非花,霧\\{非霧 。{{foo();}}下
175 一句`));
176 console.log("RESULT=" + repr(tokens));
177 var mainTokenPair = {
178 matched: [],
179 remained: tokens,
180 ast: []
181 };
182 let a = thenDo(thenDo(toSome(mainTokenPair), matchToken('IdentifierName')), notDo(matchToken('Punctuator', ';')));
183 console.log("RESULT=" + repr(a));
184 if (a._tag == "Some") {
185 console.log("SEXP=" + tkTreeToSExp(a.value.ast));
186 }