Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
Going through it. The wizardry. Changed the something/lookahead funcs…
Browse files Browse the repository at this point in the history
… to take rather than just tuples that can take a type/value compare, also can take functions that can also do the match check, and then use the i_changed function to return a bool same as the other funcs
  • Loading branch information
Arghnews committed Feb 13, 2017
1 parent 53d897c commit 6ed9475
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 22 deletions.
Binary file removed .actual_grammar.swp
Binary file not shown.
Binary file removed .baba.py.swp
Binary file not shown.
Binary file removed .example.lua.swp
Binary file not shown.
6 changes: 3 additions & 3 deletions actual_grammar
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ args_back ::= args | `:´ Name args

explist ::= { exp `,´ } exp

exp ::= nil exp_p | false exp_p | true exp_p | Number exp_p | String exp_p | `...´ exp_p | function exp_p |
exp ::= nil exp_p | false exp_p | true exp_p | Number exp_p | String exp_p | `...´ exp_p | functiondef exp_p |
prefixexp exp_p | tableconstructor exp_p | unop exp exp_p

exp_p ::= binop exp exp_p | epsilon

args ::= `(´ [ explist ] `)´ | tableconstructor | String

function ::= function funcbody
functiondef ::= function funcbody

funcbody ::= `(´ [ parlist ] `)´ block end

tableconstructor ::= `{´ [ fieldlist ] `}´


fieldlist ::= field { fieldsep field } [ fieldsep ]

field ::= `[´ exp `]´ `=´ exp | Name `=´ exp | exp


funcname ::= Name { `.´ Name } [ `:´ Name ]

namelist ::= Name { `,´ Name }
Expand Down
82 changes: 71 additions & 11 deletions baba.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,17 @@ def tokenize(code):
# what to look for, ie tuples of ",",MATCH_VALUE
MATCH_TYPE = "type"
MATCH_VALUE = "value"
MATCH_FUNCTION = "function_type"

firstSets = dict()

def parse(fname):

firstSets["binop"] = [('+',MATCH_VALUE), ('-',MATCH_VALUE), ('*',MATCH_VALUE), ('/',MATCH_VALUE), ('^',MATCH_VALUE), ('%',MATCH_VALUE), '..', ('<',MATCH_VALUE), '<=', ('>',MATCH_VALUE), '>=', '==', '~=', 'and', 'or']
firstSets["unop"] = [('-',MATCH_VALUE), ('not',MATCH_VALUE), ('#',MATCH_VALUE)]
firstSets["fieldsep"] = [(',',MATCH_VALUE), (';',MATCH_VALUE)]
firstSets["exp"] = [('nil',MATCH_VALUE),('false',MATCH_VALUE),('true',MATCH_VALUE),('Number', MATCH_TYPE), ('String', MATCH_TYPE), ('...', MATCH_VALUE),('function',MATCH_VALUE),('{',MATCH_VALUE),('Name',MATCH_TYPE),('-',MATCH_VALUE),('not',MATCH_VALUE),('#',MATCH_VALUE)]

# ^([0-9]*)(\.[0-9]+)?([eE]-?[0-9]+)?$|^([0-9]+)(\.[0-9]*)?([eE]-?[0-9]+)?$|^0x([0-9a-fA-F]*)(\.[0-9a-fA-F]+)?([pP]-?[0-9]+)?$|^0x([0-9a-fA-F]+)(\.[0-9a-fA-F]*)?([pP]-?[0-9]+)?$
program = ""
with open(fname, "r") as ins:
Expand Down Expand Up @@ -131,9 +140,41 @@ def parse(fname):
i, tokens = funcname(i, tokens)
print("funcname",[str(t.type)+": "+str(t.value) for t in tokens[i_b:i]])

i_b = i
i, tokens = field(i, tokens)
print("field",[str(t.type)+": "+str(t.value) for t in tokens[i_b:i]])

i_b = i
i, tokens = fieldlist(i, tokens)
print("fieldlist",[str(t.type)+": "+str(t.value) for t in tokens[i_b:i]])


def fieldlist(i, tokens):
i, tokens = field(i, tokens)
i, tokens = star(i, tokens, [(fieldsep,MATCH_FUNCTION),(field,MATCH_FUNCTION)], 2)
i, tokens = optional(i, tokens, [(fieldsep,MATCH_FUNCTION)], 1)
return i, tokens

def field(i, tokens):
if contains(i, tokens, [("[",MATCH_VALUE)]):
# todo, fill the aftermath of each of these
# with checks and should haves
i, tokens = matchValueNow(i, tokens, "[")
i, tokens = exp(i, tokens)
i, tokens = matchValueNow(i, tokens, "]")
i, tokens = matchValueNow(i, tokens, "=")
i, tokens = exp(i, tokens)
elif contains(i, tokens, [("Name",MATCH_TYPE)]):
i, tokens = matchTypeNow(i, tokens, "Name")
i, tokens = matchValueNow(i, tokens, "=")
i, tokens = exp(i, tokens)
elif contains(i, tokens, firstSets["exp"]):
i, tokens = exp(i, tokens)
return i, tokens

def exp(i, tokens):
return i, tokens

def funcname(i, tokens):
i, tokens = matchTypeNow(i,tokens,"Name")
i, tokens = star(i, tokens, [(".",MATCH_VALUE), ("Name",MATCH_TYPE)], 2)
Expand All @@ -142,10 +183,10 @@ def funcname(i, tokens):

def parlist(i, tokens):
# chosen the ... only
if lookahead(i, tokens, [("...",MATCH_VALUE)], 1):
if contains(i, tokens, [("...",MATCH_VALUE)]):
i, tokens = matchValueNow(i, tokens, "...")
# namelist
elif lookahead(i, tokens, [("Name",MATCH_TYPE)], 1):
elif contains(i, tokens, [("Name",MATCH_TYPE)]):
i, tokens = namelist(i, tokens)
i, tokens = optional(i, tokens, [(",",MATCH_VALUE),("...",MATCH_VALUE)], 2)
else:
Expand All @@ -159,16 +200,15 @@ def namelist(i, tokens):
return i, tokens

def binop(i, tokens):
binop_list = ['+', '-', '*', '/', '^', '%', '..', '<', '<=', '>', '>=', '==', '~=', 'and', 'or']
return matchTerminalListError(i, tokens, binop_list, "Could not find binary")
return matchTerminalInList(i, tokens, firstSets["binop"])

def unop(i, tokens):
unop_list = ['-', 'not', '#']
return matchTerminalListError(i, tokens, unop_list, "Could not find unary operator")
return matchTerminalInList(i, tokens, firstSets["unop"])

def fieldsep(i, tokens):
fieldsep_list = [',', ';']
return matchTerminalListError(i, tokens, fieldsep_list, "Could not find field separator")
return matchTerminalInList(i, tokens, firstSets["fieldsep"])



Expand All @@ -178,17 +218,28 @@ def fieldsep(i, tokens):



def contains(i, tokens, firstSet):
for _, (val,match_type) in enumerate(firstSet):
b = False
if match_type == MATCH_VALUE:
b = match_v(tokens, i, val)
elif match_type == MATCH_TYPE:
b = match_t(tokens, i, val)
if b:
return True
return False

def matchTerminalListError(i, tokens, list, err):
i_b = i
i, tokens = matchTerminalInList(i, tokens, list)
if i == i_b:
print(err)
#if i == i_b:
#print(err)
return i, tokens

def matchTerminalInList(i, tokens, list):
for op in list:
if lookahead(i, tokens, [(op,MATCH_VALUE)], 1):
i, tokens = matchValueNow(i, tokens, op)
if lookahead(i, tokens, [op], 1):
i, tokens = matchValueNow(i, tokens, op[0])
break
return i, tokens

Expand Down Expand Up @@ -224,10 +275,17 @@ def lookahead(i, tokens, func_tuples, lookahead_n):
b = match_v(tokens, i+j, func_tuples[j][0])
elif func_tuples[j][1] == MATCH_TYPE:
b = match_t(tokens, i+j, func_tuples[j][0])
elif func_tuples[j][1] == MATCH_FUNCTION:
b = i_changed(i+j, tokens, func_tuples[j][0])
if not b:
return False
return True

def i_changed(i, tokens, f):
i_b = i
i_after, _ = f(i, tokens)
return i_b != i_after

def star_do(i, tokens, funcs):
while True:
for f in funcs:
Expand All @@ -243,11 +301,13 @@ def something(i, tokens, func_tuples, lookahead_n, repeater):
# creates list of funcs that are that grammar
# thing that we're doing
funcs = []
for j, (match, match_type) in enumerate(func_tuples):
for _, (match, match_type) in enumerate(func_tuples):
if match_type == MATCH_VALUE:
funcs.append(matchValue(match))
elif match_type == MATCH_TYPE:
funcs.append(matchType(match))
elif match_type == MATCH_FUNCTION:
funcs.append(match)

if lookahead(i, tokens, func_tuples, lookahead_n):
for i, tokens in repeater(i, tokens, funcs):
Expand Down
Loading

0 comments on commit 6ed9475

Please sign in to comment.