~ivle-dev/ivle/codemirror

« back to all changes in this revision

Viewing changes to ivle/webapp/filesystem/browser/media/codemirror/contrib/lua/js/parselua.js

  • Committer: David Coles
  • Date: 2010-05-31 10:38:53 UTC
  • Revision ID: coles.david@gmail.com-20100531103853-8xypjpracvwy0qt4
Editor: Added CodeMirror-0.67 Javascript code editor source from 
http://marijn.haverbeke.nl/codemirror/ (zlib-style licence)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 Simple parser for LUA 
 
3
 Written for Lua 5.1, based on parsecss and other parsers. 
 
4
 features: highlights keywords, strings, comments (no leveling supported! ("[==[")),tokens, basic indenting
 
5
 
 
6
 to make this parser highlight your special functions pass table with this functions names to parserConfig argument of creator,
 
7
        
 
8
 parserConfig: ["myfunction1","myfunction2"],
 
9
 */
 
10
 
 
11
 
 
12
function findFirstRegexp(words) {
 
13
    return new RegExp("^(?:" + words.join("|") + ")", "i");
 
14
}
 
15
 
 
16
function matchRegexp(words) {
 
17
    return new RegExp("^(?:" + words.join("|") + ")$", "i");
 
18
}
 
19
 
 
20
 
 
21
 
 
22
var luaCustomFunctions= matchRegexp([]);
 
23
 
 
24
function configureLUA(parserConfig){
 
25
        if(parserConfig)
 
26
        luaCustomFunctions= matchRegexp(parserConfig);
 
27
}
 
28
 
 
29
 
 
30
//long list of standard functions from lua manual
 
31
var luaStdFunctions = matchRegexp([
 
32
"_G","_VERSION","assert","collectgarbage","dofile","error","getfenv","getmetatable","ipairs","load","loadfile","loadstring","module","next","pairs","pcall","print","rawequal","rawget","rawset","require","select","setfenv","setmetatable","tonumber","tostring","type","unpack","xpcall",
 
33
 
 
34
"coroutine.create","coroutine.resume","coroutine.running","coroutine.status","coroutine.wrap","coroutine.yield",
 
35
 
 
36
"debug.debug","debug.getfenv","debug.gethook","debug.getinfo","debug.getlocal","debug.getmetatable","debug.getregistry","debug.getupvalue","debug.setfenv","debug.sethook","debug.setlocal","debug.setmetatable","debug.setupvalue","debug.traceback",
 
37
 
 
38
"close","flush","lines","read","seek","setvbuf","write",
 
39
 
 
40
"io.close","io.flush","io.input","io.lines","io.open","io.output","io.popen","io.read","io.stderr","io.stdin","io.stdout","io.tmpfile","io.type","io.write",
 
41
 
 
42
"math.abs","math.acos","math.asin","math.atan","math.atan2","math.ceil","math.cos","math.cosh","math.deg","math.exp","math.floor","math.fmod","math.frexp","math.huge","math.ldexp","math.log","math.log10","math.max","math.min","math.modf","math.pi","math.pow","math.rad","math.random","math.randomseed","math.sin","math.sinh","math.sqrt","math.tan","math.tanh",
 
43
 
 
44
"os.clock","os.date","os.difftime","os.execute","os.exit","os.getenv","os.remove","os.rename","os.setlocale","os.time","os.tmpname",
 
45
 
 
46
"package.cpath","package.loaded","package.loaders","package.loadlib","package.path","package.preload","package.seeall",
 
47
 
 
48
"string.byte","string.char","string.dump","string.find","string.format","string.gmatch","string.gsub","string.len","string.lower","string.match","string.rep","string.reverse","string.sub","string.upper",
 
49
 
 
50
"table.concat","table.insert","table.maxn","table.remove","table.sort"
 
51
]);
 
52
 
 
53
 
 
54
 
 
55
 var luaKeywords = matchRegexp(["and","break","elseif","false","nil","not","or","return",
 
56
                                "true","function", "end", "if", "then", "else", "do", 
 
57
                                "while", "repeat", "until", "for", "in", "local" ]);
 
58
 
 
59
 var luaIndentKeys = matchRegexp(["function", "if","repeat","for","while", "[\(]", "{"]);
 
60
 var luaUnindentKeys = matchRegexp(["end", "until", "[\)]", "}"]);
 
61
 
 
62
 var luaUnindentKeys2 = findFirstRegexp(["end", "until", "[\)]", "}"]);
 
63
 var luaMiddleKeys = findFirstRegexp(["else","elseif"]);
 
64
 
 
65
 
 
66
 
 
67
var LUAParser = Editor.Parser = (function() {
 
68
  var tokenizeLUA = (function() {
 
69
    function normal(source, setState) {
 
70
      var ch = source.next();
 
71
 
 
72
   if (ch == "-" && source.equals("-")) {
 
73
        source.next();
 
74
                setState(inSLComment);
 
75
        return null;
 
76
      } 
 
77
        else if (ch == "\"" || ch == "'") {
 
78
        setState(inString(ch));
 
79
        return null;
 
80
      }
 
81
    if (ch == "[" && (source.equals("[") || source.equals("="))) {
 
82
        var level = 0;
 
83
                while(source.equals("=")){
 
84
                        level ++;
 
85
                        source.next();
 
86
                }
 
87
                if(! source.equals("[") )
 
88
                        return "lua-error";             
 
89
                setState(inMLSomething(level,"lua-string"));
 
90
        return null;
 
91
      } 
 
92
            
 
93
      else if (ch == "=") {
 
94
        if (source.equals("="))
 
95
                source.next();
 
96
        return "lua-token";
 
97
      }
 
98
        
 
99
      else if (ch == ".") {
 
100
        if (source.equals("."))
 
101
                source.next();
 
102
        if (source.equals("."))
 
103
                source.next();
 
104
        return "lua-token";
 
105
      }
 
106
     
 
107
      else if (ch == "+" || ch == "-" || ch == "*" || ch == "/" || ch == "%" || ch == "^" || ch == "#" ) {
 
108
        return "lua-token";
 
109
      }
 
110
      else if (ch == ">" || ch == "<" || ch == "(" || ch == ")" || ch == "{" || ch == "}" || ch == "[" ) {
 
111
        return "lua-token";
 
112
      }
 
113
      else if (ch == "]" || ch == ";" || ch == ":" || ch == ",") {
 
114
        return "lua-token";
 
115
      }
 
116
      else if (source.equals("=") && (ch == "~" || ch == "<" || ch == ">")) {
 
117
        source.next();
 
118
        return "lua-token";
 
119
      }
 
120
 
 
121
     else if (/\d/.test(ch)) {
 
122
        source.nextWhileMatches(/[\w.%]/);
 
123
        return "lua-number";
 
124
      }
 
125
      else {
 
126
        source.nextWhileMatches(/[\w\\\-_.]/);
 
127
        return "lua-identifier";
 
128
      }
 
129
    }
 
130
 
 
131
function inSLComment(source, setState) {
 
132
      var start = true;
 
133
        var count=0;
 
134
      while (!source.endOfLine()) {
 
135
                var ch = source.next();
 
136
                var level = 0;
 
137
                if ((ch =="[") && start)
 
138
                        while(source.equals("=")){
 
139
                        source.next();
 
140
                        level++;
 
141
                        }
 
142
                        if (source.equals("[")){
 
143
                                setState(inMLSomething(level,"lua-comment"));
 
144
                                return null;
 
145
                                }
 
146
                 start = false; 
 
147
        }
 
148
        setState(normal);               
 
149
     return "lua-comment";
 
150
        
 
151
    }
 
152
 
 
153
    function inMLSomething(level,what) {
 
154
        //wat sholud be "lua-string" or "lua-comment", level is the number of "=" in opening mark.
 
155
        return function(source, setState){
 
156
      var dashes = 0;
 
157
      while (!source.endOfLine()) {
 
158
        var ch = source.next();
 
159
        if (dashes == level+1 && ch == "]" ) {
 
160
          setState(normal);
 
161
          break;
 
162
        }
 
163
                if (dashes == 0) 
 
164
                        dashes = (ch == "]") ? 1:0;
 
165
                else
 
166
                        dashes = (ch == "=") ? dashes + 1 : 0;
 
167
        }
 
168
      return what;
 
169
         }
 
170
    }
 
171
 
 
172
 
 
173
    function inString(quote) {
 
174
      return function(source, setState) {
 
175
        var escaped = false;
 
176
        while (!source.endOfLine()) {
 
177
          var ch = source.next();
 
178
          if (ch == quote && !escaped)
 
179
            break;
 
180
          escaped = !escaped && ch == "\\";
 
181
        }
 
182
        if (!escaped)
 
183
          setState(normal);
 
184
        return "lua-string";
 
185
      };
 
186
    }
 
187
 
 
188
    return function(source, startState) {
 
189
      return tokenizer(source, startState || normal);
 
190
    };
 
191
  })();
 
192
 
 
193
  function indentLUA(indentDepth, base) {
 
194
    return function(nextChars) {
 
195
 
 
196
      var closing = (luaUnindentKeys2.test(nextChars) || luaMiddleKeys.test(nextChars));
 
197
 
 
198
        
 
199
        return base + ( indentUnit * (indentDepth - (closing?1:0)) );
 
200
    };
 
201
  }
 
202
 
 
203
  
 
204
function parseLUA(source,basecolumn) {
 
205
     basecolumn = basecolumn || 0;
 
206
    
 
207
        var tokens = tokenizeLUA(source);
 
208
    var indentDepth = 0;
 
209
 
 
210
    var iter = {
 
211
      next: function() {
 
212
        var token = tokens.next(), style = token.style, content = token.content;
 
213
 
 
214
 
 
215
        
 
216
        if (style == "lua-identifier" && luaKeywords.test(content)){
 
217
          token.style = "lua-keyword";
 
218
        }       
 
219
        if (style == "lua-identifier" && luaStdFunctions.test(content)){
 
220
          token.style = "lua-stdfunc";
 
221
        }
 
222
        if (style == "lua-identifier" && luaCustomFunctions.test(content)){
 
223
          token.style = "lua-customfunc";
 
224
        }
 
225
 
 
226
        if (luaIndentKeys.test(content))
 
227
        indentDepth++;
 
228
        else if (luaUnindentKeys.test(content))
 
229
                indentDepth--;
 
230
        
 
231
 
 
232
        if (content == "\n")
 
233
          token.indentation = indentLUA( indentDepth, basecolumn);
 
234
 
 
235
        return token;
 
236
      },
 
237
 
 
238
      copy: function() {
 
239
        var  _tokenState = tokens.state, _indentDepth = indentDepth;
 
240
        return function(source) {
 
241
          tokens = tokenizeLUA(source, _tokenState);
 
242
      
 
243
          indentDepth = _indentDepth;
 
244
          return iter;
 
245
        };
 
246
      }
 
247
    };
 
248
    return iter;
 
249
  }
 
250
 
 
251
  return {make: parseLUA, configure:configureLUA, electricChars: "delf})"};   //en[d] els[e] unti[l] elsei[f]  // this should be taken from Keys keywords
 
252
})();
 
253