Module: Ibex::Frontend::AST::Node
- Included in:
- Alternative, Conversion, Convert, DisplayName, Empty, Expect, ExpectRR, Fragment, GrammarTest, Group, Include, InlineAction, Lexer, LexerRule, LexerState, NodeAnnotation, OnErrorReduce, Optional, Options, Parameter, ParameterizedReference, ParserConfiguration, ParserSetting, Plus, Precedence, PrecedenceLevel, Printer, Recovery, Root, Rule, SemanticType, SeparatedList, Star, Start, SymbolReference, Tokens, UserCode
- Defined in:
- lib/ibex/frontend/ast.rb,
sig/ibex/frontend/ast.rbs
Overview
Adds deterministic, recursively serializable hashes to Struct nodes.
Instance Method Summary collapse
Instance Method Details
#serialize(value) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/ibex/frontend/ast.rb', line 64 def serialize(value) return value if value.nil? case value when Array then value.map { |item| serialize(item) } when Hash then value.to_h { |key, item| [key, serialize(item)] } else value.respond_to?(:to_h) ? value.to_h : value end end |
#to_h ⇒ Hash[Symbol, untyped]
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ibex/frontend/ast.rb', line 49 def to_h fields = each_pair.to_h { |name, value| [name, serialize(value)] } fields.delete(:aliases) if self.class.name.end_with?("::Tokens") && !fields[:aliases] if self.class.name.end_with?("::Root") fields.delete(:extended) unless fields[:extended] fields.delete(:extended_loc) unless fields[:extended_loc] fields.delete(:cst) unless fields[:cst] end fields.delete(:node_annotation) if self.class.name.end_with?("::Alternative") && !fields[:node_annotation] { node: self.class.name.split("::").last }.merge(fields) end |