Class: Litbuild::BlueprintParser

Inherits:
Object
  • Object
show all
Includes:
StringIndentation
Defined in:
lib/litbuild/blueprint_parser.rb

Overview

This is a kludgy hand-built parser, written by someone who does not know how to write parsers. Blueprint structure is not (at least, at this point) complicated enough that I can see any point in defining a grammar and using a parser generator. The structure of blueprints, and the intended behavior of this parser, are described informally in ‘doc/blueprints.txt` (and blueprint-type-specific files under `doc` as well).

The entire parse process occurs during initialization, so after creating a BlueprintParser you will either be able to ask it for the result of the parse operation, or have an exception to deal with.

Essentially all methods are therefore used within initialization. Use great care when modifying this class!

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringIndentation

#indent_for, #strip_indentation_from_array, #strip_indentation_from_string, #strip_indentation_from_value

Constructor Details

#initialize(file_text:, parameters: nil) ⇒ BlueprintParser

Returns a new instance of BlueprintParser.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/litbuild/blueprint_parser.rb', line 34

def initialize(file_text:, parameters: nil)
  @text = file_text.dup
  substitute_parameters(parameters) if parameters
  resolve_conditional_blocks if @text.match?(/^#IF /)

  @phase_directives = {}
  @phase_grafs = {}
  phase_chunks = @text.split(/^phase: ([a-z -_]+)$/)

  # The first part of the blueprint,
  # before any phase directive,
  # becomes the base directives and base narrative for the blueprint.
  # If there are no phase directives, this is the entire blueprint.
  @base_grafs = []
  @base_directives = parse_phase(phase_chunks.shift, {}, @base_grafs)
  @base_directives['full-name'] ||= @base_directives['name']

  # The rest of the blueprint, if any, consists of directives
  # and narrative for specific phases.
  # The directives for each phase include all the base directives,
  # as well as those specific to the phase.
  until phase_chunks.empty?
    phase_name = phase_chunks.shift
    phase_contents = phase_chunks.shift
    grafs = []
    @phase_directives[phase_name] = parse_phase(phase_contents,
                                                @base_directives,
                                                grafs)
    @phase_grafs[phase_name] = grafs
  end

  # Any directives at the beginning of a blueprint are
  # actually a file header
  # that should not be considered as part of the narrative for it.
  @base_grafs.shift while @base_grafs[0].is_a?(Hash)
rescue StandardError => e
  msg = "Cannot parse blueprint starting: #{@text.lines[0..3].join}"
  raise(Litbuild::ParseError, "#{msg} -- #{e}")
end

Instance Attribute Details

#base_directivesObject (readonly)

directives are for use in scripts. grafs are for use in documents.



32
33
34
# File 'lib/litbuild/blueprint_parser.rb', line 32

def base_directives
  @base_directives
end

#base_grafsObject (readonly)

directives are for use in scripts. grafs are for use in documents.



32
33
34
# File 'lib/litbuild/blueprint_parser.rb', line 32

def base_grafs
  @base_grafs
end

#phase_directivesObject (readonly)

directives are for use in scripts. grafs are for use in documents.



32
33
34
# File 'lib/litbuild/blueprint_parser.rb', line 32

def phase_directives
  @phase_directives
end

#phase_grafsObject (readonly)

directives are for use in scripts. grafs are for use in documents.



32
33
34
# File 'lib/litbuild/blueprint_parser.rb', line 32

def phase_grafs
  @phase_grafs
end