Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#: instead of #:: #64

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ gem "rake", "~> 13.2"

gem "minitest", "~> 5.24"

gem "steep", "~> 1.7.0", require: false
gem "steep", "~> 1.8.0.dev", require: false
gem "strscan"
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ GEM
rbs (3.5.1)
logger
securerandom (0.3.1)
steep (1.7.1)
steep (1.8.0.dev.1)
activesupport (>= 5.1)
concurrent-ruby (>= 1.1.10)
csv (>= 3.0.9)
Expand Down Expand Up @@ -82,7 +82,7 @@ DEPENDENCIES
minitest (~> 5.24)
rake (~> 13.2)
rbs-inline!
steep (~> 1.7.0)
steep (~> 1.8.0.dev)
strscan

BUNDLED WITH
Expand Down
12 changes: 12 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ Rake::TestTask.new(:test) do |t|
end

task default: :test

namespace :rbs do
task :generate do
sh "rbs-inline --opt-out --output lib"
end

task :watch do
sh "fswatch -0 lib | xargs -n1 -0 rbs-inline --opt-out --output lib"
rescue Interrupt
# nop
end
end
65 changes: 33 additions & 32 deletions lib/rbs/inline/annotation_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class AnnotationParser
# ```
#
class ParsingResult
attr_reader :comments #:: Array[Prism::Comment]
attr_reader :annotations #:: Array[AST::Annotations::t | AST::CommentLines]
attr_reader :first_comment_offset #:: Integer
attr_reader :comments #: Array[Prism::Comment]
attr_reader :annotations #: Array[AST::Annotations::t | AST::CommentLines]
attr_reader :first_comment_offset #: Integer

#:: () { (AST::Annotations::t) -> void } -> void
#:: () -> Enumerator[AST::Annotations::t, void]
#: () { (AST::Annotations::t) -> void } -> void
#: () -> Enumerator[AST::Annotations::t, void]
def each_annotation(&block)
if block
annotations.each do |annot|
Expand All @@ -39,7 +39,7 @@ def each_annotation(&block)
end

# @rbs first_comment: Prism::Comment
def initialize(first_comment)
def initialize(first_comment) #: void
@comments = [first_comment]
@annotations = []
content = first_comment.location.slice
Expand Down Expand Up @@ -77,7 +77,7 @@ def add_comment(comment)
end

# @rbs trim: bool -- `true` to trim the leading whitespaces
def content(trim: false) #:: String
def content(trim: false) #: String
if trim
leading_spaces = lines[0][/\A\s*/]
offset = leading_spaces ? leading_spaces.length : 0
Expand All @@ -95,15 +95,15 @@ def content(trim: false) #:: String
end
end

def lines #:: Array[String]
def lines #: Array[String]
comments.map { _1.location.slice[1...] || "" }
end
end

attr_reader :input #:: Array[Prism::Comment]
attr_reader :input #: Array[Prism::Comment]

# @rbs input: Array[Prism::Comment]
def initialize(input)
def initialize(input) #: void
@input = input
end

Expand Down Expand Up @@ -150,15 +150,15 @@ def parse
# Test if the comment is an annotation comment
#
# - Returns `nil` if the comment is not an annotation.
# - Returns `true` if the comment is `#::` or `#[` annotation. (Offset is `1`)
# - Returns `true` if the comment is `#:` or `#[` annotation. (Offset is `1`)
# - Returns Integer if the comment is `#@rbs` annotation. (Offset is the number of leading spaces including `#`)
#
#:: (Prism::Comment) -> (Integer | true | nil)
#: (Prism::Comment) -> (Integer | true | nil)
def annotation_comment?(comment)
line = comment.location.slice

# No leading whitespace is allowed
return true if line.start_with?("#::")
return true if line.start_with?("#:")
return true if line.start_with?("#[")

if match = line.match(/\A#(\s*)@rbs(\b|!)/)
Expand All @@ -176,7 +176,7 @@ def annotation_comment?(comment)
#
# Yields an array of comments, and a boolean indicating if the comments may be an annotation.
#
#:: (ParsingResult) { (Array[Prism::Comment], bool is_annotation) -> void } -> void
#: (ParsingResult) { (Array[Prism::Comment], bool is_annotation) -> void } -> void
def each_annotation_paragraph(result, &block)
yield_paragraph([], result.comments.dup, &block)
end
Expand Down Expand Up @@ -306,7 +306,8 @@ class Tokenizer
"unchecked" => :kUNCHECKED,
"self" => :kSELF,
"skip" => :kSKIP,
} #:: Hash[String, Symbol]
"yields" => :kYIELDS,
} #: Hash[String, Symbol]
KW_RE = /#{Regexp.union(KEYWORDS.keys)}\b/

PUNCTS = {
Expand All @@ -325,16 +326,16 @@ class Tokenizer
"(" => :kLPAREN,
"&" => :kAMP,
"?" => :kQUESTION,
} #:: Hash[String, Symbol]
PUNCTS_RE = Regexp.union(PUNCTS.keys) #:: Regexp
} #: Hash[String, Symbol]
PUNCTS_RE = Regexp.union(PUNCTS.keys) #: Regexp

attr_reader :scanner #:: StringScanner
attr_reader :scanner #: StringScanner

# Token that comes after the current position
attr_reader :lookahead1 #:: token?
attr_reader :lookahead1 #: token?

# Token that comes after `lookahead1`
attr_reader :lookahead2 #:: token?
attr_reader :lookahead2 #: token?

# Returns the current char position of the scanner
#
Expand All @@ -345,14 +346,14 @@ class Tokenizer
# ^ <= scanner.charpos
# ```
#
def current_position #:: Integer
def current_position #: Integer
start = scanner.charpos
start -= lookahead1[1].size if lookahead1
start -= lookahead2[1].size if lookahead2
start
end

def lookaheads #:: Array[Symbol?]
def lookaheads #: Array[Symbol?]
[lookahead1&.[](0), lookahead2&.[](0)]
end

Expand Down Expand Up @@ -416,15 +417,15 @@ def advance(tree, eat: false)
end

# Returns true if the scanner cannot consume next token
def stuck? #:: bool
def stuck? #: bool
lookahead1.nil? && lookahead2.nil?
end

# Skips characters
#
# This method ensures the `current_position` will be the given `position`.
#
# @rbs size: Integer -- The new position
# @rbs position: Integer -- The new position
# @rbs tree: AST::Tree -- Tree to insert trivia tokens
# @rbs return: void
def reset(position, tree)
Expand All @@ -447,7 +448,7 @@ def reset(position, tree)
advance(tree)
end

def rest #:: String
def rest #: String
buf = +""
buf << lookahead1[1] if lookahead1
buf << lookahead2[1] if lookahead2
Expand Down Expand Up @@ -480,7 +481,7 @@ def consume_token!(*types, tree:)

# Test if current token has specified `type`
#
# @rbs types: *Symbol
# @rbs *types: Symbol
# @rbs return: bool
def type?(*types)
types.any? { lookahead1 && lookahead1[0] == _1 }
Expand Down Expand Up @@ -597,7 +598,7 @@ def parse_annotation(comments)
tree << parse_method_type_annotation(tokenizer)
AST::Annotations::Method.new(tree, comments)
end
when tokenizer.type?(:kCOLON2)
when tokenizer.type?(:kCOLON)
tokenizer.advance(tree, eat: true)
tree << parse_type_method_type(tokenizer, tree)
AST::Annotations::Assertion.new(tree, comments)
Expand Down Expand Up @@ -822,7 +823,7 @@ def parse_rbs_annotation(tokenizer)
tree
end

# @rbs tokznier: Tokenizer
# @rbs tokenizer: Tokenizer
# @rbs return: AST::Tree
def parse_inherits(tokenizer)
tree = AST::Tree.new(:rbs_inherits)
Expand Down Expand Up @@ -965,7 +966,7 @@ def parse_module_self(tokenizer)
#
# @rbs tokenizer: Tokenizer
# @rbs *types: Symbol
# @rbs block: ^() -> AST::Tree
# @rbs &block: () -> AST::Tree
# @rbs return: AST::Tree?
def parse_optional(tokenizer, *types, &block)
if tokenizer.type?(*types)
Expand Down Expand Up @@ -1001,7 +1002,7 @@ def parse_generic(tokenizer)
tree
end

#:: (Tokenizer) -> AST::Tree
#: (Tokenizer) -> AST::Tree
def parse_ivar_type(tokenizer)
tree = AST::Tree.new(:ivar_type)

Expand All @@ -1020,7 +1021,7 @@ def parse_ivar_type(tokenizer)
tree
end

#:: (Tokenizer) -> AST::Tree
#: (Tokenizer) -> AST::Tree
def parse_splat_param_type(tokenizer)
tree = AST::Tree.new(:splat_param_type)

Expand All @@ -1037,7 +1038,7 @@ def parse_splat_param_type(tokenizer)
tree
end

#:: (Tokenizer) -> AST::Tree
#: (Tokenizer) -> AST::Tree
def parse_block_type(tokenizer)
tree = AST::Tree.new(:block_type)

Expand Down
Loading