Class: Caphir::SimpleCParser

Inherits:
CTokenizer::LexerBase show all
Includes:
CTokenizer::Scoped
Defined in:
lib/caphir/parser.rb

Overview

TODO: why is this class necessary anyway? should all this stuff done in CType ?

Constant Summary

Constants included from CTokenizer

CTokenizer::CP_RESERVED, CTokenizer::C_RESERVED, CTokenizer::EOF_TOKEN

Instance Attribute Summary

Attributes included from CTokenizer::Scoped

#scope

Attributes included from CTokenizer::Sourced

#source

Instance Method Summary collapse

Methods included from CTokenizer::Scoped

#macro?, #process_scope, #start_of_line?

Methods included from CTokenizer::Sourced

#empty?, #file, #file=, #line, #line=, #match?, #scan

Methods included from CTokenizer

#collect, #each, #error, error, line_count, #parse_error, #to_a, #token_error, #unmatched_error, #warning

Constructor Details

#initialize(str, file = nil, line = 1) ⇒ SimpleCParser

Returns a new instance of SimpleCParser.


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/caphir/parser.rb', line 13

def initialize(str, file=nil, line=1)
    super(str, file, line)
    
    @scope = 0
    @macro = false
    @start_line = true # begining of file
    
    # @ctype_parser.parse(source) -> parses source until a compelete
    # top level C statement has been parsed.  If the statement is a 
    # function definition it stops when the opening '{' is reached.
    @ctype_parser = CType::Parser.new()
end

Instance Method Details

#each_statementObject


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/caphir/parser.rb', line 31

def each_statement
    until @source.empty?
        context = @ctype_parser.parse(self)
        # if scope != 0 then we are in a function body
        if f_body = (@scope != 0)
            # first yield function header
            yield(nil)
            until @scope == 0 or @source.empty?
                # get back to group zero
                # don't check for contract comments
                process_scope(@source.shift)
            end
        end #if
        
        # add type definitions
        context.each {|ctxt|
            CType[ctxt.identifier] = ctxt if ctxt.typedef?
        }
        
        yield(f_body ? context.first : nil)
    end # until
end

#shiftObject


26
27
28
29
# File 'lib/caphir/parser.rb', line 26

def shift
    t = process_scope(super)
    t
end