view parse.rhope @ 134:386f4a874821

More work on new parser
author Mike Pavone <pavone@retrodev.com>
date Wed, 10 Nov 2010 22:29:49 -0500
parents 1f238280047f
children 47ab97730865
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 Call
{
	Worker
	Blueprint
	Arguments
}

Worker Call[worker,blueprint:out]
{
	out <- [[[Build[Worker Call()]]Worker <<[worker]]Blueprint <<[blueprint]]Arguments <<[()]
}

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

Blueprint Global Reference
{
	Store
	Variable
}

Blueprint Pipe Value
{
	Name
}

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

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

Blueprint Literal
{
	Value
}

Literal[val:out]
{
	out <- [Build[Literal()]]Value <<[val]
}

Blueprint Block
{
	Tree
}

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

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

Blueprint Field Call
{
	Name
	Set?
	Arguments
}

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

After Literal[tokens,current,literal:out,out index]
{
	next index <- [tokens]Next[current]
	{
		after <- [tokens]Index[~]
		[after]Type Match["Call Separator"]
		{
			out,out index <- To Call[tokens,next index,[()]Append[literal]]
		}{
			[after]Type Match[("Newline","Block End","List End")]
			{
				out <- literal
				out index <- current
			}
		}
	}{
		out <- literal
		out index <- current
	}
}

Parse Hex[text:out,error]
{

}

Parse Number[text:out,error]
{
	[text]Starts With["0x"]
	{
		[text]Slice[2] {}
		{ out,error <- Parse Hex[~] }
	}{
		[text]Contains["."]
		{
		
		}{
			val,sign,size <-[text]Partition[("u","i")] {} {} {}
			{
				//TODO: Produce "bigint" instead of Int32
				out <- Int32[text]
			}
		}
	}
}

Expression[tokens,current:out,out index,none]
{
	token <- [tokens]Index[current]
	[token]Type Match["Symbol"]
	{
		,end stream <- [tokens]Next[current]
		{
			after <- [tokens]Index[~]
			[after]Type Match["Args Begin"]
			{
				
			}{
				[after]Type Match["Call Separator"]
				{
					
				}{
					[after]Type Match["Newline"]
					{
					
					}{
					
					}
				}
			}
		}
	}{
		[token]Type Match[("Field Get","Field Set")]
		{
			[token]Type Match["Field Set"]
			{ set? <- Yes }
			{ set? <- No }
			Field Call[[token]Text >>, set?]
		}{
			[token]Type Match["Args Begin"]
			{
			
			}{
				[token]Type Match["List Begin"]
				{
				
				}{
					[token]Type Match["String Literal"]
					{
						lit <- [token]Text >>
					}{
						[token]Type Match["Numeric Literal"]
						{
							lit <- Parse Number[[token]Text >>]
						}{
							[token]Type Match["Newline"]
							{
								,none <- [tokens]Next[current]
								{ out, out index <- Expression[tokens, ~] }
							}{
								none, unexpected token <- [token]Type Match["Block End"]
							}
						}
					}
					out,out index <- After Literal[tokens,current,Literal[lit]]
				}
			}
		}
	}
}
	

Body[node,tokens,current,depth:out,out index, done]
{
	Print[["Body: Depth="]Append[String[depth]]]
	If[[depth] > [0]]
	{
		//symbol list = Symbol [List Separator symbol list]
		//value = String Literal | Number Literal | List Literal | Symbol | Block Reference | expression
		//call = [Args Begin arg list Args End]Symbol[Args Begin arg list Args End] | (value | Arg Placeholder) Call Separator Symbol
		//arg list = value | Arg Placeholder [List Separator arg list]
		//block = Block Begin [expressions] Block End
		//blocks = block [blocks]
		//expressions = expression [expressions]
		//expression = [symbol list Assignment] call | value blocks | Newline
		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"]
		{
			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]
	}
}

NotComment[token:out]
{
	[token]Type Match[("Block Comment","Line Comment")]
	{
		out <- No
	}{
		out <- Yes
	}
}

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