Class: Syntax::AnsiC

Inherits:
Tokenizer show all
Defined in:
lib/syntax/lang/ansic.rb

Constant Summary collapse

ANSIC_KEYWORDS =
Set.new %w{asm break case continue default do else for goto if return
switch while struct union enum typedef static register
auto extern sizeof volatile const inline restrict}
ANSIC_PREDEFINED_TYPES =
Set.new %w{int long short char void signed unsigned 
float double bool complex}
ANSIC_PREDEFINED_CONSTANTS =
%w{EOF NULL true false}
ANSIC_ESCAPE =
/ [rbfnrtv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x

Instance Attribute Summary

Attributes inherited from Tokenizer

#chunk, #group

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tokenizer

#finish, #option, #set, #setup, #start, #teardown, #tokenize

Class Method Details

.add_type(name) ⇒ Object


7
8
9
# File 'lib/syntax/lang/ansic.rb', line 7

def self.add_type(name)
  ANSIC_PREDEFINED_TYPES << name
end

Instance Method Details

#stepObject


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/syntax/lang/ansic.rb', line 25

def step
  case
  when scan(/\s+/)
    start_group :normal, matched
  when match = scan(/#\s*(\w*)/)
    match << scan_until(/\n/)
    start_group :preprocessor, match
  when scan(/ L?' (?: [^\'\n\\] | \\ #{ANSIC_ESCAPE} )? '? /ox)
    start_group :char, matched
  when scan(/0[xX][0-9A-Fa-f]+/)
    start_group :hex, matched
  when scan(/(?:0[0-7]+)(?![89.eEfF])/)
    start_group :oct, matched
  when scan(/(?:\d+)(?![.eEfF])/)
    start_group :integer, matched
  when scan(/\d[fF]?|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
    start_group :float, matched
  when scan(/"(?:[^"\\]|\\.)*"/)
    start_group :string, matched
  when scan( %r{ ('(?: . | [\t\b\n] )') }x )
    start_group :char, matched
  when scan(/[a-z_][a-z_\d]+/)
    if ANSIC_KEYWORDS.include?( matched )
      start_group :keyword, matched
    elsif ANSIC_PREDEFINED_TYPES.include?( matched )
      start_group :predefined_types, matched
    else
      start_group :ident, matched
    end
  when scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
    start_group :comment, matched
  else
    start_group :other, scan(/./x)
  end
end