]> git.kianting.info Git - clo/blobdiff - src/index.ts
english breakline, and generate try to count the text size
[clo] / src / index.ts
index 55e3d65db7b9894c9aead54e69cfa73e657a1520..44aa79282788f7f8fd28679017ceb114c49c3149 100644 (file)
@@ -1,46 +1,68 @@
 var fs = require('fs');
+var argv : any = require('minimist')(process.argv.slice(2));
 
-import * as tk from './tokenize.js';
+import * as parser from "./parser.js";
 
+/**
+ * help for inputing `--help` parameter.
+ */
+export let helpDesc =
+`
+clo: clo INPUT_FILE --output-js OUTPUT_JS_FILE
 
+\ta little typesetter powered by TypeScript/Javascript.
 
-let b : Array<tk.Token> = tk.tokenize("2+2");
+## Arguments
+INPUT_FILE\tan input .clo file
 
-export interface TokenMatcheePair {
-    matched: tk.Token[]
-    remained: tk.Token[]
-}
+## Parameters
+---
+--output-js\tgenerated the output middle JS file
+
+
+Report bugs to: clo@kianting.info
+clo home page: <https://kianting.info/wiki/w/Project:Clo>
+`
+
+processArgv(argv, helpDesc);
 
 /**
- * @description
- * it returns a function which test if the first char of the `remained` part of
- *  the argument of the function is `c`, if it's true, update the `MatchedPair` wrapped
- * in `Some`. Otherwise, it returns `None`.
- *  * @param t : the char to be test.
- * @returns the updated `MatchedPair` wrapped in `Some(x)` or `None`.
+ * processing the passed `argv` (arguments)
  */
-export function match1token(t: tk.Token): (m: TokenMatcheePair) => tk.Maybe<TokenMatcheePair> {
-    return (m: TokenMatcheePair) => {
-        if (m.remained.length == 0) {
-            return { _tag: "None" };
-        }
-        const tokenToBeMatched = m.remained[0];
-        if (tokenToBeMatched === t) {
-            m.matched.push(tokenToBeMatched);
-            return {
-                _tag: "Some", value: {
-                    matched: m.matched,
-                    remained: m.remained.slice(1)
-                }
-            };
-        }
-        else {
-            return { _tag: "None" };
-        }
+
+export function processArgv(argv : any, helpDesc : string){
+    let inputFile : string[] = argv['_'];
+    let outputJSFile : string | true = argv['output-js'];
+
+    let NoInputFile : boolean = (inputFile.length == 0);
+    let NoOutputJSFile : boolean = (outputJSFile === undefined || outputJSFile == true);
+    let helpTriggered : boolean = argv['help'];
+
+    if (inputFile.length > 1){
+        console.log("Sorry, the input file should be only one.");
     }
-};
+
+    /** output --help */
+    if (helpTriggered || NoInputFile || NoOutputJSFile){
+        console.log(helpDesc);
+    }else{
+        fs.readFile(inputFile[0], 'utf8', (err : Error, inputText : string) => {
+            if (err) throw err;
+
+            let tree = parser.inputTextToTree(inputText);
+
+            let output = parser.treeToJS(tree);
+
+            fs.writeFile(outputJSFile, output , (err : Error) => {
+                if (err) throw err;
+              });
+
+          }); 
+    }
+
+}
+
+
 
 
 
-let c = tk.toSome(b);
-console.log(thenDo(c,match1token(tk.tokenize("+")[0])));
\ No newline at end of file