diff --git a/prism/prism.c b/prism/prism.c index bf32cedc2c..864dd49f51 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -9829,7 +9829,7 @@ parser_lex(pm_parser_t *parser) { ) { // Since we know we're about to add an __END__ comment, we know we - // need to add all of the newlines to get the correct column + // need at add all of the newlines to get the correct column // information for it. const uint8_t *cursor = parser->current.end; while ((cursor = next_newline(cursor, parser->end - cursor)) != NULL) { diff --git a/prism/templates/template.rb b/prism/templates/template.rb index 12749add22..2831b09187 100755 --- a/prism/templates/template.rb +++ b/prism/templates/template.rb @@ -10,6 +10,39 @@ module Prism JAVA_BACKEND = ENV["PRISM_JAVA_BACKEND"] || "truffleruby" JAVA_STRING_TYPE = JAVA_BACKEND == "jruby" ? "org.jruby.RubySymbol" : "String" + # This module contains methods for escaping characters in JavaDoc comments. + module JavaDoc + ESCAPES = { + "'" => "'", + "\"" => """, + "@" => "@", + "&" => "&", + "<" => "<", + ">" => ">" + }.freeze + + def self.escape(value) + value.gsub(/['&"<>@]/, ESCAPES) + end + end + + # A comment attached to a field or node. + class Comment + attr_reader :value + + def initialize(value) + @value = value + end + + def each_line(&block) + value.each_line { |line| yield line.prepend(" ").rstrip } + end + + def each_java_line(&block) + Comment.new(JavaDoc.escape(value)).each_line(&block) + end + end + # This represents a field on a node. It contains all of the necessary # information to template out the code for that field. class Field @@ -21,8 +54,12 @@ module Prism @options = options end - def each_comment_line - comment.each_line { |line| yield line.prepend(" ").rstrip } if comment + def each_comment_line(&block) + Comment.new(comment).each_line(&block) if comment + end + + def each_comment_java_line(&block) + Comment.new(comment).each_java_line(&block) if comment end def semantic_field? @@ -317,8 +354,12 @@ module Prism @comment = config.fetch("comment") end - def each_comment_line - comment.each_line { |line| yield line.prepend(" ").rstrip } + def each_comment_line(&block) + Comment.new(comment).each_line(&block) + end + + def each_comment_java_line(&block) + Comment.new(comment).each_java_line(&block) end def semantic_fields