diff 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
line wrap: on
line diff
--- a/modules/parser.tp	Thu Jan 02 21:04:10 2014 -0800
+++ b/modules/parser.tp	Thu Jan 02 22:49:51 2014 -0800
@@ -492,10 +492,13 @@
 
 	string <- match: "\"" . Chars . "\"" where: {
 		Chars  <- zeroPlus: (matchOne: [
-			(charClass: "^\"\\")
+			match: Reg where: { Reg <- charClass: "^\"\\" } yield: { Reg }
 			escape
 		])
 	} yield: {
+		if: (Chars length) = 0 {
+			Chars <- []
+		}
 		Chars join: ""
 	}
 
@@ -876,6 +879,12 @@
 		}
 	}
 
+	parenexp <- match: "(" . ws . Expr . ws . ")" where: {
+		Expr <- match: expr
+	} yield: {
+		Expr
+	}
+
 	primlitsym <- match: hws . Lit where: {
 		Lit <- matchOne: [
 			hexlit
@@ -886,11 +895,18 @@
 			object
 			listlit
 			arraylit
+			string
+			parenexp
 		]
 	} yield: {
 		Lit
 	}
 
+	top <- matchOne: [
+		object
+		lambda
+	]
+
 	testmatchintlit <- :val matchfun {
 		res <- matchfun: val
 		if: (res matched?) {
@@ -901,7 +917,7 @@
 		}
 	}
 
-	main <- {
+	main <- :args {
 		cmatch <- alpha: "czx0123"
 		zeromatch <- alpha: "01234"
 		if: (cmatch matched?) {
@@ -941,7 +957,11 @@
 		testmatchintlit: "0x42u64" :s {hexlit: s}
 		testmatchintlit: "0b10101" :s {binary: s}
 		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 }}"
-		codem <- expr: code
+		if: (args length) > 1 {
+			file <- os open: (args get: 1) (os O_RDONLY)
+			code <- os read: file 1024
+		}
+		codem <- top: code
 		if: (codem matched?) {
 			print: code . "\nmatched with yield:\n" . (codem yield) . "\n"
 		} else: {