]>
git.kianting.info Git - clo/blob - src/index.js
6b49d56f4b9c14b43a2d4306bf351c41d83b807f
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 Object
.defineProperty(exports
, "__esModule", { value
: true });
26 exports
.tkTreeToSExp
= void 0;
27 var fs
= require('fs');
28 const util
= __importStar(require("util"));
29 const p
= __importStar(require("typescript-parsec"));
35 * convert a `tkTree` AST to S-expr string
36 * @param t the `tkTree`
37 * @returns S-expr String
39 function tkTreeToSExp(t
) {
41 if (Array
.isArray(t
)) {
42 let strArray
= t
.map((x
) => tkTreeToSExp(x
));
43 str
= "(" + strArray
.join(" ") + ")";
46 if (t
=== undefined) {
55 exports
.tkTreeToSExp
= tkTreeToSExp
;
56 /**inspect the inner of the representation. */
57 let repr
= (x
) => { return util
.inspect(x
, { depth
: null }); };
59 (function (TokenKind
) {
60 TokenKind
[TokenKind
["Seperator"] = 0] = "Seperator";
61 TokenKind
[TokenKind
["Semicolon"] = 1] = "Semicolon";
62 TokenKind
[TokenKind
["Number"] = 2] = "Number";
63 TokenKind
[TokenKind
["Op"] = 3] = "Op";
64 TokenKind
[TokenKind
["ExprMark"] = 4] = "ExprMark";
65 TokenKind
[TokenKind
["ExcapeAt"] = 5] = "ExcapeAt";
66 TokenKind
[TokenKind
["Paren"] = 6] = "Paren";
67 TokenKind
[TokenKind
["SpaceNL"] = 7] = "SpaceNL";
68 TokenKind
[TokenKind
["Id"] = 8] = "Id";
69 TokenKind
[TokenKind
["Str"] = 9] = "Str";
70 TokenKind
[TokenKind
["Comment"] = 10] = "Comment";
71 })(TokenKind
|| (TokenKind
= {}));
75 const lexer
= p
.buildLexer([
76 [true, /^\d+(\.\d+)?/g, TokenKind
.Number
],
77 [true, /^\\\@/g, TokenKind
.ExcapeAt
],
78 [true, /^\/\*([^/]|\/[^*])*\*\//g, TokenKind
.Comment
],
79 [true, /^\;/g, TokenKind
.Semicolon
],
80 [true, /^[-][-][-]/g, TokenKind
.Seperator
],
81 [true, /^[\+\-\*\/\&\|\!\^\<\>\~\=\?]+/g, TokenKind
.Op
],
82 [true, /^\@/g, TokenKind
.ExprMark
],
83 [true, /^[()\[\]{}]/g, TokenKind
.Paren
],
84 [true, /^["]([\"]|[\\].)*["]/g, TokenKind
.Str
],
85 [true, /^[']([\']|[\\].)*[']/g, TokenKind
.Str
],
86 [true, /^[()\[\]{}]/g, TokenKind
.Paren
],
87 [true, /^[^\/\\\@\s\n\t\r;]+/g, TokenKind
.Id
],
88 [true, /^(\s|\n|\r|\t)+/g, TokenKind
.SpaceNL
],
94 const inputTxt
= `import a as b; /*bacourt*/
110 const PROG
= p
.rule();
111 const SEGMENT
= p
.rule();
112 const IMPORT
= p
.rule();
113 const IMPORTS
= p
.rule();
114 const SEMICOLON
= p
.rule();
115 const EXCAPE_AT
= p
.rule();
116 const NOT_AT_TEXT
= p
.rule();
117 const CONTENT
= p
.rule();
118 let doubleMinus
= { type
: 'Punctuator', value
: '--' };
119 let doubleMinus2
= p
.str('--');
120 const TERM
= p
.rule();
121 function applySegment(input
) {
122 let unpackedInnerExprs
= input
[1].map((x
) => { return x
.text
; });
123 return ["%exprs", unpackedInnerExprs
];
125 function applySemiColon(value
) {
128 function applyParts(first
, second
) {
129 return ["%clo", first
, second
[1]];
131 function applyComment(value
) {
134 function applyImport(input
) {
135 let importTail
= input
[1].map(x
=> x
.text
);
136 return ["import"].concat(importTail
);
140 function applyImportComment(input: [Token<TokenKind>,Token<TokenKind>[],
141 tkTree, Token<TokenKind.Comment>]) : tkTree{
142 let importTail = input[1].map(x=>x.text);
143 let comment = [input[3].text];
144 return ["import"].concat(importTail).concat(comment);
146 function applyImports(input
) {
147 let resultBody
= [input
[0]].concat(input
[1]);
148 let resultWrapper
= ["%import", resultBody
];
149 return resultWrapper
;
152 function applyNotAtText(value
) {
153 if (value
.text
== "\\\@") {
161 function applyText(input
) {
162 return ["%text", input
];
165 function applyContent(input
) {
166 return ["%content", input
];
169 function applySpaceNL(value
) {
173 * IMPORTEE: Number, Op, Paren, Id, Str, Comment,
175 let IMPORTEE
= p
.alt(p
.tok(TokenKind
.Number
), p
.tok(TokenKind
.Op
), p
.tok(TokenKind
.Paren
), p
.tok(TokenKind
.Id
), p
.tok(TokenKind
.Str
), p
.tok(TokenKind
.SpaceNL
), p
.tok(TokenKind
.Comment
));
176 let NOT_AT
= p
.alt(p
.tok(TokenKind
.Seperator
), p
.tok(TokenKind
.Semicolon
), p
.tok(TokenKind
.Number
), p
.tok(TokenKind
.ExcapeAt
), p
.tok(TokenKind
.Op
), p
.tok(TokenKind
.Paren
), p
.tok(TokenKind
.SpaceNL
), p
.tok(TokenKind
.Id
), p
.tok(TokenKind
.Str
), p
.tok(TokenKind
.Comment
));
178 * PROG : IMPORTS '---' CONTENT;
180 PROG
.setPattern(p
.lrec_sc(IMPORTS
, p
.seq(p
.str('---'), CONTENT
), applyParts
));
182 * NOT_AT_TEXT : NOT_AT
184 NOT_AT_TEXT
.setPattern(p
.apply(NOT_AT
, applyNotAtText
));
185 IMPORTS
.setPattern(p
.apply(p
.seq(IMPORT
, p
.rep(IMPORT
)), applyImports
));
188 * 'import' IMPORTEE* SEMICOLON |
191 IMPORT
.setPattern(p
.alt(p
.apply(p
.seq(p
.str('import'), p
.rep_sc(IMPORTEE
), SEMICOLON
), applyImport
), p
.apply(p
.tok(TokenKind
.Comment
), applyComment
), p
.apply(p
.tok(TokenKind
.SpaceNL
), applySpaceNL
)));
195 SEMICOLON
.setPattern(p
.apply(p
.tok(TokenKind
.Semicolon
), applySemiColon
));
197 * SEGMENT : '@' NOT_AT* '@' |
198 * (NOT_AT_TEXT | EXCAPE_AT)*
200 SEGMENT
.setPattern(p
.alt(p
.apply(p
.rep_sc(NOT_AT_TEXT
), applyText
), p
.apply(p
.seq(p
.str('@'), p
.rep(NOT_AT
), p
.str('@')), applySegment
)));
204 CONTENT
.setPattern(p
.apply(p
.rep(SEGMENT
), applyContent
));
205 console
.log(repr(lexer
.parse(inputTxt
)));
206 let tree
= p
.expectSingleResult(p
.expectEOF(PROG
.parse(lexer
.parse(inputTxt
))));
207 console
.log("RESULT=" + tkTreeToSExp(tree
));