public void CheckEqualityTypes(LexList InputList, Type type) { if (ResultType == null && type == null) { return; } if (ResultType == null && !type.IsValueType) { return; } if (!ResultType.IsValueType && (type == null)) { return; } if (ResultType.IsEnum && type == Enum.GetUnderlyingType(ResultType)) { return; } if (type.IsEnum && ResultType == Enum.GetUnderlyingType(type)) { return; } if ((ResultType == type) || ((IsIntType(ResultType) || IsRealType(ResultType)) && (IsIntType(type) || IsRealType(type)))) { return; } InputList.ThrowException("The left hand operand type '" + ResultType.ToString() + "'\n is not Equality compatible with the right hand operand type '" + type.ToString() + "'."); }
private void ListAdd(string str, LexKind kind) { if (kind >= LexKind.Symbol00 && kind <= LexKind.Symbol11 && Expansions.ContainsKey(str)) { object o; Expansions.TryGetValue(str, out o); if (o is string) { string so = o as string; o = LexList.Get(so); } if (o is LexList) { for (int i = 0; i < ((LexList)o).Count; i++) { LexToken tok = ((LexList)o)[i]; bool toPrevious = (kind == LexKind.Symbol10 || kind == LexKind.Symbol11) && i == 0; bool toNext = (kind == LexKind.Symbol01 || kind == LexKind.Symbol11) && i == ((LexList)o).Count - 1; ListAddToken(toPrevious, new LexToken(tok), toNext); } } else { LexToken token = new LexToken(o, List, List.Count); List.Add(token); } } else { ListAddToken(kind == LexKind.Symbol10 || kind == LexKind.Symbol11, new LexToken(str, kind, List, List.Count), kind == LexKind.Symbol01 || kind == LexKind.Symbol11); } }
//Different ways of generating a LexList public static void TestLexList1() { LexList lex1 = LexList.Get(@" public str Convert ( int i ) { return i.ToString() ; }" ); string s1 = lex1.CodeFormatText(false); string[] lines = new string[] { "public str Convert ( int i )", "{", " return i.ToString() ; ", "}" }; LexList lex2 = LexList.Get(lines); string s3 = "public str Convert(int i)\r\n{\r\n return i.ToString();\r\n}\r\n"; string s2 = lex2.CodeFormatText(false); if (s1 != s2 || s3 != s1) { MessageBox.Show("Error in TesetLexList1"); } }
public void CheckTypeIs(LexList list, Type type, string msg) { if (ResultType != type) { list.ThrowException(msg); } }
private void CompileNextFieldDefinition(LexList theLexList, Type varType) { string varId = theLexList.GetIdentifier("Expected the name of the field here."); MostRecentNameAdded = varId; theLexList.CheckStr(";", "Expected a ; here"); if (MadeFieldsDict.ContainsKey(varId)) { theLexList.ThrowException("This field already defined."); } if (CompileMethodsDict.ContainsKey(varId)) { theLexList.ThrowException("This field name already used as a method name."); } MadeFieldsDict.Add(varId, new MadeField() { VarName = varId, VarType = varType, Index = MadeFieldsDict.Count }); FieldInfo fi = ClassType.GetField("Fields", BindingFlags.Public | BindingFlags.Instance); if (fi == null) { theLexList.ThrowException("Can only add fields to a class that has a 'List<Object> Fields' field."); } if (fi.FieldType != typeof(List <object>)) { theLexList.ThrowException("Can only add fields to a class where its Fields field is of type List<object>."); } theLexList.Next(); }
public LexReadDialog(LexList theList, FontFamily family, double fontSize) : base(family, fontSize) { TheList = new LexList(theList); TheList.Index = theList.Index; SetUpLineStarts_LexList(); }
private static int CountCommas(LexList list, int rank, bool arrayIndexActualsAllowed) { Stack <string> stack = new Stack <string>(); if (arrayIndexActualsAllowed) { int saveIndex = list.Index - 1; bool finished = false; while (!list.AtEnd() && !finished) { switch (list.Str) { case ",": if (stack.Count == 0) { rank++; } break; case "(": stack.Push(")"); break; case "{": stack.Push("}"); break; case "[": stack.Push("]"); break; case ")": case "}": if (stack.Pop() != list.Str) { list.ThrowException("Unbalanced brackets in array initialiser"); } break; case "]": if (stack.Count == 0) { list.Prev(); finished = true; } else { if (stack.Pop() != list.Str) { list.ThrowException("Unbalanced brackets in array initialiser"); } } break; } list.Next(); } list.Index = saveIndex; } else { while (list.Str == ",") { list.Next(); rank++; } } return(rank); }
public void CheckIsBoolType(LexList InputList, string msg) { if (ResultType == typeof(bool)) { return; } InputList.ThrowException(msg); }
public void CheckIsArrayType(LexList InputList, string p) { if (ResultType.IsArray) { return; } InputList.ThrowException(p); }
public void CheckIsIntType(LexList InputList, string p) { if (IsIntType(ResultType)) { return; } InputList.ThrowException(p); }
public void ThrowException(string msg, LexList theList) { ErrorLocation = msg; if (LexToken.ShowError != null) { LexToken.ShowError(msg, theList); } throw new LexListException(ErrorLocation, this); }
public Type ParseType(LexList list, bool typeOnly, bool arrayIndexActualsAllowed, out bool pendingCloseAngle, BindingFlags visibility) { Type type = Parse(list, typeOnly, arrayIndexActualsAllowed, out pendingCloseAngle, visibility); if (pendingCloseAngle) { list.ThrowException("Unexpected > here"); } return(type); }
private static List <LexToken> Get(string[] source) { List <LexToken> list = new List <LexToken>(source.Length); foreach (var s in source) { list.AddRange(LexList.Get(s)); } return(list); }
public static void ErrorHandlingUsingMessageBox() { LexToken.ShowError = (msg, theList) => { MessageBox.Show( msg + "\n" + theList.CodeFormat, "Error found"); }; LexList.Get("MoreText '"); // will produce an error here. }
private static List <LexToken> Get(ReadOnlyCollection <string> source) { List <LexToken> list = new List <LexToken>(source.Count); foreach (var s in source) { list.AddRange(LexList.Get(s)); } return(list); }
//Setting up error handling. #if Dialog public static void ErrorHandlingUsingLexErrorDialog() { LexToken.ShowError = (msg, theList) => { new LexErrorDialog() { Message = msg, CompilerList = theList, }.Show(); }; LexList.Get("MoreText '"); // will produce an error here. }
public void DeclareArg(Type type, LexToken name, LexList theList) { VarInfo found; if (VarAccess.TryGetValue(name.Str, out found)) { name.ThrowException("Argument name '" + name.Str + "' is already used.", theList); } VarAccess.Add(name.Str, new VarInfo(type, true, ArgNames.Count, -1)); ArgNames.Add(name.Str); ArgTypes.Add(type); }
public static void TestLexList3() { LexList lex1 = MakeTheMethod3("Var1"); LexList lex2 = LexList.Get(@"public int GetValue () { return Examples.Var1 ; }"); string s1 = lex1.CodeFormatText(false); string s2 = lex2.CodeFormatText(false); if (s1 != s2 || s1 != "public int GetValue()\r\n{\r\n return Examples.Var1;\r\n}\r\n") { MessageBox.Show("Error in TestLexList2"); } }
static public void TokenPasting5() { LexListBuilder llb = new LexListBuilder(); llb.Add("return typeof(`Type) ;", "Type", typeof(MyClassType)); LexList lex = llb.ToLexList(); string s = lex.CodeFormatText(false); if (s != "return typeof(MyClassType);\r\n" || lex[3].ActualObject != typeof(MyClassType)) { MessageBox.Show("Error in TokenPasting5"); } }
public static void TestLexList4() { LexList lex1 = MakeTheMethod4("A", "B"); LexList lex2 = LexList.Get( @"public int GetValue () { return Alpha.A + Alpha.B + Alpha.A ; }"); string s1 = lex1.CodeFormatText(false); string s2 = lex2.CodeFormatText(false); if (s1 != s2 || s1 != "public int GetValue()\r\n{\r\n return Alpha.A+Alpha.B+Alpha.A;\r\n}\r\n") { MessageBox.Show("Error in TestLexList2"); } }
private void CompileNextMethodHeader(LexList theLexList, bool overwriteAllowed, List <CompileMethod> currentListOfMethods) { CompileMethod cm = DoOneMethod(theLexList); currentListOfMethods.Add(cm); AddMethodToDictionary(cm, overwriteAllowed); if (cm.MethodName != "FieldsInitialiser") { MostRecentNameAdded = cm.MethodName; } if (MadeFieldsDict.ContainsKey(cm.MethodName)) { theLexList.ThrowException("This method name already used for a field."); } }
public static MethodInfo GetDelegateInfo(LexList list, Type type, out Type returnType, out Type[] paras) { if (type.BaseType != typeof(MulticastDelegate)) { list.ThrowException("Type '" + type.Name + " is not a multicast delegate type."); } MethodInfo invoke = type.GetMethod("Invoke"); if (invoke == null) { list.ThrowException("Type '" + type.Name + " is not a delegate type."); } paras = (from p in invoke.GetParameters() select p.ParameterType).ToArray(); returnType = invoke.ReturnType; return(invoke); }
public void CheckComparisonTypes(LexList InputList, Type type) { if ((ResultType == typeof(string) || ResultType == typeof(bool) || ResultType == typeof(char)) && (ResultType == type)) { return; } if ((IsIntType(ResultType) || IsRealType(ResultType)) && (IsIntType(type) || IsRealType(type))) { return; } if (ResultType.IsEnum && type.IsEnum && ResultType == type) { return; } InputList.ThrowException("The left hand operand type '" + ResultType.ToString() + "'\n is not Comparison compatible with the right hand operand type '" + type.ToString() + "'."); }
public static Type GetMethodDelegateType(LexList list, Type[] argumentTypes, bool lastIsReturnType) { if (lastIsReturnType) { switch (argumentTypes.Length) { case 1: return(typeof(Func <>).MakeGenericType(argumentTypes)); case 2: return(typeof(Func <,>).MakeGenericType(argumentTypes)); case 3: return(typeof(Func <, ,>).MakeGenericType(argumentTypes)); case 4: return(typeof(Func <, , ,>).MakeGenericType(argumentTypes)); case 5: return(typeof(Func <, , , ,>).MakeGenericType(argumentTypes)); case 6: return(typeof(Func <, , , , ,>).MakeGenericType(argumentTypes)); case 7: return(typeof(Func <, , , , , ,>).MakeGenericType(argumentTypes)); } } else { switch (argumentTypes.Length) { case 0: return(typeof(Action)); case 1: return(typeof(Action <>).MakeGenericType(argumentTypes)); case 2: return(typeof(Action <,>).MakeGenericType(argumentTypes)); case 3: return(typeof(Action <, ,>).MakeGenericType(argumentTypes)); case 4: return(typeof(Action <, , ,>).MakeGenericType(argumentTypes)); case 5: return(typeof(Action <, , , ,>).MakeGenericType(argumentTypes)); case 6: return(typeof(Action <, , , , ,>).MakeGenericType(argumentTypes)); case 7: return(typeof(Action <, , , , , ,>).MakeGenericType(argumentTypes)); } } list.ThrowException("GetMethodDelegateType program error " + argumentTypes.Length + " " + lastIsReturnType); return(null); }
private static void ExamineCurly(string line, ref int nesting, ref bool foundCurly) { LexList list = LexList.Get(line); foreach (var tok in list) { if (tok.Str == "{") { nesting++; foundCurly = true; } else if (tok.Str == "}") { nesting--; foundCurly = true; } } }
private void MakeFieldInitialiser(List <CompileMethod> currentListOfMethods) { LexListBuilder lb = new LexListBuilder(); lb.AddAndPromoteQuotes("public void FieldsInitialiser () { "); lb.AddAndPromoteQuotes("if (Fields == null) Fields = new List<object> () ;"); foreach (var f in (from f in MadeFieldsDict orderby f.Value.Index select f)) { Type theType = f.Value.VarType; lb.AddAndPromoteQuotes("`type `name ; if (Fields.Count == `index) Fields.Add ( (object)`name ) ; ", "type", theType, "name", f.Value.VarName, "index", f.Value.Index); // The conditional 'if (Fields.Count == f.Value.Index)' bit means that this initialisation function can be called repeatedly, and only the new ones will be initialised. } lb.AddAndPromoteQuotes("}"); LexList ll = lb.ToLexList(); ll.CrosslinkBrackets(); CompileNextMethodHeader(ll, true, currentListOfMethods); }
public LocalBuilder DeclareLocal(Type type, LexToken name, int nestingLevel, LexList theList) { if (!IL.Active) { return(null); } VarInfo found; if (VarAccess.TryGetValue(name.Str, out found)) { name.ThrowException("Local name '" + name.Str + "' is already used.", theList); } LocalBuilder lb = IL.DeclareLocal_opCodes(type, name.Str); VarAccess.Add(name.Str, new VarInfo(type, false, LocalNames.Count, nestingLevel, lb)); LocalNames.Add(name.Str); LocalTypes.Add(type); return(lb); }
static public void QuotePromotion() { string ActualValue = "VarOne"; LexListBuilder llb = new LexListBuilder(); llb.AddAndPromoteQuotes( "string s = 'The string contents' + `Variable ; ", "Variable", ActualValue); llb.AddAndPromoteQuotes( "string anotherStr = 'Another string contents' ;"); llb.AddAndPromoteQuotes("char ch = `c` ; "); LexList lex = llb.ToLexList(); string s = lex.CodeFormatText(false); if (s != "string s=\"The string contents\"+VarOne;\r\nstring anotherStr=\"Another string contents\";\r\nchar ch='c';\r\n") { MessageBox.Show("Error in QuotePromotion"); } }
// Syntax: // Member = '.' StaticMember . public ExpState ParseMember(Type theClass, LexList list, BindingFlags flags, bool implicitDot) { if (!implicitDot) { list.CheckStrAndAdvance(".", "Expected a dot followed by a static member name here."); } LexToken token = list.CurrentToken(); string name = list.GetIdentifier("Expected the name of a static member here."); FieldInfo fi = theClass.GetField(name, flags); if (fi != null) { return(new ExpState(fi)); } list.Prev(); return(null); }
static public void MakeClass1() { SetUpTypeParser(); MakeClass mc = new MakeClass(parser, LexList.Get(@" partial class Examples.TestClass { public int GetTwoTimesTheInt () { return TheInt * 2 ; } public void SetTheString ( string s ) { TheString = s ; } }")); Func <TestClass, int> GetTwoTimesTheInt; Action <TestClass, string> SetTheString; mc.GetFunc <TestClass, int>( "GetTwoTimesTheInt", out GetTwoTimesTheInt); mc.GetAction <TestClass, string>( "SetTheString", out SetTheString); TestClass tc = new TestClass(); tc.TheInt = 34; int i = GetTwoTimesTheInt(tc); if (i != 68) { MessageBox.Show("MakeClass1 error 1"); } SetTheString(tc, "New string value"); if (tc.TheString != "New string value") { MessageBox.Show("MakeClass1 error 2"); } }