class Rley::Formatter::Json
A formatter class that renders a parse tree in JSON format
Attributes
Current indentation level
Array of booleans (one per indentation level). Set to true after first child was visited.
Public Class Methods
Source
# File lib/rley/formatter/json.rb, line 21 def initialize(anIO) super(anIO) @indentation = 0 @sibling_flags = [false] end
Constructor. @param anIO [IO] The output stream to which the rendered grammar is written.
Rley::Formatter::BaseFormatter::new
Public Instance Methods
Source
# File lib/rley/formatter/json.rb, line 89 def after_ptree(_ptree) dedent dedent print_text("\n", '}') end
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 @param _ptree [ParseTree]
Source
# File lib/rley/formatter/json.rb, line 78 def after_subnodes(_parent, _subnodes) sibling_flags.pop print_text("\n", ']') dedent print_text("\n", '}') end
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. @param _parent [NonTerminalNode] @param _subnodes [Array] array of children nodes
Source
# 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
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 @param nonterm_node [NonTerminalNode]
Source
# File lib/rley/formatter/json.rb, line 31 def before_ptree(_ptree) print_text('', "{\n") indent print_text('', '"root":') indent end
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 @param _ptree [ParseTree]
Source
# File lib/rley/formatter/json.rb, line 54 def before_subnodes(_parent, _subnodes) print_text('[', nil) indent sibling_flags.push(false) end
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 @param _parent [NonTerminalNode] @param _subnodes [Array] array of children nodes
Source
# 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
Method called by a ParseTreeVisitor
to which the formatter subscribed. Notification of a visit event: the visitor is about to visit a terminal node @param term_node [TerminalNode]
Private Instance Methods
Source
# File lib/rley/formatter/json.rb, line 105 def print_text(aSeparator, aText) output.print aSeparator output.print "#{' ' * 2 * @indentation}#{aText}" unless aText.nil? end