Mercurial > repos > tabletprog
comparison modules/parser.tp @ 231:e48c74a7539e
Fix string parsing in grammar and add it to the primlitsym rule. Add parentheses expressions. Allow parsing from a file.
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 02 Jan 2014 22:49:51 -0800 |
parents | 195f02ba349b |
children | 25b800094623 |
comparison
equal
deleted
inserted
replaced
230:195f02ba349b | 231:e48c74a7539e |
---|---|
490 (match: "\\\"" yield: {"\""}) | 490 (match: "\\\"" yield: {"\""}) |
491 ] | 491 ] |
492 | 492 |
493 string <- match: "\"" . Chars . "\"" where: { | 493 string <- match: "\"" . Chars . "\"" where: { |
494 Chars <- zeroPlus: (matchOne: [ | 494 Chars <- zeroPlus: (matchOne: [ |
495 (charClass: "^\"\\") | 495 match: Reg where: { Reg <- charClass: "^\"\\" } yield: { Reg } |
496 escape | 496 escape |
497 ]) | 497 ]) |
498 } yield: { | 498 } yield: { |
499 if: (Chars length) = 0 { | |
500 Chars <- [] | |
501 } | |
499 Chars join: "" | 502 Chars join: "" |
500 } | 503 } |
501 | 504 |
502 bdigit <- matchOne: [ | 505 bdigit <- matchOne: [ |
503 (match: "0" yield: {0i64}) | 506 (match: "0" yield: {0i64}) |
874 "}" | 877 "}" |
875 } | 878 } |
876 } | 879 } |
877 } | 880 } |
878 | 881 |
882 parenexp <- match: "(" . ws . Expr . ws . ")" where: { | |
883 Expr <- match: expr | |
884 } yield: { | |
885 Expr | |
886 } | |
887 | |
879 primlitsym <- match: hws . Lit where: { | 888 primlitsym <- match: hws . Lit where: { |
880 Lit <- matchOne: [ | 889 Lit <- matchOne: [ |
881 hexlit | 890 hexlit |
882 binary | 891 binary |
883 decimal | 892 decimal |
884 symexpr | 893 symexpr |
885 lambda | 894 lambda |
886 object | 895 object |
887 listlit | 896 listlit |
888 arraylit | 897 arraylit |
898 string | |
899 parenexp | |
889 ] | 900 ] |
890 } yield: { | 901 } yield: { |
891 Lit | 902 Lit |
892 } | 903 } |
904 | |
905 top <- matchOne: [ | |
906 object | |
907 lambda | |
908 ] | |
893 | 909 |
894 testmatchintlit <- :val matchfun { | 910 testmatchintlit <- :val matchfun { |
895 res <- matchfun: val | 911 res <- matchfun: val |
896 if: (res matched?) { | 912 if: (res matched?) { |
897 y <- res yield | 913 y <- res yield |
899 } else: { | 915 } else: { |
900 print: val . " did not match\n" | 916 print: val . " did not match\n" |
901 } | 917 } |
902 } | 918 } |
903 | 919 |
904 main <- { | 920 main <- :args { |
905 cmatch <- alpha: "czx0123" | 921 cmatch <- alpha: "czx0123" |
906 zeromatch <- alpha: "01234" | 922 zeromatch <- alpha: "01234" |
907 if: (cmatch matched?) { | 923 if: (cmatch matched?) { |
908 print: "czx0123 matched with length " . (cmatch matchlen) . "\n" | 924 print: "czx0123 matched with length " . (cmatch matchlen) . "\n" |
909 } else: { | 925 } else: { |
939 testmatchintlit: "123u16" :s {decimal: s} | 955 testmatchintlit: "123u16" :s {decimal: s} |
940 testmatchintlit: "0x20" :s {hexlit: s} | 956 testmatchintlit: "0x20" :s {hexlit: s} |
941 testmatchintlit: "0x42u64" :s {hexlit: s} | 957 testmatchintlit: "0x42u64" :s {hexlit: s} |
942 testmatchintlit: "0b10101" :s {binary: s} | 958 testmatchintlit: "0b10101" :s {binary: s} |
943 code <- "#{ foo <- 123 > 0x42 && 42 < 104\n bar <- 0xABC + 0b1010101\n baz <- 0b1010 * 5\n qux <- fo: 38 shizzle: bam\n quine <- 123 | [4 5 6 fiddle sticks]\n quizzle <- #[receiver meth: arg]\n blah <- :arg arg2 :arg3 { arg + arg2 + arg3 }}" | 959 code <- "#{ foo <- 123 > 0x42 && 42 < 104\n bar <- 0xABC + 0b1010101\n baz <- 0b1010 * 5\n qux <- fo: 38 shizzle: bam\n quine <- 123 | [4 5 6 fiddle sticks]\n quizzle <- #[receiver meth: arg]\n blah <- :arg arg2 :arg3 { arg + arg2 + arg3 }}" |
944 codem <- expr: code | 960 if: (args length) > 1 { |
961 file <- os open: (args get: 1) (os O_RDONLY) | |
962 code <- os read: file 1024 | |
963 } | |
964 codem <- top: code | |
945 if: (codem matched?) { | 965 if: (codem matched?) { |
946 print: code . "\nmatched with yield:\n" . (codem yield) . "\n" | 966 print: code . "\nmatched with yield:\n" . (codem yield) . "\n" |
947 } else: { | 967 } else: { |
948 print: code . "\ndid not match\n" | 968 print: code . "\ndid not match\n" |
949 } | 969 } |