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.
19 20 21 22 23 |
# File 'lib/rley/formatter/json.rb', line 19 def initialize(anIO) super(anIO) @indentation = 0 @sibling_flags = [ false ] end |
Instance Attribute Details
#indentation ⇒ Object (readonly)
Current indentation level
10 11 12 |
# File 'lib/rley/formatter/json.rb', line 10 def indentation @indentation end |
#sibling_flags ⇒ Object (readonly)
Array of booleans (one per indentation level). Set to true after first child was visited.
14 15 16 |
# File 'lib/rley/formatter/json.rb', line 14 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
88 89 90 91 92 |
# File 'lib/rley/formatter/json.rb', line 88 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.
77 78 79 80 81 82 |
# File 'lib/rley/formatter/json.rb', line 77 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
40 41 42 43 44 45 |
# File 'lib/rley/formatter/json.rb', line 40 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
29 30 31 32 33 34 |
# File 'lib/rley/formatter/json.rb', line 29 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
52 53 54 55 56 |
# File 'lib/rley/formatter/json.rb', line 52 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
62 63 64 65 66 67 68 69 |
# File 'lib/rley/formatter/json.rb', line 62 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 |