Skip to content

Commit

Permalink
Handle erlang comments during parsing of yaws scripts in yaws_compile…
Browse files Browse the repository at this point in the history
… module
  • Loading branch information
capflam committed Jun 8, 2017
1 parent de83b7e commit 0cd06f4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/yaws_compile.erl
Expand Up @@ -319,6 +319,16 @@ compile_file(<<$\n,Bin/binary>>, Comp, erl, Spec) ->
numchars = Comp#comp.numchars+1,
erlcode = {SLine, ELine+1, ModName, [$\n|Code]}},
compile_file(Bin, NewComp, erl, Spec);
compile_file(<<$%, $%, Bin/binary>>, Comp, erl, Spec) ->
{SLine, ELine, ModName, Code} = Comp#comp.erlcode,
NewComp = Comp#comp{numchars = Comp#comp.numchars+2,
erlcode = {SLine, ELine, ModName, [$%,$%|Code]}},
compile_file(Bin, NewComp, erl_comment, Spec);
compile_file(<<$%, Bin/binary>>, Comp, erl, Spec) ->
{SLine, ELine, ModName, Code} = Comp#comp.erlcode,
NewComp = Comp#comp{numchars = Comp#comp.numchars+1,
erlcode = {SLine, ELine, ModName, [$%|Code]}},
compile_file(Bin, NewComp, erl_comment, Spec);
compile_file(<<C,Bin/binary>>, Comp, erl, Spec) ->
{SLine, ELine, ModName, Code} = Comp#comp.erlcode,
NewComp = Comp#comp{numchars = Comp#comp.numchars+1,
Expand Down Expand Up @@ -371,6 +381,18 @@ compile_file(<<C,Bin/binary>>, Comp, erl_atom, Spec) ->
erlcode = {SLine, ELine, ModName, [C|Code]}},
compile_file(Bin, NewComp, erl_atom, Spec);

%% erl_comment state
compile_file(<<$\n,Bin/binary>>, Comp, erl_comment, Spec) ->
{SLine, ELine, ModName, Code} = Comp#comp.erlcode,
NewComp = Comp#comp{numchars = Comp#comp.numchars+1,
erlcode = {SLine, ELine, ModName, [$\n|Code]}},
compile_file(Bin, NewComp, erl, Spec);
compile_file(<<C,Bin/binary>>, Comp, erl_comment, Spec) ->
{SLine, ELine, ModName, Code} = Comp#comp.erlcode,
NewComp = Comp#comp{numchars = Comp#comp.numchars+1,
erlcode = {SLine, ELine, ModName, [C|Code]}},
compile_file(Bin, NewComp, erl_comment, Spec);

%% erl_close_tag state
compile_file(<<$\n,Bin/binary>>, Comp, erl_close_tag, Spec) ->
NewComp = Comp#comp{line = Comp#comp.line+1,
Expand Down
15 changes: 13 additions & 2 deletions testsuite/yaws_compile_SUITE.erl
Expand Up @@ -22,7 +22,8 @@ groups() ->
compile_bad_yaws_dir,
compile_not_out_fun,
compile_compilation_error,
compile_bad_module_name]},
compile_bad_module_name,
compile_erl_comments]},
{request_tests, [], [request_www_scripts,
request_erl_tag,
request_verbatim_tag,
Expand All @@ -32,7 +33,8 @@ groups() ->
request_bad_yaws_dir,
request_not_out_fun,
request_compilation_error,
request_bad_module_name]}
request_bad_module_name,
request_erl_comments]}
].

%%====================================================================
Expand Down Expand Up @@ -129,6 +131,10 @@ compile_bad_module_name(Config) ->
?assertMatch({ok, 1, [{error, _, _}|_]}, compile_script(Config, "bad_module3.yaws")),
ok.

compile_erl_comments(Config) ->
?assertMatch({ok, 0, _}, compile_script(Config, "comments.yaws")),
ok.

request_www_scripts(Config) ->
[begin
Path = "/"++filename:basename(S),
Expand Down Expand Up @@ -230,6 +236,11 @@ request_bad_module_name(Config) ->
[caseless, {capture, none}])),
ok.

request_erl_comments(Config) ->
Res = <<"Hello World! % this is not a comment %\n">>,
?assertMatch({ok, {{_,200,_}, _, Res}}, request_script(Config, "/comments.yaws")),
ok.


%%====================================================================
compile_script(_Config, File) ->
Expand Down
8 changes: 8 additions & 0 deletions testsuite/yaws_compile_SUITE_data/comments.yaws
@@ -0,0 +1,8 @@
<erl>
out(_A) ->
%% Simple comment 1
% Simple comment 2
%% Comment with special characters or string: ', ", <, >, <erl>, </erl>
% Comment with special characters or string: ', ", <, >, <erl>, </erl>
{html, "Hello World! % this is not a comment %"}.
</erl>

0 comments on commit 0cd06f4

Please sign in to comment.