示例#1
0
        public static IEnumerable<Token<DicTokenType>> Tokenize(string source, string data)
        {
            var reader = new StringeReader(data);
            Chare currentChar;
            while (!reader.EndOfStringe)
            {
                if (char.IsWhiteSpace((currentChar = reader.ReadChare()).Character)) continue;
                switch (currentChar.Character)
                {
                    case '#':
                        yield return new Token<DicTokenType>(DicTokenType.Directive, ReadRestOfLine(reader).Trim());
                        break;
                    case '@':
                        reader.ReadUntilAny('\n', '\r');
                        break;
                    case '>':
                    {
                        bool diffmark = reader.Eat('>');
                        yield return new Token<DicTokenType>(diffmark ? DicTokenType.DiffEntry : DicTokenType.Entry,
                            ReadRestOfLine(reader, diffmark).Trim());
                        break;
                    }
                    case '|':
                        yield return new Token<DicTokenType>(DicTokenType.Property, ReadRestOfLine(reader).Trim());
                    break;
                    default:
                        throw new InvalidDataException($"{source}: (Line {currentChar.Line}, Col {currentChar.Column}) Unexpected token: '{currentChar}'.");
                }
            }
            yield return new Token<DicTokenType>(DicTokenType.EOF, "");
		}
示例#2
0
文件: Lexer.cs 项目: dandrews/Manhood
 public static IEnumerable<Token<TokenType>> GenerateTokens(string input)
 {
     var reader = new StringeReader(input);
     while (!reader.EndOfStringe)
     {
         yield return reader.ReadToken(Rules);
     }
 }
示例#3
0
文件: Lexer.cs 项目: Spanfile/Englang
		public IEnumerator<Token<TokenType>> GetEnumerator()
		{
			StringeReader reader = new StringeReader(text);
			Token<TokenType> token;
			while ((token = reader.ReadToken(lexerRules)).Identifier != TokenType.EOF)
				yield return token;
			yield return token; // this makes sure the EOF token is returned
		}
示例#4
0
 /// <summary>
 /// Generates beautiful tokens.
 /// </summary>
 /// <param name="name">The source name of the input.</param>
 /// <param name="input">The input string to tokenize.</param>
 /// <returns></returns>
 public static IEnumerable<Token<R>> GenerateTokens(string name, Stringe input)
 {
     var reader = new StringeReader(input)
     {
         Origin = name
     };
     while (!reader.EndOfStringe)
     {
         yield return reader.ReadToken(Rules);
     }
 }
示例#5
0
 private static Stringe ReadRestOfLine(StringeReader reader, bool isDiffmark = false) => isDiffmark
     ? reader.ReadUntilAny('>', '\n', '\r', '#', '@')
     : reader.ReadUntilAny('>', '|', '\n', '\r', '#', '@');