Module: LangScan::JavaScript

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

Defined Under Namespace

Classes: Tokenizer

Constant Summary collapse

Keywords =
%w(
  break else new var
  case finally return void
  catch for switch while
  continue function this with
  default if throw
  delete in try
  do instanceof typeof
  null true false
)
KeywordsHash =
{}
Types =
[]
TypesHash =
{}
IdentType =
Hash.new(:ident)

Class Method Summary collapse

Class Method Details

.abbrevObject



24
25
26
# File 'lib/langscan/javascript.rb', line 24

def abbrev
  "js"
end

.extnamesObject



28
29
30
# File 'lib/langscan/javascript.rb', line 28

def extnames
  [".js"]
end

.nameObject



20
21
22
# File 'lib/langscan/javascript.rb', line 20

def name
  "JavaScript"
end

.scan(input, &block) ⇒ Object

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



34
35
36
37
# File 'lib/langscan/javascript.rb', line 34

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

.scan_unsorted(input, &block) ⇒ Object



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
# File 'lib/langscan/javascript.rb', line 39

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::JavaScript::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 == '{'
            before_open_token.type = :fundef
          else
            before_open_token.type = :funcall
          end
        else
          before_open_token.type = :funcall
        end
      end
    end
  }
end