shithub: mc

Download patch

ref: da5bfc6d15a50130afdcd37ed9af737a81724506
parent: a3e640134dc9e002a2b56ee14f7250c36871d1e8
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Jan 8 20:40:59 EST 2016

Parse use statements.

--- a/mparse/parse.myr
+++ b/mparse/parse.myr
@@ -26,12 +26,11 @@
 		.ns = std.mkht(std.strhash, std.streq),
 	])
 	f.globls.super = `std.Some f.builtin
+	optendlns(ts)
 	while true
 		match tokpeek(ts)
 		| (loc, `Teof):	
 			break
-		| (loc, `Tendln):
-			toknext(ts)
 		| (loc, `Tuse):
 			usestmt(ts, f)
 		| (loc, `Tpkg):
@@ -40,14 +39,27 @@
 			traitdef(ts, f)
 		| (loc, `Ttype):
 			tydef(ts, f)
-		| _:	
-			decl(ts, ts)
+		| (loc, tok):	
+			if !decl(ts, ts)
+				err(loc, "invalid top level item near {}", tok)
+			;;
 		;;
+		endlns(ts)
 	;;
 	-> f
 }
 
 const usestmt = {ts, f
+	match toknext(ts)
+	| (loc, `Tuse): /* ok */
+	| (loc, t):	err(loc, "unexpected token in use {}\n", t)
+	;;
+
+	match toknext(ts)
+	| (loc, `Tstrlit str):	f.uses = std.slpush(f.uses, `Ulocal str)
+	| (loc, `Tident id):	f.uses = std.slpush(f.uses, `Ulib id)
+	| (loc, t):	err(loc, "unexpected {} after use\n", t)
+	;;
 }
 
 const pkgdef = {ts, f
@@ -73,7 +85,7 @@
 }
 
 const pkgbody = {ts, f
-	endlns(ts)
+	optendlns(ts)
 }
 
 const traitdef = {ts, f
@@ -83,9 +95,17 @@
 }
 
 const decl = {ts, f
+	-> false
 }
 
 const endlns = {ts
+	match tokpeek(ts)
+	| (loc, `Tendln):	optendlns(ts)
+	| (loc, t):	err(loc, "expected \\n, got {}\n", t)
+	;;
+}
+
+const optendlns = {ts
 	while true
 		match tokpeek(ts)
 		| (loc, `Tendln):	toknext(ts)