public SyntaxMargin() { this.timer.Tick += (sender, e) => { if (this.Document == null) { return; } try { Simula.Scripting.Parser.Tokenizer tokenizer = new Scripting.Parser.Tokenizer(); var tokenCollection = tokenizer.Tokenize(this.Document.Text); Simula.Scripting.Parser.Parser parser = new Scripting.Parser.Parser(); var program = parser.Parse(tokenCollection); if (this.manager == null) { this.manager = new BookMarkManager(); } this.manager.Bookmarks.Clear(); foreach (var item in program.Body) { VisitSyntaxTree(item); } } catch { } }; timer.Interval = TimeSpan.FromMilliseconds(500); timer.Start(); }
public void HandleRun(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); Simula.Scripting.Parser.Tokenizer tokenizer = new Scripting.Parser.Tokenizer(); var tokenCollection = tokenizer.Tokenize(editor.Text); Simula.Scripting.Parser.Parser parser = new Scripting.Parser.Parser(); var program = parser.Parse(tokenCollection); var syntaxTree = Simula.Scripting.Analysis.SyntaxTree.Analyse(0, program); Simula.Scripting.Analysis.AnalysisEnvironment environment = new Scripting.Analysis.AnalysisEnvironment(); var programBuilder = environment.Load(syntaxTree, "autogen"); sw.Stop(); System.Windows.MessageBox.Show("编译用时 " + sw.ElapsedMilliseconds + " ms"); // entry point is the static method 'void program.statics.___entry()' var assembly = programBuilder.Assembly; var statics = assembly.GetType("program.statics"); var entry = statics.GetMethod("___entry"); entry.Invoke(null, null); }
static void Main() { Parser.Tokenizer tokenizer = new Parser.Tokenizer(); var str1 = @" module sys.io use sys.io ' this is a line comment debugger on def d1 = data(private int32 i = 0, ... public static readonly int32 j) end def d2 = data(params int32 k) : d1 def a = func(int32 i) alert(hello) end end def a1 = data() end d3 = data(int32 l) : d1, d2 assert a1 end def a = func (int32 i, int32 j) => int32 another = 0 variable = [block return i + j end, 2 2, 3 4, 5 ] (another, variable) = (0, func (uint16 c1, uint16 c2) => uint16 return c1 + c2 end ) [ a1 a2 a3 ] = [ 0; 1; 2 ] [1, b] = [1, 2] end b = 5.886 + 867 v = u.prop1.getid[4].tostring(show()) c = f1 <*> b try if (b == 0) return 1 eif b == 1 return 2 else return 3 end config conf util = uint16 id = config t obj = null end end catch ex touch sbls end iter 19293 iter a in collection a = 1 end iter a in collection at pos alert(pos) end end "; var str2 = @" module sys.io use sys.io def classA = data ( int32 memberAa, logical memberAb, matrix(int32) memberAc ) def init = func ( int32 paramFunc ) => func insideDefined = 4 insideDefined = memberFunc3(insideDefined) ' ___debug.log(insideDefined) log(insideDefined) alertString(1,2) this.anotherMemberFunction (1, 2) this.init end def anotherMemberFunction = func ( int32 param1, int64 param2 ) end def memberFunc3 = func ( int32 i ) => int32 i end def m4 = func() end end def classB = data ( classA memberBa, logical memberBb, classC recursive ) ' b refers to c end def classC = data ( classA memberCa, classB memberCb, classC memberCc ) def anotherMemberFunction = func ( int32 param1, int64 param2 ) end ' c refers to b def typeReturning = func () => data classB end end def classExclusion = data ( int32 memberAa ) end def classD = data ( int32 memberAa, logical memberAb, matrix(int64) memberAc ) end def classE = data ( classD ! classExclusion backToA, classA & classB another ) end def alert = func ( logical booleans ) => logical booleans end def alertString = func ( int32 i1, float64 f2 ) end log(1) "; var str3 = "a.b.c().d.e().f"; var result = tokenizer.Tokenize(str2); Console.WriteLine("Lexical Analysis"); Console.WriteLine(""); Console.WriteLine(result.ToString(Parser.TokenFormatterOption.Table)); Console.WriteLine(""); Console.WriteLine("Grammar Analysis"); Console.WriteLine(""); Parser.Parser parser = new Scripting.Parser.Parser(); var program = parser.Parse(result); if (program == null) { Console.WriteLine("Null Program"); } else { PrintAst(program.Body, 0); Console.WriteLine(""); Console.WriteLine("Diagnostics: {0} Errors, {1} Warnings", program.Result.Fatals, program.Result.Warnings); foreach (var item in program.Result.Diagnostics) { switch (item.Severity) { case Parser.Severity.Information: Console.Write("(i) "); break; case Parser.Severity.Warning: Console.Write("(!) "); break; case Parser.Severity.Fatal: Console.Write("(x) "); break; default: break; } Console.Write("({0},{1})-({2},{3})", item.Location.Start.Line, item.Location.Start.Column, item.Location.End.Line, item.Location.End.Column); Console.WriteLine(" " + item.Error.ToString()); } Analysis.SyntaxTree syntax = Analysis.SyntaxTree.Analyse(0, program); Console.WriteLine("Syntax Static Analysis"); Console.WriteLine(""); Console.WriteLine("Diagnostics: {0} Errors, {1} Warnings", syntax.Diagnostics.Fatals, syntax.Diagnostics.Warnings); foreach (var item in syntax.Diagnostics.Diagnostics) { switch (item.Severity) { case Parser.Severity.Information: Console.Write("(i) "); break; case Parser.Severity.Warning: Console.Write("(!) "); break; case Parser.Severity.Fatal: Console.Write("(x) "); break; default: break; } Console.Write(item.File + " "); Console.Write("({0},{1})-({2},{3})", item.Location.Start.Line, item.Location.Start.Column, item.Location.End.Line, item.Location.End.Column); Console.WriteLine(" " + item.Error.ToString()); } Analysis.AnalysisEnvironment environment = new Analysis.AnalysisEnvironment(); environment.Load(syntax); Console.WriteLine("Static Analysis"); Console.WriteLine(""); foreach (var item in environment.Current.Diagnostics.Diagnostics) { switch (item.Severity) { case Parser.Severity.Information: Console.Write("(i) "); break; case Parser.Severity.Warning: Console.Write("(!) "); break; case Parser.Severity.Fatal: Console.Write("(x) "); break; default: break; } Console.Write(item.File + " "); Console.Write("({0},{1})-({2},{3})", item.Location.Start.Line, item.Location.Start.Column, item.Location.End.Line, item.Location.End.Column); Console.WriteLine(" " + item.Error.ToString()); } } }