Class: Rley::Formatter::Json

Inherits:
BaseFormatter show all
Defined in:
lib/rley/formatter/json.rb

Overview

A formatter class that renders a parse tree in JSON format

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#render

Constructor Details

#initialize(anIO) ⇒ Json

Constructor. is written.

Parameters:

  • anIO (IO)

    The output stream to which the rendered grammar



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

#indentationObject (readonly)

Current indentation level



10
11
12
# File 'lib/rley/formatter/json.rb', line 10

def indentation
  @indentation
end

#sibling_flagsObject (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

Parameters:

  • _ptree (ParseTree)


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.

Parameters:

  • _parent (NonTerminalNode)
  • _subnodes (Array)

    array of children nodes



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

Parameters:

  • nonterm_node (NonTerminalNode)


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

Parameters:

  • _ptree (ParseTree)


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

Parameters:

  • _parent (NonTerminalNode)
  • _subnodes (Array)

    array of children nodes



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

Parameters:

  • term_node (TerminalNode)


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