Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rebootstrap FTW.

  • Loading branch information...
commit adb132b62de7ac65652d1401e2d86746f22107bf 1 parent ed04d23
@seancribbs authored
Showing with 29 additions and 33 deletions.
  1. +1 −1  src/neotoma.erl
  2. +28 −32 src/neotoma_parse.erl
View
2  src/neotoma.erl
@@ -107,4 +107,4 @@ generate_transform_stub(XfFile,ModName) ->
%% @doc Bootstraps the neotoma metagrammar. Intended only for internal development!
%% @equiv file("src/neotoma_parse.peg")
bootstrap() ->
- file("priv/neotoma_parse.peg").
+ file("priv/neotoma_parse.peg", [{output, "src/"}]).
View
60 src/neotoma_parse.erl
@@ -72,8 +72,6 @@ verify_rules() ->
end, NTs),
Root.
-
-
file(Filename) -> {ok, Bin} = file:read_file(Filename), parse(Bin).
parse(List) when is_list(List) -> parse(list_to_binary(List));
@@ -263,33 +261,23 @@ end
-
-
-
-
p(Inp, Index, Name, ParseFun) ->
p(Inp, Index, Name, ParseFun, fun(N, _Idx) -> N end).
p(Inp, StartIndex, Name, ParseFun, TransformFun) ->
- % Grab the memo table from ets
- Memo = get_memo(StartIndex),
- % See if the current reduction is memoized
- case proplists:lookup(Name, Memo) of
- % If it is, return the result
- {Name, Result} -> Result;
- % If not, attempt to parse
- _ ->
+ case get_memo(StartIndex, Name) of % See if the current reduction is memoized
+ {ok, Memo} -> %Memo; % If it is, return the stored result
+ Memo;
+ _ -> % If not, attempt to parse
case ParseFun(Inp, StartIndex) of
- % If it fails, memoize the failure
- {fail,_} = Failure ->
- memoize(StartIndex, [{Name, Failure}|Memo]),
- Failure;
- % If it passes, transform and memoize the result.
- {Result, InpRem, NewIndex} ->
- Transformed = TransformFun(Result, StartIndex),
- memoize(StartIndex, [{Name, {Transformed, InpRem, NewIndex}}|Memo]),
- {Transformed, InpRem, NewIndex}
- end
+ {fail,_} = Failure -> % If it fails, memoize the failure
+ Result = Failure;
+ {Match, InpRem, NewIndex} -> % If it passes, transform and memoize the result.
+ Transformed = TransformFun(Match, StartIndex),
+ Result = {Transformed, InpRem, NewIndex}
+ end,
+ memoize(StartIndex, Name, Result),
+ Result
end.
setup_memo() ->
@@ -298,14 +286,22 @@ setup_memo() ->
release_memo() ->
ets:delete(memo_table_name()).
-memoize(Position, Struct) ->
- ets:insert(memo_table_name(), {Position, Struct}).
-
-get_memo(Position) ->
- case ets:lookup(memo_table_name(), Position) of
- [] -> [];
- [{Position, PList}] -> PList
- end.
+memoize(Index, Name, Result) ->
+ Memo = case ets:lookup(memo_table_name(), Index) of
+ [] -> [];
+ [{Index, Plist}] -> Plist
+ end,
+ ets:insert(memo_table_name(), {Index, [{Name, Result}|Memo]}).
+
+get_memo(Index, Name) ->
+ case ets:lookup(memo_table_name(), Index) of
+ [] -> {error, not_found};
+ [{Index, Plist}] ->
+ case proplists:lookup(Name, Plist) of
+ {Name, Result} -> {ok, Result};
+ _ -> {error, not_found}
+ end
+ end.
memo_table_name() ->
get(parse_memo_table).
Please sign in to comment.
Something went wrong with that request. Please try again.