Class: Natsuzora::Contract::Parser

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

Overview

Parser for contract notation. Uses LexerKit stream directly without creating Token objects.

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser

Returns a new instance of Parser.



20
21
22
23
24
25
26
# File 'lib/natsuzora/contract/parser.rb', line 20

def initialize(input)
  @stream = CompiledLexer.instance.stream(input)
  # Position of the most recently consumed token; used for EOF error messages
  # and for errors that point back at the just-read identifier (e.g. unknown scalar type).
  @last_line = 1
  @last_col = 1
end

Instance Method Details

#parse_fileObject

Parse the entire contract file.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/natsuzora/contract/parser.rb', line 29

def parse_file
  types = {}

  skip_separators

  while type_definition?
    type_name, contract = parse_type_def
    types[type_name] = TypeDef.new(contract)
    skip_separators
  end

  root = parse_object_body

  Document.new(
    types: types,
    fields: build_fields_from_object(root)
  )
end

#parse_file_with_diffObject

Parse with diff markers (2-generation support).



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/natsuzora/contract/parser.rb', line 49

def parse_file_with_diff
  types = {}
  fields = {}

  skip_separators

  until eof?
    marker = try_parse_diff_marker

    if type_definition?
      type_name, type_def = parse_type_def_with_diff(marker)
      types[type_name] = type_def
    elsif current_type == :IDENTIFIER
      name, field = parse_field_with_diff(marker)
      fields[name] = field
    elsif marker
      raise_current_parse_error("expected field name or 'type' after diff marker")
    else
      break
    end

    skip_separators
  end

  Document.new(types: types, fields: fields)
end