]>
git.kianting.info Git - clo/blob - src/index.js
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
]; } };
8 Object
.defineProperty(o
, k2
, desc
);
9 }) : (function(o
, m
, k
, k2
) {
10 if (k2
=== undefined) k2
= k
;
13 var __setModuleDefault
= (this && this.__setModuleDefault
) || (Object
.create
? (function(o
, v
) {
14 Object
.defineProperty(o
, "default", { enumerable
: true, value
: v
});
18 var __importStar
= (this && this.__importStar
) || function (mod
) {
19 if (mod
&& mod
.__esModule
) return mod
;
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
);
25 var __importDefault
= (this && this.__importDefault
) || function (mod
) {
26 return (mod
&& mod
.__esModule
) ? mod
: { "default": mod
};
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"));
38 * convert a `tkTree` AST to S-expr string
39 * @param t the `tkTree`
40 * @returns S-expr String
42 function tkTreeToSExp(t
) {
44 if (Array
.isArray(t
)) {
45 let strArray
= t
.map((x
) => tkTreeToSExp(x
));
46 str
= "(" + strArray
.join(" ") + ")";
49 if (t
=== undefined) {
58 exports
.tkTreeToSExp
= tkTreeToSExp
;
59 /**inspect the inner of the representation. */
60 let repr
= (x
) => { return util
.inspect(x
, { depth
: null }); };
66 return { _tag
: "Some", value
: x
};
69 * like `m ==> f` in ocaml
70 * @param m matchee wrapped
71 * @param f matching function
72 * @returns wrapped result
74 function thenDo(m
, f
) {
75 if (m
._tag
== "None") {
80 if (a
._tag
== "Some") {
81 a
.value
.ast
= m
.value
.ast
.concat(a
.value
.ast
);
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`.
93 function matchAny(m
) {
94 if (m
.remained
.length
>= 1) {
96 _tag
: "Some", value
: {
97 matched
: m
.matched
.concat(m
.remained
[0]),
98 remained
: m
.remained
.slice(1),
104 return { _tag
: "None" };
107 exports
.matchAny
= matchAny
;
109 * like `f1 | f2` in regex
110 * @param f1 the first tried function
111 * @param f2 the second tried function
112 * @returns wrapped result
114 function orDo(f1
, f2
) {
117 if (res1
._tag
== "Some") {
128 * @param f input token function. one token only.
129 * @returns combined finction
134 if (res1
._tag
== "Some") {
135 return { _tag
: "None" };
138 let res2
= matchAny(x
);
143 function matchToken(typeName
, value
) {
145 let headToken
= t
.remained
[0];
146 if (headToken
.type
!= typeName
) {
147 return { _tag
: "None" };
150 if (value
=== undefined || value
== headToken
.value
) {
152 matched
: t
.matched
.concat(headToken
),
153 remained
: t
.remained
.slice(1),
156 return { _tag
: "Some", value
: newTokenPair
};
159 return { _tag
: "None" };
170 const tokens
= Array
.from((0, js_tokens_1
.default)(`import; foo from\t 'bar';
171 import * as util from 'util';
174 花非花,霧\\{非霧 。{{foo();}}下
176 console
.log("RESULT=" + repr(tokens
));
177 var mainTokenPair
= {
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
));