var fs = require('fs');
+import jsTokens from "js-tokens";
+import * as util from 'util';
-import * as tk from './tokenize.js';
-
-
-
-let b : Array<tk.Token> = tk.tokenize("2+2");
-
-export interface TokenMatcheePair {
- matched: tk.Token[]
- remained: tk.Token[]
-}
+/**inspect the inner of the representation. */
+let repr = (x : any)=>{return util.inspect(x, {depth: null})};
/**
- * @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`.
+ * like `m ==> f` in ocaml
+ * @param m matchee wrapped
+ * @param f matching function
+ * @returns wrapped result
*/
-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" };
+function thenDo(m : tk.Maybe<TokenMatcheePair>, f : Function){
+ if (m._tag == "None"){
+ return m;
+ }else{
+ var a : tk.Maybe<TokenMatcheePair> = f(m.value);
+ if (a._tag == "Some"){
+ a.value.ast = concat(m.value.ast, a.value.ast);
}
+
+ return a;
}
-};
+}
+
+const tokens = Array.from(jsTokens(`
+import foo from\t 'bar';
+import * as util from 'util';
+
+
+花非花,霧\\{非霧 。{{foo();}}下
+一句`));
+console.log("RESULT="+repr(tokens));
-let c = tk.toSome(b);
-console.log(thenDo(c,match1token(tk.tokenize("+")[0])));
\ No newline at end of file