Ejemplo n.º 1
0
 public override void IndentLines(ITextEditor editor, int beginLine, int endLine)
 {
     //DocumentAccessor acc = new DocumentAccessor(editor.Document, beginLine, endLine);
     //CSharpIndentationStrategy indentStrategy = new CSharpIndentationStrategy();
     //indentStrategy.IndentationString = editor.Options.IndentationString;
     //indentStrategy.Indent(acc, true);
     SharpLua.Visitors.NonModifiedAstBeautifier b = null;
     try
     {
         Lexer l = new Lexer();
         Parser p = new Parser(l.Lex(editor.Document.Text));
         SharpLua.Ast.Chunk c = p.Parse();
         b = new SharpLua.Visitors.NonModifiedAstBeautifier();
         //SharpLua.Visitors.ExactReconstruction b = new SharpLua.Visitors.ExactReconstruction();
         b.options.Tab = editor.Options.IndentationString;
         b.options.TabsToSpaces = editor.Options.ConvertTabsToSpaces;
         int off = editor.Caret.Offset;
         //editor.Document.Text = b.Reconstruct(c);
         editor.Document.Text = b.Beautify(c);
         editor.Caret.Offset = off >= editor.Document.TextLength ? 0 : off;
     }
     catch (LuaSourceException ex)
     {
         LoggingService.Warn("Error parsing document: " + System.IO.Path.GetFileName(ex.GenerateMessage(editor.FileName)));
     }
     catch (System.Exception ex)
     {
         LoggingService.Error("Error formatting document:", ex);
         MessageBox.Show(b.index.ToString() + " "+ b.tok.Count);
         MessageBox.Show("Error formatting Lua script: " + ex.ToString() + "\r\n\r\nPlease report this to the SharpLua GitHub page so it can get fixed");
     }
 }
Ejemplo n.º 2
0
 static void Main(string[] args)
 {
     try
     {
         Lexer l = new Lexer();
         Parser p = new Parser(l.Lex(File.ReadAllText(args[0])));
         Chunk c = p.Parse();
         var doc = ExtractDocumentationComments.Extract(c);
         File.WriteAllText(Path.ChangeExtension(args[0], ".xml"),
             Documentation.Write(doc));
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }
Ejemplo n.º 3
0
        public static int lua_load(LuaState L, lua_Reader reader, object data,
                                   CharPtr chunkname)
        {
            ZIO z = new ZIO();
            int status;
            lua_lock(L);
            if (chunkname == null) chunkname = "?";

            #if OVERRIDE_LOAD || true
            //#if false
            if (data is LoadS)
            {
                LoadS d = data as LoadS;
                if (d.size > 0 && d.s.chars[0] != LUA_SIGNATURE[0]) // if its not binary
                {
                    Lexer l = new Lexer();
                    try
                    {
                        //Console.WriteLine(d.s);
                        TokenReader tr = l.Lex(d.s);
                        Parser p = new Parser(tr);
                        Ast.Chunk c = p.Parse();

                        Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                        string s = lco.Format(c);
                        d.s = s;
                        d.size = (lu_mem)s.Length;
                    }
                    catch (LuaSourceException ex)
                    {
                        throw ex;
                    }
                }
                else
                {
                    d.s.index = 0;

                    // Why isn't the size equal to the chars.Length?
                    Debug.WriteLine("Binary data: d.size=" + d.size + " d.s.chars.Length=" + d.s.chars.Length);
                    Debug.WriteLine("Equal: " + (d.size == d.s.chars.Length));
                    //Debug.Assert(d.size == d.s.chars.Length);
                    d.size = (uint)d.s.chars.Length;
                }
            }
            else if (data is LoadF)
            {
                LoadF lf = data as LoadF;

                if (lf.f.ReadByte() != LUA_SIGNATURE[0]) // if its not binary
                {
                    lf.f.Position = 0;
                    MemoryStream ms = new MemoryStream();
                    while (lf.f.Position < lf.f.Length)
                        ms.WriteByte((byte)lf.f.ReadByte());
                    ms.Position = 0;

                    // not binary file
                    ms.Position = 0;
                    StringBuilder sb = new StringBuilder();
                    while (ms.Position < ms.Length)
                        sb.Append((char)ms.ReadByte());

                    try
                    {
                        Lexer l = new Lexer();
                        TokenReader tr = l.Lex(sb.ToString());
                        Parser p = new Parser(tr);
                        Ast.Chunk c = p.Parse();
                        Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                        string s = lco.Format(c);
                        ms = new MemoryStream();
                        // TODO: there HAS to be a better way...
                        foreach (char c2 in s)
                            ms.WriteByte((byte)c2);
                        ms.Position = 0;
                        lf.f = ms;
                    }
                    catch (LuaSourceException ex)
                    {
                        lua_pushstring(L, ex.GenerateMessage(chunkname));
                        return 1;
                        //throw ex;
                    }
                }
                else
                {
                    lf.f.Position = 0; // reset the read character
                }
            }
            #endif
            luaZ_init(L, z, reader, data);
            status = luaD_protectedparser(L, z, chunkname);
            lua_unlock(L);
            if (data is LoadF)
            {
                LoadF f = data as LoadF;
                if (f.f != null)
                {
                    f.f.Close();
                    f.f.Dispose();
                }
            }
            return status;
        }
Ejemplo n.º 4
0
 private void updateDocument(string s)
 {
     try
     {
         Lexer l = new Lexer();
         Parser p = new Parser(l.Lex(s));
         p.ThrowParsingErrors = false;
         SharpLua.Ast.Chunk c = p.Parse();
         this.l.items.AddRange(AstExtractor.ExtractSymbols(c));
         var cmts = SharpLua.XmlDocumentation.ExtractDocumentationComments.Extract(c);
         if (false)
             LoggingService.Info(cmts.Count);
         DocumentationManager.Add(cmts);
     }
     catch (System.Exception ex)
     {
         LoggingService.Warn("Error parsing document");
     }
 }
Ejemplo n.º 5
0
		/// <summary>
		/// Checks the gwz file for:
		/// - Lua file
		/// - Info file
		/// - Lua code for right format
		/// - Media files for completeness.
		/// Errors could be found in the list Errors (with line and col, if known).
		/// </summary>
        /// <param name='zip'>
		/// Zip file for later use.
		/// </param>
		/// <returns>
		/// <c>true</c>, if the checked gwz was correct and complete, <c>false</c> otherwise.
		/// </returns>
		public bool CheckGWZ ( ZipFile zip, Cartridge cartridge )
		{
			// Get input stream for Lua file
			string luaCode = getLuaCode(zip,filenameGwz);
            
			// Now parse the Lua file
			Lexer lexer = new Lexer ();
			// Split the Lua code into tokens
			TokenReader tr = null;
			try
			{
				tr = lexer.Lex ( luaCode );
			}
			catch ( LuaSourceException e )
			{
				// There are something strange in the Lua file, so that the lexer couldn't create tokens
				Errors.Add ( new Error ( e.Line, e.Column, e.Message ) );
				return false;
			}
			// Jump to the first token
			tr.p = 0;
			// Save cartridge name for later use (the last is allways End of Stream token)
			cartridge.Variable = tr.tokens[tr.tokens.Count-2].Data;
			// Now parse the tokens, if it is all right with the Lua file
			SharpLua.Parser parser = new SharpLua.Parser ( tr );
			SharpLua.Ast.Chunk c = null;
			try
			{
				c = parser.Parse();
			}
			catch ( Exception e )
			{
				// Was there an error?
				if ( parser.Errors.Count > 0 )
				{
					foreach ( SharpLua.LuaSourceException error in parser.Errors )
						Errors.Add ( new Error ( error.Line, error.Column, error.Message ) );
					return false;
				}
			}
			// Now we are shure, that Lua code is allright, so we could go on
			// Create a dictionary, so we could get fast connection variable <-> media list entry
			Dictionary<String,Media> mediaDict = new Dictionary<String,Media> ();
			// Go through all statements and search cartridge info and all media definitions
			for ( int i = 0; i < c.Body.Count; i++ )
			{
				// We only check assignments in the parser result
				if ( c.Body[i] is AssignmentStatement )
				{
					AssignmentStatement statement = (AssignmentStatement) c.Body[i];
					// First get the left side ...
					Expression statementLeft = statement.Lhs[0];
					String left = getExpressionAsString ( statementLeft );
					// ... and than the right side
					Expression statementRight = statement.Rhs[0];
					String right = getExpressionAsString ( statementRight );
					// Is it an entry for the cartridge
					if ( left.Contains ( "." ) && cartridge.Variable.Equals ( left.Split ( '.' )[0] ) )
					{
						string key = left.Split ( '.' )[1];

						switch ( key )
						{
    						case "Id":
	    						cartridge.Id = right;
		    					break;
			    			case "Name":
				    			cartridge.Name = right;
					    		break;
						    case "Description":
    							cartridge.Description = removeLongString(right);
	    						break;
		    				case "Activity":
			    				cartridge.Activity = right;
				    			break;
					    	case "StartingLocationDescription":
						    	cartridge.StartingLocationDescription = removeLongString(right);
							    break;
    						case "StartingLocation":
                                if (statementRight is SharpLua.Ast.Expression.MemberExpr)
                                {
                                    // Play anywhere cartridge with starting location at 360.0 / 360.0 / 360.0
                                    cartridge.Latitude = 360.0;
                                    cartridge.Longitude = 360.0;
                                    cartridge.Altitude = 360.0;
                                }
                                else 
		    					    getZonePoints ( (SharpLua.Ast.Expression.CallExpr) statementRight, ref cartridge.Latitude, ref cartridge.Longitude, ref cartridge.Altitude );
			    				break;
				    		case "Version":
					    		cartridge.Version = right;
						    	break;
    						case "Company":
	    						cartridge.Company = right;
		    					break;
			    			case "Author":
				    			cartridge.Author = right;
					    		break;
						    case "BuilderVersion":
							    cartridge.BuilderVersion = right;
    							break;
	    					case "CreateDate":
		    					cartridge.CreateDate = right;
			    				break;
				    		case "PublishDate":
					    		cartridge.PublishDate = right;
						    	break;
    						case "UpdateDate":
	    						cartridge.UpdateDate = right;
		    					break;
			    			case "LastPlayedDate":
				    			cartridge.LastPlayedDate = right;
					    		break;
						    case "TargetDevice":
    							cartridge.TargetDevice = right;
	    						break;
		    				case "TargetDeviceVersion":
			    				cartridge.TargetDeviceVersion = right;
				    			break;
					    	case "StateId":
						    	cartridge.StateId = right;
							    break;
    						case "CountryId":
	    						cartridge.CountryId = right;
		    					break;
			    			case "Visible":
				    			cartridge.Visible = right.Equals ( "true" ) ? true : false;
					    		break;
						    case "Complete":
    							cartridge.Complete = right.Equals ( "true" ) ? true : false;
	    						break;
		    				case "UseLogging":
			    				cartridge.UseLogging = right.Equals ( "true" ) ? true : false;
				    			break;
					    	case "Media":
						    	cartridge.Splash = right;
							    break;
    						case "Icon":
	    						cartridge.Icon = right;
		    					break;
			    		}
				    }
					// Is it a ZMedia definition?
					if ( right.Equals ( "Wherigo.ZMedia" ) )
					{
						Media media = new Media ();
						media.Variable = left;
						cartridge.MediaList.Add ( media );
						mediaDict.Add ( left, media );
					}
					// Is it a ZMedia entry?
					if ( left.Contains ( "." ) &&  mediaDict.ContainsKey ( left.Split ( '.' )[0] ) )
					{
						// We found an entry to a ZMedia
						string key = left.Split ( '.' )[0];
						string value = left.Split ( '.' )[1];
						// Which key did we have found?
						switch ( value )
						{
							case "Name":
								mediaDict[key].Name = right;
								break;
							case "Description":
								mediaDict[key].Description = right;
								break;
							case "AltText":
								mediaDict[key].AltText = right;
								break;
							case "Id":
								mediaDict[key].Id = right;
								break;
							case "Resources":
								getMediaResources ( (SharpLua.Ast.Expression.TableConstructorExpr) statementRight, mediaDict[key].Resources );
								break;
						}
					}
				}
			}

			// Now check, if each media has a file in the gwz
			foreach ( Media m in cartridge.MediaList )
			{
				foreach ( MediaResource resource in m.Resources )
				{
					if ( !files.Contains ( resource.Filename.ToLower () ) )
					{
						// This filename couldn't be found in the gwz file
						Errors.Add ( new Error ( 0, 0, "Media file not found: " + resource.Filename ) );
					}
				}
			}

			// If there are no errors, than return true
			return Errors.Count == 0;
		}
Ejemplo n.º 6
0
        static void TestFindImpl()
        {
            string s = @"local a = 1
local b = 2
local x, y, z = 1, 2, 3
print(a, b)
x += 1
print(z)
function print2(...)
    _G.print(...)
end
";
            Lexer l = new Lexer();
            Parser p = new Parser(l.Lex(s));
            Chunk c = p.Parse();
            Console.WriteLine(s);
            Location l1 = Refactoring.FindImplementation(c, c.Scope.GetVariable("a"));
            Console.WriteLine("impl of 'a': " + l1.Line + ":" + l1.Column);
            l1 = Refactoring.FindImplementation(c, c.Scope.GetVariable("b"));
            Console.WriteLine("impl of 'b': " + l1.Line + ":" + l1.Column);
            l1 = Refactoring.FindImplementation(c, c.Scope.GetVariable("x"));
            Console.WriteLine("impl of 'x': " + l1.Line + ":" + l1.Column);
            l1 = Refactoring.FindImplementation(c, c.Scope.GetVariable("z"));
            Console.WriteLine("impl of 'z': " + l1.Line + ":" + l1.Column);
            l1 = Refactoring.FindImplementation(c, c.Scope.GetVariable("print2"));
            Console.WriteLine("impl of 'print2': " + l1.Line + ":" + l1.Column);
        }
Ejemplo n.º 7
0
 static void TestInline()
 {
     Lexer l = new Lexer();
     Parser p = new Parser(l.Lex("local b = function(a, ...) return a + 1, ... end"));
     Chunk c = p.Parse();
     AssignmentStatement a = c.Body[0] as AssignmentStatement;
     a.Rhs[0] = Refactoring.InlineFunction(a.Rhs[0] as SharpLua.Ast.Expression.AnonymousFunctionExpr);
     Visitors.BasicBeautifier e = new Visitors.BasicBeautifier();
     a.ScannedTokens = null;
     Console.WriteLine(e.Beautify(c));
     Visitors.ExactReconstruction e2 = new Visitors.ExactReconstruction();
     Console.WriteLine(e2.Reconstruct(c));
 }
Ejemplo n.º 8
0
 private static void TestRename()
 {
     Lexer l = new Lexer();
     Parser p = new Parser(l.Lex("local a = 5; function c() print(a) end c()"));
     Chunk c = p.Parse();
     c.Scope.RenameVariable("a", "b");
     c.Scope.RenameVariable("c", "testfunc");
     Visitors.BasicBeautifier e = new Visitors.BasicBeautifier();
     Console.WriteLine(e.Beautify(c));
     Visitors.ExactReconstruction e2 = new Visitors.ExactReconstruction();
     Console.WriteLine(e2.Reconstruct(c));
 }
Ejemplo n.º 9
0
        private static void TestFindRef()
        {
            string s = @"local a = 1
local b = 2
local x, y, z = 1, 2, 3
print(a, b)
a += 3
b += a
print(b, a)
";
            Lexer l = new Lexer();
            Parser p = new Parser(l.Lex(s));
            Chunk c = p.Parse();
            Console.WriteLine(s);
            try
            {
                List<Location> l1 = Refactoring.FindReferences(c, c.Scope.GetVariable("a"));
                foreach (Location l2 in l1)
                    Console.WriteLine("Reference of 'a': " + l2.Line + ":" + l2.Column);
                l1 = Refactoring.FindReferences(c, c.Scope.GetVariable("b"));
                foreach (Location l2 in l1)
                    Console.WriteLine("Reference of 'b': " + l2.Line + ":" + l2.Column);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            TestRename();
            TestInline();
            TestFindImpl();
            TestFindRef();
            TestExtractDocComments();

            while (true)
            {
                string line = Console.ReadLine();
                try
                {
                    Lexer l = new Lexer();
                    TokenReader r = l.Lex(line);
                    //Console.WriteLine("---------------------------------");
                    foreach (Token t in r.tokens)
                    {
                        Console.WriteLine(t.Print());
                        foreach (Token t2 in t.Leading)
                            Console.WriteLine("    " + t2.Print());
                    }
                    //Console.WriteLine("- PARSER OUTPUT -");
                    Parser p = new Parser(r);
                    Chunk c = p.Parse();
                    Console.WriteLine("- Simplifying -");
                    c = (Chunk)c.Simplify();
                    Console.WriteLine("- Success! -");
                    //dump(c.Body);
                    Console.WriteLine("- Basic Beautifier (No Token Stream) -");
                    Visitors.BasicBeautifier b = new Visitors.BasicBeautifier();
                    Console.WriteLine(b.Beautify(c));
                    Console.WriteLine("- Lua Compatible -");
                    Visitors.LuaCompatibleOutput lco = new Visitors.LuaCompatibleOutput();
                    Console.WriteLine(lco.Format(c));
                    Console.WriteLine("- Exact reconstruction -");
                    Console.WriteLine(new Visitors.ExactReconstruction().Reconstruct(c));
                    Console.WriteLine("- Minified -");
                    Console.WriteLine(new Visitors.Minifier().Minify(c));
                    Console.WriteLine("- Beautifier -");
                    Console.WriteLine(new Visitors.Beautifier().Beautify(c));
                    Console.WriteLine("- NonModifiedAstBeautifer -");
                    Console.WriteLine(new Visitors.NonModifiedAstBeautifier().Beautify(c));
                    Console.WriteLine("- Misspelled Variables -");
                    List<Tuple<Variable, Variable>> vars = Refactoring.FindMisspelledVariables(c);
                    //Console.WriteLine(vars.Count);
                    foreach (Tuple<Variable, Variable> v in vars)
                    {
                        Console.WriteLine(v.Item1.Name + " is close to " + v.Item2.Name);
                        Console.Write("\t");
                        if (v.Item1.References > v.Item2.References)
                            Console.WriteLine(v.Item1.Name + " is the best match with " + v.Item1.References + " references");
                        else if (v.Item1.References < v.Item2.References)
                            Console.WriteLine(v.Item2.Name + " is the best match with " + v.Item2.References + " references");
                        else
                            Console.WriteLine("Both have the same amount of references (" + v.Item1.References + ")!");

                    }
                    List<Variable> unused = Refactoring.FindUnusedVariables(c);
                    Console.WriteLine("- Unused Variables -");
                    foreach (Variable v in unused)
                        Console.WriteLine("  " + v.Name);

                    unused = Refactoring.FindUnusedLocalVariables(c);
                    Console.WriteLine("- Unused Local Variables -");
                    foreach (Variable v in unused)
                        Console.WriteLine("  " + v.Name);
                    Refactoring.AddModuleDependency(c, "module1");
                    Refactoring.AddModuleDependency(c, "module2", "local_module2");

                    Refactoring.AddClrDependency(c, "AClrLib", "AClrLib.NamespaceA.AClrType");

                    Console.WriteLine("- With Added Dependencies -");
                    Console.WriteLine(new Visitors.Beautifier().Beautify(c));

                    c.Scope.ObfuscateLocals();
                    Console.WriteLine("- With obfuscated locals");
                    Console.WriteLine(new Visitors.Beautifier().Beautify(c));
                }
                catch (LuaSourceException ex)
                {
                    Console.WriteLine(line);
                    Console.WriteLine(" ".Repeat(ex.Column - 1) + "^");
                    Console.WriteLine("<stdin>:" + ex.Line + ":" + ex.Column + ":" + ex.Message);
                    Console.WriteLine(ex.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
Ejemplo n.º 11
0
        private static void TestExtractDocComments()
        {
            string str = @"
---<summary>
--- somefin
---</summary>
---<returns>nothing</returns>
function a()

end

---<summary>
--- ugh.
---</summary>
function tbl.dosomething()

end

---<summary>
--- a var
---</summary>
---<returns>wut?</returns>
x = 1
";

            Lexer l = new Lexer();
            Parser p = new Parser(l.Lex(str));
            Chunk c = p.Parse();
            List<DocumentationComment> docs = ExtractDocumentationComments.Extract(c);
            foreach (DocumentationComment d in docs)
            {
                Console.WriteLine("Documentation comment: " + d.Text + "Var: " + (d.Ident == null ? "<none>" : d.Ident));
            }
            string s = Documentation.Write(docs);
            Console.WriteLine(s);
            Console.WriteLine(Documentation.Read(s).Count);
            if (docs.Count == 0)
                Console.WriteLine("No doc comments. Wut?");
        }