-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module RBS | ||
module Inline | ||
class Parser < Prism::Visitor | ||
attr_reader :decls, :surrounding_decls, :comments | ||
|
||
def initialize() | ||
@decls = [] | ||
@surrounding_decls = [] | ||
@comments = {} | ||
end | ||
|
||
def self.parse(result) | ||
instance = Parser.new() | ||
|
||
annots = AnnotationParser.parse(result.comments) | ||
annots.each do |result| | ||
instance.comments[result.line_range.end] = result | ||
end | ||
|
||
instance.visit(result.value) | ||
|
||
[ | ||
[], | ||
instance.decls | ||
] | ||
end | ||
|
||
def visit_class_node(node) | ||
visit node.constant_path | ||
visit node.superclass | ||
|
||
if node.location | ||
associated_comment = comments[node.location.start_line - 1] | ||
end | ||
|
||
class_decl = AST::Declarations::ClassDecl.new(node, associated_comment) | ||
|
||
push_class_module_decl(class_decl) do | ||
visit node.body | ||
end | ||
end | ||
|
||
def current_class_module_decl | ||
surrounding_decls.last | ||
end | ||
|
||
def push_class_module_decl(decl) | ||
if current = current_class_module_decl | ||
current.members << decl | ||
else | ||
decls << decl | ||
end | ||
|
||
surrounding_decls.push(decl) | ||
begin | ||
yield | ||
ensure | ||
surrounding_decls.pop() | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use Prism::* | ||
|
||
module RBS | ||
module Inline | ||
class Parser < Prism::Visitor | ||
# A mappning from the last line to the result | ||
# | ||
attr_reader comments: Hash[Integer, AnnotationParser::ParsingResult] | ||
|
||
attr_reader decls: Array[AST::Declarations::t] | ||
|
||
attr_reader surrounding_decls: Array[AST::Declarations::ClassDecl] | ||
|
||
def initialize: () -> void | ||
|
||
def self.parse: (ParseResult[ProgramNode]) -> [Array[AST::Annotations::Use], Array[AST::Declarations::t]] | ||
|
||
def visit_class_node: (ClassNode node) -> void | ||
|
||
def current_class_module_decl: () -> AST::Declarations::ClassDecl? | ||
|
||
def push_class_module_decl: (AST::Declarations::ClassDecl) { () -> void } -> void | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class RBS::Inline::ParserTest < Minitest::Test | ||
include RBS::Inline | ||
|
||
def parse_ruby(src) | ||
Prism.parse(src, filepath: "a.rb") | ||
end | ||
|
||
def test_class_decl | ||
Parser.parse(parse_ruby(<<~RUBY)) | ||
class Foo | ||
# Hello world | ||
class Bar < Object | ||
end | ||
end | ||
RUBY | ||
end | ||
end |