view parse.rhope @ 131:0a4682be2db2

Modify lexer and new parser to work in compiler
author Mike Pavone <pavone@retrodev.com>
date Fri, 05 Nov 2010 02:43:34 +0000
parents a4d2d633a356
children 1f238280047f
line wrap: on
line source


Blueprint Error
{
	Message
	Line
	Column
}

Error[msg,text:out]
{
	out <- [[[Build[Error()]]Message <<[msg]]Line << [ [[text]Line >>]+[1] ]]Column << [ [[text]Column >>]+[1] ]
}

String@Error[error:out]
{
	out <- [[[[[error]Message >>]Append[" on line "]]Append[String[[error]Line >>]]]Append[" at column "]]Append[String[[error]Column >>]]
}

Blueprint PImport Node
{
	File
}

PImport Node[file:out]
{
	out <- [Build[PImport Node()]]File <<[file]
}

String@PImport Node[node:out]
{
	out <- ["Import: "]Append[[node]File >>]
}

Blueprint Worker Node
{
	Name
	Blueprint
	Inputs
	Outputs
	Tree
}

Add Node Input@Worker Node[node,input:out]
{
	Print["Add Node Input"]
	Print[["Input: "]Append[String[input]]]
	out <- [node]Inputs <<[[[node]Inputs >>]Append[input]]
}

Add Node Output@Worker Node[node,output:out]
{
	out <- [node]Outputs <<[[[node]Outputs >>]Append[output]]
}

String@Worker Node[node:out]
{
	out <- [[[[["Worker: "]Append[[node]Name >>]
		]Append[ ["\n\tBlueprint:\t"]Append[String[[node]Blueprint >>]] ]
		]Append[ ["\n\tInputs: \t"]Append[[[node]Inputs >>]Join[", "]] ]
		]Append[ ["\n\tOutputs:\t"]Append[[[node]Outputs >>]Join[", "]] ]
		]Append[ ["\n\tTree:   \t"]Append[ [[String[[node]Tree >>]]Split["\n"]]Join["\n\t"] ] ]
}

Blueprint Statement
{
	Expression
	Assignments
	Blocks
}

Statement[expression,assignments:out]
{
	out <- [[[Build[Statement()]]Expression <<[expression]]Assignments <<[assignments]]Blocks <<[()]
}

Add Block@Statement[statement,block:out]
{
	out <- [statement]Blocks <<[ [[statement]Blocks >>]Append[block] ]
}

String@Statement[statement:out]
{
	out <- [[["Statement\n\tExpression:\t"
		]Append[ [[String[[statement]Expression >>]]Split["\n"]]Join["\n\t"] ]
		]Append[ ["\n\tAssignments:\t"]Append[[[statement]Assignments >>]Join[", "]] ]
		]Append[ ["\n\tBlocks:\t"]Append[ [[String[[statement]Blocks >>]]Split["\n"]]Join["\n\t"] ] ]
}

Blueprint Worker Expression
{
	Worker
	Blueprint
	Arguments
}

String@Worker Expression[exp:out]
{
	out <- ["Worker Expression\n\tWorker:\t"]Append[[exp]Worker >>]
}

Blueprint Global Expression
{
	Store
	Variable
}

Blueprint Pipe Expression
{
	Name
}

Pipe Expression[name:out]
{
	out <- [Build[Pipe Expression()]]Name <<[name]
}

String@Pipe Expression[pipe:out]
{
	out <- ["Pipe Expression\n\tName:\t"]Append[[pipe]Name >>]
}

Blueprint Block
{
	Tree
}

Block[:out]
{
	out <- [Build[Block()]]Tree <<[()]
}

String@Block[block:out]
{
	out <- Fold[_String Seq[?], "Block", [block]Tree >>]
}

Blueprint Field Expression
{
	Name
	Set?
}

Field Expression[name,set?:out]
{
	out <- [[Build[Field Expression()]]Name <<[name]]Set? <<[set?]
}

Body[node,tokens,current,depth:out,out index, done]
{
	Print[["Body: Depth="]Append[String[depth]]]
	If[[depth] > [0]]
	{
		token <- [tokens]Index[current]
		[token]Type Match["Block Begin"]
		{
			,end stream <- [tokens]Next[current]
			{
				out,out index, done <- Body[node,tokens,~,[depth]+[1]]
			}
		}{
			[token]Type Match["Block End"]
			{
				[tokens]Next[current]
				{
					out,out index, done <- Body[node,tokens,~,[depth]-[1]]
				}{
					,end stream <- If[[depth] = [1]]
					{
						Print["done"]
						out <- Val[node]
						done <- Yes
					}
				}
			}{
				,end stream <- [tokens]Next[current]
				{
					out,out index, done <- Body[node,tokens,~,depth]
				}
			}
		}
	}{
		out <- node
		out index <- current
	}
	Val[end stream]
	{
		Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
	}
}

Before Body[node,tokens,current:out,out index, done]
{
	Print["Before body"]
	token <- [tokens]Index[current]
	next,end stream <- [tokens]Next[current]
	{
		[token]Type Match["Block Begin"]
		{
			out,out index, done <- Body[node,tokens,next,1]
		}{
			out,out index, done <- Before Body[node,tokens,next]
		}
	}
	Val[end stream]
	{
		Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
	}
}

Outputs[node,tokens,current:out,out index, done]
{
	Print["outputs"]
	token <- [tokens]Index[current]
	next,end stream <- [tokens]Next[current]
	{
		[token]Type Match["Symbol"]
		{
			out,out index, done <- Outputs[[node]Add Node Output[[token]Text >>], tokens, next]
		}{
			[token]Type Match["Args End"]
			{
				out,out index, done <- Before Body[node, tokens, next]
			}{
				[token]Type Match[("List Separator","Block Comment","Line Comment","Newline")]
				{
					out,out index, done <- Outputs[node, tokens, next]
				}{
					Print[String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
				}
			}
		}
	}
	Val[end stream]
	{
		Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
	}
}

Inputs[node,tokens,current:out,out index, done]
{
	Print["Inputs"]
	token <- [tokens]Index[current]
	next,end stream <- [tokens]Next[current]
	{
		[token]Type Match["Symbol"]
		{

			out,out index, done <- Inputs[[node]Add Node Input[[token]Text >>], tokens, next]
		}{
			[token]Type Match["Name Separator"]
			{
				Print["in out sep"]
				out,out index, done <- Outputs[node, tokens, next]
			}{
				[token]Type Match["Args End"]
				{
					out,out index, done <- Before Body[node, tokens, next]
				}{
					[token]Type Match[("List Separator","Block Comment","Line Comment","Newline")]
					{
						out,out index,done <- Inputs[node, tokens, next]
					}{
						Print[String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
					}
				}
			}
		}
	}
	Val[end stream]
	{
		Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
	}
}

Method[node,tokens,current:out,out index,done]
{
	token <- [tokens]Index[current]
	next <- [tokens]Next[current]
	{
		[token]Type Match["Symbol"]
		{
			out,out index,done <- Before Inputs[[node]Blueprint <<[[token]Text >>], tokens, next]
		}{
			[token]Type Match[("List Separator","Block Comment","Line Comment","Newline")]
			{
				out,out index,done <- Method[node, tokens, next]
			}{
				Print[String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
			}
		}
	}{
		Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
	}
}

//TODO: support method declarations
Before Inputs[node, tokens, current:out, out index, done]
{
	Print["Before Inputs"]
	token <- [tokens]Index[current]
	[token]Type Match["Args Begin"]
	{
		[tokens]Next[current]
		{
			out,out index, done <- Inputs[node, tokens, ~]
		}{
			Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
		}
	}{
		[token]Type Match["Method Separator"]
		{
			[tokens]Next[current]
			{
				out,out index,done <- Method[node, tokens, ~]
			}{
				Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
			}
		}{
			[token]Type Match[("Line Comment","Block Comment","Newline")]
			{
				continue <- Yes
			}{
				Print[String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
			}
			Val[continue]
			{
				[tokens]Next[current]
				{
					out,out index, done <- Before Inputs[node, tokens, ~]
				}{
					Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
				}
			}
		}
	}
}

Worker Node[name, tokens, current:out,out index, done]
{
	Print[["Worker: "]Append[String[name]]]
	out,out index, done <- Before Inputs[[[[[[Build[Worker Node()]]Name <<[name]]Inputs <<[()]]Outputs <<[()]]Blueprint <<[No]]Tree <<[()], tokens, current]
}

Skip Nodes[toskip, stop, tokens, current:out]
{
	token <- [tokens]Index[current]
	next <- [tokens]Next[current]
	{
		[token]Type Match[stop]
		{
			out <- Val[next]
		}{
			[token]Type Match[toskip]
			{
				out <- Skip Nodes[toskip, stop, tokens, next]
			}{
				Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
			}
		}
	}{
		Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
	}
}

Blueprint PBlueprint Node
{
	Name
	Fields
}

Fields[node,tokens,current:out,out index,done]
{
	token <- [tokens]Index[current]
	next <- [tokens]Next[current]
	{
		[token]Type Match["Symbol"]
		{
			out,out index,done <- Fields[[node]Add BField[[token]Text >>], tokens, next]
		}{
			[token]Type Match["Block End"]
			{
				out <- Val[node]
				out index <- Val[next]
			}{
				[token]Type Match[("Newline","Line Comment","Block Comment")]
				{
					out,out index,done <- Fields[node, tokens, next]
				}{
					Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
				}
			}
		}
	}{
		[token]Type Match["Block End"]
		{
			out <- Val[node]
			done <- Yes
		}{
			Print[String[Error[["Unexpected end of stream after token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
		}
	}
}

PBlueprint Node[name, tokens, current:out,out index,done]
{
	node <- [[Build[PBlueprint Node()]]Name <<[name]]Fields <<[()]
	next <- Skip Nodes[("Newline","Block Comment","Comment"), "Block Begin", tokens, current]
	out,out index,done <- Fields[node, tokens, next]
}

Add BField@PBlueprint Node[node,field:out]
{
	out <- [node]Fields <<[[[node]Fields >>]Append[field]]
}

Top Level[tokens, current, nodes:out]
{
	token <- [tokens]Index[current]
	Print[[token]Type >>]
	[token]Type Match["Symbol"]
	{
		[[token]Text >>]After["Import "]
		{
			next nodes <- [nodes]Append[PImport Node[~]]
			next index <- Val[current]
		}{
			blueprint name,worker name <- [~]After["Blueprint "]
			{
				[tokens]Next[current]
				{
					, next index <- PBlueprint Node[blueprint name, tokens, ~]
					{
						next nodes <- [nodes]Append[~]
					} {} {
						Print["done!"]
						out <- Val[next nodes]
					}
				}{
					Print[String[Error[["Unexpected end of stream after symbol "]Append[[token]Text >>], [token]Raw Text >>]]]
				}
			}{
				[tokens]Next[current]
				{
					, next index <- Worker Node[worker name, tokens, ~]
					{
						next nodes <- [nodes]Append[~]
					} {} {
						Print["done!"]
						out <- Val[next nodes]
					}
				}{
					Print[String[Error[["Unexpected end of stream after symbol "]Append[[token]Text >>], [token]Raw Text >>]]]
				}
			}
		}
		
	}{
		[token]Type Match[("Newline","Block Comment","Line Comment")]
		{
			next nodes <- Val[nodes]
			next index <- Val[current]
		}{
			Print[String[Error[["Unexpected token of type "]Append[[token]Type >>], [token]Raw Text >>]]]
		}
	}
	[tokens]Next[next index]
	{
		out <- Top Level[tokens, ~, next nodes]
	}{
		out <- Val[next nodes]
	}
}

Parse[tokens:out]
{
	[tokens]First
	{
		out <- Top Level[tokens, ~, ()]
	}{
		out <- ()
	}
}