Module: LangScan::Lua

Defined in:
lib/langscan/lua.rb

Constant Summary collapse

Pattern =
[
  [:comment, '--.*'],
  [:string, '\\[\\[', '\\]\\]'],
  [:string, '""'],
  [:string, '"', '[^\\\\]"'],
  [:string, "''"],
  [:string, "'", "[^\\\\]'"],
  [:floating, '\\d+\\.\\d+(?:[eE]-?\\d+)?'],
  [:integer, '\\d+'],
  [:ident, "[a-zA-Z_]\\w*"],
  [:punct, '[*+-/^=<>(){}\\[\\];:,\\.]'],
  [:punct, '(?:~=|<=|>=|==|\\.\\.|\\.\\.\\.)'],
]
Types =
[]
Keywords =
%w(and break do else elseif end false for function if in local
nil not or repeat return then true until while)
IdentType =
Hash.new(:ident)

Class Method Summary collapse

Class Method Details

.abbrevObject



22
23
24
# File 'lib/langscan/lua.rb', line 22

def abbrev
  "lua"
end

.extnamesObject



26
27
28
# File 'lib/langscan/lua.rb', line 26

def extnames
  [".lua"]
end

.nameObject



18
19
20
# File 'lib/langscan/lua.rb', line 18

def name
  "Lua"
end

.parse_token(t, new_tokens) ⇒ Object



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

def parse_token(t, new_tokens)
  if t.type == :ident
    t.type = :funcall
  end

  last_token = new_tokens.last
  return if last_token.nil?

  return unless t.type == :punct and last_token.type == :funcall

  if t.text == ':=' || t.text == '='
    last_token.type = :fundef
  end
end

.scan(input, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/langscan/lua.rb', line 64

def scan(input, &block)
  pm = LangScan::PairMatcher.new(3,2,2,2)
  pm.define_intertoken_fragment :comment, nil
  pm.define_pair :paren, :punct, "(", :punct, ")"
  pm.define_pair :brace, :punct, "{", :punct, "}"
  pm.define_pair :bracket, :punct, "[", :punct, "]"

  tokens = Array.new
  scanner = EasyScanner.new(Pattern, Types, Keywords)
  scanner.scan(input) do |t|
    tokens << [t.type, t.text, t.lineno, nil, t.byteno, nil, nil, nil]
  end

  def tokens.get_token
    self.shift
  end

  pm.parse(tokens, lambda {|f|
    if f.type == :ident
      f.type = IdentType[f.text]
    end
    yield f
  }) {|pair|
    if (pair.pair_type == :paren)
      fun = pair.around_open(-1)
      if (fun)
        if (fun.type == :ident)
          f = pair.around_open(-2)
          if (f && f.type == :keyword && f.text == 'function')
            fun.type = :fundef
          else
            fun.type = :funcall
          end
        elsif (fun.type == :keyword && fun.text == 'function')
          f = pair.around_open(-2)
          if (f && f.type == :punct && f.text == '=')
            f = pair.around_open(-3)
            if (f && f.type == :ident)
              f.type = :fundef
            end
          end
        end
      end
    end
  }
end