Repository navigation
Add Psych.parse, Psych.parse_file, Psych.parse_stream and Psych::Nodes - #3304
Open
hasannadeem wants to merge 1 commit into
Open
hasannadeem wants to merge 1 commit into
hasannadeem wants to merge 1 commit into
Conversation
The parse methods return the YAML AST in Psych::Nodes, which had no signatures. nodes.rbs adds Node and its Alias, Document, Mapping, Scalar, Sequence and Stream subclasses, with their attributes, style and encoding constants, to_ruby and yaml.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Psych.parse,Psych.parse_fileandPsych.parse_streamhad no signatures, because they return the YAML AST inPsych::Nodes, which had none either. (I left them out of #3288 for that reason.)Parse methods (
psych.rbs):Psych.parse(String yaml, ?filename: ...) -> (Nodes::Document | false):falsefor an empty documentPsych.parse_file(path filename) -> (Nodes::Document | false)and[T] (path filename, fallback: T) -> (Nodes::Document | T)Psych.parse_stream(String yaml, ?filename: ...) -> Nodes::Stream, and with a block that receives eachNodes::DocumentThe
filename:type is the same as on the existingPsych.load/load_stream.AST (new
nodes.rbs):Psych::Nodes::NodeincludesEnumerable[Node](eachwalks the tree depth first) and has:children,tag;start_line/start_column/end_line/end_column:Integer?, because they arenilon a node built by hand rather than parsed;to_ruby/transform;yaml/to_yaml: a String, or the given IO, likePsych.dump;alias?…stream?predicates.Alias,Document,Mapping,Scalar,SequenceandStreamadd theirinitializeand attributes, the style constants (Scalar::LITERAL,Mapping::FLOW, …) and the encoding constants (Stream::UTF8, …). Each overrides its own predicate to returntrue.to_rubytakes the samesymbolize_names:,freeze:andstrict_integer:keywords asPsych.loadhere.parse_symbols:exists only in psych 5, and theloadsignatures don't have it either. The API is otherwise the same in psych 4.0.6 and 5.3.1.module Psychinnodes.rbshas%a{annotate:rdoc:skip}, as indbm.rbs, so the module's overview isn't copied there. The parse methods use%a{annotate:rdoc:copy:...}, likeload_stream.The tests parse a document with a
%YAMLand a%TAGdirective, a tag, an anchor and an alias, so every node class and attribute is exercised on parsed nodes. Thenillocations androotare checked on nodes built by hand, and every constant is checked too. They pass with psych 4.0.6 (the bundle), 5.2.6 (Ruby 3.4) and 5.3.1 (Ruby 4.0.7). Without the signatures the test file fails to load, and the parse tests fail.The AST-building example from the
Psych::Nodesdocs type-checks with Steep:So do
tree.select(&:mapping?).map(&:start_line)andPsych.parse_file(path, fallback: nil)&.root, whilescalar.value = 1is still an error. Annotatingcoreandstdlibfrom the full Ruby v4.0.7 source leaves both files unchanged, andrake validateandrake typecheck_testpass.