Class: Ast::Merge::Ruleset::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/ruleset/parser.rb

Overview

Parses and validates a compact merge-ruleset document into normalized attributes.

Constant Summary collapse

REQUIRED_DIRECTIVES =
%i[format owners match read attach].freeze
OPTIONAL_SINGLE_VALUE_DIRECTIVES =
%i[comment_style render].freeze
MULTI_VALUE_DIRECTIVES =
%i[capability logical_owner repair surface delegate].freeze
KNOWN_DIRECTIVES =
(REQUIRED_DIRECTIVES + OPTIONAL_SINGLE_VALUE_DIRECTIVES + MULTI_VALUE_DIRECTIVES).freeze
READ_STRATEGIES =
%i[
  source_augmented_portable_write
  native_read_portable_write
  native_mutation
].freeze
IDENTIFIER_RE =
/\A[A-Za-z][A-Za-z0-9_.-]*\z/
TOKEN_RE =
/\A[!-~]+\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, path: nil) ⇒ Parser

Returns a new instance of Parser.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ast/merge/ruleset/parser.rb', line 31

def initialize(source, path: nil)
  @source = source.to_s
  @path = path
  @directives = []
  @capabilities = {}
  @logical_owners = {}
  @repair_policies = {}
  @surfaces = []
  @delegation_policies = []
  @parsed_values = {}
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



22
23
24
# File 'lib/ast/merge/ruleset/parser.rb', line 22

def capabilities
  @capabilities
end

#delegation_policiesObject (readonly)

Returns the value of attribute delegation_policies.



22
23
24
# File 'lib/ast/merge/ruleset/parser.rb', line 22

def delegation_policies
  @delegation_policies
end

#directivesObject (readonly)

Returns the value of attribute directives.



22
23
24
# File 'lib/ast/merge/ruleset/parser.rb', line 22

def directives
  @directives
end

#logical_ownersObject (readonly)

Returns the value of attribute logical_owners.



22
23
24
# File 'lib/ast/merge/ruleset/parser.rb', line 22

def logical_owners
  @logical_owners
end

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/ast/merge/ruleset/parser.rb', line 22

def path
  @path
end

#repair_policiesObject (readonly)

Returns the value of attribute repair_policies.



22
23
24
# File 'lib/ast/merge/ruleset/parser.rb', line 22

def repair_policies
  @repair_policies
end

#sourceObject (readonly)

Returns the value of attribute source.



22
23
24
# File 'lib/ast/merge/ruleset/parser.rb', line 22

def source
  @source
end

#surfacesObject (readonly)

Returns the value of attribute surfaces.



22
23
24
# File 'lib/ast/merge/ruleset/parser.rb', line 22

def surfaces
  @surfaces
end

Class Method Details

.parse(source, path: nil) ⇒ Object



26
27
28
# File 'lib/ast/merge/ruleset/parser.rb', line 26

def parse(source, path: nil)
  new(source, path: path).parse
end

Instance Method Details

#parseObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ast/merge/ruleset/parser.rb', line 43

def parse
  parse_source!
  validate_required_directives!

  {
    source: source,
    path: path,
    directives: directives.dup,
    capabilities: capabilities.dup,
    logical_owners: logical_owners.dup,
    repair_policies: repair_policies.dup,
    surfaces: surfaces.map(&:dup),
    delegation_policies: delegation_policies.map(&:dup)
  }.merge(@parsed_values)
end