Class: Steep::AnnotationParser
- Inherits:
-
Object
- Object
- Steep::AnnotationParser
- Defined in:
- lib/steep/annotation_parser.rb
Defined Under Namespace
Classes: SyntaxError
Constant Summary collapse
- VAR_NAME =
/[a-z][A-Za-z0-9_]*/
- METHOD_NAME =
Regexp.union( /[A-Za-z][A-Za-z0-9_]*[?!]?/ )
- CONST_NAME =
Regexp.union( /(::)?([A-Z][A-Za-z0-9_]*::)*[A-Z][A-Za-z0-9_]*/ )
- DYNAMIC_NAME =
/(self\??\.)?#{METHOD_NAME}/
- IVAR_NAME =
/@[^:\s]+/
- TYPE =
/(?<type>.*)/
- COLON =
/\s*:\s*/
- PARAM =
/[A-Z][A-Za-z0-9_]*/
- TYPE_PARAMS =
/(\[(?<params>#{PARAM}(,\s*#{PARAM})*)\])?/
Instance Attribute Summary collapse
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
Instance Method Summary collapse
-
#initialize(factory:) ⇒ AnnotationParser
constructor
A new instance of AnnotationParser.
-
#keyword_and_type(keyword) ⇒ Object
Example: @type break: String @type self: Foo.
-
#keyword_subject_type(keyword, name) ⇒ Object
Example: @type const Foo::Bar: String @type var xyzzy: Array.
- #parse(src, location:) ⇒ Object
- #parse_type(string) ⇒ Object
Constructor Details
#initialize(factory:) ⇒ AnnotationParser
Returns a new instance of AnnotationParser.
15 16 17 |
# File 'lib/steep/annotation_parser.rb', line 15 def initialize(factory:) @factory = factory end |
Instance Attribute Details
#factory ⇒ Object (readonly)
Returns the value of attribute factory.
13 14 15 |
# File 'lib/steep/annotation_parser.rb', line 13 def factory @factory end |
Instance Method Details
#keyword_and_type(keyword) ⇒ Object
Example: @type break: String
@type self: Foo
50 51 52 |
# File 'lib/steep/annotation_parser.rb', line 50 def keyword_and_type(keyword) /@type\s+#{keyword}#{COLON}#{TYPE}/ end |
#keyword_subject_type(keyword, name) ⇒ Object
Example: @type const Foo::Bar: String
@type var xyzzy: Array[String]
43 44 45 |
# File 'lib/steep/annotation_parser.rb', line 43 def keyword_subject_type(keyword, name) /@type\s+#{keyword}\s+(?<name>#{name})#{COLON}#{TYPE}/ end |
#parse(src, location:) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/steep/annotation_parser.rb', line 54 def parse(src, location:) case src when keyword_subject_type("var", VAR_NAME) Regexp.last_match.yield_self do |match| name = match[:name] type = match[:type] AST::Annotation::VarType.new(name: name.to_sym, type: parse_type(type), location: location) end when keyword_subject_type("method", METHOD_NAME) Regexp.last_match.yield_self do |match| name = match[:name] type = match[:type] method_type = factory.method_type(Ruby::Signature::Parser.parse_method_type(type), self_type: AST::Types::Self.new) AST::Annotation::MethodType.new(name: name.to_sym, type: method_type, location: location) end when keyword_subject_type("const", CONST_NAME) Regexp.last_match.yield_self do |match| name = match[:name] type = parse_type(match[:type]) AST::Annotation::ConstType.new(name: Names::Module.parse(name), type: type, location: location) end when keyword_subject_type("ivar", IVAR_NAME) Regexp.last_match.yield_self do |match| name = match[:name] type = parse_type(match[:type]) AST::Annotation::IvarType.new(name: name.to_sym, type: type, location: location) end when keyword_and_type("return") Regexp.last_match.yield_self do |match| type = parse_type(match[:type]) AST::Annotation::ReturnType.new(type: type, location: location) end when keyword_and_type("block") Regexp.last_match.yield_self do |match| type = parse_type(match[:type]) AST::Annotation::BlockType.new(type: type, location: location) end when keyword_and_type("self") Regexp.last_match.yield_self do |match| type = parse_type(match[:type]) AST::Annotation::SelfType.new(type: type, location: location) end when keyword_and_type("instance") Regexp.last_match.yield_self do |match| type = parse_type(match[:type]) AST::Annotation::InstanceType.new(type: type, location: location) end when keyword_and_type("module") Regexp.last_match.yield_self do |match| type = parse_type(match[:type]) AST::Annotation::ModuleType.new(type: type, location: location) end when keyword_and_type("break") Regexp.last_match.yield_self do |match| type = parse_type(match[:type]) AST::Annotation::BreakType.new(type: type, location: location) end when /@dynamic\s+(?<names>(#{DYNAMIC_NAME}\s*,\s*)*#{DYNAMIC_NAME})/ Regexp.last_match.yield_self do |match| names = match[:names].split(/\s*,\s*/) AST::Annotation::Dynamic.new( names: names.map {|name| case name when /^self\./ AST::Annotation::Dynamic::Name.new(name: name[5..].to_sym, kind: :module) when /^self\?\./ AST::Annotation::Dynamic::Name.new(name: name[6..].to_sym, kind: :module_instance) else AST::Annotation::Dynamic::Name.new(name: name.to_sym, kind: :instance) end }, location: location ) end when /@implements\s+(?<name>#{CONST_NAME})#{TYPE_PARAMS}$/ Regexp.last_match.yield_self do |match| type_name = Names::Module.parse(match[:name]) params = match[:params]&.yield_self {|params| params.split(/,/).map {|param| param.strip.to_sym } } || [] name = AST::Annotation::Implements::Module.new(name: type_name, args: params) AST::Annotation::Implements.new(name: name, location: location) end end rescue Ruby::Signature::Parser::SyntaxError, Ruby::Signature::Parser::SemanticsError => exn raise SyntaxError.new(source: src, location: location, exn: exn) end |
#parse_type(string) ⇒ Object
36 37 38 |
# File 'lib/steep/annotation_parser.rb', line 36 def parse_type(string) factory.type(Ruby::Signature::Parser.parse_type(string)) end |