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