Class: Glyph::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/glyph/interpreter.rb

Overview

A Glyph::Interpreter object perform the following actions:

  • Parses a string of text containing Glyph macros

  • Creates a document based on the parsed syntax tree

  • Analyzes and finalizes the document

Instance Method Summary collapse

Constructor Details

#initialize(text, context = {}) ⇒ Interpreter

Creates a new Glyph::Interpreter object.

Parameters:

  • text (String)

    the string to interpret

  • context (Hash) (defaults to: {})

    the context to pass along when expanding macros



14
15
16
17
18
19
# File 'lib/glyph/interpreter.rb', line 14

def initialize(text, context={})
  @context = context
  @context[:source] ||= {:name => "--", :file => nil, :topic => nil}
  @text = text
  @parser = Glyph::Parser.new text, @context[:source][:name]
end

Instance Method Details

#documentGlyph::Document

Returns the finalized @document (calls self#process and self#postprocess if necessary)

Returns:



34
35
36
37
38
39
40
# File 'lib/glyph/interpreter.rb', line 34

def document
  parse unless @tree
  return @document if @document.finalized?
  process if @document.new?
  postprocess if @document.analyzed?
  @document
end

#parseGlyph::SyntaxNode

Parses the string provided during initialization

Returns:

Since:

  • 0.3.0



45
46
47
48
49
50
51
# File 'lib/glyph/interpreter.rb', line 45

def parse
  Glyph.info "Parsing: #{@context[:source][:name]}" if Glyph.debug? && @context[:info] && @context[:source][:name]
  @tree = @parser.parse
  @document = Glyph::Document.new @tree, @context
  @document.inherit_from @context[:document] if @context[:document]
  @tree
end

#postprocessObject

See Also:



28
29
30
# File 'lib/glyph/interpreter.rb', line 28

def postprocess
  @document.finalize
end

#processObject

See Also:



22
23
24
25
# File 'lib/glyph/interpreter.rb', line 22

def process
  parse unless @tree
  @document.analyze
end