bug 401928: use IValidator to validate file instead of loadstring in for
formatter.
diff --git a/libraries/luaformatter/formatter.lua b/libraries/luaformatter/formatter.lua
index 4b4e2ab..f4de13f 100644
--- a/libraries/luaformatter/formatter.lua
+++ b/libraries/luaformatter/formatter.lua
@@ -322,10 +322,6 @@
 --------------------------------------------------------------------------------
 local function getindentlevel(source, indenttable)
 
-  if not loadstring(source, 'CheckingFormatterSource') then
-    return
-  end
-
   -----------------------------------------------------------------------------
   -- Walk through AST
   --
@@ -514,10 +510,6 @@
     source = table.concat({COMMENT, source})
   end
 
-  -- Check code validity
-  local status, message = loadstring(source,"isCodeValid")
-  if not status then return status, message end
-
   --
   -- Seek for delimiters positions
   --
diff --git a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/internal/editor/formatter/LuaFormatter.java b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/internal/editor/formatter/LuaFormatter.java
index e5fea00..67caf2d 100644
--- a/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/internal/editor/formatter/LuaFormatter.java
+++ b/plugins/org.eclipse.ldt.ui/src/org/eclipse/ldt/ui/internal/editor/formatter/LuaFormatter.java
@@ -12,22 +12,27 @@
 
 import java.util.Map;
 
+import org.eclipse.core.resources.IProject;
 import org.eclipse.dltk.formatter.AbstractScriptFormatter;
 import org.eclipse.dltk.ui.formatter.FormatterException;
+import org.eclipse.dltk.ui.formatter.IScriptFormatterExtension;
 import org.eclipse.dltk.ui.text.util.TabStyle;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.Document;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.IRegion;
+import org.eclipse.ldt.core.grammar.IGrammar;
+import org.eclipse.ldt.core.grammar.ILuaSourceValidator;
 import org.eclipse.ldt.core.internal.formatter.LuaFormatterException;
 import org.eclipse.ldt.core.internal.formatter.LuaFormatterModule;
+import org.eclipse.ldt.core.internal.grammar.LuaGrammarManager;
 import org.eclipse.ldt.ui.internal.Activator;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.text.edits.ReplaceEdit;
 import org.eclipse.text.edits.TextEdit;
 
-public class LuaFormatter extends AbstractScriptFormatter {
+public class LuaFormatter extends AbstractScriptFormatter implements IScriptFormatterExtension {
 	static final String ID = "org.eclipse.ldt.formatter"; //$NON-NLS-1$
 	private final TabStyle tabPolicy;
 	private final int tabSize;
@@ -35,6 +40,7 @@
 	private final String delimiter;
 	private final String tabulation;
 	private final boolean formatTableValues;
+	private IProject project;
 
 	private static final LuaFormatterModule LUA_FORMAT_MODULE = new LuaFormatterModule();
 
@@ -70,11 +76,29 @@
 		}
 	}
 
+	private ILuaSourceValidator getValidator() {
+		// Get grammar
+		IGrammar grammar = LuaGrammarManager.getDefaultGrammarFor(project);
+		if (grammar != null)
+			return grammar.getValidator();
+		return null;
+	}
+
 	/**
 	 * @see org.eclipse.dltk.ui.formatter.IScriptFormatter#format(String, int, int, int)
 	 */
 	@Override
 	public TextEdit format(final String source, final int offset, final int length, final int indentationLevel) throws FormatterException {
+
+		// valid lua code
+		ILuaSourceValidator validator = getValidator();
+		if (validator != null) {
+			boolean valid = validator.valid(source);
+			if (!valid) {
+				throw new FormatterException("can not format file with error :" + validator.getErrorMessage()); //$NON-NLS-1$
+			}
+		}
+
 		/*
 		 * Format given source code
 		 */
@@ -141,4 +165,12 @@
 	public int detectIndentationLevel(final IDocument document, final int offset) {
 		return 0;
 	}
+
+	/**
+	 * @see org.eclipse.dltk.ui.formatter.IScriptFormatterExtension#initialize(org.eclipse.core.resources.IProject)
+	 */
+	@Override
+	public void initialize(IProject p) {
+		project = p;
+	}
 }
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 8d51b2b..b30085d 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
@@ -15,11 +15,7 @@
 import java.util.Map;
 
 import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.ProjectScope;
 import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.Platform;
-import org.eclipse.core.runtime.preferences.IScopeContext;
-import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.dltk.ast.ASTNode;
 import org.eclipse.dltk.ast.parser.AbstractSourceParser;
 import org.eclipse.dltk.ast.parser.IModuleDeclaration;
@@ -38,8 +34,6 @@
 import org.eclipse.ldt.core.grammar.IGrammar;
 import org.eclipse.ldt.core.grammar.ILuaSourceValidator;
 import org.eclipse.ldt.core.internal.Activator;
-import org.eclipse.ldt.core.internal.LuaLanguageToolkit;
-import org.eclipse.ldt.core.internal.PreferenceInitializer;
 import org.eclipse.ldt.core.internal.ast.models.LuaDLTKModelUtils;
 import org.eclipse.ldt.core.internal.ast.models.api.LuaFileAPI;
 import org.eclipse.ldt.core.internal.ast.models.common.LuaSourceRoot;
@@ -206,19 +200,8 @@
 	}
 
 	private ILuaSourceValidator getValidator(IProject project) throws CoreException {
-		// Create context
-		IScopeContext[] context;
-		if (project != null)
-			context = new IScopeContext[] { new ProjectScope(project), InstanceScope.INSTANCE };
-		else
-			context = new IScopeContext[] { InstanceScope.INSTANCE };
-
-		// Get grammarName
-		String grammarName = Platform.getPreferencesService().getString(LuaLanguageToolkit.getDefault().getPreferenceQualifier(),
-				PreferenceInitializer.GRAMMAR_DEFAULT_ID, PreferenceInitializer.GRAMMAR_DEFAULT_ID_VALUE, context);
-
 		// Get grammar
-		IGrammar grammar = LuaGrammarManager.getAvailableGrammar(grammarName);
+		IGrammar grammar = LuaGrammarManager.getDefaultGrammarFor(project);
 		if (grammar != null)
 			return grammar.getValidator();
 		return null;
diff --git a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/grammar/LuaGrammarManager.java b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/grammar/LuaGrammarManager.java
index 1aea8ae..b0cba97 100644
--- a/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/grammar/LuaGrammarManager.java
+++ b/plugins/org.eclipse.ldt/src/org/eclipse/ldt/core/internal/grammar/LuaGrammarManager.java
@@ -13,9 +13,12 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ProjectScope;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IConfigurationElement;
 import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.preferences.IScopeContext;
 import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.ldt.core.buildpath.LuaExecutionEnvironment;
 import org.eclipse.ldt.core.grammar.IGrammar;
@@ -111,19 +114,14 @@
 		return grammars;
 	}
 
-	public static IGrammar getDefaultGrammar() {
-		// get in preference the default grammar
-		ScopedPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, LuaLanguageToolkit.getDefault()
-				.getPreferenceQualifier());
-		String defaultGrammarID = preferenceStore.getString(PreferenceInitializer.GRAMMAR_DEFAULT_ID);
-
+	private static IGrammar getGrammarWithLua51GrammarFallback(String name) {
 		// check the grammar is available
 		try {
-			IGrammar defaultGrammar = getAvailableGrammar(defaultGrammarID);
+			IGrammar defaultGrammar = getAvailableGrammar(name);
 			if (defaultGrammar != null)
 				return defaultGrammar;
 		} catch (CoreException e) {
-			String message = String.format("The default grammar %s is not available.", defaultGrammarID);//$NON-NLS-1$
+			String message = String.format("The default grammar %s is not available.", name);//$NON-NLS-1$
 			Activator.logWarning(message, e);
 		}
 
@@ -137,6 +135,31 @@
 		}
 	}
 
+	public static IGrammar getDefaultGrammar() {
+		// get in preference the default grammar
+		ScopedPreferenceStore preferenceStore = new ScopedPreferenceStore(InstanceScope.INSTANCE, LuaLanguageToolkit.getDefault()
+				.getPreferenceQualifier());
+		String defaultGrammarID = preferenceStore.getString(PreferenceInitializer.GRAMMAR_DEFAULT_ID);
+
+		return getGrammarWithLua51GrammarFallback(defaultGrammarID);
+	}
+
+	public static IGrammar getDefaultGrammarFor(IProject project) {
+		// Create context
+		IScopeContext[] context;
+		if (project != null)
+			context = new IScopeContext[] { new ProjectScope(project), InstanceScope.INSTANCE };
+		else
+			context = new IScopeContext[] { InstanceScope.INSTANCE };
+
+		// Get grammarName
+		String grammarName = Platform.getPreferencesService().getString(LuaLanguageToolkit.getDefault().getPreferenceQualifier(),
+				PreferenceInitializer.GRAMMAR_DEFAULT_ID, PreferenceInitializer.GRAMMAR_DEFAULT_ID_VALUE, context);
+
+		// Get grammar
+		return getGrammarWithLua51GrammarFallback(grammarName);
+	}
+
 	public static IGrammar getDefaultGrammarFor(LuaExecutionEnvironment ee) {
 		// if ee has no grammar defined use the default one
 		if (ee == null || ee.getLuaGrammar() == null)