Module: LangScan::Java

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

Defined Under Namespace

Classes: Tokenizer

Constant Summary collapse

Keywords =
%w(
  abstract boolean break byte case catch char class const continue default
  do double else extends final finally float for goto if implements import
  int interface long native new package private protected public return
  short static strictfp super switch synchronized this throw throws
  transient try void volatile while true false null
)
KeywordsHash =
{}
Types =
%w(boolean byte char double float int long short void)
TypesHash =
{}
IdentType =
Hash.new(:ident)

Class Method Summary collapse

Class Method Details

.abbrevObject



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

def abbrev
  "java"
end

.extnamesObject



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

def extnames
  [".java"]
end

.nameObject



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

def name
  "Java"
end

.scan(input, &block) ⇒ Object

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



33
34
35
36
# File 'lib/langscan/java.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/java.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.define_pair :brace, :punct, "{", :punct, "}"
  pm.define_pair :bracket, :punct, "[", :punct, "]"
  pm.parse(LangScan::Java::Tokenizer.new(input), lambda {|f|
    if f.type == :ident
      f.type = IdentType[f.text]
    end
    yield f
  }) {|pair|
    if pair.pair_type == :paren &&
       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).type == :ident &&
              pair.around_close(1).text == 'throws')
            before_open_token.type = :fundef
          else
            before_open_token.type = :funcall
          end
        else
          before_open_token.type = :funcall
        end
      end
    end
  }
end