Просмотр исходного кода

Add inline comments to erlydtl parse tree.

Сергей Прохоров 11 лет назад
Родитель
Сommit
2bc8343f68
3 измененных файлов с 25 добавлено и 4 удалено
  1. 11 0
      src/erlydtl_parser.yrl
  2. 12 3
      src/erlydtl_scanner.erl
  3. 2 1
      src/erlydtl_scanner.slex

+ 11 - 0
src/erlydtl_parser.yrl

@@ -52,6 +52,7 @@ Nonterminals
     BlockBraced
     EndBlockBraced
 
+    CommentInline
     CommentBlock
     CommentBraced
     EndCommentBraced
@@ -142,6 +143,7 @@ Terminals
     call_keyword
     close_tag
     close_var
+    comment_inline
     comment_keyword
     cycle_keyword
     elif_keyword
@@ -224,6 +226,7 @@ Elements -> Elements BlockTransBlock : '$1' ++ ['$2'].
 Elements -> Elements CallTag : '$1' ++ ['$2'].
 Elements -> Elements CallWithTag : '$1' ++ ['$2'].
 Elements -> Elements CommentBlock : '$1' ++ ['$2'].
+Elements -> Elements CommentInline : '$1' ++ ['$2'].
 Elements -> Elements CustomTag : '$1' ++ ['$2'].
 Elements -> Elements CycleTag : '$1' ++ ['$2'].
 Elements -> Elements ExtendsTag : '$1' ++ ['$2'].
@@ -296,6 +299,8 @@ CommentBlock -> CommentBraced Elements EndCommentBraced : {comment, '$2'}.
 CommentBraced -> open_tag comment_keyword close_tag.
 EndCommentBraced -> open_tag endcomment_keyword close_tag.
 
+CommentInline -> comment_inline : {comment, inline_comment_to_string('$1')}.
+
 CycleTag -> open_tag cycle_keyword CycleNamesCompat close_tag : {cycle_compat, '$3'}.
 CycleTag -> open_tag cycle_keyword CycleNames close_tag : {cycle, '$3'}.
 
@@ -421,4 +426,10 @@ Args -> Args identifier '=' Value : '$1' ++ [{'$2', '$4'}].
 CallTag -> open_tag call_keyword identifier close_tag : {call, '$3'}.
 CallWithTag -> open_tag call_keyword identifier with_keyword Value close_tag : {call, '$3', '$5'}.
 
+Erlang code.
+
+inline_comment_to_string({comment_inline, Pos, S}) ->
+    %% inline comment converted to block comment for simplicity
+    [{string, Pos, S}].
+
 %% vim: syntax=erlang

+ 12 - 3
src/erlydtl_scanner.erl

@@ -36,7 +36,7 @@
 %%%-------------------------------------------------------------------
 -module(erlydtl_scanner).
 
-%% This file was generated 2014-02-27 13:29:50 UTC by slex 0.2.1.
+%% This file was generated 2014-03-21 23:07:32 UTC by slex 0.2.1.
 %% http://github.com/erlydtl/slex
 -slex_source(["src/erlydtl_scanner.slex"]).
 
@@ -178,8 +178,15 @@ scan("#}-->" ++ T, S, {R, C}, {_, "#}-->"}) ->
     scan(T, S, {R, C + 5}, in_text);
 scan("#}" ++ T, S, {R, C}, {_, "#}"}) ->
     scan(T, S, {R, C + 2}, in_text);
-scan([H | T], S, {R, C}, {in_comment, E} = St) ->
-    scan(T, S,
+scan([H | T], S, {R, C} = P, {in_comment, E} = St) ->
+    scan(T,
+	 case S of
+	   [{comment_inline, _, L} = M | Ss] ->
+	       [setelement(3, M, [H | L]) | Ss];
+	   _ ->
+	       [{comment_inline, P, [H]} | post_process(S,
+							comment_inline)]
+	 end,
 	 case H of
 	   $\n -> {R + 1, 1};
 	   _ -> {R, C + 1}
@@ -533,6 +540,8 @@ post_process(_, {string, _, L} = T, _) ->
     setelement(3, T, begin L1 = lists:reverse(L), L1 end);
 post_process(_, {string_literal, _, L} = T, _) ->
     setelement(3, T, begin L1 = lists:reverse(L), L1 end);
+post_process(_, {comment_inline, _, L} = T, _) ->
+    setelement(3, T, begin L1 = lists:reverse(L), L1 end);
 post_process(_, {number_literal, _, L} = T, _) ->
     setelement(3, T,
 	       begin

+ 2 - 1
src/erlydtl_scanner.slex

@@ -72,7 +72,7 @@ end.
 30 #} any+: skip, in_text-.
 
 %% must come before the `space any' rule
-40 any in_comment: skip.
+40 any in_comment: +comment_inline.
 %% end comment rules
 
 %% The rest is "just" text..
@@ -257,6 +257,7 @@ end.
 
 string: lists reverse.
 string_literal: lists reverse.
+comment_inline: lists reverse.
 number_literal: lists reverse, list_to_integer.
 open_var: to_atom.
 close_var: to_atom.