Class: Recorder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRecorder

Returns a new instance of Recorder.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sjScript.rb', line 24

def initialize()

    # 存放所有内容
    @contents = String.new

    # 存放变更的字段. (里面是 Property 对象)
    @trimPropertyFields = Array.new

    # 记录解析的阶段
    @parseInterface = false
    @parseImplementation = false

    # 第一阶段解析完毕, 开头添加 @syn name = _name
    @parseImpPhaseOneFinished = false

    # 第二阶段解析完毕, 底部添加 - (type)filed {}
    @parseImpPhaseTwoFinished = false
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



17
18
19
# File 'lib/sjScript.rb', line 17

def contents
  @contents
end

#parseImplementationObject

Returns the value of attribute parseImplementation.



17
18
19
# File 'lib/sjScript.rb', line 17

def parseImplementation
  @parseImplementation
end

#parseImpPhaseOneFinishedObject

Returns the value of attribute parseImpPhaseOneFinished.



17
18
19
# File 'lib/sjScript.rb', line 17

def parseImpPhaseOneFinished
  @parseImpPhaseOneFinished
end

#parseImpPhaseTwoFinishedObject

Returns the value of attribute parseImpPhaseTwoFinished.



17
18
19
# File 'lib/sjScript.rb', line 17

def parseImpPhaseTwoFinished
  @parseImpPhaseTwoFinished
end

#parseInterfaceObject

Returns the value of attribute parseInterface.



17
18
19
# File 'lib/sjScript.rb', line 17

def parseInterface
  @parseInterface
end

#trimPropertyFieldsObject

Returns the value of attribute trimPropertyFields.



17
18
19
# File 'lib/sjScript.rb', line 17

def trimPropertyFields
  @trimPropertyFields
end

Instance Method Details

#parsing(line:) ⇒ Object



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
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
113
114
115
116
117
118
119
# File 'lib/sjScript.rb', line 43

def parsing(line:)
    # 记录解析阶段
    if /@interface/ =~ line then
        self.parseInterface = true
    end

    # imp
    if /@implementation/ =~ line then
        self.parseImplementation = true
    end

    # end
    if /@end/ =~ line then
        if self.parseInterface then
            self.parseInterface = false
        end
    
        if self.parseImplementation then
            self.parseImplementation = false
        end
    end

    # 如果正在解析 interface
    if self.parseInterface then
    # [^readonly|readwrite] 匹配除 readonly|readwrite 以外的字符串
        if /\(nonatomic.*[^readonly|readwrite]\)/ =~ line then
            # 将此行添加 readonly
            line.gsub!(/\)/, ", readonly)")
        
            # 记录 属性, imp 生成时需要.
        
            # 记录 type
            arr = line.split
            type = arr[-2] + " *"
        
            # 记录 field
            field = arr.last.gsub!(/\*|;/, "")
            self.trimPropertyFields.push(Property.new(type:type, field:field))
        end
    end

    # 如果正在解析 imp
    if self.parseImplementation then
        if !self.parseImpPhaseOneFinished then
            self.parseImpPhaseOneFinished = true
            self.trimPropertyFields.each do |property|
                line << "@synthesize " + property.field + " = _" + property.field + ";\n";
            end
        end
    end

    # 如果解析到了最后 imp
    if /@end/ =~ line && !self.parseImplementation && self.parseImpPhaseOneFinished then
        self.parseImpPhaseTwoFinished = true;

        self.trimPropertyFields.each do |property|
            # - (type *)name {\n
            # \t  if ( _name ) return _name; \n
            # \t    \n
            # \t  return _name; \n
            # }
            type = property.type
            field = property.field
#            self.contents << "\n- (#{type})#{field} {\n\tif ( _#{field} ) return _#{field};\n\t\n\treturn _#{field};\n}\n\n"
            self.contents << ("            - (\#{type})\#{field} {\n                if ( _\#{field} ) return _\#{field};\n            \n                return _\#{field};\n            }\n            EOB\n        end\n        puts \"\u5168\u90E8\u89E3\u6790\u5B8C\u6BD5\"\n    end\n\n    self.contents += line;\nend\n")