Mercurial Hosting > luan
comparison src/luan/impl/LuanParser.java @ 1520:d9a5405a3102
try statement
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 21 Jun 2020 18:14:13 -0600 |
parents | 6c830be6be98 |
children | d4407e8de707 |
comparison
equal
deleted
inserted
replaced
1519:3ebf9781707c | 1520:d9a5405a3102 |
---|---|
320 || (stmt=ForStmt()) != null | 320 || (stmt=ForStmt()) != null |
321 || (stmt=DoStmt()) != null | 321 || (stmt=DoStmt()) != null |
322 || (stmt=WhileStmt()) != null | 322 || (stmt=WhileStmt()) != null |
323 || (stmt=RepeatStmt()) != null | 323 || (stmt=RepeatStmt()) != null |
324 || (stmt=IfStmt()) != null | 324 || (stmt=IfStmt()) != null |
325 || (stmt=TryStmt()) != null | |
325 || (stmt=SetStmt()) != null | 326 || (stmt=SetStmt()) != null |
326 || (stmt=ExpressionsStmt()) != null | 327 || (stmt=ExpressionsStmt()) != null |
327 ) { | 328 ) { |
328 return stmt; | 329 return stmt; |
329 } | 330 } |
479 if( !Keyword("in") ) | 480 if( !Keyword("in") ) |
480 return parser.failure(null); | 481 return parser.failure(null); |
481 Expr expr = RequiredExpr().single(); | 482 Expr expr = RequiredExpr().single(); |
482 RequiredKeyword("do"); | 483 RequiredKeyword("do"); |
483 | 484 |
484 String fnVar = "fn"+ ++forCounter; | 485 String fnVar = "fn" + ++forCounter; |
485 Expr fnExp = new Expr(null,false); | 486 Expr fnExp = new Expr(null,false); |
486 fnExp.add( fnVar + ".call()" ); | 487 fnExp.add( fnVar + ".call()" ); |
487 Stmts stmt = new Stmts(); | 488 Stmts stmt = new Stmts(); |
488 stmt.add( "" | 489 stmt.add( "" |
489 +"LuanFunction "+fnVar+" = Luan.checkFunction(" | 490 +"LuanFunction "+fnVar+" = Luan.checkFunction(" |
589 | 590 |
590 private Stmts RepeatStmt() throws ParseException { | 591 private Stmts RepeatStmt() throws ParseException { |
591 parser.begin(); | 592 parser.begin(); |
592 if( !Keyword("repeat") ) | 593 if( !Keyword("repeat") ) |
593 return parser.failure(null); | 594 return parser.failure(null); |
594 Stmts loop =RequiredLoopBlock(); | 595 Stmts loop = RequiredLoopBlock(); |
595 RequiredKeyword("until"); | 596 RequiredKeyword("until"); |
596 Expr cnd = RequiredExpr().single(); | 597 Expr cnd = RequiredExpr().single(); |
597 Stmts stmt = new Stmts(); | 598 Stmts stmt = new Stmts(); |
598 stmt.add( "do { " ); | 599 stmt.add( "do { " ); |
599 stmt.addAll( loop ); | 600 stmt.addAll( loop ); |
648 hasReturn = false; | 649 hasReturn = false; |
649 } | 650 } |
650 RequiredEnd("end_if"); | 651 RequiredEnd("end_if"); |
651 stmt.add( "} " ); | 652 stmt.add( "} " ); |
652 stmt.hasReturn = hasReturn; | 653 stmt.hasReturn = hasReturn; |
654 return parser.success( stmt ); | |
655 } | |
656 | |
657 int catchCounter = 0; | |
658 | |
659 private Stmts TryStmt() throws ParseException { | |
660 parser.begin(); | |
661 if( !Keyword("try") ) | |
662 return parser.failure(null); | |
663 Stmts tryBlock = RequiredBlock(); | |
664 Stmts catchBlock = null; | |
665 Stmts finallyBlock = null; | |
666 Stmts stmt = new Stmts(); | |
667 stmt.add( "try { LuanImpl.nopTry(); " ); | |
668 stmt.addAll( tryBlock ); | |
669 if( Keyword("catch") ) { | |
670 String name = Name(); | |
671 Expr exp = new Expr(Val.SINGLE,false); | |
672 String var = "e" + ++catchCounter; | |
673 exp.add( var+".table(luan)" ); | |
674 stmt.add( "} catch(LuanException "+var+") { " ); | |
675 stmt.addAll( addSymbol(name,exp) ); | |
676 catchBlock = RequiredBlock(); | |
677 stmt.addAll( catchBlock ); | |
678 popSymbols(1); | |
679 } | |
680 if( Keyword("finally") ) { | |
681 finallyBlock = RequiredBlock(); | |
682 stmt.add( "} finally { " ); | |
683 stmt.addAll( finallyBlock ); | |
684 } | |
685 RequiredEnd("end_try"); | |
686 if( catchBlock==null && finallyBlock==null ) | |
687 stmt.add( "} finally { " ); | |
688 stmt.add( "} " ); | |
689 stmt.hasReturn = finallyBlock!=null && finallyBlock.hasReturn || tryBlock.hasReturn && (catchBlock==null || catchBlock.hasReturn); | |
653 return parser.success( stmt ); | 690 return parser.success( stmt ); |
654 } | 691 } |
655 | 692 |
656 private Stmts SetStmt() throws ParseException { | 693 private Stmts SetStmt() throws ParseException { |
657 parser.begin(); | 694 parser.begin(); |
1465 } | 1502 } |
1466 | 1503 |
1467 private static final Set<String> keywords = new HashSet<String>(Arrays.asList( | 1504 private static final Set<String> keywords = new HashSet<String>(Arrays.asList( |
1468 "and", | 1505 "and", |
1469 "break", | 1506 "break", |
1507 "catch", | |
1470 "continue", | 1508 "continue", |
1471 "do", | 1509 "do", |
1472 "else", | 1510 "else", |
1473 "elseif", | 1511 "elseif", |
1474 "end", | 1512 "end", |
1475 "end_do", | 1513 "end_do", |
1476 "end_for", | 1514 "end_for", |
1477 "end_function", | 1515 "end_function", |
1478 "end_if", | 1516 "end_if", |
1517 "end_try", | |
1479 "end_while", | 1518 "end_while", |
1480 "false", | 1519 "false", |
1520 "finally", | |
1481 "for", | 1521 "for", |
1482 "function", | 1522 "function", |
1483 "goto", | 1523 "goto", |
1484 "if", | 1524 "if", |
1485 "in", | 1525 "in", |