Class: Pray::ManifestMethods::BlockParser

Inherits:
Object
  • Object
show all
Includes:
ParserBlocks, ParserHelpers
Defined in:
lib/pray/manifest_parser.rb

Instance Method Summary collapse

Methods included from ParserBlocks

#apply_destination_statement, #apply_file_body_statement, #apply_local_pray_path, #apply_pray_statement, #bind_compose_local, #bind_file_package, #bind_pray_package, #package_signal?, #parse_destination_block, #parse_destination_body, #parse_file_block, #parse_file_body, #parse_group_block, #parse_symbols_block, #parse_target_block

Methods included from ParserHelpers

apply_target_statement, infer_source_kind, keyword_array, keyword_bool, keyword_string, package_constraint, package_exports, parse_call, parse_group_header, parse_keyword_segment, parse_local_decl, parse_package_decl, parse_render_policy, parse_source, parse_target_header, source_kind_and_url, string_from_literal, string_from_value

Constructor Details

#initialize(lines) ⇒ BlockParser

Returns a new instance of BlockParser.



9
10
11
12
13
14
# File 'lib/pray/manifest_parser.rb', line 9

def initialize(lines)
  @lines = lines
  @cursor = 0
  @group_stack = []
  @surface = StatementSurface::Reader.new
end

Instance Method Details

#apply_agent_package(manifest, rest) ⇒ Object



110
111
112
113
# File 'lib/pray/manifest_parser.rb', line 110

def apply_agent_package(manifest, rest)
  manifest.note_deprecated_keyword("agent")
  Destination.upsert_package(manifest, parse_package_with_groups(rest))
end

#apply_compose_or_output(manifest, statement, rest) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pray/manifest_parser.rb', line 90

def apply_compose_or_output(manifest, statement, rest)
  if statement.start_with?("output ")
    unless statement.end_with?(" do")
      raise Error.parse(
        "manifest",
        "top-level output must use a compose block (output \"path\" do ... end)"
      )
    end
    manifest.note_deprecated_keyword("output")
  end
  parse_destination_block(manifest, rest, "compose")
end

#apply_group(manifest, rest) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/pray/manifest_parser.rb', line 80

def apply_group(manifest, rest)
  group_names, is_block = parse_group_header(rest)
  raise Error.parse("manifest", "group must use a block") unless is_block
  raise Error.parse("manifest", "nested group blocks are not supported") if @group_stack.any?

  @group_stack.push(group_names)
  parse_group_block(manifest)
  @group_stack.pop
end

#apply_legacy_target(manifest, statement, rest, allow_target) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/pray/manifest_parser.rb', line 71

def apply_legacy_target(manifest, statement, rest, allow_target)
  raise Error.parse("manifest", "target must use a block") if !allow_target && !statement.end_with?(" do")

  manifest.note_deprecated_keyword("target")
  target, is_block = parse_target_header(rest)
  manifest.targets << target
  parse_target_block(manifest, manifest.targets.length - 1) if is_block
end

#apply_statement(manifest, statement, allow_target) ⇒ Object



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
# File 'lib/pray/manifest_parser.rb', line 38

def apply_statement(manifest, statement, allow_target)
  case statement
  when /\Aprayfile (.+)\z/
    manifest.prayfile_version = string_from_literal(Regexp.last_match(1))
  when /\Asource (.+)\z/
    manifest.sources << parse_source(Regexp.last_match(1))
  when /\Atarget (.+)\z/
    apply_legacy_target(manifest, statement, Regexp.last_match(1), allow_target)
  when /\Agroup (.+)\z/
    apply_group(manifest, Regexp.last_match(1))
  when "pray do", "template do"
    parse_symbols_block(manifest)
  when /\A(?:compose|output) (.+)\z/
    apply_compose_or_output(manifest, statement, Regexp.last_match(1))
  when /\A(?:tree|folder|skills) (.+)\z/
    apply_tree_or_folder(manifest, statement, Regexp.last_match(1))
  when /\Afile (.+)\z/
    parse_file_block(manifest, Regexp.last_match(1))
  when /\Aagent (.+)\z/
    apply_agent_package(manifest, Regexp.last_match(1))
  when /\Apackage (.+)\z/
    Destination.upsert_package(manifest, parse_package_with_groups(Regexp.last_match(1)))
  when /\A(?:pray|use|include) (.+)\z/
    apply_pray_statement(manifest, Regexp.last_match(1), nil)
  when /\Alocal (.+)\z/
    apply_unbound_local(manifest, Regexp.last_match(1))
  when /\Arender (.+)\z/
    manifest.render = parse_render_policy(Regexp.last_match(1))
  else
    raise Error.parse("manifest", "unrecognized statement: #{statement}")
  end
end

#apply_tree_or_folder(manifest, statement, rest) ⇒ Object



103
104
105
106
107
108
# File 'lib/pray/manifest_parser.rb', line 103

def apply_tree_or_folder(manifest, statement, rest)
  if statement.start_with?("folder ", "skills ") && !statement.end_with?(" do")
    raise Error.parse("manifest", "top-level folder/skills must use a tree block")
  end
  parse_destination_block(manifest, rest, "tree")
end

#apply_unbound_local(manifest, rest) ⇒ Object



115
116
117
118
119
# File 'lib/pray/manifest_parser.rb', line 115

def apply_unbound_local(manifest, rest)
  local = parse_local_decl(rest)
  local.bound = false
  Destination.upsert_local(manifest, local)
end

#next_statementObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pray/manifest_parser.rb', line 127

def next_statement
  pending = @surface.next
  return pending if pending

  while @cursor < @lines.length
    statement = @lines[@cursor].strip
    @cursor += 1
    next if statement.empty?

    while !statement.end_with?(" do") && statement != "end" && @cursor < @lines.length &&
        (statement.rstrip.end_with?(",") || !Literal.is_balanced?(statement))
      next_line = @lines[@cursor].strip
      @cursor += 1
      next if next_line.empty?

      statement = "#{statement} #{next_line}"
    end
    @surface.push_raw(statement)
    pending = @surface.next
    return pending if pending
  end
  nil
end

#parse_nested(manifest, stop_on_end:) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/pray/manifest_parser.rb', line 28

def parse_nested(manifest, stop_on_end:)
  while (statement = next_statement)
    return if statement == "end" && stop_on_end
    raise Error.parse("manifest", "unexpected 'end'") if statement == "end"

    apply_statement(manifest, statement, true)
  end
  raise Error.parse("manifest", "missing 'end'") if stop_on_end
end

#parse_package_with_groups(rest) ⇒ Object



121
122
123
124
125
# File 'lib/pray/manifest_parser.rb', line 121

def parse_package_with_groups(rest)
  package = parse_package_decl(rest)
  package.groups = @group_stack.last&.dup || []
  package
end

#parse_rootObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pray/manifest_parser.rb', line 16

def parse_root
  manifest = Manifest.new
  while (statement = next_statement)
    raise Error.parse("manifest", "unexpected 'end'") if statement == "end"

    apply_statement(manifest, statement, false)
  end
  raise Error.manifest("missing prayfile version") if manifest.prayfile_version.empty?

  manifest
end