Class: Rley::Formatter::Json
- Inherits:
-
BaseFormatter
- Object
- BaseFormatter
- Rley::Formatter::Json
- Defined in:
- lib/rley/formatter/json.rb
Overview
A formatter class that renders a parse tree in JSON format
Instance Attribute Summary collapse
-
#indentation ⇒ Object
readonly
Current indentation level.
-
#sibling_flags ⇒ Object
readonly
Array of booleans (one per indentation level).
Attributes inherited from BaseFormatter
Instance Method Summary collapse
-
#after_ptree(_ptree) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed.
-
#after_subnodes(_parent, _subnodes) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed.
-
#before_non_terminal(nonterm_node) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed.
-
#before_ptree(_ptree) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed.
-
#before_subnodes(_parent, _subnodes) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed.
-
#before_terminal(term_node) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed.
-
#initialize(anIO) ⇒ Json
constructor
Constructor.
Methods inherited from BaseFormatter
Constructor Details
#initialize(anIO) ⇒ Json
Constructor. is written.
21 22 23 24 25 |
# File 'lib/rley/formatter/json.rb', line 21 def initialize(anIO) super(anIO) @indentation = 0 @sibling_flags = [false] end |
Instance Attribute Details
#indentation ⇒ Object (readonly)
Current indentation level
12 13 14 |
# File 'lib/rley/formatter/json.rb', line 12 def indentation @indentation end |
#sibling_flags ⇒ Object (readonly)
Array of booleans (one per indentation level). Set to true after first child was visited.
16 17 18 |
# File 'lib/rley/formatter/json.rb', line 16 def sibling_flags @sibling_flags end |
Instance Method Details
#after_ptree(_ptree) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed. Notification of a visit event: the visitor completed the visit of the given parse tree
89 90 91 92 93 |
# File 'lib/rley/formatter/json.rb', line 89 def after_ptree(_ptree) dedent dedent print_text("\n", '}') end |
#after_subnodes(_parent, _subnodes) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed. Notification of a visit event: the visitor completed the visit of the children of a non-terminal node.
78 79 80 81 82 83 |
# File 'lib/rley/formatter/json.rb', line 78 def after_subnodes(_parent, _subnodes) sibling_flags.pop print_text("\n", ']') dedent print_text("\n", '}') end |
#before_non_terminal(nonterm_node) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a non-terminal node
42 43 44 45 46 47 |
# File 'lib/rley/formatter/json.rb', line 42 def before_non_terminal(nonterm_node) separator = sibling_flags[-1] ? ",\n" : "\n" name = nonterm_node.symbol.name print_text(separator, "{ \"#{name}\": ") sibling_flags[-1] = true end |
#before_ptree(_ptree) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit the given parse tree
31 32 33 34 35 36 |
# File 'lib/rley/formatter/json.rb', line 31 def before_ptree(_ptree) print_text('', "{\n") indent print_text('', '"root":') indent end |
#before_subnodes(_parent, _subnodes) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit the children of a non-terminal node
54 55 56 57 58 |
# File 'lib/rley/formatter/json.rb', line 54 def before_subnodes(_parent, _subnodes) print_text('[', nil) indent sibling_flags.push(false) end |
#before_terminal(term_node) ⇒ Object
Method called by a ParseTreeVisitor to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a terminal node
64 65 66 67 68 69 70 71 |
# File 'lib/rley/formatter/json.rb', line 64 def before_terminal(term_node) separator = sibling_flags[-1] ? ",\n" : "\n" name = term_node.symbol.name lexeme = term_node.token.lexeme print_text(separator, "{\"#{name}\": \"#{lexeme}\"}") sibling_flags[-1] = true end |