Class: Token::Resolver::Document
- Inherits:
-
Object
- Object
- Token::Resolver::Document
- Defined in:
- lib/token/resolver/document.rb
Overview
Parses input text and provides access to the resulting text and token nodes.
Document is the primary public API for parsing. It uses Grammar to parse the input and Transform to convert the parse tree into node objects.
Instance Attribute Summary collapse
-
#config ⇒ Config
readonly
The config used for parsing.
-
#nodes ⇒ Array<Node::Text, Node::Token>
readonly
Parsed nodes.
Instance Method Summary collapse
-
#initialize(input, config: Config.default) ⇒ Document
constructor
Parse input text into a Document.
-
#text_only? ⇒ Boolean
Whether the input contains no tokens.
-
#to_s ⇒ String
Reconstruct the original input from nodes (roundtrip fidelity).
-
#token_keys ⇒ Array<String>
Return the unique token keys found in the input.
-
#tokens ⇒ Array<Node::Token>
Return only the Token nodes.
Constructor Details
Instance Attribute Details
#config ⇒ Config (readonly)
Returns The config used for parsing.
27 28 29 |
# File 'lib/token/resolver/document.rb', line 27 def config @config end |
#nodes ⇒ Array<Node::Text, Node::Token> (readonly)
Returns Parsed nodes.
24 25 26 |
# File 'lib/token/resolver/document.rb', line 24 def nodes @nodes end |
Instance Method Details
#text_only? ⇒ Boolean
Whether the input contains no tokens.
63 64 65 |
# File 'lib/token/resolver/document.rb', line 63 def text_only? tokens.empty? end |
#to_s ⇒ String
Reconstruct the original input from nodes (roundtrip fidelity).
56 57 58 |
# File 'lib/token/resolver/document.rb', line 56 def to_s @nodes.map(&:to_s).join end |
#token_keys ⇒ Array<String>
Return the unique token keys found in the input.
49 50 51 |
# File 'lib/token/resolver/document.rb', line 49 def token_keys @token_keys ||= tokens.map(&:key).uniq end |
#tokens ⇒ Array<Node::Token>
Return only the Token nodes.
42 43 44 |
# File 'lib/token/resolver/document.rb', line 42 def tokens @tokens ||= @nodes.select(&:token?) end |