]> git.kianting.info Git - clo/blob - src/parser.ts
5ffc25961b87dbaed8e8dac179d8abc851fa95bd
[clo] / src / parser.ts
1 /**
2 * parser.ts - parser and js generator of clo.
3 */
4 import * as p from 'typescript-parsec';
5 import { Token } from 'typescript-parsec';
6
7 /**
8 *
9 * # REPRESENTATION
10 */
11
12 /**
13 * convert a `tkTree` AST to S-expr string
14 * @param t the `tkTree`
15 * @returns S-expr String
16 *
17 export function tkTreeToSExp(t: tkTree): string{
18 var str = "";
19
20 if (Array.isArray(t)){
21 let strArray = t.map((x)=>tkTreeToSExp(x));
22 str = "(" + strArray.join("◎") + ")";
23 }else{
24 if (t=== undefined){
25 str = "%undefined"
26 }else{
27 str = t;
28 }
29 }
30
31 return str;
32 }*/
33
34 export type tkTree = string | tkTree[];
35
36 enum TokenKind {
37 Seperator, // ---
38 Semicolon, // ;
39 Number,
40 Op,
41 ExprMark, // @
42 ExcapeAt, // \@
43 Paren,
44 SpaceNL, // \s\t\n\r
45 Id,
46 Str,
47 Comment, // /* ooo */
48 }
49
50 /**
51 * Parsing
52 */
53 const lexer = p.buildLexer([
54 [true, /^\d+(\.\d+)?/g, TokenKind.Number],
55 [true, /^[\\][\\]/g, TokenKind.Op],
56 [true, /^\\\@/g, TokenKind.ExcapeAt],
57 [true, /^\/\*([^/]|\/[^*])*\*\//g, TokenKind.Comment],
58 [true, /^\;/g, TokenKind.Semicolon],
59 [true, /^[-][-][-]/g, TokenKind.Seperator],
60 [true, /^[\+\-\*\/\&\|\!\^\<\>\~\=\?]+/g, TokenKind.Op],
61 [true, /^\@/g, TokenKind.ExprMark],
62 [true, /^[()\[\]{}]/g, TokenKind.Paren],
63 [true, /^[\"]([^\"]|[\\].)*[\"]/g, TokenKind.Str],
64 [true, /^[\']([^\']|[\\].)*[\']/g, TokenKind.Str],
65 [true, /^[()\[\]{}]/g, TokenKind.Paren],
66 [true, /^[^\/\\\@\s\n\t\r;]+/g, TokenKind.Id],
67 [true, /^(\s|\n|\r|\t)+/g, TokenKind.SpaceNL],
68
69 ]);
70
71 /**
72 *
73 * # TEST
74 */
75
76
77
78 const PROG = p.rule<TokenKind, tkTree>();
79 const SEGMENT = p.rule<TokenKind, tkTree>();
80 const IMPORT = p.rule<TokenKind, tkTree>();
81 const IMPORTS = p.rule<TokenKind, tkTree>();
82 const SEMICOLON = p.rule<TokenKind, tkTree>();
83 const NOT_AT_TEXT = p.rule<TokenKind, tkTree>();
84 const CONTENT = p.rule<TokenKind, tkTree>();
85
86
87 function applySegment(input: [Token<TokenKind>, Token<TokenKind>[],
88 Token<TokenKind>]): tkTree[]{
89 let unpackedInnerExprs = input[1].map((x)=>{return x.text});
90 return ["%exprs", unpackedInnerExprs];
91 }
92
93 function applySemiColon(value: Token<TokenKind.Semicolon>): tkTree{
94 return value.text;
95 }
96
97 function applyParts(first: tkTree,
98 second: [Token<TokenKind>, tkTree]):tkTree {
99 return ["%clo", first , second[1]];
100 }
101
102 function applyPartsWithoutImport(parsed: [Token<TokenKind>, tkTree]):tkTree {
103 return ["%clo", "" , parsed[1]];
104 }
105
106
107 function applyComment(value: Token<TokenKind.Comment>): tkTree[]{
108 return [value.text];
109 }
110
111
112 function applyImport(input: [Token<TokenKind>,Token<TokenKind>[], tkTree]) : tkTree{
113 let importTail = input[1].map(x=>x.text);
114 return ["import"].concat(importTail);
115 };
116
117
118 /*
119 function applyImportComment(input: [Token<TokenKind>,Token<TokenKind>[],
120 tkTree, Token<TokenKind.Comment>]) : tkTree{
121 let importTail = input[1].map(x=>x.text);
122 let comment = [input[3].text];
123 return ["import"].concat(importTail).concat(comment);
124 };*/
125
126 function applyImports(input : [tkTree, tkTree[]]): tkTree{
127 let resultBody = [input[0]].concat(input[1]);
128 let resultWrapper = ["%import", resultBody];
129 return resultWrapper;
130 };
131
132
133
134
135 function applyNotAtText(value : Token<TokenKind>): tkTree{
136 if (value.text == "\\\@"){
137 return '@';
138 }
139 else{return value.text;}
140 };
141
142 function applyText (input : tkTree): tkTree[]{
143 return ["%text", input];
144 };
145
146 function applyContent(input : tkTree[]): tkTree[]{
147 return ["%content", input];
148 };
149
150 function applySpaceNL(value : Token<TokenKind.SpaceNL>): tkTree{
151 return value.text;
152 }
153
154 /**
155 * IMPORTEE: Number, Op, Paren, Id, Str, Comment,
156 */
157 let IMPORTEE = p.alt(p.tok(TokenKind.Number),
158 p.tok(TokenKind.Op),
159 p.tok(TokenKind.Paren),
160 p.tok(TokenKind.Id),
161 p.tok(TokenKind.Str),
162 p.tok(TokenKind.SpaceNL),
163 p.tok(TokenKind.Comment));
164
165 let NOT_AT = p.alt(p.tok(TokenKind.Seperator),
166 p.tok(TokenKind.Semicolon),
167 p.tok(TokenKind.Number),
168 p.tok(TokenKind.ExcapeAt),
169 p.tok(TokenKind.Op),
170 p.tok(TokenKind.Paren),
171 p.tok(TokenKind.SpaceNL),
172 p.tok(TokenKind.Id),
173 p.tok(TokenKind.Str),
174 p.tok(TokenKind.Comment),
175 );
176
177 /**
178 * PROG : IMPORTS '---' CONTENT | '---' CONTNENT
179 */
180 PROG.setPattern(
181 p.alt(
182 p.lrec_sc(IMPORTS, p.seq(p.str('---'), CONTENT), applyParts),
183 p.apply(p.seq(p.str('---'), CONTENT), applyPartsWithoutImport))
184
185 )
186
187 /**
188 * NOT_AT_TEXT : NOT_AT
189 */
190 NOT_AT_TEXT.setPattern(
191 p.apply(NOT_AT, applyNotAtText)
192 );
193
194 IMPORTS.setPattern(
195 p.apply( p.seq(IMPORT, p.rep(IMPORT)), applyImports)
196 );
197
198 /**
199 * IMPORT :
200 * 'import' IMPORTEE* SEMICOLON |
201 * COMMENT |
202 */
203 IMPORT.setPattern(
204 p.alt(
205 p.apply(p.seq(p.str('import'), p.rep_sc(IMPORTEE), SEMICOLON),
206 applyImport),
207 p.apply(p.tok(TokenKind.Comment), applyComment),
208 p.apply(p.tok(TokenKind.SpaceNL), applySpaceNL)
209
210 )
211 );
212
213 /**
214 * SEMICOLON : ';';
215 */
216 SEMICOLON.setPattern(
217 p.apply(p.tok(TokenKind.Semicolon), applySemiColon)
218 );
219
220
221
222 /**
223 * SEGMENT : '@' NOT_AT* '@' |
224 * (NOT_AT_TEXT | EXCAPE_AT)*
225 */
226 SEGMENT.setPattern(
227 p.alt(
228 p.apply(p.rep_sc(NOT_AT_TEXT), applyText),
229 p.apply(p.seq(p.str('@'), p.rep(NOT_AT), p.str('@')), applySegment),
230 )
231 );
232
233 /**
234 * CONTENT : SEGMENT*
235 */
236 CONTENT.setPattern(
237 p.apply(p.rep(SEGMENT), applyContent)
238 );
239
240
241
242 /**
243 * the head part of the output JS code : before import
244 */
245 let outputHead = `
246 /* clo, a typesetting engine, generated JS file*/
247 /* CLO: beginning of head*/
248
249 let cloLib = require("./src/libclo/index.js");
250 let clo = new cloLib.Clo();
251
252 /* CLO: end of head*/\n`
253
254 /**
255 * the middle part of the output JS code : between import part and content part
256 */
257 let outputMiddle =`
258 /* CLO: beginning of middle part*/
259 clo.mainStream = /* CLO: end of middle part*/
260 `
261 let outputEnd =`
262 /* CLO: beginning of end part*/
263 clo.generatePdf();
264 /*CLO : end of end part*/
265 `
266
267 /**
268 * Convert `tree` (ASTTree; `tkTree`) to JS Code.
269 */
270 export function treeToJS(tree : tkTree): string{
271
272 let head = tree[0];
273 if (head == "%clo"){
274 let totalResult = outputHead + treeToJS(tree[1]) +
275 outputMiddle + treeToJS(tree[2]) + outputEnd;
276 return totalResult;
277 }
278 if (head == "%import"){
279 let imports = tree[1];
280 if (Array.isArray(imports)){
281 let importsText = imports.map(
282 (x)=>{
283 if (Array.isArray(x)){
284 return x.join('') + ';';
285 }
286 else{
287 return x;
288 }
289 });
290 let importTextCombined = importsText.join('');
291 return importTextCombined;
292 }
293 else{
294 return imports;
295 }
296 }
297 if (head == "%content"){
298 let tail = tree[1];
299 if (Array.isArray(tail)){
300 if (tail.length == 1){
301 return tail.map((x)=>treeToJS(x)).join("').concat('")+ ";";
302 }
303 let tailStrings = tail.map((x)=>treeToJS(x));
304 return "(" + tailStrings.join(').concat(') + ");";
305 }else{
306 return tail;
307 }
308 }
309 if (head == "%text"){
310 let textContents = tree[1];
311 if (Array.isArray(textContents)){
312 let decoratedArray = textContents
313 .flatMap(x=>String(x))
314 .map(x=>x.replace("\`","\\\`"));
315
316 return "[`" + decoratedArray.join("\`, \`") + "`]";
317 }else{
318 let decorated = textContents.replace("\`","\\\`");
319
320 return "[`" + decorated + "`]";
321 }
322 }
323
324 if (head == "%exprs"){
325 let content = tree[1];
326 if (Array.isArray(content)){
327 let flattenContent = content.flat();
328 return flattenContent.join('');
329 }
330 else{
331 return content;
332 }
333
334 }
335 else{
336 if (Array.isArray(tree)){
337 return tree.join('');
338 }else{
339 return tree;
340 }
341 }
342 }
343
344
345 /**
346 * `inputText` to `tkTree` (ASTTree)
347 */
348 export function inputTextToTree(inputText : string){
349 return p.expectSingleResult(
350 p.expectEOF(PROG.parse(lexer.parse(inputText))));
351 }