Mercurial Hosting > luan
comparison src/luan/interp/LuaParser.java @ 16:2a30281ef47c
finish number literals;
add long string literal;
git-svn-id: https://luan-java.googlecode.com/svn/trunk@17 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 29 Nov 2012 10:36:38 +0000 |
parents | bb59d7ea223b |
children | 09d41f7490a8 |
comparison
equal
deleted
inserted
replaced
15:bb59d7ea223b | 16:2a30281ef47c |
---|---|
19 import luan.LuaNumber; | 19 import luan.LuaNumber; |
20 import luan.LuaState; | 20 import luan.LuaState; |
21 | 21 |
22 | 22 |
23 public class LuaParser extends BaseParser<Object> { | 23 public class LuaParser extends BaseParser<Object> { |
24 static final Object PAREN = new Object(); | 24 int nEquals; |
25 int parens = 0; | |
26 | |
27 boolean nEquals(int n) { | |
28 nEquals = n; | |
29 return true; | |
30 } | |
31 | |
32 boolean incParens() { | |
33 parens++; | |
34 return true; | |
35 } | |
36 | |
37 boolean decParens() { | |
38 parens--; | |
39 return true; | |
40 } | |
25 | 41 |
26 public Rule Target() { | 42 public Rule Target() { |
27 return Sequence( | 43 return Sequence( |
28 Spaces(), | 44 Spaces(), |
29 FirstOf( | 45 FirstOf( |
275 ); | 291 ); |
276 } | 292 } |
277 | 293 |
278 Rule TableExpr() { | 294 Rule TableExpr() { |
279 return Sequence( | 295 return Sequence( |
280 '{', Spaces(), | 296 '{', incParens(), Spaces(), |
281 push(PAREN), | |
282 push( new ArrayList<TableExpr.Field>() ), | 297 push( new ArrayList<TableExpr.Field>() ), |
283 push( 1.0 ), // counter | 298 push( 1.0 ), // counter |
284 Optional( | 299 Optional( |
285 Field(), | 300 Field(), |
286 ZeroOrMore( | 301 ZeroOrMore( |
287 FieldSep(), | 302 FieldSep(), |
288 Field() | 303 Field() |
289 ), | 304 ), |
290 Optional( FieldSep() ) | 305 Optional( FieldSep() ) |
291 ), | 306 ), |
292 '}', | 307 '}', decParens(), |
293 push( newTableExpr() ), | 308 push( newTableExpr() ), |
294 drop(1), | |
295 Spaces() | 309 Spaces() |
296 ); | 310 ); |
297 } | 311 } |
298 | 312 |
299 TableExpr newTableExpr() { | 313 TableExpr newTableExpr() { |
355 | 369 |
356 Rule Var() { | 370 Rule Var() { |
357 return Sequence( | 371 return Sequence( |
358 FirstOf( | 372 FirstOf( |
359 Sequence( | 373 Sequence( |
360 '(', Spaces(), push(PAREN), Expr(), ')', drop(1), Spaces(), | 374 '(', incParens(), Spaces(), Expr(), ')', decParens(), Spaces(), |
361 push(expr(pop())), | 375 push(expr(pop())), |
362 push(null) // marker | 376 push(null) // marker |
363 ), | 377 ), |
364 Sequence( | 378 Sequence( |
365 push(EnvExpr.INSTANCE), | 379 push(EnvExpr.INSTANCE), |
390 // function should be on top of the stack | 404 // function should be on top of the stack |
391 Rule Args() { | 405 Rule Args() { |
392 return Sequence( | 406 return Sequence( |
393 FirstOf( | 407 FirstOf( |
394 Sequence( | 408 Sequence( |
395 '(', Spaces(), push(PAREN), Expressions(), ')', drop(1), Spaces() | 409 '(', incParens(), Spaces(), Expressions(), ')', decParens(), Spaces() |
396 ), | 410 ), |
397 Sequence( | 411 Sequence( |
398 TableExpr(), | 412 TableExpr(), |
399 push( new ExpList.SingleExpList(expr(pop())) ) | 413 push( new ExpList.SingleExpList(expr(pop())) ) |
400 ), | 414 ), |
437 } | 451 } |
438 return true; | 452 return true; |
439 } | 453 } |
440 | 454 |
441 Rule SubExpr() { | 455 Rule SubExpr() { |
442 return Sequence( '[', Spaces(), push(PAREN), Expr(), ']', drop(1), Spaces() ); | 456 return Sequence( '[', incParens(), Spaces(), Expr(), ']', decParens(), Spaces() ); |
443 } | 457 } |
444 | 458 |
445 Rule Name() { | 459 Rule Name() { |
446 return Sequence( | 460 return Sequence( |
447 Sequence( | 461 Sequence( |
531 | 545 |
532 Rule Number() { | 546 Rule Number() { |
533 return FirstOf( | 547 return FirstOf( |
534 Sequence( | 548 Sequence( |
535 IgnoreCase("0x"), | 549 IgnoreCase("0x"), |
536 OneOrMore(HexDigit()), | 550 HexNumber() |
537 push((double)Long.parseLong(match(),16)) | |
538 ), | 551 ), |
539 Sequence( | 552 Sequence( |
540 DecNumber(), | 553 DecNumber(), |
541 push(Double.parseDouble(match())) | 554 push(Double.parseDouble(match())) |
542 ) | 555 ) |
564 | 577 |
565 Rule Int() { | 578 Rule Int() { |
566 return OneOrMore(Digit()); | 579 return OneOrMore(Digit()); |
567 } | 580 } |
568 | 581 |
582 Rule Digit() { | |
583 return CharRange('0', '9'); | |
584 } | |
585 | |
586 Rule HexNumber() { | |
587 return FirstOf( | |
588 Sequence( | |
589 HexInt(), | |
590 push( (double)Long.parseLong(match(),16) ), | |
591 Optional( '.', Optional(HexDec()) ), | |
592 HexExponent() | |
593 ), | |
594 Sequence( push(0.0), '.', HexDec(), HexExponent() ) | |
595 ); | |
596 } | |
597 | |
598 Rule HexDec() { | |
599 return Sequence( | |
600 HexInt(), | |
601 push( (Double)pop() + (double)Long.parseLong(match(),16) / Math.pow(16,matchLength()) ) | |
602 ); | |
603 } | |
604 | |
605 Rule HexExponent() { | |
606 return Optional( | |
607 IgnoreCase('p'), | |
608 Sequence( | |
609 Optional(AnyOf("+-")), | |
610 HexInt() | |
611 ), | |
612 push( (Double)pop() * Math.pow(2,(double)Long.parseLong(match())) ) | |
613 ); | |
614 } | |
615 | |
616 Rule HexInt() { | |
617 return OneOrMore(Digit()); | |
618 } | |
619 | |
620 | |
569 Rule HexDigit() { | 621 Rule HexDigit() { |
570 return FirstOf( | 622 return FirstOf( |
571 Digit(), | 623 Digit(), |
572 AnyOf("abcdefABCDEF") | 624 AnyOf("abcdefABCDEF") |
573 ); | 625 ); |
574 } | 626 } |
575 | 627 |
576 Rule Digit() { | |
577 return CharRange('0', '9'); | |
578 } | |
579 | |
580 Rule StringLiteral() { | 628 Rule StringLiteral() { |
581 return FirstOf( | 629 return FirstOf( |
582 QuotedString('"'), | 630 QuotedString('"'), |
583 QuotedString('\'') | 631 QuotedString('\''), |
632 LongString() | |
633 ); | |
634 } | |
635 | |
636 Rule LongString() { | |
637 return Sequence( | |
638 "[", | |
639 ZeroOrMore('='), | |
640 nEquals(matchLength()), | |
641 '[', | |
642 ZeroOrMore( | |
643 TestNot(LongBracketsEnd()), | |
644 ANY | |
645 ), | |
646 push( match() ), | |
647 LongBracketsEnd() | |
584 ); | 648 ); |
585 } | 649 } |
586 | 650 |
587 Rule QuotedString(char quote) { | 651 Rule QuotedString(char quote) { |
588 return Sequence( | 652 return Sequence( |
647 return ZeroOrMore( | 711 return ZeroOrMore( |
648 FirstOf( | 712 FirstOf( |
649 AnyOf(" \t"), | 713 AnyOf(" \t"), |
650 Comment(), | 714 Comment(), |
651 Sequence( '\\', EndOfLine() ), | 715 Sequence( '\\', EndOfLine() ), |
652 Sequence( AnyOf("\r\n"), inParen() ) | 716 Sequence( AnyOf("\r\n"), parens > 0 ) |
653 ) | 717 ) |
654 ); | 718 ); |
655 } | |
656 | |
657 boolean inParen() { | |
658 ValueStack stack = getContext().getValueStack(); | |
659 int size = stack.size(); | |
660 for( int i=0; i<size; i++ ) { | |
661 if( peek(i) == PAREN ) | |
662 return true; | |
663 } | |
664 return false; | |
665 } | 719 } |
666 | 720 |
667 Rule Comment() { | 721 Rule Comment() { |
668 Var<Integer> n = new Var<Integer>(); | |
669 return Sequence( | 722 return Sequence( |
670 "--[", | 723 "--[", |
671 ZeroOrMore('='), | 724 ZeroOrMore('='), |
672 n.set(matchLength()), | 725 nEquals(matchLength()), |
673 '[', | 726 '[', |
674 ZeroOrMore( | 727 ZeroOrMore( |
675 TestNot(CommentEnd(n)), | 728 TestNot(LongBracketsEnd()), |
676 ANY | 729 ANY |
677 ), | 730 ), |
678 CommentEnd(n) | 731 LongBracketsEnd() |
679 ); | 732 ); |
680 } | 733 } |
681 | 734 |
682 Rule CommentEnd(Var<Integer> n) { | 735 Rule LongBracketsEnd() { |
683 return Sequence( ']', ZeroOrMore('='), n.get()==matchLength(), ']' ); | 736 return Sequence( ']', ZeroOrMore('='), nEquals==matchLength(), ']' ); |
684 } | 737 } |
685 | 738 |
686 // for debugging | 739 // for debugging |
687 boolean print(Object o) { | 740 boolean print(Object o) { |
688 System.out.println(o); | 741 System.out.println(o); |