プリプロセッサだった

そうか、それでいいのか……。コメントの中で終了記号が出てきたらどうか、とかクオートされてたら、とかは考えなくても良かったのか。

世の中のテンプレートエンジンってのはそういうもんらしい。

aescm

<?scm
(print "?>")
;; ?>
(print "?>")
?>
?>

(print "?>")
?>

コメント中の ?> に反応してコード部分が終了している。

別のパターンでやってみる。

<?scm
(print '?>)
;; ?>
(print '?>)
?>
*** READ-ERROR: Read error at "(stdin)":line 10: EOF inside a list (starting from line 4)
Stack Trace:
_______________________________________

文字列中の ?> は無視するけど、クオートについては考慮されないようだ。 -E オプションを使って生成されたコードを出力してみるとこうなる。

(define escm_version "aescm 0.31")
(define escm_input_file "template2.html")
(define escm_interpreter #f)
(print '
(display ")")(newline)
(display ";; ?>")(newline)
(display "(print '?>)")(newline)
(display "?>")(newline)

php の場合

<?php
print "?>\n";
# ?>
print "?>\n";
?>
?>
print "?>\n";
?>

コメント中の ?> に引っ掛かってコード部分が終了する。

erb の場合

<%
puts "%>"
# %>
puts "%>"
%>
hoge.erb:2: compile error (SyntaxError)
hoge.erb:2: syntax error, unexpected $undefined, expecting $end
puts "; _erbout.concat "\"\n"
                         ^

文字列中の %> でコード部分が終わる。これは嬉しくないよな。

_erbout = ''; 
puts "; _erbout.concat "\"\n"
_erbout.concat "# %>\n"
_erbout.concat "puts \"%>\"\n"
>
_erbout

コメント中の終了記号についても試してみる。

<%
puts "hoge"
# %>
puts "hoge"
%>
hoge.erb:5: compile error (SyntaxError)
hoge.erb:5: syntax error, unexpected '>', expecting $end

何が起きてんのか分からないので生成されたコードを見てみる。

_erbout = ''; 
puts "hoge"
# ; _erbout.concat "\n"
_erbout.concat "puts \"hoge\"\n"
>
_erbout

コメント中の %> でコード部が終了するのは良いけど、その次の %> がうまく処理できずにエラーになってしまうのはバグっぽい気もする。