Class: LanguageParser::CLanguageScanner

Inherits:
LanguageScanner show all
Defined in:
lib/cgialib/lp/CLanguageScanner.rb

Overview

class : CLanguageScanner

This is the CLanguageScanner specialized to read prototypes for C functions.

Direct Known Subclasses

CPPLanguageScanner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLanguageScanner

initialize()

Constructs the C language scanner class



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cgialib/lp/CLanguageScanner.rb', line 21

def initialize()

  # Create the prototype array

  @prototypes = []
  
# Create the defines array

@defines = {}
  
  # Set the prototype class to build to the default
  # prototype class

  @prototypeClass = Prototype

end

Instance Attribute Details

#definesObject (readonly)

The array of ‘#defines’ found



40
41
42
# File 'lib/cgialib/lp/CLanguageScanner.rb', line 40

def defines
  @defines
end

#prototypeClassObject

The prototype class to build



39
40
41
# File 'lib/cgialib/lp/CLanguageScanner.rb', line 39

def prototypeClass
  @prototypeClass
end

#prototypesObject (readonly)

The array of prototypes found



38
39
40
# File 'lib/cgialib/lp/CLanguageScanner.rb', line 38

def prototypes
  @prototypes
end

Instance Method Details

#parse(tokens) ⇒ Object

parse( tokens )

tokens - An array of tokens built by a Tokenizer

This method reads the stream of tokens built by a Tokenizer and fills the @prototypes array with the prototypes that are found.



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cgialib/lp/CLanguageScanner.rb', line 68

def parse( tokens )
  
  # This is the code fragment leading up to the interior
  # of the function
  
  codefrag = TokenStream.new()
  
  # 'level' is the level of bracket nesting.
  
  level = 0
  
  # This will be true if we are looking at precompiler tokens
  
  hold_until_return = false

# This will be true if we are looking for a '#define'
  
parsing_define = false
define_name = nil
define_value = nil
  
  # Look through each token

  tokens.each { |tok|
  
    if ( parsing_define )
  
      unless ( define_name )
  
        define_name = tok.to_s if ( tok.is_a?( CodeToken ) )
  
      else
  
        if ( tok.to_s =~ /^\n/ )
  
        define_value.strip!
        @defines[ define_name ] = define_value.to_s
  
        parsing_define = false
        define_name = nil
        define_value = nil
  
        else
  
        define_value.push( tok )
  
        end
  
      end
  
      next
  
    elsif ( hold_until_return )
  
      hold_until_return = false if ( tok.to_s =~ /^\n/ )
      next
  
    end

    if tok.to_s =~ /^#define$/
  
# This is a #define macro, so we need to start parsing
# it

      parsing_define = true
      define_name = nil
      define_value = TokenStream.new()
  
    elsif tok.to_s =~ /^#/
  
      # This is a precompiler directive, so
      # we should ignore all of the tokens
      # until a return
  
      hold_until_return = true
    
    elsif tok.to_s == "{"
  
      # If we are at level zero then we are
      # opening a function for code, so we
      # should interpret what we have
      # up until now.

      if level == 0

        parse_prototype( codefrag )
  
        codefrag = TokenStream.new()

      end
  
      # Now increment the level

      level += 1

    elsif tok.to_s == "}"
  
      # Decrement the level

      level -= 1

    elsif tok.to_s == ";"
  
      # If we see a ";" and we are at level
      # zero then we have a constant
      # declaration 

      codefrag = TokenStream.new() if ( level == 0 )

    else
  
      # Otherwise push the fragment

      codefrag.push( tok ) if level == 0

    end

  }

end

#to_sObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cgialib/lp/CLanguageScanner.rb', line 42

def to_s()
  
  text = "Prototypes:\n"
  
  @prototypes.each { |proto|
    text += "  #{proto}\n"
  }
  
text += "\nDefines:\n"
  
  @defines.each { |key,value|
    text += "  #{key} = '#{value}'\n"
  }
  
  text
  
end