From fc656acee9d17f72d9f7b09630d7f03e981beef3 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Fri, 23 Feb 2024 21:56:56 -0500 Subject: [PATCH] [PRISM] Sync to latest prism --- prism/prism.c | 2 +- prism/templates/template.rb | 49 ++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/prism/prism.c b/prism/prism.c index bf32cedc2c1753..864dd49f511b3d 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 12749add22d24e..2831b091878a71 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 @@ def initialize(name:, comment: nil, **options) @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 @@ def initialize(config) @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