Class: ParserSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/caphir/parsersettings.rb

Overview

we need this object to config the parser environment TODO: move to separate file TODO: simplify iface

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParserSettings

Returns a new instance of ParserSettings.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/caphir/parsersettings.rb', line 26

def initialize
    @dest_file       = nil
    @src_file        = nil
    @source          = nil
    @overwrite       = true
    @preproc_opts    = []
    @preprocess_only = nil
    @includes        = []
    
    @search_path         = SearchPath.new
    @search_usr_include  = true

    # TODO: check Preprocessor::Parser interface and usage for 
    # search_path_limited - couldnt it be "generated" by the Parser ?
    # is it really needed at all ?
    @search_path_limited = SearchPath.new

    @compiler = nil
    @version  = nil
end

Instance Attribute Details

#compilerObject

Returns the value of attribute compiler


24
25
26
# File 'lib/caphir/parsersettings.rb', line 24

def compiler
  @compiler
end

#dest_fileObject

source, destination files


10
11
12
# File 'lib/caphir/parsersettings.rb', line 10

def dest_file
  @dest_file
end

#includesObject

Returns the value of attribute includes


20
21
22
# File 'lib/caphir/parsersettings.rb', line 20

def includes
  @includes
end

#overwriteObject

overwrite existing target file (default: true)


16
17
18
# File 'lib/caphir/parsersettings.rb', line 16

def overwrite
  @overwrite
end

#preprocess_onlyObject

Returns the value of attribute preprocess_only


18
19
20
# File 'lib/caphir/parsersettings.rb', line 18

def preprocess_only
  @preprocess_only
end

#search_pathObject (readonly)

Returns the value of attribute search_path


22
23
24
# File 'lib/caphir/parsersettings.rb', line 22

def search_path
  @search_path
end

#search_path_limitedObject (readonly)

Returns the value of attribute search_path_limited


22
23
24
# File 'lib/caphir/parsersettings.rb', line 22

def search_path_limited
  @search_path_limited
end

#sourceObject (readonly)

input stream of the source file


13
14
15
# File 'lib/caphir/parsersettings.rb', line 13

def source
  @source
end

#src_fileObject

source, destination files


10
11
12
# File 'lib/caphir/parsersettings.rb', line 10

def src_file
  @src_file
end

#versionObject

Returns the value of attribute version


24
25
26
# File 'lib/caphir/parsersettings.rb', line 24

def version
  @version
end

Instance Method Details

#add_include_path(path) ⇒ Object


142
143
144
145
146
147
148
149
# File 'lib/caphir/parsersettings.rb', line 142

def add_include_path(path)
    if path
        @search_path.unshift(path)
    else
        # -I with no argument means don't search /usr/include
        @search_usr_include = false
    end
end

#checkObject

Check different parser environment settings


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/caphir/parsersettings.rb', line 64

def check
    if @dest_file
        if File.exists?(@dest_file) and not @overwrite
            raise "destination file '#{@dest_file}' exists"
        end
        
        # this is always bad - so don't allow it even if @overwrite is true
        if @src_file and 
            File.expand_path(@dest_file) == File.expand_path(@src_file)

            raise "destination file and source file are the same"
        end
   end

    if @src_file
        begin
            @source = File.open(@src_file, 'r') do |in_file|
                in_file.read
            end
        rescue
            raise "could not open '#{@src_file}': #{$?}"
        end
    else
        @source = STDIN.read
    end
end

#cleanObject

cleans up the settings this is also used to force GC instance vars are NOT set to defaults but nil


134
135
136
137
138
139
140
# File 'lib/caphir/parsersettings.rb', line 134

def clean
    instance_variables.each do |var|
        unless (var == "@dest_file" || var == "@preprocess_only")
            instance_variable_set(var, nil) 
        end
    end
end

#define_macro(macro) ⇒ Object

takes an option as string


48
49
50
51
52
53
# File 'lib/caphir/parsersettings.rb', line 48

def define_macro(macro)
    unless macro =~ /\A([A-Za-z_]\w*)(?:\(([\w,\s]*)\))?(?:=(.*))?\Z/
        raise "macro names must be identifiers '#{macro}'"
    end
    @preproc_opts << [:DEFINE, $1, $2 && $2.split(/\s*,\s*/), $3 || '1']
end

#executeObject

builds the environment for the parser based on the settings defined


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/caphir/parsersettings.rb', line 93

def execute
    check
    
    begin
        create_dest_dirs
    rescue
        raise "could not create destination directory: #{$?}"
    end

    # the following are added to the end of the search path
    if @search_usr_include
        if File.exists?('/usr/include')
            @search_path << '/usr/include'
            if File.exists?('/usr/local/include')
                @search_path << '/usr/local/include' 
            end

            # search for /usr/lib/gcc*/<system>/<version>/include
            Dir['/usr/lib/gcc*/*/*/include'].each do |gcc_inc|
                @search_path << gcc_inc if File.directory?(gcc_inc)
            end
        elsif env_include = ENV['INCLUDE']
            env_include.split(';').each do |p|
            # any anci c installation should have stdlib.h or stdio.h
                if File.exists?(File.join(p, 'stdlib.h'))
                    @search_path << p
                end
            end # each
        end
    end

    @search_path_limited = @search_path.dup
    # Add directory of file to the begining of the search_path
    # search_path_limited does not include the directory of the file being 
    # parsed
    @search_path.unshift( @src_file ? File.dirname(@src_file) : '.' )
end

#macrosObject


59
60
61
# File 'lib/caphir/parsersettings.rb', line 59

def macros
    @preproc_opts
end

#undefine_macro(macro) ⇒ Object


55
56
57
# File 'lib/caphir/parsersettings.rb', line 55

def undefine_macro(macro)
    @preproc_opts << [:UNDEF, macro]
end