Created
July 15, 2011 03:02
-
-
Save bluegraybox/1083968 to your computer and use it in GitHub Desktop.
This is the core functionality from https://github.com/bluegraybox/examples/blob/first-concurrent/indent-parser/parse_indents.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
build_nodes(Parent, Group, []) -> Parent ! {Group, []}; | |
build_nodes(Parent, Group, Lines) -> | |
[First|Rest] = Lines, | |
%% split off our children from the rest of the Lines. | |
%% the first line with an indent =< ours is a sibling, not a child | |
Criterion = fun(X) -> X#line.indent =< First#line.indent end, | |
[ChildLines, SiblingLines] = split_list(Criterion, Rest), | |
spawn(?MODULE, build_nodes, [self(), children, ChildLines]), | |
spawn(?MODULE, build_nodes, [self(), siblings, SiblingLines]), | |
receive | |
{children, Children} -> | |
Node = #node{content=First#line.content, children=Children} | |
end, | |
receive | |
{siblings, Siblings} -> | |
io:fwrite( "[~s] ~p~n", [Node#node.content, self()]), | |
Parent ! {Group, [Node|Siblings]} | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the full code for the concurrent Erlang version of the indent parser. There's also a sequential Erlang version and a Lisp version.