}//Run private void flush() { if ( (!isString) && (!isCommentBlock) && (!isCommentLine) && (buffer.Length == 0) ) { return; } string text = buffer.ToString(); object value = null; buffer.Length = 0; CSTokenType type = CSTokenType.tUnknown; if (isString) { type = CSTokenType.tStringLiteral; if (!isVerbatim) { try //expand escapes { text = CSStrings.UnescapeString(text); } catch (StringEscapeErrorException err) { lexer.EmitMessage(MessageType.Error, (int)CSMsgCode.eInvalidStringEscape, tagStartPos, null, err.ErroredEscape); return; } } } else if (isCommentLine && isDirective)//directives treated similar to line comments { type = CSTokenType.tDirective; } else if (isCommentBlock || isCommentLine) { type = CSTokenType.tComment; } else { try { value = CSNumbers.Convert(text, out type); } catch (ArgumentException err) { lexer.EmitMessage(MessageType.Error, (int)CSMsgCode.eValueTooBig, tagStartPos, null, err.Message); return; } if (value == null) //not number { type = CSKeywords.Resolve(text); if (type == CSTokenType.tIdentifier) { if (text.StartsWith("@")) { text = text.Remove(0, 1); //take care of verbatim names like: @class, @void, @var etc.. tagStartPos = new SourcePosition(tagStartPos.LineNumber, tagStartPos.ColNumber + 1, tagStartPos.CharNumber + 1); } if (!CSIdentifiers.Validate(text)) { lexer.EmitMessage(MessageType.Error, (int)CSMsgCode.eInvalidIdentifier, tagStartPos, null, text); return; } } } //not a number } //not a comment if (type == CSTokenType.tStringLiteral) { value = text; } tokens.Add(new CSToken(lexer, type, tagStartPos, tagEndPos, text, value)); }
public CSToken(CSLexer lexer, CSTokenType type, SourcePosition startPos, SourcePosition endPos, string text, object value = null) : base(lexer, startPos, endPos, text, value) { Type = type; }
/// <summary> /// Tries to convert string to number, returns null if conversion could not be made. /// Raises exception if operand is wider than specifier allows /// </summary> public static object Convert(string str, out CSTokenType type) { ulong inum = 0; double fnum = 0.0; decimal dnum = 0.0M; if (strToInt(str, out inum)) { if (inum > UInt32.MaxValue) { type = CSTokenType.tLongIntLiteral; } else { type = CSTokenType.tIntLiteral; } return(inum); } if (strToFloat(str, out fnum)) { if (inum > Single.MaxValue) { type = CSTokenType.tDoubleLiteral; } else { type = CSTokenType.tFloatLiteral; } return(fnum); } type = CSTokenType.tUnknown; str = str.ToUpperInvariant(); //No XLAT Hashtable on purpose , speed and simplicity //order of IFs is important because some types have the same endings if (str.EndsWith(S_ULONG1)) { str = str.Replace(S_ULONG1, string.Empty); type = CSTokenType.tULongIntLiteral; } else if (str.EndsWith(S_ULONG2)) { str = str.Replace(S_ULONG2, string.Empty); type = CSTokenType.tULongIntLiteral; } else if (str.EndsWith(S_UINT)) { str = str.Replace(S_UINT, string.Empty); type = CSTokenType.tUIntLiteral; } else if (str.EndsWith(S_LONG)) { str = str.Replace(S_LONG, string.Empty); type = CSTokenType.tLongIntLiteral; } else if (str.EndsWith(S_FLOAT)) { str = str.Replace(S_FLOAT, string.Empty); type = CSTokenType.tFloatLiteral; } else if (str.EndsWith(S_DOUBLE)) { str = str.Replace(S_DOUBLE, string.Empty); type = CSTokenType.tDoubleLiteral; } else if (str.EndsWith(S_DECIMAL)) { str = str.Replace(S_DECIMAL, string.Empty); type = CSTokenType.tDecimalLiteral; } else { return(null); } if ((type != CSTokenType.tDecimalLiteral) && (type != CSTokenType.tFloatLiteral) && (type != CSTokenType.tDoubleLiteral)) { if (strToInt(str, out inum)) { //safeguard - this will never happen unless system supports 128bit arithmetic if (inum > UInt32.MaxValue) { throw new ArgumentException(S_UINT + ": " + inum);//caught by lexer } return(inum); } } else if (type != CSTokenType.tDecimalLiteral) { if (strToFloat(str, out fnum)) { if ( (type == CSTokenType.tFloatLiteral) && (fnum > float.MaxValue) ) { throw new ArgumentException(S_FLOAT + ": " + fnum);//caught by lexer } return(fnum); } } else if (strToDecimal(str, out dnum)) { return(dnum); } return(null); }//Convert
/// <summary> /// Tries to convert string to number, returns null if conversion could not be made. /// Raises exception if operand is wider than specifier allows /// </summary> public static object Convert(string str, out CSTokenType type) { ulong inum = 0; double fnum = 0.0; decimal dnum = 0.0M; if (strToInt(str, out inum)) { if (inum > UInt32.MaxValue) type = CSTokenType.tLongIntLiteral; else type = CSTokenType.tIntLiteral; return inum; } if (strToFloat(str, out fnum)) { if (inum > Single.MaxValue) type = CSTokenType.tDoubleLiteral; else type = CSTokenType.tFloatLiteral; return fnum; } type = CSTokenType.tUnknown; str = str.ToUpperInvariant(); //No XLAT Hashtable on purpose , speed and simplicity //order of IFs is important because some types have the same endings if (str.EndsWith(S_ULONG1)) { str = str.Replace(S_ULONG1, string.Empty); type = CSTokenType.tULongIntLiteral; } else if (str.EndsWith(S_ULONG2)) { str = str.Replace(S_ULONG2, string.Empty); type = CSTokenType.tULongIntLiteral; } else if (str.EndsWith(S_UINT)) { str = str.Replace(S_UINT, string.Empty); type = CSTokenType.tUIntLiteral; } else if (str.EndsWith(S_LONG)) { str = str.Replace(S_LONG, string.Empty); type = CSTokenType.tLongIntLiteral; } else if (str.EndsWith(S_FLOAT)) { str = str.Replace(S_FLOAT, string.Empty); type = CSTokenType.tFloatLiteral; } else if (str.EndsWith(S_DOUBLE)) { str = str.Replace(S_DOUBLE, string.Empty); type = CSTokenType.tDoubleLiteral; } else if (str.EndsWith(S_DECIMAL)) { str = str.Replace(S_DECIMAL, string.Empty); type = CSTokenType.tDecimalLiteral; } else return null; if ((type != CSTokenType.tDecimalLiteral) && (type != CSTokenType.tFloatLiteral) && (type != CSTokenType.tDoubleLiteral)) { if (strToInt(str, out inum)) { //safeguard - this will never happen unless system supports 128bit arithmetic if (inum > UInt32.MaxValue) throw new ArgumentException(S_UINT + ": " + inum);//caught by lexer return inum; } } else if (type != CSTokenType.tDecimalLiteral) { if (strToFloat(str, out fnum)) { if ( (type == CSTokenType.tFloatLiteral) && (fnum > float.MaxValue) ) throw new ArgumentException(S_FLOAT + ": " + fnum);//caught by lexer return fnum; } } else if (strToDecimal(str, out dnum)) { return dnum; } return null; }