public void EmptyTextReaderLook() {
			using (TestStringReader reader = new TestStringReader(0)) {
				TextBuffer charBuffer = new TextBuffer(reader);
				char ch;
				Assert.False(charBuffer.TryLookahead(0, out ch));
			}
		}
		public void EmptyTextReaderRead() {
			using (TestStringReader reader = new TestStringReader(0)) {
				TextBuffer charBuffer = new TextBuffer(reader);
				LineInfo position;
				Assert.Equal(string.Empty, charBuffer.Read(0, out position));
			}
		}
		public void ParseEmpty() {
			using (TestStringReader reader = new TestStringReader("")) {
				SemanticProcessor<TestToken> processor = new SemanticProcessor<TestToken>(reader, actions);
				Assert.Equal(ParseMessage.Accept, processor.ParseAll());
				Assert.IsType<TestEmpty>(processor.CurrentToken);
			}
		}
		public void ParseNull() {
			using (TestStringReader reader = new TestStringReader("NULL")) {
				SemanticProcessor<TestToken> processor = new SemanticProcessor<TestToken>(reader, actions);
				Assert.Equal(ParseMessage.Accept, processor.ParseAll());
				Assert.IsType<TestEmpty>(processor.CurrentToken);
				Assert.False((processor.CurrentToken is TestSpecial) && ((TestSpecial)processor.CurrentToken).IsString);
			}
		}
		public void ParseSimpleExpression() {
			using (TestStringReader reader = new TestStringReader("100")) {
				SemanticProcessor<TestToken> processor = new SemanticProcessor<TestToken>(reader, actions);
				Assert.Equal(ParseMessage.Accept, processor.ParseAll());
				Assert.IsAssignableFrom<TestValue>(processor.CurrentToken);
				TestValue value = (TestValue)processor.CurrentToken;
				Assert.Equal(100, value.Compute());
			}
		}
		public void ParseComplexExpression() {
			using (TestStringReader reader = new TestStringReader("((100+5.0)/\r\n(4.5+.5))-\r\n12345.4e+1")) {
				SemanticProcessor<TestToken> processor = new SemanticProcessor<TestToken>(reader, actions);
				Assert.Equal(ParseMessage.Accept, processor.ParseAll());
				Assert.IsAssignableFrom<TestValue>(processor.CurrentToken);
				TestValue value = (TestValue)processor.CurrentToken;
				Assert.Equal(-123433.0, value.Compute());
			}
		}
		public void BlockCommentWithUnclosedString() {
			using (TestStringReader reader = new TestStringReader("/* don't */ 'do this'")) {
				Tokenizer tokenizer = new Tokenizer(reader, grammar);
				Token token;
				Assert.Equal(ParseMessage.BlockRead, tokenizer.NextToken(out token));
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Noise, token.Symbol.Kind);
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Terminal, token.Symbol.Kind);
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.End, token.Symbol.Kind);
			}
		}
		public void LineCommentEof() {
			using (TestStringReader reader = new TestStringReader("0 -- /* don't /*** 'do ***/ this */ 0")) {
				Tokenizer tokenizer = new Tokenizer(reader, grammar);
				Token token;
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Terminal, token.Symbol.Kind);
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Noise, token.Symbol.Kind);
				Assert.Equal(ParseMessage.BlockRead, tokenizer.NextToken(out token));
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.End, token.Symbol.Kind);
			}
		}
		public void MarkLongDistanceAndReplay() {
			using (TestStringReader reader = new TestStringReader(1024*20)) {
				TextBuffer charBuffer = new TextBuffer(reader);
				char ch;
				LineInfo position;
				Assert.Equal(reader.ToString().Substring(0, 128), charBuffer.Read(128, out position));
				int offset = 0;
				for (int i = 128; i < 1024*16; i++) {
					Assert.True(charBuffer.TryLookahead(ref offset, out ch));
					Assert.Equal(reader[i], ch);
				}
				offset = 0;
				for (int i = 128; i < 1024*8; i++) {
					Assert.True(charBuffer.TryLookahead(ref offset, out ch));
					Assert.Equal(reader[i], ch);
				}
				Assert.Equal(reader.ToString().Substring(128), charBuffer.Read(reader.Length-128, out position));
				Assert.False(charBuffer.TryLookahead(0, out ch));
			}
		}
		public void CheckLexicalErrorOnEnd() {
			using (TestStringReader reader = new TestStringReader("'")) {
				Tokenizer tokenizer = new Tokenizer(reader, grammar);
				Token token;
				Assert.Equal(ParseMessage.LexicalError, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Error, token.Symbol.Kind);
				Assert.Equal("'", token.Text);
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.End, token.Symbol.Kind);
			}
		}
		public void EndOfDataWithUnfinishedTerminal() {
			using (TestStringReader reader = new TestStringReader("0 'zero")) {
				Tokenizer tokenizer = new Tokenizer(reader, grammar);
				Token token;
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Terminal, token.Symbol.Kind);
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Noise, token.Symbol.Kind);
				Assert.Equal(ParseMessage.LexicalError, tokenizer.NextToken(out token));
			}
		}
		public void CheckUnmergedLexicalError() {
			using (TestStringReader reader = new TestStringReader("1+Nx*200")) {
				Tokenizer tokenizer = new Tokenizer(reader, grammar);
				Token token;
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Terminal, token.Symbol.Kind);
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Terminal, token.Symbol.Kind);
				Assert.Equal(ParseMessage.LexicalError, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Error, token.Symbol.Kind);
				Assert.Equal("N", token.Text);
				Assert.Equal(ParseMessage.LexicalError, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Error, token.Symbol.Kind);
				Assert.Equal("x", token.Text);
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Terminal, token.Symbol.Kind);
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.Terminal, token.Symbol.Kind);
				Assert.Equal(ParseMessage.TokenRead, tokenizer.NextToken(out token));
				Assert.Equal(SymbolKind.End, token.Symbol.Kind);
			}
		}
Example #13
0
		public void ReadAndRollback() {
			using (TestStringReader reader = new TestStringReader(1024*20)) {
				TextBuffer charBuffer = new TextBuffer(reader);
				LineInfo position1;
				Assert.Equal(reader.ToString().Substring(0, 1280), charBuffer.Read(1280, out position1));
				Assert.Equal(charBuffer.Position, 1280);
				charBuffer.Rollback();
				Assert.Equal(charBuffer.Position, 0);
				LineInfo position2;
				Assert.Equal(reader.ToString().Substring(0, 1280), charBuffer.Read(1280, out position2));
				Assert.Equal(position1, position2);
			}
		}
Example #14
0
		public void ReadSingleChar() {
			using (TestStringReader reader = new TestStringReader(1)) {
				TextBuffer charBuffer = new TextBuffer(reader);
				LineInfo position;
				Assert.Equal(reader[0].ToString(CultureInfo.InvariantCulture), charBuffer.Read(1, out position));
				char ch;
				Assert.False(charBuffer.TryLookahead(0, out ch));
			}
		}
Example #15
0
		public void ReadManyChars() {
			using (TestStringReader reader = new TestStringReader(1024*1024)) {
				TextBuffer charBuffer = new TextBuffer(reader);
				char ch;
				for (int i = 0; i < reader.Length; i++) {
					Assert.True(charBuffer.TryLookahead(i, out ch));
					Assert.Equal(reader[i], ch);
				}
				Assert.False(charBuffer.TryLookahead(reader.Length, out ch));
			}
		}
Example #16
0
		public void MultilineLfCr() {
			using (TestStringReader reader = new TestStringReader("1\n\r\n\r3")) {
				TextBuffer charBuffer = new TextBuffer(reader);
				LineInfo position;
				charBuffer.Read(reader.Length, out position);
				Assert.Equal(1, position.Line);
				Assert.Equal(3, charBuffer.Line);
			}
		}