Class: FixtureFox::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, lines, schema: nil) ⇒ Parser

Returns a new instance of Parser.



10
11
12
13
14
15
# File 'lib/fixture_fox/parser.rb', line 10

def initialize(file, lines, schema: nil)
  @file = file
  @lines = lines
  @schema = schema || "public"
  @anchor_files = []
end

Instance Attribute Details

#anchor_filesObject (readonly)

Name of external anchor files from @anchors directive



8
9
10
# File 'lib/fixture_fox/parser.rb', line 8

def anchor_files
  @anchor_files
end

#astObject (readonly)

Returns the value of attribute ast.



7
8
9
# File 'lib/fixture_fox/parser.rb', line 7

def ast
  @ast
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/fixture_fox/parser.rb', line 6

def file
  @file
end

Instance Method Details

#call(ast = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fixture_fox/parser.rb', line 17

def call(ast = nil)
  @ast = ast || Ast.new(file)

  # Current schema. The schema is initialized with a synthetic Ident token
  # because the #analyzer needs a token to emit an error message if the
  # public schema doesn't exist
  schema = Ident.new(file, 1, peek.initial_indent, 1, @schema)

  # Parse
  get_line
  while line
    case line
      when DirectiveLine
        case line.directive.value
          when "schema"
            schema = line.argument
            get_line
          when "include"
            parse_included_file(line.argument.value)
            get_line
          when "anchors" # TODO: Remove. Replaced by a command line option
            @anchor_files << line.argument.value
            get_line
        end

      when Line
        if line.root_table? && line.empty
          table = AstTable.new(@ast, schema, line.ident)
          get_line
        elsif line.root_table?
          peek && peek.element? or line.error("Table definition expected")
          table = AstTable.new(@ast, schema, line.ident)
          get_line
          parse_elements(table)
        elsif line.root_record?
          peek && peek.indent > 0 or line.error("Record definition expected")
          # The tokenizer recognizes root records as fields (where the
          # table name is tokenized as a Value - ie. a text). The following
          # recreates the table name as a Token and also embeds the record
          # in an AstTable. This effectively transforms a root-level record
          # definition into a single-row table definition
#             table_ident = Ident.new(
#                 file, line.value.lineno, line.initial_indent, line.value.pos, line.value.value)
          name = PgGraph.inflector.record_type2table(line.value.value)
          table_ident = Ident.new(
              file, line.value.lineno, line.initial_indent, line.value.pos, name)
          table = AstTable.new(@ast, schema, table_ident)
          record = AstRecordElement.new(table, AnchorToken.of_ident(line.ident))
          get_line
          parse_members(record, line.indent)
        else
          line.error("Table or record definition expected")
        end
      else
        raise "Oops"
      end
  end

  @ast
end