lib/rbs/parser.y in rbs-0.19.0 vs lib/rbs/parser.y in rbs-0.20.0

- old
+ new

@@ -18,11 +18,11 @@ left kAMP left kBAR nonassoc kARROW preclow - expect 2 + expect 5 rule target: type_TYPE type eof { @@ -200,61 +200,71 @@ result = Members::Private.new(location: val[0].location) } | alias_member | signature + attribute_kind: + { result = :instance } + | kSELF kDOT { result = :singleton } + attribute_member: - annotations kATTRREADER keyword type { - location = val[1].location + val[3].location - result = Members::AttrReader.new(name: val[2].value, + annotations kATTRREADER attribute_kind keyword type { + location = val[1].location + val[4].location + result = Members::AttrReader.new(name: val[3].value, ivar_name: nil, - type: val[3], + type: val[4], + kind: val[2], annotations: val[0], location: location, comment: leading_comment(val[0].first&.location || location)) } - | annotations kATTRREADER method_name attr_var_opt kCOLON type { - location = val[1].location + val[5].location - result = Members::AttrReader.new(name: val[2].value.to_sym, - ivar_name: val[3], - type: val[5], + | annotations kATTRREADER attribute_kind method_name attr_var_opt kCOLON type { + location = val[1].location + val[6].location + result = Members::AttrReader.new(name: val[3].value.to_sym, + ivar_name: val[4], + type: val[6], + kind: val[2], annotations: val[0], location: location, comment: leading_comment(val[0].first&.location || location)) } - | annotations kATTRWRITER keyword type { - location = val[1].location + val[3].location - result = Members::AttrWriter.new(name: val[2].value, + | annotations kATTRWRITER attribute_kind keyword type { + location = val[1].location + val[4].location + result = Members::AttrWriter.new(name: val[3].value, ivar_name: nil, - type: val[3], + kind: val[2], + type: val[4], annotations: val[0], location: location, comment: leading_comment(val[0].first&.location || location)) } - | annotations kATTRWRITER method_name attr_var_opt kCOLON type { - location = val[1].location + val[5].location - result = Members::AttrWriter.new(name: val[2].value.to_sym, - ivar_name: val[3], - type: val[5], + | annotations kATTRWRITER attribute_kind method_name attr_var_opt kCOLON type { + location = val[1].location + val[6].location + result = Members::AttrWriter.new(name: val[3].value.to_sym, + ivar_name: val[4], + kind: val[2], + type: val[6], annotations: val[0], location: location, comment: leading_comment(val[0].first&.location || location)) } - | annotations kATTRACCESSOR keyword type { - location = val[1].location + val[3].location - result = Members::AttrAccessor.new(name: val[2].value, + | annotations kATTRACCESSOR attribute_kind keyword type { + location = val[1].location + val[4].location + result = Members::AttrAccessor.new(name: val[3].value, ivar_name: nil, - type: val[3], + kind: val[2], + type: val[4], annotations: val[0], location: location, comment: leading_comment(val[0].first&.location || location)) } - | annotations kATTRACCESSOR method_name attr_var_opt kCOLON type { - location = val[1].location + val[5].location - result = Members::AttrAccessor.new(name: val[2].value.to_sym, - ivar_name: val[3], - type: val[5], + | annotations kATTRACCESSOR attribute_kind method_name attr_var_opt kCOLON type { + location = val[1].location + val[6].location + result = Members::AttrAccessor.new(name: val[3].value.to_sym, + ivar_name: val[4], + kind: val[2], + type: val[6], annotations: val[0], location: location, comment: leading_comment(val[0].first&.location || location)) } @@ -472,31 +482,18 @@ | method_type kBAR method_types { result = val[2].unshift(val[0]) } method_type: - start_merged_scope type_params params_opt block_opt kARROW simple_type { + start_merged_scope type_params proc_type { reset_variable_scope - location = (val[1] || val[2] || val[3] || val[4]).location + val[5].location + location = (val[1] || val[2]).location + val[2].location type_params = val[1]&.value || [] - params = val[2]&.value || empty_params_result + type, block = val[2].value - type = Types::Function.new( - required_positionals: params[0], - optional_positionals: params[1], - rest_positionals: params[2], - trailing_positionals: params[3], - required_keywords: params[4], - optional_keywords: params[5], - rest_keywords: params[6], - return_type: val[5] - ) - - block = val[3]&.value - result = MethodType.new(type_params: type_params, type: type, block: block, location: location) } @@ -505,18 +502,17 @@ { result = nil } | kLPAREN params kRPAREN { result = LocatedValue.new(value: val[1], location: val[0].location + val[2].location) } - block_opt: - { result = nil } - | kLBRACE function_type kRBRACE { - block = MethodType::Block.new(type: val[1].value, required: true) + block: + kLBRACE simple_function_type kRBRACE { + block = Types::Block.new(type: val[1].value, required: true) result = LocatedValue.new(value: block, location: val[0].location + val[2].location) } - | kQUESTION kLBRACE function_type kRBRACE { - block = MethodType::Block.new(type: val[2].value, required: false) + | kQUESTION kLBRACE simple_function_type kRBRACE { + block = Types::Block.new(type: val[2].value, required: false) result = LocatedValue.new(value: block, location: val[0].location + val[3].location) } def_name: keyword @@ -795,12 +791,13 @@ } | kSINGLETON kLPAREN class_name kRPAREN { result = Types::ClassSingleton.new(name: val[2].value, location: val[0].location + val[3].location) } - | kHAT function_type { - result = Types::Proc.new(type: val[1].value, location: val[0].location + val[1].location) + | kHAT proc_type { + type, block = val[1].value + result = Types::Proc.new(type: type, block: block, location: val[0].location + val[1].location) } | simple_type kQUESTION { result = Types::Optional.new(type: val[0], location: val[0].location + val[1].location) } | record_type @@ -849,11 +846,36 @@ result = val[0] } keyword: tLKEYWORD | tUKEYWORD | tLKEYWORD_Q_E | tUKEYWORD_Q_E - function_type: + proc_type: + params_opt block kARROW simple_type { + location = (val[0] || val[1] || val[2]).location + val[3].location + + params = val[0]&.value || [[], [], nil, [], {}, {}, nil] + + type = Types::Function.new( + required_positionals: params[0], + optional_positionals: params[1], + rest_positionals: params[2], + trailing_positionals: params[3], + required_keywords: params[4], + optional_keywords: params[5], + rest_keywords: params[6], + return_type: val[3] + ) + + block = val[1].value + + result = LocatedValue.new(value: [type, block], location: location) + } + | simple_function_type { + result = LocatedValue.new(value: [val[0].value, nil], location: val[0].location) + } + + simple_function_type: kLPAREN params kRPAREN kARROW simple_type { location = val[0].location + val[4].location type = Types::Function.new( required_positionals: val[1][0], optional_positionals: val[1][1], @@ -881,11 +903,11 @@ ) result = LocatedValue.new(value: type, location: location) } - params: + params: required_positional kCOMMA params { result = val[2] result[0].unshift(val[0]) } | required_positional { @@ -1292,10 +1314,14 @@ ANNOTATION_RE = Regexp.union(/%a\{.*?\}/, /%a\[.*?\]/, /%a\(.*?\)/, /%a\<.*?\>/, /%a\|.*?\|/) + +escape_sequences = %w[a b e f n r s t v "].map { |l| "\\\\#{l}" } +DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE = /(#{escape_sequences.join("|")})/ + def next_token if @type type = @type @type = nil return [:"type_#{type}", nil] @@ -1371,10 +1397,24 @@ when input.scan(/[A-Z]\w*\b/) new_token(:tUIDENT) when input.scan(/[a-z_]\w*\b/) new_token(:tLIDENT) when input.scan(/"(\\"|[^"])*"/) - s = input.matched.yield_self {|s| s[1, s.length - 2] }.gsub(/\\"/, '"') + s = input.matched.yield_self {|s| s[1, s.length - 2] } + .gsub(DBL_QUOTE_STR_ESCAPE_SEQUENCES_RE) do |match| + case match + when '\\a' then "\a" + when '\\b' then "\b" + when '\\e' then "\e" + when '\\f' then "\f" + when '\\n' then "\n" + when '\\r' then "\r" + when '\\s' then "\s" + when '\\t' then "\t" + when '\\v' then "\v" + when '\\"' then '"' + end + end new_token(:tSTRING, s) when input.scan(/'(\\'|[^'])*'/) s = input.matched.yield_self {|s| s[1, s.length - 2] }.gsub(/\\'/, "'") new_token(:tSTRING, s) else