Module: LangScan::Csharp

Defined in:
lib/langscan/csharp.rb,
ext/langscan/csharp/csharp/csharp.c

Defined Under Namespace

Classes: Tokenizer

Constant Summary collapse

Keywords =
%w(
  abstract as base bool break byte case catch char checked class const
  continue decimal default delegate do double else enum event explicit
  extern false finally fixed float for foreach goto if implicit in int
  interface internal is lock long namespace new null object operator
  out override params private protected public readonly ref return sbyte
  sealed short sizeof stackalloc static string struct switch this throw
  true try typeof uint ulong unchecked unsafe ushort using virtual void
  volatile while
)
KeywordsHash =
{}
Types =
%w(
  bool byte char double decimal float int long sbyte short uint ulong
  ushort void
)
TypesHash =
{}
IdentType =
Hash.new(:ident)

Class Method Summary collapse

Class Method Details

.abbrevObject



23
24
25
# File 'lib/langscan/csharp.rb', line 23

def abbrev
  "csharp"
end

.extnamesObject



27
28
29
# File 'lib/langscan/csharp.rb', line 27

def extnames
  [".cs"]
end

.nameObject



19
20
21
# File 'lib/langscan/csharp.rb', line 19

def name
  "C#"
end

.scan(input, &block) ⇒ Object

LangScan::Csharp.scan iterates over Csharp program. It yields for each Fragment.



33
34
35
36
# File 'lib/langscan/csharp.rb', line 33

def scan(input, &block)
  sorter = PairMatcher.fragmentsorter(block)
  scan_unsorted(input, &sorter)
end

.scan_unsorted(input, &block) ⇒ Object



38
39
40
41
42
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
# File 'lib/langscan/csharp.rb', line 38

def scan_unsorted(input, &block)
  pm = LangScan::PairMatcher.new(1,0,0,1)
  pm.define_intertoken_fragment :space, nil
  pm.define_intertoken_fragment :comment, nil
  pm.define_pair :paren, :punct, "(", :punct, ")"
  pm.parse(LangScan::Csharp::Tokenizer.new(input), lambda {|f|
    if f.type == :ident
      f.type = IdentType[f.text]
    end
    if f.type == :delegate
      f.type = :ident
    end
    yield f
  }) {|pair|
    if 1 <= pair.before_open_length &&
       pair.around_open(-1).type == :ident &&
       IdentType[pair.around_open(-1).text] == :ident
      before_open_token = pair.around_open(-1)
      if !KeywordsHash[before_open_token.text]
        if !(outer = pair.outer) || !outer.outer
          if 1 <= pair.after_close_length &&
             (pair.around_close(1).type == :punct &&
              pair.around_close(1).text == '{' ||
              pair.around_close(1).text == ':')
            before_open_token.type = :fundef
          else
            before_open_token.type = :funcall
          end
        else
          before_open_token.type = :funcall
        end
      end
    end
  }
end