Module: TracePreprocessor::CodeGenerator

Defined in:
lib/trace_preprocessor/code_generator.rb

Class Method Summary collapse

Class Method Details

.converter_functions_c_code(dsl) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trace_preprocessor/code_generator.rb', line 29

def self.converter_functions_c_code dsl
  result = output_token_code dsl

  dsl.lexemes.each do |name, lexeme|
    if lexeme.converter[:c]
      result += parse_lexeme_code(name, lexeme)
    end
  end

  result      
end

.generate_lex(dsl) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/trace_preprocessor/code_generator.rb', line 4

def self.generate_lex dsl
  template = 
<<-LEX
%{
COMMON_CODE

CONVERTER_FUNCTIONS  
%}    

%option nounput

%%

REGEXPS

%%
LEX

  template.gsub!("COMMON_CODE",         dsl.code)
  template.gsub!("CONVERTER_FUNCTIONS", converter_functions_c_code(dsl))
  template.gsub!("REGEXPS",             regexps(dsl))

  template
end

.output_token_code(dsl) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/trace_preprocessor/code_generator.rb', line 56

def self.output_token_code dsl
<<-CODE
void output_token(const char* name, const char* source, long value, int value_kind) {
#{dsl.output_token_code}
}
  
CODE
end

.parse_lexeme_code(name, lexeme) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/trace_preprocessor/code_generator.rb', line 65

def self.parse_lexeme_code(name, lexeme)
<<-CODE
long converter_#{name}() {
#{lexeme.converter[:c]}
}

void parse_#{name}() {
output_token("#{name}", yytext, converter_#{name}(), #{lexeme.value_kind == :exact ? 1 : 0});
}

CODE
end

.regexps(dsl) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/trace_preprocessor/code_generator.rb', line 41

def self.regexps dsl
  result = ""

  a = dsl.lexemes.values
  a.sort! { |x, y| x.priority <=> y.priority }

  a.each do |lexeme|
    if lexeme.regexp
      result += "#{lexeme.regexp.source} parse_#{lexeme.name}();\n"
    end
  end
  
  result
end