Module: LangScan

Defined in:
lib/langscan.rb,
lib/langscan/c.rb,
lib/langscan/d.rb,
lib/langscan/io.rb,
lib/langscan/sh.rb,
lib/langscan/css.rb,
lib/langscan/lua.rb,
lib/langscan/php.rb,
lib/langscan/java.rb,
lib/langscan/perl.rb,
lib/langscan/ruby.rb,
lib/langscan/text.rb,
lib/langscan/_type.rb,
lib/langscan/elisp.rb,
lib/langscan/ocaml.rb,
lib/langscan/csharp.rb,
lib/langscan/eiffel.rb,
lib/langscan/python.rb,
lib/langscan/_common.rb,
lib/langscan/rpmspec.rb,
lib/langscan/autoconf.rb,
lib/langscan/automake.rb,
lib/langscan/brainfuck.rb,
lib/langscan/javascript.rb,
lib/langscan/_easyscanner.rb,
ext/langscan/c/c/c.c,
ext/langscan/d/d/d.c,
ext/langscan/sh/sh/sh.c,
ext/langscan/_template.c,
ext/langscan/php/php/php.c,
ext/langscan/java/java/java.c,
ext/langscan/elisp/elisp/elisp.c,
ext/langscan/csharp/csharp/csharp.c,
ext/langscan/python/python/python.c,
ext/langscan/scheme/scheme/scheme.c,
ext/langscan/javascript/javascript/javascript.c,
lib/langscan/_pairmatcher.rb,
lib/langscan/scheme.rb

Overview

_type.rb - a part of LangScan

Copyright © 2004-2005 Satoru Takabayashi <[email protected]>

All rights reserved.
This is free software with ABSOLUTELY NO WARRANTY.

You can redistribute it and/or modify it under the terms of the GNU General Public License version 2.

Defined Under Namespace

Modules: Autoconf, Automake, Brainfuck, C, CSS, Csharp, D, Eiffel, EmacsLisp, Io, Java, JavaScript, LangName, Lua, OCaml, PHP, Perl, Python, RPMSpec, Ruby, Scheme, Shell, Text, Type Classes: EasyScanner, Fragment, PairMatcher, ScanFailed

Constant Summary collapse

VERSION =
"1.2"
LangScanRegistry =
{}

Class Method Summary collapse

Class Method Details

.choose(file_name, content = nil) ⇒ Object

return suitable LangScan module.



110
111
112
113
114
115
# File 'lib/langscan.rb', line 110

def choose(file_name, content = nil)
  extname = File.extname(file_name)
  scanner = LangScanRegistry[extname]
  scanner = choose_by_content(content) if scanner.nil? and content
  return scanner
end

.choose_by_content(content) ⇒ Object

return suitable LangScan module choosed by content. choose_by_content calls choose_by_shebang and choose_by_emacs_mode.



105
106
107
# File 'lib/langscan.rb', line 105

def choose_by_content(content)
  return (choose_by_shebang(content) or choose_by_emacs_mode(content))
end

.choose_by_emacs_mode(content) ⇒ Object

return suitable LangScan module choosed by emacs mode. if content contains “-*- mode: c -*-”, choose_by_emacs_mode returns LangScan::C. return nil if suitable LangScan module is not found.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/langscan.rb', line 88

def choose_by_emacs_mode(content)
  chunk = content[0, 512] # FIXME: magic number
  LangScanRegistry.each_value {|scanner|
    mode = Regexp.quote(scanner.name.downcase.gsub(/\s+/, "-"))
    if scanner.name.include?("/") # "C/C++" etc.
      mode = "(" + mode + "|"
      mode << scanner.name.split("/").map {|part| Regexp.quote(part) }.join("|")
      mode << ")"
    end
    regexp = /-\*-\s+mode:\s+#{mode}\s+-\*-/i
    return scanner if regexp.match(chunk)
  }
  return nil
end

.choose_by_shebang(content) ⇒ Object

return suitable LangScan module choosed by shebang. return nil if suitable LangScan module is not found.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/langscan.rb', line 70

def choose_by_shebang(content)
  first_line = ""
  content.each_line {|line|
    first_line = line
    break
  }
  LangScanRegistry.each_value {|scanner|
    regexp = /^#!.*\b#{scanner.abbrev}/i
    return scanner if regexp.match(first_line)
  }
  return nil
end

.loadObject

load LangScan modules.



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

def load
  load_plugins("langscan")
end

.load_plugins(plugin_path) ⇒ Object

load *.rb files in plugin_path directory.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/langscan.rb', line 18

def load_plugins(plugin_path)
  $LOAD_PATH.each {|path|
    candidate_path = File.join(path, plugin_path)
    next unless File.directory?(candidate_path)
    Dir.entries(candidate_path).each {|entry|
      if File.extname(entry) == ".rb" and not /^_/.match(entry)
        begin
          require(File.join(plugin_path, entry))
        rescue LoadError => e
          # ignore load errors
        end
      end
    }
  }
end

.modulesObject

return an array contains LangScan modules.



64
65
66
# File 'lib/langscan.rb', line 64

def modules
  LangScanRegistry.values.uniq
end

.register(mod) ⇒ Object

stub



52
53
54
55
56
57
58
59
60
61
# File 'lib/langscan.rb', line 52

def register(mod)
  validate_module(mod)
  mod.extnames.each {|extname|
    if LangScanRegistry.include?(extname)
      mod = LangScanRegistry[extname]
      raise "#{extname} is already used by #{mod.abbrev}"
    end
    LangScanRegistry[extname] = mod
  }
end

.support?(file_name) ⇒ Boolean

return true if file_name supported by LangScan.

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/langscan.rb', line 118

def support?(file_name)
  extname = File.extname(file_name)
  LangScanRegistry.include?(extname)
end

.validate_module(mod) ⇒ Object

validate that mod is a LangScan module.



40
41
42
43
44
45
46
47
48
49
# File 'lib/langscan.rb', line 40

def validate_module(mod)
  common_methods = [:name, :abbrev, :scan]
  safe_characters = "[a-z]+"
  common_methods.each {|method|
    raise "#{mod.to_s} lacks #{method}" unless mod.respond_to?(method)
  }
  unless /^#{safe_characters}$/.match(mod.abbrev)
    raise "#{mod.to_s} invalid abbreviation: #{mod.abbrev}"
  end
end