Skip to content

Commit

Permalink
added operators
Browse files Browse the repository at this point in the history
  • Loading branch information
hsk committed Jul 25, 2015
1 parent 9bfadca commit 9ed020d
Showing 1 changed file with 63 additions and 30 deletions.
93 changes: 63 additions & 30 deletions beautifier/beauty.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

sys.setrecursionlimit(1000*1000)


def startsWith(i, param):
ilen = len(i)
plen = len(param)
Expand All @@ -13,6 +14,7 @@ def startsWith(i, param):
else:
return i[0: plen] == param


class Parser:
def __or__(self, that):
return Or(self, that)
Expand All @@ -29,6 +31,15 @@ def __lshift__(self, that):
def __xor__(self, that):
return Action(self, that)

def __neg__(self):
return rep(self)

def __pos__(self):
return rep1(self)

def __invert__(self):
return opt(self)

class Or(Parser):
def __init__(self, thiz, that):
self.thiz = thiz
Expand All @@ -40,6 +51,7 @@ def __call__(self, i):
return self.that(i)
return e


class any_char_obj(Parser):
def __call__(self, i):
if len(i) > 0:
Expand All @@ -49,6 +61,7 @@ def __call__(self, i):

any_char = any_char_obj()


class nstr(Parser):
def __init__(self, param):
self.param = param
Expand All @@ -58,6 +71,7 @@ def __call__(self, i):
return None
return [self.param, i[len(self.param):]]


class nreg(Parser):
def __init__(self, param):
self.param = param
Expand All @@ -71,6 +85,7 @@ def __call__(self, i):
#print(m.group(0))
return [m.group(0), i[len(m.group(0)):]]


class Seq(Parser):
def __init__(self, thiz, that):
self.thiz = thiz
Expand All @@ -87,13 +102,15 @@ def __call__(self, i):

skip = nreg('''^(\s|\(\*.*\*\))*''')


def st(s):
return skip + nstr(s)

#print (any_char("abc"))

#print (st("ab") | any_char)(" abc")


class seq(Parser):
def __init__(self, *params):
args = []
Expand All @@ -118,6 +135,7 @@ def __call__(self, i):
#print([rs, i])
return [rs, i]


class Next(Parser):
def __init__(self, thiz, that):
self.thiz = thiz
Expand All @@ -129,6 +147,7 @@ def __call__(self, i):
return r
return [r[0][1], r[1]]


class Prev(Parser):
def __init__(self, thiz, that):
self.thiz = thiz
Expand All @@ -140,6 +159,7 @@ def __call__(self, i):
return r
return [r[0][0], r[1]]


class Action(Parser):
def __init__(self, thiz, that):
self.thiz = thiz
Expand All @@ -155,6 +175,7 @@ def __call__(self, i):

#print (st("a") + st("b") + st("c") ^ (lambda x: ["<"]+x+[">"])) ("a b c")


class opt(Parser):
def __init__(self, thiz):
if isinstance(thiz, basestring):
Expand All @@ -167,6 +188,7 @@ def __call__(self, i):
return [None, i]
return r


class rep(Parser):
def __init__(self, thiz):
if isinstance(thiz, basestring):
Expand All @@ -182,11 +204,13 @@ def __call__(self, i):
rs.append(r[0])
i = r[1]


def rep1(thiz):
if isinstance(thiz, basestring):
thiz = st(thiz)
return rep(thiz) ^ (lambda p: None if len(p) < 1 else p)


class notp(Parser):
def __init__(self, thiz):
if isinstance(thiz, basestring):
Expand All @@ -199,6 +223,7 @@ def __call__(self, i):
return ["", i]
return None


class Range(Parser):
def __init__(self, thiz):
if isinstance(thiz, basestring):
Expand All @@ -213,9 +238,11 @@ def __call__(self, i):
return [i[0], i[1:]]
return None


def reg(s):
return skip + nreg(s)


class Nest(object):
def __init__(self, name):
self.name = name
Expand All @@ -226,17 +253,20 @@ def __repr__(self):
NestM = Nest("NestM")
NestP = Nest("NestP")


def printf(text):
print text

#print([NestP, "a", NestM])

def n(a, b, c):

def n(*a):

def nf(a):
return [a[0], NestP, a[1], NestM, a[2]]
return [NestP, a, NestM]

return seq(*a) ^ nf

return seq(a, b, c) ^ nf

def flat(a):
# print("flat")
Expand All @@ -250,15 +280,16 @@ def flat(a):
return rc
return [a]


def cnv(e):
#print(e)
reg2 = '''(^.*\\n.*$)'''
whiteSpace = '''(^\\s*$)'''
reg2 = r'(^.*\n.*$)'
whiteSpace = r'(^\s*$)'
nest = 0
e2 = []
for s in e:
if isinstance(s, basestring):
m = re.search('''(^.*\\n)(\\s+$)''', s)
m = re.search(r'(^.*\n)(\s+$)', s)
if not(m is None):
s = m.group(1)
e2.append(s)
Expand Down Expand Up @@ -314,40 +345,42 @@ def makeString(s, n):
m = re.search(reg2, s)
if m is not None:
#print("add nest "+nest)
e2.append(re.sub('\n', "\n"+makeString(" ", nest), m.group(0)))
e2.append(re.sub(r'\n', "\n"+makeString(" ", nest), m.group(0)))
continue
e2.append(s)
#print(e2)
return "".join(e2)

p = seq


def parse(s):
s = re.compile("^[\\s]+", re.M).sub("", s)
s = re.compile(r"^[\s]+", re.M).sub("", s)
#s = re.sub("^\s+", "", s)

keywords = reg('''^(let|in|if|else|then|rec|begin|end|match|with|type)\\b''')

id = notp(keywords) >> reg('''^[_a-zA-Z0-9]+''') \
| reg('''^[+\\-*\\/.<>:@=][+\\-*\\/.<>:@=]*''') \
| reg('''^[,!]''') \
| reg('''^("(\\.|[^"])*")''')

def exp(i):
return (exps + rep(seq(notp(st(";;")) >> st(";"), exps)))(i)

exp1 = n("begin", exp, "end") \
| n("(", opt(exp), ")") \
| n("{", opt(exp), "}") \
| n("[", opt(exp), "]") \
| seq(n("if", exp, ""), n("then", exp, "else"), exp) \
| seq(n("let", seq(opt("rec"), exp), "in"), exp) \
| seq(n((st("match") | st("try")), exp, "with"), opt("|"), n("", exp, ""), rep(seq("|", n("", exp, "")))) \
| seq("function", opt("|"), n("", exp, ""), rep(seq("|", n("", exp, "")))) \
| n("type", seq(id, "=", n(opt("|"), exp, ""), rep(n("|", exp, ""))), opt(";;")) \
| n("type", seq(id, "=", exp), opt(";;")) \
| n("open", id + rep(seq(".", id)), opt(";;")) \
keywords = reg(r"^(let|in|if|else|then|rec|begin|end|match|with|type)\b")

id = notp(keywords) >> reg(r"^[_a-zA-Z0-9]+") \
| reg(r'^[+\-*\/.<>:@=][+\-*\/.<>:@=]*') \
| reg(r'^[,!]') \
| reg(r'^("(\\.|[^"])*")')

exp = p(lambda i: (exps + -p(notp(";;") >> p(";"), exps))(i))

exp1 = p("begin", n(exp), "end") \
| p("(", n(~exp), ")") \
| p("{", n(~exp), "}") \
| p("[", n(~exp), "]") \
| p("if", n(exp), "then", n(exp), "else", exp) \
| p("let", n(~p("rec"), exp), "in", exp) \
| p((st("match") | st("try")), n(exp), "with", ~p("|"), n(exp), -p("|", n(exp))) \
| p("function", ~p("|"), n(exp), -p("|", n(exp))) \
| p("type", n(id, "=", ~p("|"), n(exp), -p("|", n(exp))), ~p(";;")) \
| p("type", n(id, "=", exp), ~p(";;")) \
| p("open", n(id, -p(".", id)), ~p(";;")) \
| id

exps = rep1(exp1)
exps = +exp1

e = flat(exp(s))
#print("parse e="),
Expand Down

0 comments on commit 9ed020d

Please sign in to comment.