Skip to content

Commit

Permalink
Track END_STMT tokens and generate END_FUNCTION tokens where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlei committed Aug 29, 2016
1 parent d1c0bf8 commit 9f6bf43
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
40 changes: 25 additions & 15 deletions smop/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import readline
import options

class unbalanced_end(Exception):
pass

class IllegalCharacterError(Exception):
pass

Expand All @@ -20,7 +23,8 @@ class IllegalCharacterError(Exception):
"MINUS","MINUSMINUS","MINUSEQ","MUL","MULEQ","NE", "NEG",
"NUMBER", "OR","OREQ", "OROR", "PLUS", "PLUSEQ","PLUSPLUS",
"RBRACE", "RBRACKET", "RPAREN", "SEMI", "STRING",
"TRANSPOSE", "ERROR_STMT", "COMMENT", "END_FUNCTION","POW", ]
"TRANSPOSE", "ERROR_STMT", "COMMENT", "END_FUNCTION",
"END_UNEXPECTED","POW", ]

reserved = {
"break" : "BREAK",
Expand Down Expand Up @@ -149,22 +153,28 @@ def t_IDENT(t):
# is illegal, but foo.return=1 is fine.
t.type = "FIELD"
return t
if t.value == "endfunction":
t.type = "END_FUNCTION"
if (t.value == "end" and (t.lexer.parens > 0 or
t.lexer.brackets > 0 or
t.lexer.braces > 0)):
t.type = "END_EXPR"
return t
if t.value in ("endwhile", "endif","endfor",
"endswitch","end_try_catch"):
t.type = "END_STMT"
return t
if t.value == "end":
if (t.lexer.parens > 0 or
t.lexer.brackets > 0 or
t.lexer.braces > 0):
t.type = "END_EXPR"
if t.value in ("end","endif","endfunction","endwhile",
"endfor","endswitch","end_try_catch"):
keyword = t.lexer.stack.pop() # if,while,etc.
#assert keyword == t.value or keyword == "try"
if keyword == "function":
t.type = "END_FUNCTION"
else:
t.type = "END_STMT"
return t
else:
t.type = reserved.get(t.value,"IDENT")
if t.value in ("if","function","while",
"for","switch","try"):
# lexer stack may contain only these
# six words, ever, because there is
# one place to push -- here
t.lexer.stack.append(t.value)
if (t.type != "IDENT" and
t.lexer.lexdata[t.lexer.lexpos]=="'"):
t.lexer.begin("afterkeyword")
Expand Down Expand Up @@ -326,9 +336,9 @@ def t_error(t):

lexer = lex.lex(reflags=re.MULTILINE)
lexer.brackets = 0 # count open square brackets
lexer.parens = 0 # count open parentheses
lexer.braces = 0 # count open curly braces
lexer.stack = []
lexer.parens = 0 # count open parentheses
lexer.braces = 0 # count open curly braces
lexer.stack = []
return lexer

def main():
Expand Down
3 changes: 3 additions & 0 deletions smop/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,9 @@ def p_error(p):
#print "Discarded comment", p.value
parser.errok()
return
if p.type == "END_UNEXPECTED":
raise syntax_error(p)

raise syntax_error(p)

parser = yacc.yacc(start="top")
Expand Down

0 comments on commit 9f6bf43

Please sign in to comment.