bug 401928 : Use a different metalua compiler according to the grammar
diff --git a/plugins/org.eclipse.ldt/script/local/javamodelsbuilder.lua b/plugins/org.eclipse.ldt/script/local/javamodelsbuilder.lua
index b381778..1c8eb38 100644
--- a/plugins/org.eclipse.ldt/script/local/javamodelsbuilder.lua
+++ b/plugins/org.eclipse.ldt/script/local/javamodelsbuilder.lua
@@ -12,21 +12,64 @@
 
 require 'metalua.loader'
 local compiler = require 'metalua.compiler'
-local mlc = compiler.new()
-
 local javamodelfactory = require 'javamodelfactory'
 
+local mlc51, mlc52
+
 -- Just redefining classic print, as there is a flush problem calling it from Java
 local print = function(...) print(...) io.flush() end
 
 local M = {}
 
+function M.newMLC51()
+  local function lexer51()
+    local generic_lexer = require 'metalua.grammar.lexer'
+    local lexer = generic_lexer.lexer :clone()
+
+    local keywords = {
+      "and", "break", "do", "else", "elseif",
+      "end", "false", "for", "function",
+      "if",
+      "in", "local", "nil", "not", "or", "repeat",
+      "return", "then", "true", "until", "while",
+      "...", "..", "==", ">=", "<=", "~=",
+      "+{", "-{"
+    }
+
+    for _, w in ipairs(keywords) do lexer :add (w) end
+
+    return lexer
+  end
+
+  local newmlc = compiler.new()
+  newmlc.parser.lexer = lexer51()
+  newmlc.parser.stat:del("goto")
+  newmlc.parser.stat:del("::")
+
+  return newmlc
+end
+
 ---
 -- Build Java Model from source code
 --
 -- @param #string source Code to parse
 -- @param LuaSourceRoot, DLTK node, root of DLTK AST
-function M.build(source, modulename, root)
+function M.build(source, modulename, root, luaGrammar)
+
+  local mlc
+  if (luaGrammar == "lua-5.1") then
+    if (not mlc51) then
+      mlc51 = M.newMLC51()
+    end
+
+    mlc = mlc51
+  else
+    if (not mlc52) then
+      mlc52 = compiler.new()
+    end
+
+    mlc = mlc52
+  end
 
   -- manage shebang
   if source then source = source:gsub("^(#.-\n)", function (s) return string.rep(' ',string.len(s)) end) end
diff --git a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/ast/parser/LuaSourceParser.java b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/ast/parser/LuaSourceParser.java
index b30085d..238a150 100644
--- a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/ast/parser/LuaSourceParser.java
+++ b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/ast/parser/LuaSourceParser.java
@@ -122,7 +122,11 @@
 				}
 
 				// Valid source code
-				ILuaSourceValidator sourceValidator = getValidator(getProject(input));
+				IGrammar grammar = getGrammar(getProject(input));
+				ILuaSourceValidator sourceValidator = null;
+				if (grammar != null) {
+					sourceValidator = grammar.getValidator();
+				}
 				if (sourceValidator == null) {
 					Activator.logWarning(NLS.bind("No validator found for input {0}.", input.getFileName())); //$NON-NLS-1$
 					module.setProblem(1, 1, 0, 0, "No validator have have been found for this file."); //$NON-NLS-1$
@@ -134,7 +138,7 @@
 
 					// Build AST
 					if (cleanedSource != null)
-						astBuilder.buildAST(cleanedSource, moduleName, module);
+						astBuilder.buildAST(cleanedSource, moduleName, module, grammar.getName());
 
 					// Fix AST
 					module.traverse(new EncodingVisitor(fixer));
@@ -199,11 +203,11 @@
 		return module;
 	}
 
-	private ILuaSourceValidator getValidator(IProject project) throws CoreException {
+	private IGrammar getGrammar(IProject project) throws CoreException {
 		// Get grammar
 		IGrammar grammar = LuaGrammarManager.getDefaultGrammarFor(project);
 		if (grammar != null)
-			return grammar.getValidator();
+			return grammar;
 		return null;
 	}
 
diff --git a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/ast/parser/ModelsBuilderLuaModule.java b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/ast/parser/ModelsBuilderLuaModule.java
index d3ac6ef..2be1ef2 100644
--- a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/ast/parser/ModelsBuilderLuaModule.java
+++ b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/ast/parser/ModelsBuilderLuaModule.java
@@ -34,7 +34,7 @@
 

 	private LuaState lua = null;

 

-	public synchronized void buildAST(final String source, final String modulename, final LuaSourceRoot root) {

+	public synchronized void buildAST(final String source, final String modulename, final LuaSourceRoot root, final String luaGrammar) {

 		if (lua == null)

 			lua = loadLuaModule();

 

@@ -43,7 +43,8 @@
 		lua.pushString(source);

 		lua.pushString(modulename);

 		lua.pushJavaObject(root);

-		lua.call(3, 0);

+		lua.pushString(luaGrammar);

+		lua.call(4, 0);

 		lua.pop(1);

 

 		// lua.close();