X-Git-Url: https://git.kianting.info/?a=blobdiff_plain;f=src%2Findex.ts;h=405a1f0d465ffcf04867c6a2bb3c09c083c3b9b6;hb=ae71f2351bd6ffb5e054687dd1e3eb99f72545fe;hp=55e3d65db7b9894c9aead54e69cfa73e657a1520;hpb=cda463d89022d09a5273b59bcd3e776f1127697a;p=clo diff --git a/src/index.ts b/src/index.ts index 55e3d65..405a1f0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,46 +1,37 @@ var fs = require('fs'); +import jsTokens from "js-tokens"; +import * as util from 'util'; -import * as tk from './tokenize.js'; - - - -let b : Array = 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 { - 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, f : Function){ + if (m._tag == "None"){ + return m; + }else{ + var a : tk.Maybe = 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