private CodeExpression BuildExpression(XSharpParser.ExpressionContext expression, bool right ) { CodeExpression expr = null; // if (expression is XSharpParser.PrimaryExpressionContext) // xyz.SimpleName { expr = BuildExpression((XSharpParser.PrimaryExpressionContext)expression); } else if (expression is XSharpParser.AccessMemberContext) // xyz.SimpleName { XSharpParser.AccessMemberContext member = (XSharpParser.AccessMemberContext)expression; //what is the left hand side ? // Self -> check if Right is in the member of CurrentClass --> FieldReference // else --> always Property bool isMember = false; CodeExpression left = BuildExpression(member.Expr,false); if (left is CodeThisReferenceExpression) { string fieldCandidate = member.Name.GetText(); foreach (CodeTypeMember cm in this.CurrentClass.Members) { if (cm is CodeMemberField) { if (String.Compare(fieldCandidate, cm.Name, true) == 0) { isMember = true; break; } } } } // It seems to be a member... if (isMember) { expr = new CodeFieldReferenceExpression(BuildExpression(member.Expr,false), member.Name.GetText()); } else { // Let's guess that on the Left member, we should have a Property if it is not a Field if (!right) { expr = new CodePropertyReferenceExpression(BuildExpression(member.Expr, false), member.Name.GetText()); } else { // We are processing the Right member of an Assignment... // Most likely Enum Value, which is a typereference expression followed by a DOT and a field if (member.DOT() != null) { var typeexpr = new CodeTypeReferenceExpression(member.Expr.GetText()); expr = new CodeFieldReferenceExpression(typeexpr, member.Name.GetText()); } else { expr = new CodeSnippetExpression(member.GetText()); } } } } else if (expression is XSharpParser.MethodCallContext) { XSharpParser.MethodCallContext meth = (XSharpParser.MethodCallContext)expression; CodeExpression target = BuildExpression(meth.Expr,false); List<CodeExpression> exprlist = new List<CodeExpression>(); if (meth.ArgList != null) { foreach (var arg in meth.ArgList._Args) { exprlist.Add(BuildExpression(arg.Expr,false)); } } if (target is CodeFieldReferenceExpression) { // expr = new CodeMethodInvokeExpression(((CodeFieldReferenceExpression)target).TargetObject, ((CodeFieldReferenceExpression)target).FieldName, exprlist.ToArray()); } else if (target is CodePropertyReferenceExpression) { // expr = new CodeMethodInvokeExpression(((CodePropertyReferenceExpression)target).TargetObject, ((CodePropertyReferenceExpression)target).PropertyName, exprlist.ToArray()); } else expr = new CodeMethodInvokeExpression(null, meth.Expr.GetText(), exprlist.ToArray()); } else { expr = new CodeSnippetExpression(expression.GetText()); } // return expr; }
/// <summary> /// Get a LiteralValueContext containing a BIN_CONST, INT_CONST, HEX_CONST, or a REAL_CONST /// as a String, and convert it to the "real" value, with the corresponding Type. /// </summary> /// <param name="context"></param> /// <returns>An Object of the needed Type, with the value</returns> private object GetNumericValue(XSharpParser.LiteralValueContext context) { Object ret = null; String value = context.GetText(); // if (context.BIN_CONST() != null || context.INT_CONST() != null || context.HEX_CONST() != null) { bool isUnsigned = (value.Substring(0, 1).ToLower().CompareTo("u") == 0); // -1 for Unsigned; -2 for 0x or 0b int len = value.Length - (isUnsigned ? 1 : 0) - (context.BIN_CONST() != null || context.HEX_CONST() != null ? 2 : 0); // if (context.BIN_CONST() != null) { if (len > 64) { ret = Double.NaN; } else { // Don't forget to remove the prefix !!! value = value.Substring(2); // BIN are always unsigned (??) UInt64 bin64; try { bin64 = Convert.ToUInt64(value, 2); // Maybe 32 bits is enough ? if (bin64 <= UInt32.MaxValue) { UInt32 bin32 = Convert.ToUInt32(bin64); ret = bin32; } else { ret = bin64; } } catch { ret = Double.NaN; } } } else if (context.HEX_CONST() != null) { if (len > 16) { ret = Double.NaN; } else { // Don't forget to remove the prefix !!! value = value.Substring(2); // HEX are always unsigned (??) UInt64 hex64; try { hex64 = Convert.ToUInt64(value, 16); // Maybe 32 bits is enough ? if (hex64 <= UInt32.MaxValue) { UInt32 hex32 = Convert.ToUInt32(hex64); ret = hex32; } else { ret = hex64; } } catch { ret = Double.NaN; } } } else { // context.INT_CONST() != null if (len > 64) { ret = Double.NaN; } else if (isUnsigned) { UInt64 myUInt64; try { myUInt64 = Convert.ToUInt64(value, 10); // Maybe 32 bits is enough ? if (myUInt64 <= UInt32.MaxValue) { UInt32 myUInt32 = Convert.ToUInt32(myUInt64); ret = myUInt32; } else { ret = myUInt64; } } catch { ret = Double.NaN; } } else { Int64 myInt64; try { myInt64 = Convert.ToInt64(value, 10); // Maybe 32 bits is enough ? if ((myInt64 >= UInt32.MinValue) && (myInt64 <= UInt32.MaxValue)) { Int32 myInt32 = Convert.ToInt32(myInt64); ret = myInt32; } else { ret = myInt64; } } catch { ret = Double.NaN; } } } } else { double d; // Should be REAL_CONST if (!double.TryParse(value, out d)) { d = double.NaN; } ret = d; } return ret; }
private CodeTypeReference BuildXBaseType(XSharpParser.XbaseTypeContext xbaseType) { CodeTypeReference expr = null; // if ((xbaseType.ARRAY() != null) || (xbaseType.CODEBLOCK() != null) || (xbaseType.DATE() != null) || (xbaseType.FLOAT() != null) || (xbaseType.PSZ() != null) || (xbaseType.SYMBOL() != null) || (xbaseType.USUAL() != null)) { expr = new CodeTypeReference(xbaseType.GetText()); } // return expr; }
private CodeExpression BuildLiteralValue(XSharpParser.LiteralValueContext context) { CodeExpression expr = null; ITerminalNode node; // node = context.BIN_CONST(); if (node != null) { expr = new CodePrimitiveExpression(GetNumericValue(context)); } node = context.INT_CONST(); if (node != null) { expr = new CodePrimitiveExpression(GetNumericValue(context)); } // node = context.HEX_CONST(); if (node != null) { expr = new CodePrimitiveExpression(GetNumericValue(context)); } // node = context.REAL_CONST(); if (node != null) { expr = new CodePrimitiveExpression(GetNumericValue(context)); } // node = context.TRUE_CONST(); if (node != null) { expr = new CodePrimitiveExpression(true); } // node = context.FALSE_CONST(); if (node != null) { expr = new CodePrimitiveExpression(false); } // node = context.STRING_CONST(); if (node != null) { // Remove the quotes String value = context.GetText(); value = value.Substring(1, value.Length - 2); expr = new CodePrimitiveExpression(value); } // node = context.ESCAPED_STRING_CONST(); if (node != null) { // Remove the e in front of quotes, AND the Quotes String value = context.GetText(); value = value.Substring(1); value = value.Substring(1, value.Length - 2); expr = new CodePrimitiveExpression(BuildUnEscapedString(value)); } // node = context.CHAR_CONST(); if (node != null) { // Remove the quotes String value = context.GetText(); value = value.Substring(1, value.Length - 2); if (value.Length >= 1) expr = new CodePrimitiveExpression(value[0]); } // node = context.NIL(); if (node != null) { expr = new CodeSnippetExpression("NIL"); } // node = context.NULL(); if (node != null) { expr = new CodePrimitiveExpression(null); } // node = context.NULL_ARRAY(); if (node != null) { expr = new CodeSnippetExpression("NULL_ARRAY"); } // node = context.NULL_CODEBLOCK(); if (node != null) { expr = new CodeSnippetExpression("NULL_CODEBLOCK"); } // node = context.NULL_DATE(); if (node != null) { expr = new CodeSnippetExpression("NULL_DATE"); } // node = context.NULL_OBJECT(); if (node != null) { expr = new CodeSnippetExpression("NULL_OBJECT"); } // node = context.NULL_PSZ(); if (node != null) { expr = new CodeSnippetExpression("NULL_PSZ"); } // node = context.NULL_PTR(); if (node != null) { expr = new CodeSnippetExpression("NULL_PTR"); } // node = context.NULL_STRING(); if (node != null) { expr = new CodeSnippetExpression("NULL_STRING"); } // node = context.NULL_SYMBOL(); if (node != null) { expr = new CodeSnippetExpression("NULL_SYMBOL"); } // node = context.SYMBOL_CONST(); if (node != null) { expr = new CodeSnippetExpression(context.GetText().ToUpper()); } // node = context.DATE_CONST(); if (node != null) { expr = new CodeSnippetExpression(context.GetText()); } // if ( expr == null ) { expr = new CodeSnippetExpression(context.GetText()); } return expr; }
private CodeTypeReference BuildNativeType(XSharpParser.NativeTypeContext nativeType) { CodeTypeReference expr = null; // if ((nativeType.BYTE() != null) || (nativeType.CHAR() != null) || (nativeType.DWORD() != null) || (nativeType.DYNAMIC() != null) || (nativeType.INT() != null) || (nativeType.INT64() != null) || (nativeType.LOGIC() != null) || (nativeType.LONGINT() != null) || (nativeType.OBJECT() != null) || (nativeType.PTR() != null) || (nativeType.REAL4() != null) || (nativeType.REAL8() != null) || (nativeType.SHORTINT() != null) || (nativeType.STRING() != null) || (nativeType.UINT64() != null) || (nativeType.VOID() != null) || (nativeType.WORD() != null)) { expr = BuildNativeType(nativeType.GetText()); } // return expr; }