Class: DataMetaDom::SourceFile

Inherits:
VerDoccable show all
Defined in:
lib/dataMetaDom/sourceFile.rb

Overview

A Model coupled with the DataMeta DOM source file info

For command line details either check the new method’s source or the README.rdoc file, the usage section.

Instance Attribute Summary collapse

Attributes inherited from VerDoccable

#ver

Attributes inherited from Documentable

#docs

Instance Method Summary collapse

Methods inherited from VerDoccable

#resetEntity, verConsumed?

Methods inherited from Documentable

#addDoc, #all, #clear, #docConsumed?, #getDoc, #has?, #ids

Constructor Details

#initialize(path, name, line = nil, lineNum = 0, namespace = nil) ⇒ SourceFile

Create an instance with the given parameters.

  • Parameters:

    • path - directory where the source file is located

    • name - the base name of this source file

    • line - source line if any, useful when creating the source reference from the code when source is not trivial.

    • lineNum - line number, useful when creating the source reference from the code

    • namespace - namespace associated with this source file, useful when creating the source reference from the code



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dataMetaDom/sourceFile.rb', line 52

def initialize(path, name, line = nil, lineNum = 0, namespace = nil)
    #noinspection RubyArgCount
    super()
    @path = path
    @name = name
    @namespace = namespace
    # use the Absolute Path to avoid double-dipping via different subdir references
    @key = File.absolute_path("#{@path}#{File::SEPARATOR}#{@name}").to_sym
    @lineNum = lineNum
    @line = line # nil interpolates to an empty string
end

Instance Attribute Details

#keyObject (readonly)

Unique key of this source file to use in hash maps, the absolute path to avoid duplicates by different ways to point to the file, turned into a symbol.



26
27
28
# File 'lib/dataMetaDom/sourceFile.rb', line 26

def key
  @key
end

#lineObject (readonly)

Current source line.



36
37
38
# File 'lib/dataMetaDom/sourceFile.rb', line 36

def line
  @line
end

#lineNumObject (readonly)

Current source line number



41
42
43
# File 'lib/dataMetaDom/sourceFile.rb', line 41

def lineNum
  @lineNum
end

#nameObject (readonly)

The name of this source file in the directory indicated by the path property.



20
21
22
# File 'lib/dataMetaDom/sourceFile.rb', line 20

def name
  @name
end

#namespaceObject (readonly)

The namespace associated with the source file.



31
32
33
# File 'lib/dataMetaDom/sourceFile.rb', line 31

def namespace
  @namespace
end

#pathObject (readonly)

The directory to this source file.



15
16
17
# File 'lib/dataMetaDom/sourceFile.rb', line 15

def path
  @path
end

Instance Method Details

#fullNameObject

Full name of this source file, absolute path. Derived from the key property turned into a string.



142
# File 'lib/dataMetaDom/sourceFile.rb', line 142

def fullName; @key.to_s end

#nextLine(verbatim = false) ⇒ Object

Advances a line, skipping empty lines and comments. Parameter:

  • verbatim - pass true to maintain formatting and keep empty lines



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dataMetaDom/sourceFile.rb', line 119

def nextLine(verbatim = false)
    @file = File.open("#{@path}#{File::SEPARATOR}#{@name}") unless defined?(@file) && @file
    while (line = @file.gets)
        unless line
            @file.close
            nil
        end
        @lineNum += 1
        return (@line = line) if verbatim
        @line = line.chomp.strip

        case @line
            when '', /^\s*#.*$/ # skip comments and empty lines
                next
            else
                return @line
        end
    end
end

#parse(model) ⇒ Object

Parses this DataMeta DOM source into the given Model.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dataMetaDom/sourceFile.rb', line 77

def parse model
    while nextLine
        puts "Source: #{@line}" if $DEBUG
        next if docConsumed?(self)
        if (newVer = VerDoccable.verConsumed?(self))
            raise RuntimeError, "Only one version definition allowed, second one found in line #{@lineNum}" if self.ver
           self.ver = newVer
           model.ver = newVer # plant it straight into the model per the latest design
           raise ArgumentError,
                 %<Model version already defined as #{model.ver} but the file #{@path} tries to redefine it to #{newVer}.
This is not allowed: all included files should define same version> unless model.ver && newVer == model.ver
           next
        end
        case @line
        # treat the namespace operator as a special case
            when /^\s*#{NAMESPACE}\s+([\w\.]+)$/
                @namespace = $1
                next
            when /^\s*#{INCLUDE}\s+(\S+)$/
                model.sources.queue "#{$1}.dmDom"
                next
            else
                isTokenOk = false
                MODEL_LEVEL_TOKENS.each { |c|
                    isTokenOk = c.consumed?(model, self)
                    if isTokenOk
                        resetEntity
                        break
                    end
                }
                raise "Syntax error; #{model.diagn}" unless isTokenOk

        end

    end # while
end

#snapshotObject

Create a shapshot of the source file information, useful for saving a status about an element currently parsed. Can not use this instance - as the parsing progresses, the stateful information will change.



68
69
70
71
72
# File 'lib/dataMetaDom/sourceFile.rb', line 68

def snapshot # for the history
    snap = SourceFile.new(@path, @name, @line, @lineNum, @namespace)
    snap.ver = Ver.new(self.ver.full)
    snap
end

#to_sObject

Textual representation of this source file reference, includes line number and the current source line.



147
# File 'lib/dataMetaDom/sourceFile.rb', line 147

def to_s; "#{fullName}##{@lineNum}{{#{@line}}}" end