add pretty string
This commit is contained in:
parent
9219068a86
commit
eb8e3b81d6
1 changed files with 62 additions and 7 deletions
|
@ -19,7 +19,7 @@ function strng(c)
|
||||||
: nothing)
|
: nothing)
|
||||||
end
|
end
|
||||||
|
|
||||||
function type(t)
|
function typ(t)
|
||||||
return Psr((x)-> length(x) >= 1 ?
|
return Psr((x)-> length(x) >= 1 ?
|
||||||
(x[1][2] == t ?
|
(x[1][2] == t ?
|
||||||
ParserResult([x[1]], x[2:end])
|
ParserResult([x[1]], x[2:end])
|
||||||
|
@ -58,8 +58,8 @@ function seq(parserLst)
|
||||||
if tmp == nothing
|
if tmp == nothing
|
||||||
return nothing
|
return nothing
|
||||||
else
|
else
|
||||||
s = tmp.remained)
|
s = tmp.remained
|
||||||
push!(result, tmp.matched)
|
push!(result, tmp.matched[1])
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -102,8 +102,7 @@ matchEntirely = Regex(matchAll)
|
||||||
print(matchEntirely)
|
print(matchEntirely)
|
||||||
|
|
||||||
|
|
||||||
inp = "123 + 345 - 456 * a ^^^"
|
inp = "123 + 345 - 456 * a"
|
||||||
|
|
||||||
print("~~~\n")
|
print("~~~\n")
|
||||||
isEntirelyMatched = match(matchEntirely, inp)
|
isEntirelyMatched = match(matchEntirely, inp)
|
||||||
|
|
||||||
|
@ -113,6 +112,25 @@ if isEntirelyMatched == false
|
||||||
end
|
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)
|
mI = eachmatch(matchEachItem, inp)
|
||||||
|
|
||||||
|
@ -133,8 +151,45 @@ withoutSpaces = filter((x)-> x[2] != "sp", zippedTokenList)
|
||||||
initWrapped = ParserResult([], withoutSpaces)
|
initWrapped = ParserResult([], withoutSpaces)
|
||||||
|
|
||||||
|
|
||||||
println(initWrapped >> strng("123") >> (strng("+")|strng("-")))
|
test1 = initWrapped >> strng("123") >> (strng("+")|strng("-"))
|
||||||
println(initWrapped >> seq([strng("123"), 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
|
end
|
||||||
|
|
Loading…
Reference in a new issue