Skip to content

Commit

Permalink
Merge pull request #204 from Typee-Language/#150-Modify-Correct-Parse…
Browse files Browse the repository at this point in the history
…r-Code

#150 modify correct parser code
  • Loading branch information
PhHays committed Mar 19, 2019
2 parents 3086a62 + 9810ef0 commit 891aedb
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Language-specifications/typee_specs_LL1-v9-2-EBNF.grm
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ SOFTWARE.
<target list> ::= <typed target> ( ',' <typed target> )*

<try otherwise> ::= 'otherwise' '(' ##
[ 'Exception' 'as' <identifier> ] ')' ##
[ <identifier> 'as' <identifier> ] ')' ##

<try except> ::= 'except' '('
[ <try expr as> ( ',' <try expr as> )* | ##
Expand Down
2 changes: 1 addition & 1 deletion Language-specifications/typee_specs_LL1-v9-2.grm
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ SOFTWARE.
<try finally> ::= 'finally'

<try otherwise> ::= 'otherwise' '(' <try otherwise'> ')' ##
<try otherwise'> ::= 'Exception' 'as' <identifier> ##
<try otherwise'> ::= <identifier> 'as' <identifier> ##
| EPS

<try statement> ::= 'try' <statements block> <try statement excepts> <try statement otherwise> <try statement finally> ##
Expand Down
7 changes: 6 additions & 1 deletion src/FrontEnd/Errors/fe_syntax_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,15 @@ class FESyntaxErrors:
TEMPLATE_TYPES_LIST = 'missing or badly formed expression or type identifier within types-and-expressions list in template specification',
TRY_AS_IDENT = 'missing or badly formed identifier after keyword "as" in except clause',
TRY_BODY = 'missing instruction or instructions block after keyword "try"',
TRY_ELSE_BODY = 'missing instruction or instructions block after keyword "otherwise" in try-except instruction',
TRY_EXCEPT_BODY = 'missing instruction or instructions block after except clause in try-except instruction',
TRY_EXCEPT_LIST = 'missing identifier of exception after comma in try-except clause',
TRY_EXCEPTS = 'missing or badly formed except clauses in try-except instruction',
TRY_FINALLY_BODY = 'missing instruction or instructions block after keyword "finally" in try-except instruction',
TRY_OTHER_AS = 'missing keyword "as" after Exception identifier in clause otherwise of a try-except statement',
TRY_OTHER_BODY = 'missing instruction or instructions block after keyword "otherwise" in try-except instruction',
TRY_OTHER_IDENT = 'missing identifier afetr keyword "as" in clause otherwise of a try-except statement',
TRY_OTHER_PARCL = 'missing ) at end of clause otherwise in try-except statement',
TRY_OTHER_PAROP = 'missing ( after keyword otherwise in try-except statement',
TYPE_ALIAS = 'missing or badly formed type identifier after keyword "type" in type alias instruction',
TYPE_AS = 'missing keyword "as" after type identifier in type alias instruction',
TYPE_AS_IDENT = 'missing or badly formed identifier after keyword "as" in type alias instruction',
Expand Down
95 changes: 72 additions & 23 deletions src/FrontEnd/Parser/fe_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3525,18 +3525,6 @@ def _true(self) -> bool:
else:
return False

#-------------------------------------------------------------------------
def _try_else(self) -> bool:
#=======================================================================
# <try else> ::= 'otherwise'
#=======================================================================
if self._current.is_OTHERWISE():
self._append_syntaxic_node()
self._next_token_node()
return True
else:
return False

#-------------------------------------------------------------------------
def _try_except(self) -> bool:
#=======================================================================
Expand All @@ -3563,22 +3551,38 @@ def _try_except(self) -> bool:
#-------------------------------------------------------------------------
def _try_except1(self) -> bool:
#=======================================================================
# <try except'> ::= <expression> <try except">
# | 'all'
# <try except'> ::= <expression> <try except''> <try except'''>
# | 'all' <try except''>
# | EPS
#=======================================================================
if self._expression():
self._try_except2() ## (notice: always returns True)
self._try_except3() ## (notice: always returns True)
elif self._current.is_ALL():
self._append_syntaxic_node()
self._next_token_node()
self._try_except2()
return True

#-------------------------------------------------------------------------
def _try_except2(self) -> bool:
#=======================================================================
# <try except"> ::= 'as' <identifier>
# | EPS
# <try except''> ::= ',' <expression> <try except''>
# | EPS
#=======================================================================
while self._current.is_COMMA():
self._append_syntaxic_node()
self._next_token_node()
if not self._identifier():
self._append_error( FESyntaxErrors.TRY_EXCEPT_LIST )
self._try_except2()
return True

#-------------------------------------------------------------------------
def _try_except3(self) -> bool:
#=======================================================================
# <try except'''> ::= 'as' <identifier>
# | EPS
#=======================================================================
if self._current.is_AS():
self._append_syntaxic_node()
Expand All @@ -3599,11 +3603,55 @@ def _try_finally(self) -> bool:
else:
return False

#-------------------------------------------------------------------------
def _try_otherwise(self) -> bool:
#=======================================================================
# <try otherwise> ::= 'otherwise' '(' <try otherwise'> ')'
#=======================================================================
if self._current.is_OTHERWISE():
self._append_syntaxic_node()
self._next_token_node()
if self._current.is_PAROP():
self._append_syntaxic_node()
self._next_token_node()
else:
self._append_error( FESyntaxErrors.TRY_OTHER_PAROP )
self._try_otherwise()
if self._current.is_PARCL():
self._append_syntaxic_node()
self._next_token_node()
else:
self._append_error( FESyntaxErrors.TRY_OTHER_PARCL )
return True
else:
return False

#-------------------------------------------------------------------------
def _try_otherwise1(self) -> bool:
#===============================================================================
# <try otherwise'> ::= 'Exception' 'as' <identifier>
# | EPS
#===============================================================================
if self._current.is_IDENT():
self._append_syntaxic_node()
self._next_token_node()
if self._current.is_AS():
self._append_syntaxic_node()
self._next_token_node()
else:
self._append_error( FESyntaxErrors.TRY_OTHER_AS )
if self._current.is_IDENT():
self._append_syntaxic_node()
self._next_token_node()
else:
self._append_error( FESyntaxErrors.TRY_OTHER_IDENT )
return True

#-------------------------------------------------------------------------
def _try_statement(self) -> bool:
#=======================================================================
# <try statement> ::= 'try' <statements block> <try statement excepts>
# <try statement else> <try statement finally>
# <try statement otherwise> <try statement finally>
#=======================================================================
if self._current.is_TRY():
self._append_syntaxic_node()
Expand All @@ -3612,21 +3660,21 @@ def _try_statement(self) -> bool:
self._append_error( FESyntaxErrors.TRY_BODY )
if not self._try_statement_excepts():
self._append_error( FESyntaxErrors.TRY_EXCEPTS )
self._try_statement_else()
self._try_statement_otherwise()
self._try_statement_finally()
return True
else:
return False

#-------------------------------------------------------------------------
def _try_statement_else(self) -> bool:
def _try_statement_otherwise(self) -> bool:
#=======================================================================
# <try statement else> ::= <try else> <statements block> ##
# | EPS ##
# <try statement otherwise> ::= <try otherwise> <statements block> ##
# | EPS ##
#=======================================================================
if self._try_else():
if self._try_otherwise():
if not self._statements_block():
self._append_error( FESyntaxErrors.TRY_ELSE_BODY )
self._append_error( FESyntaxErrors.TRY_OTHER_BODY )
return True

#-------------------------------------------------------------------------
Expand Down Expand Up @@ -4127,4 +4175,5 @@ def _next_token_node(self) -> FEICodeTokenNode:
#-------------------------------------------------------------------------
def __up_to_parent_block(self):
self._out_syntax_ic.up_level()

#===== end of FrontEnd.Parser.parser =====#

0 comments on commit 891aedb

Please sign in to comment.