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



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

#indentationObject (readonly)

Current indentation level



12
13
14
# File 'lib/rley/formatter/json.rb', line 12

def indentation
  @indentation
end

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

Parameters:

  • _ptree (ParseTree)


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.

Parameters:

  • _parent (NonTerminalNode)
  • _subnodes (Array)

    array of children nodes



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

Parameters:

  • nonterm_node (NonTerminalNode)


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

Parameters:

  • _ptree (ParseTree)


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

Parameters:

  • _parent (NonTerminalNode)
  • _subnodes (Array)

    array of children nodes



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

Parameters:

  • term_node (TerminalNode)


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