diff --git a/src/parser.jl b/src/parser.jl index 7578b31..4886159 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -19,7 +19,7 @@ function strng(c) : nothing) end -function type(t) +function typ(t) return Psr((x)-> length(x) >= 1 ? (x[1][2] == t ? ParserResult([x[1]], x[2:end]) @@ -58,8 +58,8 @@ function seq(parserLst) if tmp == nothing return nothing else - s = tmp.remained) - push!(result, tmp.matched) + s = tmp.remained + push!(result, tmp.matched[1]) end end @@ -102,8 +102,7 @@ matchEntirely = Regex(matchAll) print(matchEntirely) -inp = "123 + 345 - 456 * a ^^^" - +inp = "123 + 345 - 456 * a" print("~~~\n") isEntirelyMatched = match(matchEntirely, inp) @@ -113,6 +112,25 @@ if isEntirelyMatched == false end +function prettyString(ele) + if isa(ele, String) + return "\"" * ele * "\"" + elseif isa(ele, Tuple) + mappedEle = map(prettyString, ele) + mappedString = "(" * join(mappedEle, ", ") * ")" + return mappedString + elseif isa(ele, Array) + mappedEle = map(prettyString, ele) + mappedString = "[" * join(mappedEle, ", ") * "]" + return mappedString + elseif isa(ele, ParserResult) + res = "ParserResult(" * prettyString(ele.matched) * "," * prettyString(ele.remained) * ")" + return res + else #number + return string(ele) + end + +end mI = eachmatch(matchEachItem, inp) @@ -133,8 +151,45 @@ withoutSpaces = filter((x)-> x[2] != "sp", zippedTokenList) initWrapped = ParserResult([], withoutSpaces) -println(initWrapped >> strng("123") >> (strng("+")|strng("-"))) -println(initWrapped >> seq([strng("123"), strng("+")])) +test1 = initWrapped >> strng("123") >> (strng("+")|strng("-")) +test2 = initWrapped >> seq([strng("123"), strng("+")]) + +println(prettyString(test1)) +println(prettyString(test2)) + +""" +term = int +exp = (term (+|-) term) | term +""" +term = typ("int") + +function longExpAux(input) + rawFunc = seq([term, (strng("+") | strng("-")), term]) + rawRes = rawFunc.fun(input) + if rawRes != nothing + matched = [rawRes.matched[2], rawRes.matched[1], rawRes.matched[3]] + res = ParserResult(matched, rawRes.remained) + return res + else + return rawRes + end +end + +function expAux(input) + longExp = Psr(longExpAux) + rawFunc = longExp | term + res = rawFunc.fun(input) + + return res + +end + +exp = Psr(expAux) + + + +test3 = initWrapped >> exp +println(prettyString(test3)) end