Class: J2119::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/j2119/parser.rb

Constant Summary collapse

ROOT =
Regexp.new('This\s+document\s+specifies\s+' +
'a\s+JSON\s+object\s+called\s+an?\s+"([^"]+)"\.')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(j2119_file) ⇒ Parser

Returns a new instance of Parser.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/j2119/parser.rb', line 26

def initialize(j2119_file)

  have_root = false
  @failed = false
  @constraints = RoleConstraints.new
  @finder = RoleFinder.new
  @allowed_fields = AllowedFields.new
  
  j2119_file.each_line do |line|
    if line =~ ROOT
      if have_root
        fail "Only one root declaration"
      else
        @root = $1
        @matcher = Matcher.new(root)
        @assigner =
          Assigner.new(@constraints, @finder, @matcher, @allowed_fields)
        have_root = true
      end
    else
      if !have_root
        fail "Root declaration must come first"
      else
        proc_line(line)
      end
    end
  end
  if @failed
    raise "Could not create parser"
  end
end

Instance Attribute Details

#finderObject (readonly)

for debugging



24
25
26
# File 'lib/j2119/parser.rb', line 24

def finder
  @finder
end

#rootObject (readonly)

Returns the value of attribute root.



21
22
23
# File 'lib/j2119/parser.rb', line 21

def root
  @root
end

Instance Method Details

#allows_any?(roles) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/j2119/parser.rb', line 101

def allows_any?(roles)
  @allowed_fields.any?(roles)
end

#fail(message) ⇒ Object



76
77
78
79
# File 'lib/j2119/parser.rb', line 76

def fail(message)
  @failed = true
  STDERR.puts message
end

#field_allowed?(roles, child) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/j2119/parser.rb', line 97

def field_allowed?(roles, child)
  @allowed_fields.allowed?(roles, child)
end

#find_child_roles(roles, name) ⇒ Object



89
90
91
# File 'lib/j2119/parser.rb', line 89

def find_child_roles(roles, name)
  @finder.find_child_roles(roles, name)
end

#find_grandchild_roles(roles, name) ⇒ Object



85
86
87
# File 'lib/j2119/parser.rb', line 85

def find_grandchild_roles(roles, name)
  @finder.find_grandchild_roles(roles, name)
end

#find_more_roles(node, roles) ⇒ Object



81
82
83
# File 'lib/j2119/parser.rb', line 81

def find_more_roles(node, roles)
  @finder.find_more_roles(node, roles)
end

#get_constraints(role) ⇒ Object



93
94
95
# File 'lib/j2119/parser.rb', line 93

def get_constraints(role)
  @constraints.get_constraints(role)
end

#proc_line(line) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/j2119/parser.rb', line 58

def proc_line(line)
  if @matcher.is_constraint_line(line)
    @assigner.assign_constraints @matcher.build_constraint(line)
  elsif @matcher.is_only_one_match_line(line)
    @assigner.assign_only_one_of(@matcher.build_only_one(line))
  elsif line =~ /^Each of a/
    eaches_line = @matcher.eachof_match.match(line)
    eaches = Oxford.break_role_list(@matcher, eaches_line['each_of'])
    eaches.each do |each|
      proc_line("A #{each} #{eaches_line['trailer']}")
    end
  elsif @matcher.is_role_def_line(line)
    @assigner.assign_roles @matcher.build_role_def(line)
  else
    fail "Unrecognized line: #{line}"
  end
end