Skip to content

Commit

Permalink
Merge pull request #9 from tsloughter/blanks
Browse files Browse the repository at this point in the history
handle empty comments and empty lines
  • Loading branch information
artemeff authored Oct 17, 2017
2 parents 26c2e6c + 576692a commit f2c94b7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
2 changes: 2 additions & 0 deletions examples/queries.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

--
-- Get all users from database
-- :get_all_users
SELECT *
Expand Down
6 changes: 2 additions & 4 deletions src/eql_parse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ parse(Input) when is_binary(Input) ->

-spec 'querydef'(input(), index()) -> parse_result().
'querydef'(Input, Index) ->
p(Input, Index, 'querydef', fun(I,D) -> (p_choose([p_seq([p_zero_or_more(fun 'comment'/2), p_label('name', p_choose([fun 'colon_name'/2, fun 'name'/2])), p_zero_or_more(fun 'comment'/2), p_label('sql', fun 'query'/2)]), p_string(<<"">>)]))(I,D) end, fun(Node, _Idx) ->
p(Input, Index, 'querydef', fun(I,D) -> (p_seq([fun 'space'/2, p_zero_or_more(fun 'comment'/2), p_label('name', p_choose([fun 'colon_name'/2, fun 'name'/2])), p_zero_or_more(fun 'comment'/2), p_label('sql', fun 'query'/2)]))(I,D) end, fun(Node, _Idx) ->
begin
Trim = fun(String, Regex) -> re:replace(String, Regex, "", [global, {return,binary}]) end,
LeftTrim = fun(X) -> Trim(X, "(^\\s+)") end,
Expand All @@ -60,9 +60,7 @@ end

-spec 'comment'(input(), index()) -> parse_result().
'comment'(Input, Index) ->
p(Input, Index, 'comment', fun(I,D) -> (p_seq([fun 'comment_marker'/2, fun 'space'/2, p_not(p_string(<<":">>)), p_not(p_string(<<"name:">>)), p_label('comment', p_zero_or_more(p_seq([p_not(fun 'crlf'/2), p_anything()]))), fun 'crlf'/2]))(I,D) end, fun(Node, _Idx) ->
iolist_to_binary(proplists:get_value(comment, Node))
end).
p(Input, Index, 'comment', fun(I,D) -> (p_choose([p_seq([fun 'comment_marker'/2, p_zero_or_more(p_charclass(<<"[\s\s]">>)), fun 'crlf'/2]), p_seq([fun 'comment_marker'/2, fun 'space'/2, p_not(p_string(<<":">>)), p_not(p_string(<<"name:">>)), p_zero_or_more(p_seq([p_not(fun 'crlf'/2), p_anything()])), fun 'crlf'/2])]))(I,D) end, fun(Node, _Idx) ->Node end).

-spec 'name'(input(), index()) -> parse_result().
'name'(Input, Index) ->
Expand Down
7 changes: 2 additions & 5 deletions src/eql_parse.peg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ queries <- (querydef crlf?)* / ''
{ok, lists:flatten(Node)}
`;

querydef <- comment* name:(colon_name / name) comment* sql:query / ''
querydef <- space comment* name:(colon_name / name) comment* sql:query
`
begin
Trim = fun(String, Regex) -> re:replace(String, Regex, "", [global, {return,binary}]) end,
Expand All @@ -27,10 +27,7 @@ end
end
`;

comment <- comment_marker space !":" !"name:" comment:(!crlf .)* crlf
`
iolist_to_binary(proplists:get_value(comment, Node))
`;
comment <- comment_marker [ \s]* crlf / comment_marker space !":" !"name:" (!crlf .)* crlf ~;

name <- comment_marker space "name:" space name:(!crlf .)+ crlf
`binary_to_atom(iolist_to_binary(proplists:get_value(name, Node)), utf8)`;
Expand Down

0 comments on commit f2c94b7

Please sign in to comment.