Skip to content

Instantly share code, notes, and snippets.

@bluegraybox
Created July 15, 2011 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluegraybox/1083968 to your computer and use it in GitHub Desktop.
Save bluegraybox/1083968 to your computer and use it in GitHub Desktop.
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.
@bluegraybox
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment