internal ContentTypeReader[] LoadAssetReaders() { // Dummy variables required for it to work on iDevices ** DO NOT DELETE ** // This forces the classes not to be optimized out when deploying to iDevices ListReader<Char> hCharListReader = new ListReader<Char>(); ListReader<Rectangle> hRectangleListReader = new ListReader<Rectangle>(); ListReader<Vector3> hVector3ListReader = new ListReader<Vector3>(); ListReader<StringReader> hStringListReader = new ListReader<StringReader>(); SpriteFontReader hSpriteFontReader = new SpriteFontReader(); Texture2DReader hTexture2DReader = new Texture2DReader(); CharReader hCharReader = new CharReader(); RectangleReader hRectangleReader = new RectangleReader(); StringReader hStringReader = new StringReader(); Vector3Reader hVector3Reader = new Vector3Reader(); CurveReader hCurveReader = new CurveReader(); int numberOfReaders; // The first content byte i read tells me the number of content readers in this XNB file numberOfReaders = _reader.Read7BitEncodedInt(); contentReaders = new ContentTypeReader[numberOfReaders]; // For each reader in the file, we read out the length of the string which contains the type of the reader, // then we read out the string. Finally we instantiate an instance of that reader using reflection for (int i = 0; i < numberOfReaders; i++) { // This string tells us what reader we need to decode the following data // string readerTypeString = reader.ReadString(); string originalReaderTypeString = _reader.ReadString(); // Need to resolve namespace differences string readerTypeString = originalReaderTypeString; readerTypeString = PrepareType(readerTypeString); Type l_readerType = Type.GetType(readerTypeString); if(l_readerType !=null) contentReaders[i] = (ContentTypeReader)Activator.CreateInstance(l_readerType,true); else throw new ContentLoadException("Could not find matching content reader of type " + originalReaderTypeString + " (" + readerTypeString + ")"); // I think the next 4 bytes refer to the "Version" of the type reader, // although it always seems to be zero int typeReaderVersion = _reader.ReadInt32(); } return contentReaders; }
private static void ConsumeAssemblyNameProperties(CharReader reader, StringBuilder assemblyName) { ConsumeWhitespace(reader, assemblyName); while (ConsumeAssemblyNamePropertyName(reader, assemblyName)) { ConsumeWhitespace(reader, assemblyName); if (reader.Peek() != '=') { throw new ArgumentException($"Invalid type name; Missing value for assembly name property."); } assemblyName.Append((char)reader.Read()); // Consume the equal sign. ConsumeAssemblyNamePropertyValue(reader, assemblyName); ConsumeWhitespace(reader, assemblyName); if (reader.Peek() != ',') { break; } // Consume the comma. assemblyName.Append((char)reader.Read()); } }
public void TestReserved() { const string test = "aaa bbb <reserved ccc=\"ddddd\"> eeee </reserved> ffff <reserved ggg=\"hhhh\"/> <other/>"; var set = new HashSet <String> { "reserved" }; var reader = new HTMLStripCharFilter(CharReader.Get(new StringReader(test)), set); var builder = new StringBuilder(); int ch = 0; while ((ch = reader.Read()) != -1) { builder.Append((char)ch); } var result = builder.ToString(); // System.out.println("Result: " + result); assertTrue("Escaped tag not preserved: " + result.IndexOf("reserved"), result.IndexOf("reserved") == 9); assertTrue("Escaped tag not preserved: " + result.IndexOf("reserved", 15), result.IndexOf("reserved", 15) == 38); assertTrue("Escaped tag not preserved: " + result.IndexOf("reserved", 41), result.IndexOf("reserved", 41) == 54); assertTrue("Other tag should be removed", result.IndexOf("other") == -1); }
private static TokenType ReadCaptureName(CharReader reader) { var startPosition = reader.Position; reader.MoveNext(); while (!reader.IsAtEnd) { switch (reader.Current) { case '{': throw new FormatException($"At index {reader.Position}, there is an unexpected '{{' character."); case '}': reader.MoveNext(); return(TokenType.Literal); } reader.MoveNext(); } throw new FormatException($"There is a capture section starting at index {startPosition} without a terminating '}}' character."); }
public static IEnumerable <Token> Tokenize(string text) { var tokenStart = 0; var tokenType = TokenType.Literal; var reader = new CharReader(text); while (!reader.IsAtEnd) { TokenType nextTokenType; int tokenEnd; switch (tokenType) { case TokenType.Literal: nextTokenType = ReadLiteral(reader); tokenEnd = reader.Position; break; case TokenType.CaptureName: tokenStart++; nextTokenType = ReadCaptureName(reader); tokenEnd = reader.Position - 1; break; default: throw new NotImplementedException(); } var value = text.Substring(tokenStart, tokenEnd - tokenStart); value = value.Replace("{{", "{").Replace("}}", "}"); yield return(new Token(tokenType, value)); tokenType = nextTokenType; tokenStart = reader.Position; } }
private static IList <TypeIdentifier> ParseGenericArguments(CharReader reader) { List <TypeIdentifier> args = new List <TypeIdentifier>(); if (reader.Peek() == '[') { do { reader.Read(); bool fullyQualified = false; if (reader.Peek() == '[') { fullyQualified = true; reader.Read(); } args.Add(ParseTypeIdentifier(reader, fullyQualified)); if (fullyQualified == true) { if (reader.Peek() == ']') { reader.Read(); } else { throw new TypeNameParserException($"Invalid type name \"{reader.Data}\"; Unexpected character '{(char)reader.Peek()}' at position {reader.Position}; expected ']'."); } } }while (reader.Peek() == ','); reader.Read(); // Consume closing ']' } return(args); }
public void TestHebrewScenarios() { const string html = "<div class=\"foo\">בדיקה ראשונה</div> וכאן נוסיף גם <a href=\"#bar\">לינק</a> ועכשיו " + "גם <a alt=\"לינק מסובך עם תיאור\" href=\"http://lucene.apache.org/\">לינק מסובך יותר</a>. " + " <!-- הערה אחת ויחידה -->"; const string gold = " בדיקה ראשונה וכאן נוסיף גם לינק ועכשיו " + "גם לינק מסובך יותר . "; var reader = new HTMLStripCharFilter(CharReader.Get(new StringReader(html))); var builder = new StringBuilder(); var ch = -1; var goldArray = gold.ToCharArray(); var position = 0; while ((ch = reader.Read()) != -1) { var theChar = (char)ch; builder.Append(theChar); Assert.IsTrue(theChar == goldArray[position], "\"" + theChar + "\"" + " at position: " + position + " does not equal: \"" + goldArray[position] + "\". Buffer so far: " + builder + "<EOB>"); position++; } Assert.AreEqual(gold, builder.ToString()); doTestOffsets("שלום X מה X שלומך חבר"); }
public void Test() { const string html = "<div class=\"foo\">this is some text</div> here is a <a href=\"#bar\">link</a> and " + "another <a href=\"http://lucene.apache.org/\">link</a>. " + "This is an entity: & plus a <. Here is an &. <!-- is a comment -->"; const string gold = " this is some text here is a link and " + "another link . " + "This is an entity: & plus a <. Here is an &. "; var reader = new HTMLStripCharFilter(CharReader.Get(new StringReader(html))); var builder = new StringBuilder(); var ch = -1; var goldArray = gold.ToCharArray(); var position = 0; while ((ch = reader.Read()) != -1) { var theChar = (char)ch; builder.Append(theChar); Assert.IsTrue(theChar == goldArray[position], "\"" + theChar + "\"" + " at position: " + position + " does not equal: \"" + goldArray[position] + "\". Buffer so far: " + builder + "<EOB>"); position++; } Assert.AreEqual(gold, builder.ToString()); }
internal ContentTypeReader[] LoadAssetReaders() { if (ContentTypeReaderManager.falseflag) { ByteReader byteReader = new ByteReader(); SByteReader sbyteReader = new SByteReader(); DateTimeReader dateTimeReader = new DateTimeReader(); DecimalReader decimalReader = new DecimalReader(); BoundingSphereReader boundingSphereReader = new BoundingSphereReader(); BoundingFrustumReader boundingFrustumReader = new BoundingFrustumReader(); RayReader rayReader = new RayReader(); ListReader<char> listReader1 = new ListReader<char>(); ListReader<Rectangle> listReader2 = new ListReader<Rectangle>(); ArrayReader<Rectangle> arrayReader1 = new ArrayReader<Rectangle>(); ListReader<Vector3> listReader3 = new ListReader<Vector3>(); ListReader<StringReader> listReader4 = new ListReader<StringReader>(); ListReader<int> listReader5 = new ListReader<int>(); SpriteFontReader spriteFontReader = new SpriteFontReader(); Texture2DReader texture2Dreader = new Texture2DReader(); CharReader charReader = new CharReader(); RectangleReader rectangleReader = new RectangleReader(); StringReader stringReader = new StringReader(); Vector2Reader vector2Reader = new Vector2Reader(); Vector3Reader vector3Reader = new Vector3Reader(); Vector4Reader vector4Reader = new Vector4Reader(); CurveReader curveReader = new CurveReader(); IndexBufferReader indexBufferReader = new IndexBufferReader(); BoundingBoxReader boundingBoxReader = new BoundingBoxReader(); MatrixReader matrixReader = new MatrixReader(); BasicEffectReader basicEffectReader = new BasicEffectReader(); VertexBufferReader vertexBufferReader = new VertexBufferReader(); AlphaTestEffectReader testEffectReader = new AlphaTestEffectReader(); EnumReader<SpriteEffects> enumReader1 = new EnumReader<SpriteEffects>(); ArrayReader<float> arrayReader2 = new ArrayReader<float>(); ArrayReader<Vector2> arrayReader3 = new ArrayReader<Vector2>(); ListReader<Vector2> listReader6 = new ListReader<Vector2>(); ArrayReader<Matrix> arrayReader4 = new ArrayReader<Matrix>(); EnumReader<Blend> enumReader2 = new EnumReader<Blend>(); NullableReader<Rectangle> nullableReader = new NullableReader<Rectangle>(); EffectMaterialReader effectMaterialReader = new EffectMaterialReader(); ExternalReferenceReader externalReferenceReader = new ExternalReferenceReader(); } int length = this._reader.Read7BitEncodedInt(); this.contentReaders = new ContentTypeReader[length]; for (int index = 0; index < length; ++index) { string str = this._reader.ReadString(); Func<ContentTypeReader> func; if (ContentTypeReaderManager.typeCreators.TryGetValue(str, out func)) { this.contentReaders[index] = func(); } else { string typeName = ContentTypeReaderManager.PrepareType(str); Type type = Type.GetType(typeName); if (type != (Type) null) { try { this.contentReaders[index] = ContentExtensions.GetDefaultConstructor(type).Invoke((object[]) null) as ContentTypeReader; } catch (TargetInvocationException ex) { throw new InvalidOperationException("Failed to get default constructor for ContentTypeReader. To work around, add a creation function to ContentTypeReaderManager.AddTypeCreator() with the following failed type string: " + str); } } else throw new ContentLoadException("Could not find matching content reader of type " + str + " (" + typeName + ")"); } this._reader.ReadInt32(); } return this.contentReaders; }
private static string[] ParseCsv(TextReader reader, ref int lineNum, char separator) { var curChar = CharReader.Create(reader); if (curChar.IsEof) { return(null); // EOF reached } if (curChar.IsEol) { lineNum++; #if !FW452 return(Array.Empty <string>()); #else return(Array <string> .Empty); #endif } var result = new List <string>(); StringBuilder curField = null; var state = ParserState.ExpectField; var column = 1; while (true) { var skip = false; while (!skip) { switch (state) { case ParserState.ExpectField: if (curChar.IsEof || curChar.IsEol) { if (result.Count > 0) // Special case - empty string not treated as single empty value { result.Add(""); } return(result.ToArray()); } if (curChar.Char == separator) { result.Add(""); state = ParserState.AfterField; break; } skip = true; if (curChar.IsWhitespace) { break; } curField = new StringBuilder(); if (curChar.IsDoubleQuota) { state = ParserState.QuotedField; break; } state = ParserState.Field; curField.Append(curChar.Char); break; case ParserState.Field: Debug.Assert(curField != null, "curField != null"); if (curChar.IsEof || curChar.IsEol || curChar.Char == separator) { result.Add(curField.ToString().Trim()); state = ParserState.AfterField; break; } skip = true; curField.Append(curChar.Char); break; case ParserState.QuotedField: Debug.Assert(curField != null, "curField != null"); if (curChar.IsEof) { throw new FormatException($"Unexpected EOF at line {lineNum} column {column}"); } skip = true; if (curChar.IsEol) { curField.Append("\r\n"); break; } if (curChar.IsDoubleQuota) { var peek = curChar.Peek(); if (peek.IsDoubleQuota) // Escaped '"' { curField.Append('"'); curChar = curChar.Next(); } else { result.Add(curField.ToString()); state = ParserState.AfterField; } break; } curField.Append(curChar.Char); break; case ParserState.AfterField: if (curChar.IsEof || curChar.IsEol) { return(result.ToArray()); } skip = true; if (curChar.IsWhitespace) { continue; } if (curChar.Char == separator) { state = ParserState.ExpectField; break; } throw new FormatException($"Unexpected char '{curChar.Char}' at line {lineNum} column {column}"); default: throw new ArgumentOutOfRangeException(); } } curChar = curChar.Next(); column++; if (curChar.IsEol) { lineNum++; column = 1; } } }
/// <summary> /// 从字符串解析Json对象 /// </summary> public static JValue Parse(string json) { // 从0开始 long seek = 0; CharReader reader = new CharReader(json); Stack <JToken> stack = new Stack <JToken>(); // 当前的字符对象 char ch = ' '; // 转义中 bool transition = false; stack.Push(new JToken(TokenType.NONE, 0)); while (reader.Read(ref ch)) { seek = reader.Seek; if (transition) { stack.Peek().AddChar(ch); transition = false; continue; } switch (ch) { case '\\': { transition = true; } break; case '{': { if ( TokenType.NONE == stack.Peek().TType || TokenType.ARRAY == stack.Peek().TType || TokenType.DATA_MARK == stack.Peek().TType || TokenType.PROPERTY_MARK == stack.Peek().TType ) { stack.Push(new JToken(TokenType.OBJECT, seek)); } } break; case '}': { if (TokenType.NUMBER == stack.Peek().TType) { stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek)); } if (TokenType.BOOLEAN == stack.Peek().TType) { stack.Push(new JToken(TokenType.BOOLEAN_CLOSE, seek)); } if (TokenType.NULL == stack.Peek().TType) { stack.Push(new JToken(TokenType.NULL_CLOSE, seek)); } if (TokenType.PROPERTY_MARK == stack.Peek().TType) { stack.Push(new JToken(TokenType.NUMBER, seek)); stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek)); } if ( TokenType.STRING_CLOSE == stack.Peek().TType || TokenType.OBJECT_CLOSE == stack.Peek().TType || TokenType.ARRAY_CLOSE == stack.Peek().TType || TokenType.NUMBER_CLOSE == stack.Peek().TType || TokenType.BOOLEAN_CLOSE == stack.Peek().TType || TokenType.NULL_CLOSE == stack.Peek().TType || TokenType.OBJECT == stack.Peek().TType ) { stack.Push(new JToken(TokenType.OBJECT_CLOSE, seek)); } } break; case '[': { if ( TokenType.NONE == stack.Peek().TType || TokenType.ARRAY == stack.Peek().TType || TokenType.DATA_MARK == stack.Peek().TType || TokenType.PROPERTY_MARK == stack.Peek().TType ) { stack.Push(new JToken(TokenType.ARRAY, seek)); } } break; case ']': { if (TokenType.NUMBER == stack.Peek().TType) { stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek)); } if (TokenType.BOOLEAN == stack.Peek().TType) { stack.Push(new JToken(TokenType.BOOLEAN_CLOSE, seek)); } if (TokenType.NULL == stack.Peek().TType) { stack.Push(new JToken(TokenType.NULL_CLOSE, seek)); } if (TokenType.DATA_MARK == stack.Peek().TType) { stack.Push(new JToken(TokenType.NUMBER, seek)); stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek)); } if ( TokenType.STRING_CLOSE == stack.Peek().TType || TokenType.OBJECT_CLOSE == stack.Peek().TType || TokenType.ARRAY_CLOSE == stack.Peek().TType || TokenType.NUMBER_CLOSE == stack.Peek().TType || TokenType.BOOLEAN_CLOSE == stack.Peek().TType || TokenType.NULL_CLOSE == stack.Peek().TType || TokenType.ARRAY == stack.Peek().TType ) { stack.Push(new JToken(TokenType.ARRAY_CLOSE, seek)); } } break; case ',': { if (TokenType.NUMBER == stack.Peek().TType) { stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek)); } if (TokenType.BOOLEAN == stack.Peek().TType) { stack.Push(new JToken(TokenType.BOOLEAN_CLOSE, seek)); } if (TokenType.NULL == stack.Peek().TType) { stack.Push(new JToken(TokenType.NULL_CLOSE, seek)); } if ( TokenType.DATA_MARK == stack.Peek().TType || TokenType.PROPERTY_MARK == stack.Peek().TType ) { stack.Push(new JToken(TokenType.NUMBER, seek)); stack.Push(new JToken(TokenType.NUMBER_CLOSE, seek)); } if ( TokenType.NUMBER_CLOSE == stack.Peek().TType || TokenType.STRING_CLOSE == stack.Peek().TType || TokenType.ARRAY_CLOSE == stack.Peek().TType || TokenType.OBJECT_CLOSE == stack.Peek().TType || TokenType.BOOLEAN_CLOSE == stack.Peek().TType || TokenType.NULL_CLOSE == stack.Peek().TType ) { stack.Push(new JToken(TokenType.DATA_MARK, seek)); } } break; case ':': { if (TokenType.STRING_CLOSE == stack.Peek().TType) { stack.Push(new JToken(TokenType.PROPERTY_MARK, seek)); } } break; case '"': { if (TokenType.STRING == stack.Peek().TType) { stack.Push(new JToken(TokenType.STRING_CLOSE, seek)); } if ( TokenType.DATA_MARK == stack.Peek().TType || TokenType.PROPERTY_MARK == stack.Peek().TType || TokenType.OBJECT == stack.Peek().TType || TokenType.ARRAY == stack.Peek().TType ) { stack.Push(new JToken(TokenType.STRING, seek)); } } break; case '-': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': { if ( TokenType.ARRAY == stack.Peek().TType || TokenType.DATA_MARK == stack.Peek().TType || TokenType.PROPERTY_MARK == stack.Peek().TType ) { stack.Push(new JToken(TokenType.NUMBER, seek)); } } break; case 't': case 'f': { if ( TokenType.ARRAY == stack.Peek().TType || TokenType.DATA_MARK == stack.Peek().TType || TokenType.PROPERTY_MARK == stack.Peek().TType ) { stack.Push(new JToken(TokenType.BOOLEAN, seek)); } } break; case 'n': { if ( TokenType.ARRAY == stack.Peek().TType || TokenType.DATA_MARK == stack.Peek().TType || TokenType.PROPERTY_MARK == stack.Peek().TType ) { stack.Push(new JToken(TokenType.NULL, seek)); } } break; default: break; } stack.Peek().AddChar(ch); } // 返回结果 return(Parse(stack)); }
// Add some non-zero constants to the mix. public void AddConstantsToTDB(RandoopConfiguration config) { foreach (SimpleTypeValues vs in config.simpleTypeValues) { Type type = Type.GetType(vs.simpleType); if (type == null) { throw new Common.RandoopBareExceptions.InternalError("invalid simple type in XML config file."); } foreach (FileName fn in vs.fileNames) { string fileName = fn.fileName; if (!File.Exists(fileName)) { throw new Common.RandoopBareExceptions.InvalidUserParamsException("Configuration file does not exist: " + fileName); } if (type.Equals(typeof(sbyte))) { SByteReader r = new SByteReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(sbyte), o)); } else if (type.Equals(typeof(byte))) { ByteReader r = new ByteReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(byte), o)); } else if (type.Equals(typeof(short))) { ShortReader r = new ShortReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(short), o)); } else if (type.Equals(typeof(ushort))) { UshortReader r = new UshortReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(ushort), o)); } else if (type.Equals(typeof(int))) { IntReader r = new IntReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(int), o)); } else if (type.Equals(typeof(uint))) { UintReader r = new UintReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(uint), o)); } else if (type.Equals(typeof(long))) { LongReader r = new LongReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(long), o)); } else if (type.Equals(typeof(ulong))) { UlongReader r = new UlongReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(ulong), o)); } else if (type.Equals(typeof(char))) { CharReader r = new CharReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(char), o)); } else if (type.Equals(typeof(float))) { FloatReader r = new FloatReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(float), o)); } else if (type.Equals(typeof(double))) { DoubleReader r = new DoubleReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(double), o)); } else if (type.Equals(typeof(bool))) { BoolReader r = new BoolReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(bool), o)); } else if (type.Equals(typeof(decimal))) { DecimalReader r = new DecimalReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(decimal), o)); } else { if (!type.Equals(typeof(string))) { throw new Common.RandoopBareExceptions.InternalError("invalid simple type in XML config file."); } Common.StringReader r = new Common.StringReader(); foreach (object o in r.Read(fileName)) this.AddPlan(Plan.Constant(typeof(string), o)); } } } }
public void ParseQuotedFail(string source) { CharReader reader = new CharReader(source); Assert.False(reader.ReadTo(':', true, '"')); }
internal ContentTypeReader[] LoadAssetReaders(ContentReader reader) { #pragma warning disable 0219, 0649 // Trick to prevent the linker removing the code, but not actually execute the code if (falseflag) { // Dummy variables required for it to work on iDevices ** DO NOT DELETE ** // This forces the classes not to be optimized out when deploying to iDevices var hByteReader = new ByteReader(); var hSByteReader = new SByteReader(); var hDateTimeReader = new DateTimeReader(); var hDecimalReader = new DecimalReader(); var hBoundingSphereReader = new BoundingSphereReader(); var hBoundingFrustumReader = new BoundingFrustumReader(); var hRayReader = new RayReader(); var hCharListReader = new ListReader<Char>(); var hRectangleListReader = new ListReader<Rectangle>(); var hRectangleArrayReader = new ArrayReader<Rectangle>(); var hVector3ListReader = new ListReader<Vector3>(); var hStringListReader = new ListReader<StringReader>(); var hIntListReader = new ListReader<Int32>(); var hSpriteFontReader = new SpriteFontReader(); var hTexture2DReader = new Texture2DReader(); var hCharReader = new CharReader(); var hRectangleReader = new RectangleReader(); var hStringReader = new StringReader(); var hVector2Reader = new Vector2Reader(); var hVector3Reader = new Vector3Reader(); var hVector4Reader = new Vector4Reader(); var hCurveReader = new CurveReader(); var hIndexBufferReader = new IndexBufferReader(); var hBoundingBoxReader = new BoundingBoxReader(); var hMatrixReader = new MatrixReader(); var hBasicEffectReader = new BasicEffectReader(); var hVertexBufferReader = new VertexBufferReader(); var hAlphaTestEffectReader = new AlphaTestEffectReader(); var hEnumSpriteEffectsReader = new EnumReader<Graphics.SpriteEffects>(); var hArrayFloatReader = new ArrayReader<float>(); var hArrayVector2Reader = new ArrayReader<Vector2>(); var hListVector2Reader = new ListReader<Vector2>(); var hArrayMatrixReader = new ArrayReader<Matrix>(); var hEnumBlendReader = new EnumReader<Graphics.Blend>(); var hNullableRectReader = new NullableReader<Rectangle>(); var hEffectMaterialReader = new EffectMaterialReader(); var hExternalReferenceReader = new ExternalReferenceReader(); var hSoundEffectReader = new SoundEffectReader(); var hSongReader = new SongReader(); var hModelReader = new ModelReader(); var hInt32Reader = new Int32Reader(); // At the moment the Video class doesn't exist // on all platforms... Allow it to compile anyway. #if ANDROID || IOS || MONOMAC || (WINDOWS && !OPENGL) || (WINRT && !WINDOWS_PHONE) var hVideoReader = new VideoReader(); #endif } #pragma warning restore 0219, 0649 // The first content byte i read tells me the number of content readers in this XNB file var numberOfReaders = reader.Read7BitEncodedInt(); var contentReaders = new ContentTypeReader[numberOfReaders]; var needsInitialize = new BitArray(numberOfReaders); _contentReaders = new Dictionary<Type, ContentTypeReader>(numberOfReaders); // Lock until we're done allocating and initializing any new // content type readers... this ensures we can load content // from multiple threads and still cache the readers. lock (_locker) { // For each reader in the file, we read out the length of the string which contains the type of the reader, // then we read out the string. Finally we instantiate an instance of that reader using reflection for (var i = 0; i < numberOfReaders; i++) { // This string tells us what reader we need to decode the following data // string readerTypeString = reader.ReadString(); string originalReaderTypeString = reader.ReadString(); Func<ContentTypeReader> readerFunc; if (typeCreators.TryGetValue(originalReaderTypeString, out readerFunc)) { contentReaders[i] = readerFunc(); needsInitialize[i] = true; } else { //System.Diagnostics.Debug.WriteLine(originalReaderTypeString); // Need to resolve namespace differences string readerTypeString = originalReaderTypeString; readerTypeString = PrepareType(readerTypeString); var l_readerType = Type.GetType(readerTypeString); if (l_readerType != null) { ContentTypeReader typeReader; if (!_contentReadersCache.TryGetValue(l_readerType, out typeReader)) { try { typeReader = l_readerType.GetDefaultConstructor().Invoke(null) as ContentTypeReader; } catch (TargetInvocationException ex) { // If you are getting here, the Mono runtime is most likely not able to JIT the type. // In particular, MonoTouch needs help instantiating types that are only defined in strings in Xnb files. throw new InvalidOperationException( "Failed to get default constructor for ContentTypeReader. To work around, add a creation function to ContentTypeReaderManager.AddTypeCreator() " + "with the following failed type string: " + originalReaderTypeString, ex); } needsInitialize[i] = true; _contentReadersCache.Add(l_readerType, typeReader); } contentReaders[i] = typeReader; } else throw new ContentLoadException( "Could not find ContentTypeReader Type. Please ensure the name of the Assembly that contains the Type matches the assembly in the full type name: " + originalReaderTypeString + " (" + readerTypeString + ")"); } var targetType = contentReaders[i].TargetType; if (targetType != null) _contentReaders.Add(targetType, contentReaders[i]); // I think the next 4 bytes refer to the "Version" of the type reader, // although it always seems to be zero reader.ReadInt32(); } // Initialize any new readers. for (var i = 0; i < contentReaders.Length; i++) { if (needsInitialize.Get(i)) contentReaders[i].Initialize(this); } } // lock (_locker) return contentReaders; }
public Scanner(string text) { _charReader = new CharReader(text); }
private static (string Namespace, List <string> NestedTypeName, IList <TypeIdentifier> GenericArguments, IList <TypeSpecifier> Specifiers, AssemblyName AssemblyName) ParseAssemblyQualifiedName(CharReader reader, bool allowTrailingCharacters) { var fullName = ParseFullName(reader, true); AssemblyName assemblyName = null; if (reader.Peek() == ',') { reader.Read(); SkipWhitespace(reader); assemblyName = ParseAssemblyName(reader); } if (!allowTrailingCharacters && reader.HasMore) { throw new TypeNameParserException($"Invalid type name \"{reader.Data}\"; Unexpected character '{(char)reader.Peek()}' at position {reader.Position}; expected end-of-string."); } return(fullName.Namespace, fullName.NestedTypeName, fullName.GenericArguments, fullName.Specifiers, assemblyName); }
public ContentTypeReader[] LoadAssetReaders(ContentReader reader) { // Dummy variables required for it to work on iDevices ** DO NOT DELETE ** // This forces the classes not to be optimized out when deploying to iDevices ListReader<Char> hCharListReader = new ListReader<Char>(); ListReader<Rectangle> hRectangleListReader = new ListReader<Rectangle>(); ListReader<Vector3> hVector3ListReader = new ListReader<Vector3>(); ListReader<StringReader> hStringListReader = new ListReader<StringReader>(); SpriteFontReader hSpriteFontReader = new SpriteFontReader(); Texture2DReader hTexture2DReader = new Texture2DReader(); CharReader hCharReader = new CharReader(); RectangleReader hRectangleReader = new RectangleReader(); StringReader hStringReader = new StringReader(); Vector3Reader hVector3Reader = new Vector3Reader(); int numberOfReaders; ContentTypeReader[] contentReaders; // The first 4 bytes should be the "XNBw" header. i use that to detect an invalid file byte[] headerBuffer = new byte[4]; reader.Read(headerBuffer, 0, 4); string headerString = Encoding.UTF8.GetString(headerBuffer, 0, 4); if (string.Compare(headerString, "XNBw", StringComparison.InvariantCultureIgnoreCase) != 0 && string.Compare(headerString, "XNBx", StringComparison.InvariantCultureIgnoreCase) != 0 && string.Compare(headerString, "XNBm", StringComparison.InvariantCultureIgnoreCase) != 0) throw new ContentLoadException("Asset does not appear to be a valid XNB file. Did you process your content for Windows?"); // I think these two bytes are some kind of version number. Either for the XNB file or the type readers byte version = reader.ReadByte(); if(version != 5) throw new ContentLoadException("Invalid XNB file version."); byte compressed = reader.ReadByte(); // The next int32 is the length of the XNB file int xnbLength = reader.ReadInt32(); if (compressed != 0 && compressed != 1) { throw new NotImplementedException("MonoGame cannot read compressed XNB files. Please use the XNB files from the Debug build of your XNA game instead. If someone wants to contribute decompression logic, that would be fantastic."); } // The next byte i read tells me the number of content readers in this XNB file numberOfReaders = reader.ReadByte(); contentReaders = new ContentTypeReader[numberOfReaders]; // For each reader in the file, we read out the length of the string which contains the type of the reader, // then we read out the string. Finally we instantiate an instance of that reader using reflection for (int i = 0; i < numberOfReaders; i++) { // This string tells us what reader we need to decode the following data // string readerTypeString = reader.ReadString(); string originalReaderTypeString = reader.ReadString(); // Need to resolve namespace differences string readerTypeString = originalReaderTypeString; readerTypeString = PrepareType(readerTypeString); Type l_readerType = Type.GetType(readerTypeString); if(l_readerType !=null) contentReaders[i] = (ContentTypeReader)Activator.CreateInstance(l_readerType,true); else throw new ContentLoadException("Could not find matching content reader of type " + originalReaderTypeString + " (" + readerTypeString + ")"); // I think the next 4 bytes refer to the "Version" of the type reader, // although it always seems to be zero int typeReaderVersion = reader.ReadInt32(); } return contentReaders; }
public IncludeContext(SourceText text) { CharReader = new CharReader(text); Text = text; }
public TokenizerContext(ICharReader reader, IEnumerable <Tokenizer> tokenizers) { this.reader = new CharReader(reader); this.tokenizers = tokenizers; }
public ContentTypeReader[] LoadAssetReaders(ContentReader reader) { // Dummy variables required for it to work on iDevices ** DO NOT DELETE ** // This forces the classes not to be optimized out when deploying to iDevices ListReader<Char> hCharListReader = new ListReader<Char>(); ListReader<Rectangle> hRectangleListReader = new ListReader<Rectangle>(); ListReader<Vector3> hVector3ListReader = new ListReader<Vector3>(); ListReader<StringReader> hStringListReader = new ListReader<StringReader>(); SpriteFontReader hSpriteFontReader = new SpriteFontReader(); Texture2DReader hTexture2DReader = new Texture2DReader(); CharReader hCharReader = new CharReader(); RectangleReader hRectangleReader = new RectangleReader(); StringReader hStringReader = new StringReader(); Vector3Reader hVector3Reader = new Vector3Reader(); int numberOfReaders; ContentTypeReader[] contentReaders; // The first 4 bytes should be the "XNBw" header. i use that to detect an invalid file byte[] headerBuffer = new byte[4]; reader.Read(headerBuffer, 0, 4); string headerString = Encoding.UTF8.GetString(headerBuffer, 0, 4); if (string.Compare(headerString, "XNBw", StringComparison.InvariantCultureIgnoreCase) != 0) throw new ContentLoadException("Asset does not appear to be a valid XNB file. Did you process your content for Windows?"); // I think these two bytes are some kind of version number. Either for the XNB file or the type readers byte version = reader.ReadByte(); byte compressed = reader.ReadByte(); // The next int32 is the length of the XNB file int xnbLength = reader.ReadInt32(); if (compressed != 0) { throw new NotImplementedException("MonoGame cannot read compressed XNB files. Please use the XNB files from the Debug build of your XNA game instead. If someone wants to contribute decompression logic, that would be fantastic."); } // The next byte i read tells me the number of content readers in this XNB file numberOfReaders = reader.ReadByte(); contentReaders = new ContentTypeReader[numberOfReaders]; // For each reader in the file, we read out the length of the string which contains the type of the reader, // then we read out the string. Finally we instantiate an instance of that reader using reflection for (int i = 0; i < numberOfReaders; i++) { // This string tells us what reader we need to decode the following data // string readerTypeString = reader.ReadString(); string originalReaderTypeString = reader.ReadString(); // Need to resolve namespace differences string readerTypeString = originalReaderTypeString; /*if(readerTypeString.IndexOf(", Microsoft.Xna.Framework") != -1) { string[] tokens = readerTypeString.Split(new char[] { ',' }); readerTypeString = ""; for(int j = 0; j < tokens.Length; j++) { if(j != 0) readerTypeString += ","; if(j == 1) readerTypeString += " Microsoft.Xna.Framework"; else readerTypeString += tokens[j]; } readerTypeString = readerTypeString.Replace(", Microsoft.Xna.Framework", "@"); }*/ if(readerTypeString.Contains("PublicKey")) { //if (readerTypeString.Contains("[[")) { readerTypeString = readerTypeString.Split(new char[] { '[', '[' })[0] + "[" + readerTypeString.Split(new char[] { '[', '[' })[2].Split(',')[0] + "]"; //} //else { // // If the readerTypeString did not contain "[[" to split the // // types then we assume it is XNA 4.0 which splits the types // // by ', ' // readerTypeString = readerTypeString.Split(new char[] { ',', ' '})[0]; // //} } readerTypeString = readerTypeString.Replace("Microsoft.Xna.Framework", "Microsoft.Xna.Framework"); Type l_readerType = Type.GetType(readerTypeString); if(l_readerType !=null) contentReaders[i] = (ContentTypeReader)Activator.CreateInstance(l_readerType,true); else throw new ContentLoadException("Could not find matching content reader of type " + originalReaderTypeString); // I think the next 4 bytes refer to the "Version" of the type reader, // although it always seems to be zero int typeReaderVersion = reader.ReadInt32(); } return contentReaders; }
/// <summary> /// Construct an OLE object /// </summary> /// <param name="br">Reader for the file containing the OLE</param> /// <param name="chrd">Chr reader of given encoding</param> /// <param name="doEcho">Echo bytes read to log?</param> public OLE(BinaryReader br, CharReader chrd, bool doEcho) { this.br = br; this.chrd = chrd; isEcho = doEcho; } //ctor
private static (string Namespace, List <string> NestedTypeName, IList <TypeIdentifier> GenericArguments, IList <TypeSpecifier> Specifiers) ParseFullName(CharReader reader, bool allowTrailingCharacters) { var namespaceTypeName = ParseNamespaceTypeName(reader, true); IList <TypeIdentifier> genericArguments; int la1 = reader.Peek(1); if (reader.Peek() == '[' && la1 != ',' && la1 != '*' && la1 != ']') { genericArguments = ParseGenericArguments(reader); } else { genericArguments = null; } IList <TypeSpecifier> spec = ParseRefPtrArrSpec(reader); if (!allowTrailingCharacters && reader.HasMore) { throw new TypeNameParserException($"Invalid type name \"{reader.Data}\"; Unexpected character '{(char)reader.Peek()}' at position {reader.Position}; expected end-of-string."); } return(namespaceTypeName.Namespace, namespaceTypeName.NestedTypeName, genericArguments, spec); }
/// <summary> /// 文字列大小判定(辞書読み順) /// </summary> /// <param name="pString1">よみ1</param> /// <param name="pString2">よみ2</param> /// <returns>負=pString2が大きい、0=等しい、正=pString1が大きい</returns> public int Compare(string pString1, string pString2) { CharReader reader1 = new CharReader(pString1); List <CharReader.CharFlag> flagList1 = new List <CharReader.CharFlag>(pString1.Length * 2); CharReader reader2 = new CharReader(pString2); List <CharReader.CharFlag> flagList2 = new List <CharReader.CharFlag>(pString2.Length * 2); bool blValid1, blValid2; char c1, c2; CharReader.CharFlag f1, f2; // かな順で判定 while (true) { // よみ1,2から「よみ」を1文字取得 blValid1 = reader1.GetCharData(out c1, out f1); blValid2 = reader2.GetCharData(out c2, out f2); // 文字列長で判定 if ((blValid1 == false) && (blValid2 == false)) { break; } if ((blValid1 == true) && (blValid2 == false)) { return(1); } if ((blValid1 == false) && (blValid2 == true)) { return(-1); } // UNICODE順による大小判定 int diff = c1 - c2; if (diff != 0) { return(diff); } // 付加情報を記録 flagList1.Add(f1); flagList2.Add(f2); } // 濁音などの付加情報で判定 int i = 0; while (true) { // よみ1,2から付加情報を1つ取得 blValid1 = i < flagList1.Count; if (blValid1) { f1 = flagList1[i]; } blValid2 = i < flagList2.Count; if (blValid2) { f2 = flagList2[i]; } // 文字列長で判定 if ((blValid1 == false) && (blValid2 == false)) { break; } if ((blValid1 == true) && (blValid2 == false)) { return(1); } if ((blValid1 == false) && (blValid2 == true)) { return(-1); } // 付加情報で判定 int diff = f1 - f2; if (diff != 0) { return(diff); } i++; } // 同一と判定 return(0); }
public IncludeContext(SourceFile file) { CharReader = new CharReader(file.Text); File = file; }
public Lexer(Stream stream, ErrorSink errors) { this.Reader = new CharReader(stream); this.Errors = errors; }
} //readBinFile private void readIndexes(BinaryReader br) { // File header fields CFformats fmt; char[] rchars = br.ReadChars(3); // get cardfile format chars e.g. MGC // always ASCII encoded even in 'Unicode' file string st1 = new string( rchars ); try { fmt = (CFformats)Enum.Parse(typeof(CFformats), st1, true); // true means case-sensitive } catch (ArgumentException) { string msg = string.Format("Unknown card file type: {0}: Invalid CardFile, expect MGC, RRG or DKO", st1); CardFile.Log.writeLn(msg); throw new ExnNoSuchCardFileType(msg); } CardFile.Log.write("Cardfile format: " + fmt.ToString() + ", "); UInt16 numCards; // count of cards in card file, from file header UInt32 lastObjID; // ID of last object in file switch (fmt) // use the appropriate code for the file header { case CFformats.MGC: numCards = br.ReadUInt16(); // count of cards in file CardFile.Log.writeLn("Num Cards: " + numCards.ToString()); break; case CFformats.RRG: lastObjID = br.ReadUInt32(); // last obj debug("Last Obj ID: " + lastObjID.ToString()); numCards = br.ReadUInt16(); // count of cards in file CardFile.Log.writeLn("Num Cards: " + numCards.ToString()); break; case CFformats.DKO: chrd = new UCS2Reader(br); // switch to UCS-2 encoded chars lastObjID = br.ReadUInt32(); // last obj debug("Last Obj ID: " + lastObjID.ToString()); numCards = br.ReadUInt16(); // count of cards in file CardFile.Log.writeLn("Num Cards: " + numCards.ToString()); break; default: string msg = "Warn: readBinFile: unknown card file type: " + fmt.ToString(); CardFile.Log.writeLn(msg); throw new ExnNoSuchCardFileType(msg); #pragma warning disable CS0162 // Unreachable code detected break; #pragma warning restore CS0162 // Unreachable code detected } //switch // Index entries - each is 52 bytes long // One index entry per card, in following byte format // 0 - 5 : null bytes (reserved for future MS use) // 6 - 9 : absolute position of card data in file // 10 : Flag (00) // 11 - 50 : Index title text content, terminated by a null byte. When Unicode -> 2n+1 // 51 : Last byte, null - for when index text line is full hence no null terminator there const int nullBytes = 6; const int partIdxLen = 12; // length of index excluding title text int idxTxtCharLen = 40; // max single-byte chars of index title text int idxTxtByteLen = 40; // max bytes of index title text if (fmt == CFformats.DKO) { idxTxtByteLen *= 2; idxTxtByteLen++; } int idxLen = idxTxtByteLen + partIdxLen; // index byte length char[] rdata = new char[idxTxtCharLen + 1]; // big enough to hold index line text plus null terminator byte[] bdata = new byte[nullBytes]; // check for expected null bytes Card card = new Card(); // For each index entry for (int i = 0; i < numCards; i++) { long startPos = br.BaseStream.Position; // save index start pos'n debugAddr(br, "\nIndex start pos'n"); bdata = br.ReadBytes(nullBytes); // null bytes - check they are null for valid cardfile foreach (byte b in bdata) { if (b != 0) { throw new ExnInvalidCardfile("Expected null bytes not found - Invalid Cardfile"); } } long dataPos = br.ReadInt32(); // pos'n of card data in file debug(string.Format("Card Data Pos'n: x{0:X4} ", dataPos)); br.ReadByte(); // flag byte - discard CardFile.Log.write("\nCard index title: "); int cIdx = 0; // count chars in index text char ic = chrd.ReadChar(); // 1st char of text content of index line while (!(ic == 0)) { if (cIdx > (idxTxtCharLen)) { CardFile.Log.writeLn(string.Format("\nreadBinFile: index has more than {0} chars - not a proper card file?", idxTxtCharLen)); } //CardFile.Log.write(ic); rdata[cIdx++] = ic; ic = chrd.ReadChar(); // 1st char of text content of index line } //CardFile.Log.writeLn(); string iStr = ""; if (cIdx > 0) { iStr = new string( rdata, 0, cIdx ); // last arg is length } CardFile.Log.writeLn(iStr); // write index title card.Title = iStr; ; debugLn(String.Format("\nIndex bytes read: {0:D}", br.BaseStream.Position - startPos)); // Now the card data br.BaseStream.Position = dataPos; // set file pos'n for card data from index debugAddr(br, "\nFile position"); // Get the card data switch (fmt) { case CFformats.MGC: card.Data = mgcData(br); break; case CFformats.RRG: card.Data = rrgData(br); break; case CFformats.DKO: card.Data = dkoData(br); break; } //switch wrtr.WriteCard(card); // write card data card.init(); br.BaseStream.Position = startPos + idxLen; // set to start of next index pos'n } //for index entries wrtr.close(); // close writer } //readIndexes
internal ContentTypeReader[] LoadAssetReaders() { #pragma warning disable 0219, 0649 // Trick to prevent the linker removing the code, but not actually execute the code if (falseflag) { // Dummy variables required for it to work on iDevices ** DO NOT DELETE ** // This forces the classes not to be optimized out when deploying to iDevices var hByteReader = new ByteReader(); var hSByteReader = new SByteReader(); var hDateTimeReader = new DateTimeReader(); var hDecimalReader = new DecimalReader(); var hBoundingSphereReader = new BoundingSphereReader(); var hBoundingFrustumReader = new BoundingFrustumReader(); var hRayReader = new RayReader(); var hCharListReader = new ListReader<Char>(); var hRectangleListReader = new ListReader<Rectangle>(); var hRectangleArrayReader = new ArrayReader<Rectangle>(); var hVector3ListReader = new ListReader<Vector3>(); var hStringListReader = new ListReader<StringReader>(); var hIntListReader = new ListReader<Int32>(); var hSpriteFontReader = new SpriteFontReader(); var hTexture2DReader = new Texture2DReader(); var hCharReader = new CharReader(); var hRectangleReader = new RectangleReader(); var hStringReader = new StringReader(); var hVector2Reader = new Vector2Reader(); var hVector3Reader = new Vector3Reader(); var hVector4Reader = new Vector4Reader(); var hCurveReader = new CurveReader(); var hIndexBufferReader = new IndexBufferReader(); var hBoundingBoxReader = new BoundingBoxReader(); var hMatrixReader = new MatrixReader(); var hBasicEffectReader = new BasicEffectReader(); var hVertexBufferReader = new VertexBufferReader(); var hAlphaTestEffectReader = new AlphaTestEffectReader(); var hEnumSpriteEffectsReader = new EnumReader<Graphics.SpriteEffects>(); var hArrayFloatReader = new ArrayReader<float>(); var hArrayVector2Reader = new ArrayReader<Vector2>(); var hListVector2Reader = new ListReader<Vector2>(); var hArrayMatrixReader = new ArrayReader<Matrix>(); var hEnumBlendReader = new EnumReader<Graphics.Blend>(); var hNullableRectReader = new NullableReader<Rectangle>(); var hEffectMaterialReader = new EffectMaterialReader(); var hExternalReferenceReader = new ExternalReferenceReader(); var hSoundEffectReader = new SoundEffectReader(); var hSongReader = new SongReader(); } #pragma warning restore 0219, 0649 int numberOfReaders; // The first content byte i read tells me the number of content readers in this XNB file numberOfReaders = _reader.Read7BitEncodedInt(); contentReaders = new ContentTypeReader[numberOfReaders]; // For each reader in the file, we read out the length of the string which contains the type of the reader, // then we read out the string. Finally we instantiate an instance of that reader using reflection for (int i = 0; i < numberOfReaders; i++) { // This string tells us what reader we need to decode the following data // string readerTypeString = reader.ReadString(); string originalReaderTypeString = _reader.ReadString(); Func<ContentTypeReader> readerFunc; if (typeCreators.TryGetValue(originalReaderTypeString, out readerFunc)) { contentReaders[i] = readerFunc(); } else { //System.Diagnostics.Debug.WriteLine(originalReaderTypeString); // Need to resolve namespace differences string readerTypeString = originalReaderTypeString; readerTypeString = PrepareType(readerTypeString); var l_readerType = Type.GetType(readerTypeString); if (l_readerType != null) { try { contentReaders[i] = l_readerType.GetDefaultConstructor().Invoke(null) as ContentTypeReader; } catch (TargetInvocationException ex) { // If you are getting here, the Mono runtime is most likely not able to JIT the type. // In particular, MonoTouch needs help instantiating types that are only defined in strings in Xnb files. throw new InvalidOperationException( "Failed to get default constructor for ContentTypeReader. To work around, add a creation function to ContentTypeReaderManager.AddTypeCreator() " + "with the following failed type string: " + originalReaderTypeString); } } else throw new ContentLoadException( "Could not find ContentTypeReader Type. Please ensure the name of the Assembly that contains the Type matches the assembly in the full type name: " + originalReaderTypeString + " (" + readerTypeString + ")"); } // I think the next 4 bytes refer to the "Version" of the type reader, // although it always seems to be zero int typeReaderVersion = _reader.ReadInt32(); } return contentReaders; }
public UnityLexer(SourceText text) { _charReader = new CharReader(text); Text = text; }
public static IEnumerable <string[]> ReadAsArrays(TextReader textReader, ParseOptions options = null) { using (var charReader = new CharReader(textReader)) return(ReadAsArrays(charReader, options)); }
public void Step(IDocumentAccessor document, IndentationSettings settings) { var line = document.Text; if (settings.LeaveEmptyLines && line.Length == 0) { return; } line = line.TrimStart(); var indent = new StringBuilder(); if (line.Length == 0) { if (this._blockComment) { return; } indent.Append(this._block.InnerIndent); indent.Append(settings.IndentString.Repeat(this._block.OneLineBlock)); if (this._block.Continuation) { indent.Append(settings.IndentString); } if (document.Text != indent.ToString()) { document.Text = indent.ToString(); } return; } if (document.TrimEnd()) { line = document.Text.TrimStart(); } var oldBlock = this._block; var startInComment = this._blockComment; this._lineComment = false; this._escape = false; this._lastNonCommentChar = '\n'; var reader = new CharReader(line); var cha = ' '; var prev = '\0'; var next = '\n'; var indented = false; while (reader.IsRemainChar) { cha = reader.Get(); prev = reader.Backward; next = reader.Ahead; if (this._lineComment) { break; } if (this._escape) { this._escape = false; continue; } switch (cha) { case '/': if (this._blockComment && prev == '*') { this._blockComment = false; } if (!this._inString) { if (!this._blockComment && next == '/') { this._lineComment = true; } if (!this._lineComment && next == '*') { this._blockComment = true; } } break; case '"': if (!(this._lineComment || this._blockComment)) { if (this._inString) { this._inString = !this._escape; } } break; case '\\': if (this._inString) { this._escape = true; } break; default: break; } if (this._lineComment || this._blockComment || this._inString) { if (this._wordBuilder.Length > 0) { this._block.LastLiteral = this._wordBuilder.ToString(); } this._wordBuilder.Length = 0; continue; } if (char.IsLetterOrDigit(cha)) { this._wordBuilder.Append(cha); } else { if (this._wordBuilder.Length > 0) { this._block.LastLiteral = this._wordBuilder.ToString(); } this._wordBuilder.Length = 0; } switch (cha) { case '(': case '{': case '[': this._block.ResetOneLineBlock(); this._blocks.Push(this._block); this._block.StartLine = document.LineNumber; if (!indented) { this._block.Indent(settings); indented = true; } this._block.Bracket = cha; break; case ')': case '}': case ']': var openBracket = StringChecker.GetOpenBracket(cha); while (this._block.Bracket != openBracket) { if (this._blocks.Count == 0) { break; } this._block = this._blocks.Pop(); } if (this._blocks.Count == 0) { break; } this._block = this._blocks.Pop(); this._block.Continuation = false; this._block.ResetOneLineBlock(); break; } if (!char.IsWhiteSpace(cha)) { this._lastNonCommentChar = cha; } } if (this._wordBuilder.Length > 0) { this._block.LastLiteral = this._wordBuilder.ToString(); } this._wordBuilder.Length = 0; if ((startInComment && line[0] != '*') || document.Text.StartsWith("//\t", StringComparison.Ordinal) || (document.Text == "//")) { return; } if ("]})".Contains(line[0])) { indent.Append(oldBlock.OuterIndent); oldBlock.ResetOneLineBlock(); oldBlock.Continuation = false; } else { indent.Append(oldBlock.InnerIndent); } if (document.IsReadOnly) { if (!oldBlock.Continuation && oldBlock.OneLineBlock == 0 && oldBlock.StartLine == this._block.StartLine && this._block.StartLine < document.LineNumber && this._lastNonCommentChar != ':') { indent.Length = 0; line = document.Text; for (int i = 0; i < line.Length; ++i) { if (!char.IsWhiteSpace(line[i])) { break; } indent.Append(line[i]); } if (startInComment && indent.Length > 0 && indent[indent.Length - 1] == ' ') { indent.Length -= 1; } this._block.InnerIndent = indent.ToString(); } return; } if (startInComment) { indent.Append(' '); } if (indent.Length != (document.Text.Length - line.Length) || !document.Text.StartsWith(indent.ToString(), StringComparison.Ordinal) || char.IsWhiteSpace(document.Text[indent.Length])) { document.Text = indent.ToString() + line; } }
// ID ('.' ID)* ('+' ID)* private static (string Namespace, List <string> NestedTypeName) ParseNamespaceTypeName(CharReader reader, bool allowTrailingCharacters) { List <string> nestedTypeName = new List <string>(); StringBuilder namespaceBuilder = new StringBuilder(); int lastDelimiterPos = -1; // Parse namespace including root type name, stop at nested types. while (reader.HasMore && TryParseIdentifierInto(reader, namespaceBuilder)) { if (reader.Peek() == '.') { lastDelimiterPos = namespaceBuilder.Length; namespaceBuilder.Append('.'); reader.Read(); } else { break; } } // Verify that we actually parsed something. if (namespaceBuilder.Length == 0) { throw new TypeNameParserException($"Failed to parse type name from \"{reader.Data}\"; Expected NamespaceTypeName, but none found."); } // The type name is the identifier following the last dot. Extract that from the namespaceBuilder. nestedTypeName.Add(namespaceBuilder.ToString(lastDelimiterPos + 1, namespaceBuilder.Length - lastDelimiterPos - 1)); namespaceBuilder.Length = lastDelimiterPos == -1 ? 0 : lastDelimiterPos; // Now parse any NestedTypeNames while (reader.Peek() == '+') { // Consume the + reader.Read(); nestedTypeName.Add(ParseIdentifier(reader)); } if (!allowTrailingCharacters && reader.HasMore) { throw new TypeNameParserException($"Invalid type name \"{reader.Data}\"; Unexpected character '{(char)reader.Peek()}' at position {reader.Position}; expected end-of-string."); } return((namespaceBuilder.Length == 0) ? null : namespaceBuilder.ToString(), nestedTypeName); }
public UnityPretokenizer(SourceText text) { _charReader = new CharReader(text); Text = text; }
private string TranslateDefs(string rawPattern, string lastMacro, string lastGlobal) { CharReader pp = new CharReader(rawPattern, 0); string pattern = ""; if (rawPattern.Contains("=") || rawPattern.Contains("&")) { while (!pp.EndOfString) { char d = pp.ReadChar(); if (d == '=') { if (pp.PeekChar() != '[') { Error("Missing '[' in macro call", pp); return ""; } pp.Position++; // Skip [ int endIndex = rawPattern.IndexOf(']', pp.Position); if (endIndex == -1) { Error("Missing ']' in macro call", pp); return ""; } string macroName = pp.ReadString(endIndex - pp.Position); if (macroName == "") { Error("Empty macro.", pp); return ""; } if (macroName.Contains("+")) { string[] macroParts = macroName.Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries); if (macroParts.Length == 0) { Error("Empty macro.", pp); return ""; } for (int i = 0; i < macroParts.Length; i++) { string macroPart = macroParts[i].Trim(); if (!macroBank.ContainsKey(macroPart)) { Error("Macro \"" + macroPart + "\" doesn't exist.", pp); return ""; } pattern += TranslateDefs(macroBank[macroPart], lastMacro, lastGlobal); } } else { if (macroName == lastMacro) { Error("Macro error: Cannot create a macro that references itself. (" + macroName + ")", pp); return ""; } if (!macroBank.ContainsKey(macroName)) { Error("Macro \"" + macroName + "\" doesn't exist.", pp); return ""; } pattern += TranslateDefs(macroBank[macroName], macroName, lastGlobal); } pp.Position++; // Skip ] } else if (d == '&') // Bracket check in case we hit a flag { if (pp.PeekChar() != '[') { Error("Missing '[' in global call", pp); return ""; } pp.Position++; // Skip [ int endIndex = rawPattern.IndexOf(']', pp.Position); if (endIndex == -1) { Error("Missing ']' in global call", pp); return ""; } string globalName = pp.ReadString(endIndex - pp.Position); if (globalName == lastGlobal) { Error("Global error: Cannot create a global that references itself. (" + globalName + ")", pp); return ""; } pp.Position++; // Skip ] if (!globalValues.ContainsKey(globalName)) { Error("Global \"" + globalName + "\" doesn't exist.", pp); return ""; } pattern += TranslateDefs(globalValues[globalName], lastMacro, globalName); } else { pattern += d; } } } else { pattern = rawPattern; } return pattern; }
internal ContentTypeReader[] LoadAssetReaders(ContentReader reader) { #pragma warning disable 0219, 0649 /* Trick to prevent the linker removing the code, but not actually execute the code * FIXME: Do we really need this in FNA? */ if (falseflag) { /* Dummy variables required for it to work on iDevices ** DO NOT DELETE ** * This forces the classes not to be optimized out when deploying to iDevices */ ByteReader hByteReader = new ByteReader(); SByteReader hSByteReader = new SByteReader(); DateTimeReader hDateTimeReader = new DateTimeReader(); DecimalReader hDecimalReader = new DecimalReader(); BoundingSphereReader hBoundingSphereReader = new BoundingSphereReader(); BoundingFrustumReader hBoundingFrustumReader = new BoundingFrustumReader(); RayReader hRayReader = new RayReader(); ListReader<char> hCharListReader = new ListReader<Char>(); ListReader<Rectangle> hRectangleListReader = new ListReader<Rectangle>(); ArrayReader<Rectangle> hRectangleArrayReader = new ArrayReader<Rectangle>(); ListReader<Vector3> hVector3ListReader = new ListReader<Vector3>(); ListReader<StringReader> hStringListReader = new ListReader<StringReader>(); ListReader<int> hIntListReader = new ListReader<Int32>(); SpriteFontReader hSpriteFontReader = new SpriteFontReader(); Texture2DReader hTexture2DReader = new Texture2DReader(); CharReader hCharReader = new CharReader(); RectangleReader hRectangleReader = new RectangleReader(); StringReader hStringReader = new StringReader(); Vector2Reader hVector2Reader = new Vector2Reader(); Vector3Reader hVector3Reader = new Vector3Reader(); Vector4Reader hVector4Reader = new Vector4Reader(); CurveReader hCurveReader = new CurveReader(); IndexBufferReader hIndexBufferReader = new IndexBufferReader(); BoundingBoxReader hBoundingBoxReader = new BoundingBoxReader(); MatrixReader hMatrixReader = new MatrixReader(); BasicEffectReader hBasicEffectReader = new BasicEffectReader(); VertexBufferReader hVertexBufferReader = new VertexBufferReader(); AlphaTestEffectReader hAlphaTestEffectReader = new AlphaTestEffectReader(); EnumReader<Microsoft.Xna.Framework.Graphics.SpriteEffects> hEnumSpriteEffectsReader = new EnumReader<Graphics.SpriteEffects>(); ArrayReader<float> hArrayFloatReader = new ArrayReader<float>(); ArrayReader<Vector2> hArrayVector2Reader = new ArrayReader<Vector2>(); ListReader<Vector2> hListVector2Reader = new ListReader<Vector2>(); ArrayReader<Matrix> hArrayMatrixReader = new ArrayReader<Matrix>(); EnumReader<Microsoft.Xna.Framework.Graphics.Blend> hEnumBlendReader = new EnumReader<Graphics.Blend>(); NullableReader<Rectangle> hNullableRectReader = new NullableReader<Rectangle>(); EffectMaterialReader hEffectMaterialReader = new EffectMaterialReader(); ExternalReferenceReader hExternalReferenceReader = new ExternalReferenceReader(); SoundEffectReader hSoundEffectReader = new SoundEffectReader(); SongReader hSongReader = new SongReader(); } #pragma warning restore 0219, 0649 /* The first content byte i read tells me the number of * content readers in this XNB file. */ int numberOfReaders = reader.Read7BitEncodedInt(); ContentTypeReader[] newReaders = new ContentTypeReader[numberOfReaders]; BitArray needsInitialize = new BitArray(numberOfReaders); contentReaders = new Dictionary<Type, ContentTypeReader>(numberOfReaders); /* Lock until we're done allocating and initializing any new * content type readers... this ensures we can load content * from multiple threads and still cache the readers. */ lock (locker) { /* For each reader in the file, we read out the * length of the string which contains the type * of the reader, then we read out the string. * Finally we instantiate an instance of that * reader using reflection. */ for (int i = 0; i < numberOfReaders; i += 1) { /* This string tells us what reader we * need to decode the following data. */ string originalReaderTypeString = reader.ReadString(); Func<ContentTypeReader> readerFunc; if (typeCreators.TryGetValue(originalReaderTypeString, out readerFunc)) { newReaders[i] = readerFunc(); needsInitialize[i] = true; } else { // Need to resolve namespace differences string readerTypeString = originalReaderTypeString; readerTypeString = PrepareType(readerTypeString); Type l_readerType = Type.GetType(readerTypeString); if (l_readerType != null) { ContentTypeReader typeReader; if (!contentReadersCache.TryGetValue(l_readerType, out typeReader)) { try { typeReader = l_readerType.GetDefaultConstructor().Invoke(null) as ContentTypeReader; } catch (TargetInvocationException ex) { /* If you are getting here, the Mono runtime * is most likely not able to JIT the type. * In particular, MonoTouch needs help * instantiating types that are only defined * in strings in Xnb files. */ throw new InvalidOperationException( "Failed to get default constructor for ContentTypeReader. " + "To work around, add a creation function to ContentTypeReaderManager.AddTypeCreator() " + "with the following failed type string: " + originalReaderTypeString, ex ); } needsInitialize[i] = true; contentReadersCache.Add(l_readerType, typeReader); } newReaders[i] = typeReader; } else { throw new ContentLoadException( "Could not find ContentTypeReader Type. " + "Please ensure the name of the Assembly that " + "contains the Type matches the assembly in the full type name: " + originalReaderTypeString + " (" + readerTypeString + ")" ); } } contentReaders.Add(newReaders[i].TargetType, newReaders[i]); /* I think the next 4 bytes refer to the "Version" of the type reader, * although it always seems to be zero. */ reader.ReadInt32(); } // Initialize any new readers. for (int i = 0; i < newReaders.Length; i += 1) { if (needsInitialize.Get(i)) { newReaders[i].Initialize(this); } } } // lock (locker) return newReaders; }
private void Error(string problem, CharReader reader) { int col; int line = reader.Source.GetLineNumberFromIndex(reader.Position, out col); errorLog.AppendFormat("ERROR (Line {0}, Col {1}): {2}\n", line, col, problem); }
internal ContentTypeReader[] LoadAssetReaders() { #pragma warning disable 0219, 0649 // Trick to prevent the linker removing the code, but not actually execute the code if (falseflag) { // Dummy variables required for it to work on iDevices ** DO NOT DELETE ** // This forces the classes not to be optimized out when deploying to iDevices var hByteReader = new ByteReader(); var hSByteReader = new SByteReader(); var hDateTimeReader = new DateTimeReader(); var hDecimalReader = new DecimalReader(); var hBoundingSphereReader = new BoundingSphereReader(); var hBoundingFrustumReader = new BoundingFrustumReader(); var hRayReader = new RayReader(); var hCharListReader = new ListReader<Char>(); var hRectangleListReader = new ListReader<Rectangle>(); var hVector3ListReader = new ListReader<Vector3>(); var hStringListReader = new ListReader<StringReader>(); var hSpriteFontReader = new SpriteFontReader(); var hTexture2DReader = new Texture2DReader(); var hCharReader = new CharReader(); var hRectangleReader = new RectangleReader(); var hStringReader = new StringReader(); var hVector2Reader = new Vector2Reader(); var hVector3Reader = new Vector3Reader(); var hVector4Reader = new Vector4Reader(); var hCurveReader = new CurveReader(); var hIndexBufferReader = new IndexBufferReader(); var hBoundingBoxReader = new BoundingBoxReader(); var hMatrixReader = new MatrixReader(); var hBasicEffectReader = new BasicEffectReader(); var hVertexBufferReader = new VertexBufferReader(); var hAlphaTestEffectReader = new AlphaTestEffectReader(); } #pragma warning restore 0219, 0649 int numberOfReaders; // The first content byte i read tells me the number of content readers in this XNB file numberOfReaders = _reader.Read7BitEncodedInt(); contentReaders = new ContentTypeReader[numberOfReaders]; // For each reader in the file, we read out the length of the string which contains the type of the reader, // then we read out the string. Finally we instantiate an instance of that reader using reflection for (int i = 0; i < numberOfReaders; i++) { // This string tells us what reader we need to decode the following data // string readerTypeString = reader.ReadString(); string originalReaderTypeString = _reader.ReadString(); // Need to resolve namespace differences string readerTypeString = originalReaderTypeString; readerTypeString = PrepareType(readerTypeString); Type l_readerType = Type.GetType(readerTypeString); if(l_readerType !=null) contentReaders[i] = (ContentTypeReader)Activator.CreateInstance(l_readerType,true); else throw new ContentLoadException("Could not find matching content reader of type " + originalReaderTypeString + " (" + readerTypeString + ")"); // I think the next 4 bytes refer to the "Version" of the type reader, // although it always seems to be zero int typeReaderVersion = _reader.ReadInt32(); } return contentReaders; }
private void GenerateFromOutline(StringBuilder stream, List<string> outlines) { string outline = outlines[rand.Next(0, outlines.Count)]; CharReader reader = new CharReader(outline, 0); while (!reader.EndOfString) { char c = reader.ReadChar(); if (c == '|') { stream.Append("\r\n"); } else if (char.IsLetter(c)) { if (!patternBank.ContainsKey(c.ToString())) { stream.Append("<PatternNotFound: " + c.ToString() + ">"); } else { GenerateFromPattern(stream, patternBank[c.ToString()]); } } else { stream.Append(c); } } }
internal static IEnumerable <string[]> ReadAsArrays(CharReader charReader, ParseOptions options = null) { using (var reader = new Parser(charReader, options ?? new ParseOptions())) return(reader.ReadAsArrays()); }
private void GenerateFromPattern(StringBuilder stream, string rawPattern) { CharReader reader = new CharReader(TranslateDefs(rawPattern, "", ""), 0); // Output stuff WordFormat currentFormat = WordFormat.None; string buffer = ""; // Input stuff char c = ' '; char prev = ' '; // This is only used by the Proper format mode to determine the start of a word // A/An conversion stuff int anIndex = -1; WordFormat anFormat = WordFormat.None; // Selector stuff int uniformSeedSalt = rand.Next(0, 1000000); List<int> activeSelectors = new List<int>(); List<int> selectorUniformIDs = new List<int>(); int currentUniformID = -1; // Word lock stuff Dictionary<string, Dictionary<string, int>> locks = new Dictionary<string, Dictionary<string, int>>(); // Repeater stuff List<RepeaterInstance> repeaters = new List<RepeaterInstance>(); // Volatile flags List<string> vflags = new List<string>(); while (!reader.EndOfString) // Read through pattern until we reach the end { prev = c; c = reader.ReadChar(); string className = ""; string subType = ""; #region Frequency indicators if (Char.IsNumber(c)) // Check if frequency indicator is here. Example: 40%{ +n[plural] are +A. } { int oldPos = reader.Position; int percentIndex = reader.Source.IndexOf('%', reader.Position); int nextSpace = reader.Source.IndexOf(' ', reader.Position); if (percentIndex > -1 && (percentIndex < nextSpace || nextSpace == -1)) { reader.Position--; // Revert reading of first digit string percentStr = reader.ReadString(percentIndex - reader.Position); int percent; if (!int.TryParse(percentStr, out percent)) { Error("Frequency indicator percent value was not a number.", reader); return; } if (percent > 100) { percent = 100; } else if (percent <= 0) { Error("0% frequency indicator detected. Why is this here?", reader); return; } reader.Position++; // Skip past '%' if ((char)reader.PeekChar() == '[') // Make sure this bitch is tight { reader.Position++; // Skip past '[' int closure = reader.Source.FindClosingSquareBracket(reader.Position); if (closure < 0) { Error("Missing closing bracket in frequency indicator.", reader); return; } if (rand.Next(0, 101) > percent) { reader.Position = closure; } continue; } else { reader.Position = oldPos; // Fall back to beginning of number if there is a false positive and it's just a number } } } #endregion #region Selectors if (c == '{') // Selector. Picks random item inside brackets. Example: {First/second/third/fourth/...} { int end = reader.Source.FindClosingCurlyBracket(reader.Position); if (end == -1) { Error("Incomplete curly brackets. Did you forget to close a selector?", reader); return; } int[] startIndices = reader.Source.GetSelectorSubs(reader.Position); if (startIndices.Length < 2) { Error("Selector is empty or only has one option.", reader); return; } activeSelectors.Add(end + 1); selectorUniformIDs.Add(currentUniformID); if (currentUniformID > -1) { LongRandom uniRand = new LongRandom(uniformSeedSalt + currentUniformID); reader.Position = startIndices[uniRand.Next(0, startIndices.Length)]; } else { reader.Position = startIndices[rand.Next(0, startIndices.Length)]; } currentUniformID = -1; } else if (c == '}') { if (activeSelectors.Count == 0) { Error("Unexpected '}' found in pattern.", reader); return; } activeSelectors.RemoveAt(activeSelectors.Count - 1); selectorUniformIDs.RemoveAt(selectorUniformIDs.Count - 1); continue; } else if (c == '/') { if (activeSelectors.Count == 0) { Error("Unexpected '/' found in pattern.", reader); return; } reader.Position = activeSelectors[activeSelectors.Count - 1]; activeSelectors.RemoveAt(activeSelectors.Count - 1); selectorUniformIDs.RemoveAt(selectorUniformIDs.Count - 1); continue; } else if (c == '*') { int bracketIndex = reader.Source.IndexOf("{", reader.Position); if (bracketIndex <= reader.Position) { Error("Uniform operator could not find a selector to associate with.", reader); return; } string strUID = reader.ReadString(bracketIndex - reader.Position); int uid; if (!int.TryParse(strUID, out uid)) { Error("Uniform ID was not a number.", reader); return; } else if (uid < 0) { Error("Uniform ID's cannot be negative.", reader); return; } currentUniformID = uid; continue; } #endregion #region Repeaters if (c == '^') { // iteration range if (reader.PeekChar() != '[') { Error("Repeater iterations parameter did not have an opening bracket.", reader); return; } reader.ReadChar(); // skip [ int rightRangeBracketIndex = reader.Source.FindClosingSquareBracket(reader.Position); if (rightRangeBracketIndex < 0) { Error("Repeater iterations parameter did not have a closing bracket.", reader); return; } string strRangeParameter = reader.ReadString(rightRangeBracketIndex - reader.Position).Trim(); reader.ReadChar(); // skip ] int constantParam = 0; int min = 0; int max = 0; if (!int.TryParse(strRangeParameter, out constantParam)) { string[] parts = strRangeParameter.Split(new char[] { '-' }); if (parts.Length != 2) { Error("Repeater range parameter must be a pair of two numbers.", reader); return; } if (!int.TryParse(parts[0], out min) || !int.TryParse(parts[1], out max)) { Error("Repeater range parameter did not contain valid numbers.", reader); return; } if (min > max || min == 0 || max == 0) { Error("Repeater range must be greater than zero, and max > min.", reader); return; } constantParam = rand.Next(min, max); } // separator if (reader.ReadChar() != '[') { Error("Repeater separator parameter did not have an opening bracket.", reader); return; } int sepIndex = reader.Position; int rightSepBracketIndex = reader.Source.FindClosingSquareBracket(reader.Position); if (rightSepBracketIndex < 0) { Error("Repeater separator parameter did not have a closing bracket.", reader); } string strSepParameter = reader.ReadString(rightSepBracketIndex - reader.Position); int sepEnd = reader.Position; reader.ReadChar(); // skip ] // content if (reader.ReadChar() != '[') { Error("Repeater content parameter did not have an opening bracket.", reader); return; } int rightContentBracketIndex = reader.Source.FindClosingSquareBracket(reader.Position); if (rightSepBracketIndex < 0) { Error("Repeater content parameter did not have a closing bracket.", reader); } int pStart = reader.Position; reader.ReadString(rightContentBracketIndex - reader.Position); int pEnd = reader.Position; repeaters.Add(new RepeaterInstance(pStart, pEnd, sepIndex, sepEnd, strSepParameter, constantParam)); reader.Position = pStart; } else if (c == ']' && repeaters.Count > 0) // End of repeater iteration? { int last = repeaters.Count - 1; RepeaterInstance rep = repeaters[last]; if (reader.Position - 1 != rep.ContentEndIndex && reader.Position - 1 != rep.SeparatorEndIndex) { continue; } if (rep.OnSeparator) // Currently writing separator? { rep.OnSeparator = false; reader.Position = rep.ContentStartIndex; } else // Currently writing content? { if (repeaters[last].Elapse()) { repeaters.RemoveAt(last); // Remove the last repeater if it's finished continue; } else { rep.OnSeparator = true; reader.Position = rep.SeparatorStartIndex; // Add separator if not } } } #endregion #region Flags else if (c == '$') { int leftBracket = reader.Source.IndexOf("[", reader.Position); if (leftBracket < 0) { Error("Missing '[' on flag call.", reader); return; } string func = reader.ReadString(leftBracket - reader.Position).ToLower(); reader.ReadChar(); // skip [ if (func.Contains(' ')) { Error("Invalid flag function.", reader); return; } int rightBracket = reader.Source.FindClosingSquareBracket(reader.Position); if (rightBracket < leftBracket) { Error("Missing ']' on flag call.", reader); return; } string firstParam = reader.ReadString(rightBracket - reader.Position); reader.ReadChar(); // skip ] if (func == "set") { if (!vflags.Contains(firstParam)) { vflags.Add(firstParam); } continue; } else if (func == "unset") { if (vflags.Contains(firstParam)) { vflags.Remove(firstParam); } continue; } else if (func == "if") { if (reader.ReadChar() != '[') { Error("Missing '[' in IF body.", reader); return; } int rightBodyBracket = reader.Source.FindClosingSquareBracket(reader.Position); if (rightBodyBracket < 0) { Error("Missing ']' in IF body.", reader); return; } if (!vflags.Contains(firstParam)) { reader.Position = rightBodyBracket; } continue; } else if (func == "ifnot") { if (reader.ReadChar() != '[') { Error("Missing '[' in IF body.", reader); return; } int rightBodyBracket = reader.Source.FindClosingSquareBracket(reader.Position); if (rightBodyBracket < 0) { Error("Missing ']' in IF body.", reader); return; } if (vflags.Contains(firstParam)) { reader.Position = rightBodyBracket; } continue; } else if (func == "g_set") { if (!flags.Contains(firstParam)) { flags.Add(firstParam); } continue; } else if (func == "g_unset") { if (flags.Contains(firstParam)) { flags.Remove(firstParam); } continue; } else if (func == "g_if") { if (reader.ReadChar() != '[') { Error("Missing '[' in IF body.", reader); return; } int rightBodyBracket = reader.Source.IndexOf("]", reader.Position); if (rightBodyBracket < 0) { Error("Missing ']' in IF body.", reader); return; } if (!flags.Contains(firstParam)) { reader.Position = rightBodyBracket; } continue; } else if (func == "g_ifnot") { if (reader.ReadChar() != '[') { Error("Missing '[' in IF body.", reader); return; } int rightBodyBracket = reader.Source.IndexOf("]", reader.Position); if (rightBodyBracket < 0) { Error("Missing ']' in IF body.", reader); return; } if (flags.Contains(firstParam)) { reader.Position = rightBodyBracket; } continue; } else { Error("Unrecognized flag function.", reader); return; } } #endregion else if (c == '+') // Random word { string lockID = ""; char symbol = reader.ReadChar(); if (symbol == ' ' || symbol == '+' || symbol == '#' || symbol == '*' || symbol == '~' || symbol == '|') { Warning("Enountered illegal symbol: '" + symbol.ToString() + "'", reader); continue; } else if (symbol == '[') // Check if there is a class accessor. (For example: +[class]n) { int closure = reader.Source.IndexOf(']', reader.Position); if (closure < 0) { Error("Incomplete brackets in class accessor.", reader); return; } className = reader.ReadString(closure - reader.Position); reader.Position++; // Skip past ']' symbol = reader.ReadChar(); } if (reader.PeekChar() == '[') // Check if there is a subtype accessor. (For example: +n[subtype] or +[class]n[subtype]) { reader.Position++; int closure = reader.Source.IndexOf(']', reader.Position); if (closure < 0) { Error("Incomplete brackets in subtype accessor.", reader); return; } subType = reader.ReadString(closure - reader.Position); reader.Position++; // Skip past ']' } if (reader.PeekChar() == '<') // Check if there is a word lock accessor. (For example: +n<#> or +[class]n[subtype]<#>) { string lockKey = className + ":" + symbol.ToString(); if (!locks.ContainsKey(lockKey)) { locks.Add(lockKey, new Dictionary<string, int>()); } reader.Position++; int closure = reader.Source.IndexOf('>', reader.Position); if (closure < 0) { Error("Incomplete brackets in word lock accessor.", reader); return; } lockID = reader.ReadString(closure - reader.Position); if (lockID == "") { Error("Empty word lock ID.", reader); return; } if (!locks[lockKey].ContainsKey(lockID) && wordBank.ContainsKey(symbol)) // add a new word lock if a new ID is found --- this allows reuse of the word! { locks[lockKey].Add(lockID, wordBank[symbol].GetRandomIndex(rand, className)); } reader.Position++; // Skip past '>' } if (!wordBank.ContainsKey(symbol)) // Make sure the symbol is registered { Warning("Word symbol not found: '" + symbol.ToString() + "'", reader); } else if (lockID != "") { buffer = wordBank[symbol].GetWordByIndex(locks[className + ":" + symbol.ToString()][lockID], subType, currentFormat); } else { if (className.Contains(",")) { string[] mcNames = className.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (mcNames.Length < 2) { Error("A multi-class expression must include more than one class name in its parameters.", reader); return; } for (int i = 0; i < mcNames.Length; i++) { mcNames[i] = mcNames[i].Trim(); // this is to get rid of spaces between the class names } for (int i = 0; i < mcNames.Length; i++) { if (!ClassExists(symbol, mcNames[i])) { Error("Bad multiclass", reader); return; } } buffer = wordBank[symbol].GetRandomWordMultiClass(rand, subType, currentFormat, mcNames); } else if (!ClassExists(symbol, className)) { Warning("Class not found: " + symbol.ToString() + " -> " + className, reader); } else { int index = wordBank[symbol].GetRandomIndex(rand, className); buffer = wordBank[symbol].GetWordByIndex(index, subType, currentFormat); if (graph != null && symbol == 'n') { graph.Add(wordBank[symbol].GetWordByIndex(index, "", WordFormat.None)); } } } buffer = buffer.Capitalize(currentFormat); if (anIndex > -1 && buffer.StartsWithVowel()) { if (anFormat == WordFormat.AllCaps) { stream.Insert(anIndex, "N"); } else { stream.Insert(anIndex, "n"); } } anIndex = -1; anFormat = WordFormat.None; } else if (c == '|' || c == '\n') // Line break { buffer = "\r\n"; } else if (c == '~') // Capitalize { if (reader.PeekChar() == '~') { reader.ReadChar(); currentFormat = WordFormat.Capitalized; } else if (currentFormat == WordFormat.Proper) { currentFormat = WordFormat.None; } else { currentFormat = WordFormat.Proper; } } else if (c == '@') // Capslock { if (currentFormat == WordFormat.AllCaps) { currentFormat = WordFormat.None; } else { currentFormat = WordFormat.AllCaps; } } else if (c == '#') // Random number { if (reader.PeekChar() == '[') { reader.Position++; int closure = reader.Source.IndexOf(']', reader.Position); if (closure < 0) { Error("Incomplete parenthases in random number range.", reader); return; } string rangeStr = reader.ReadString(closure - reader.Position); reader.Position++; // Skip past ']' string[] rangeParts = rangeStr.Split('-'); if (rangeParts.Length != 2) { Error("Invalid number of range elements for random number. Got " + rangeParts.Length + ", need 2.", reader); return; } int min; int max; if (!int.TryParse(rangeParts[0], out min)) { Error("Invalid minimum value for random number.", reader); return; } if (!int.TryParse(rangeParts[1], out max)) { Error("Invalid maximum value for random number.", reader); return; } buffer = rand.Next(min, max).ToString(); } } else if (c != '{' && c != '}' && c != '[' && c != ']' && c != '<' && c != '>') // Covers all other characters except brackets { if (prev == ' ' && c == 'a' && !char.IsLetterOrDigit((char)reader.PeekChar())) // YES! YES! { anIndex = stream.Length + 1; anFormat = currentFormat; } if (currentFormat == WordFormat.AllCaps || (currentFormat == WordFormat.Proper && !Char.IsLetterOrDigit(prev) && prev.PermitsCap())) { buffer = c.ToString().ToUpper(); } else if (currentFormat == WordFormat.Capitalized) { buffer = c.ToString().ToUpper(); currentFormat = WordFormat.None; } else { buffer = c.ToString(); } } stream.Append(buffer); buffer = ""; // Fixes the word repitition bug when terminating Proper formatting in a pattern } }
public override TokenStream TokenStream(string fieldName, System.IO.TextReader reader) { var filter = new HTMLStripCharFilter(CharReader.Get(reader)); return(base.TokenStream(fieldName, filter)); }
private void PushIncludeContext(SourceText text) { _currentFileSegmentAbsolutePosition = FileSegments.Sum(x => x.Length); var includeContext = new IncludeContext(text); _includeStack.Push(includeContext); _charReader = includeContext.CharReader; FileSegments.Add(new FileSegment(text, 0)); }
private void PopIncludeContext() { _currentFileSegmentAbsolutePosition = FileSegments.Sum(x => x.Length); _includeStack.Pop(); _charReader = _includeStack.Peek().CharReader; FileSegments.Add(new FileSegment(_includeStack.Peek().Text, _charReader.Position)); }
// Add some non-zero constants to the mix. public void AddConstantsToTDB(RandoopConfiguration config) { foreach (SimpleTypeValues vs in config.simpleTypeValues) { Type type = Type.GetType(vs.simpleType); if (type == null) { throw new Common.RandoopBareExceptions.InternalError("invalid simple type in XML config file."); } foreach (FileName fn in vs.fileNames) { string fileName = fn.fileName; if (!File.Exists(fileName)) { throw new Common.RandoopBareExceptions.InvalidUserParamsException("Configuration file does not exist: " + fileName); } if (type.Equals(typeof(sbyte))) { SByteReader r = new SByteReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(sbyte), o)); } } else if (type.Equals(typeof(byte))) { ByteReader r = new ByteReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(byte), o)); } } else if (type.Equals(typeof(short))) { ShortReader r = new ShortReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(short), o)); } } else if (type.Equals(typeof(ushort))) { UshortReader r = new UshortReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(ushort), o)); } } else if (type.Equals(typeof(int))) { IntReader r = new IntReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(int), o)); } } else if (type.Equals(typeof(uint))) { UintReader r = new UintReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(uint), o)); } } else if (type.Equals(typeof(long))) { LongReader r = new LongReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(long), o)); } } else if (type.Equals(typeof(ulong))) { UlongReader r = new UlongReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(ulong), o)); } } else if (type.Equals(typeof(char))) { CharReader r = new CharReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(char), o)); } } else if (type.Equals(typeof(float))) { FloatReader r = new FloatReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(float), o)); } } else if (type.Equals(typeof(double))) { DoubleReader r = new DoubleReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(double), o)); } } else if (type.Equals(typeof(bool))) { BoolReader r = new BoolReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(bool), o)); } } else if (type.Equals(typeof(decimal))) { DecimalReader r = new DecimalReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(decimal), o)); } } else { if (!type.Equals(typeof(string))) { throw new Common.RandoopBareExceptions.InternalError("invalid simple type in XML config file."); } Common.StringReader r = new Common.StringReader(); foreach (object o in r.Read(fileName)) { this.AddPlan(Plan.Constant(typeof(string), o)); } } } } }
/// Easy-use constructor that takes a {@link Reader}. public MappingCharFilter(NormalizeCharMap normMap, System.IO.TextReader in_Renamed) : base(CharReader.Get(in_Renamed)) { this.normMap = normMap; }
public static DataTable ReadToDataTable(TextReader textReader, ParseOptions options = null) { using (var charReader = new CharReader(textReader)) return(ReadToDataTable(charReader, options)); }